diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-12 03:10:00 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-06-12 03:10:00 +0300 |
commit | 81373fe07e0afe94e4d63a37c51a80c10132aeac (patch) | |
tree | 6f8fcfd6422d8416a9b292bef4fb03d9a3c7dd32 /spec | |
parent | 68b6fd7fb2edaaae86a4ee8df02b7f9783672050 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec')
-rw-r--r-- | spec/experiments/application_experiment_spec.rb | 6 | ||||
-rw-r--r-- | spec/fast_spec_helper.rb | 5 | ||||
-rw-r--r-- | spec/rubocop/code_reuse_helpers_spec.rb | 75 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb | 228 | ||||
-rw-r--r-- | spec/support/gitlab_experiment.rb | 10 | ||||
-rw-r--r-- | spec/support/helpers/stub_experiments.rb | 2 | ||||
-rw-r--r-- | spec/support/helpers/stubbed_feature.rb | 22 |
7 files changed, 44 insertions, 304 deletions
diff --git a/spec/experiments/application_experiment_spec.rb b/spec/experiments/application_experiment_spec.rb index 2d2b911749b..22c436e4159 100644 --- a/spec/experiments/application_experiment_spec.rb +++ b/spec/experiments/application_experiment_spec.rb @@ -19,6 +19,12 @@ RSpec.describe ApplicationExperiment, :experiment do allow(subject).to receive(:enabled?).and_return(true) end + it "naively assumes a 1x1 relationship to feature flags for tests" do + expect(Feature).to receive(:persist_used!).with('namespaced_stub') + + described_class.new('namespaced/stub') + end + it "doesn't raise an exception without a defined control" do # because we have a default behavior defined diff --git a/spec/fast_spec_helper.rb b/spec/fast_spec_helper.rb index 469c29cd2e0..2f0bcd318d9 100644 --- a/spec/fast_spec_helper.rb +++ b/spec/fast_spec_helper.rb @@ -22,8 +22,3 @@ ActiveSupport::Dependencies.autoload_paths << 'lib' ActiveSupport::Dependencies.autoload_paths << 'ee/lib' ActiveSupport::XmlMini.backend = 'Nokogiri' - -RSpec.configure do |config| - config.filter_run focus: true - config.run_all_when_everything_filtered = true -end diff --git a/spec/rubocop/code_reuse_helpers_spec.rb b/spec/rubocop/code_reuse_helpers_spec.rb index 695c152e3db..9337df368e3 100644 --- a/spec/rubocop/code_reuse_helpers_spec.rb +++ b/spec/rubocop/code_reuse_helpers_spec.rb @@ -150,31 +150,6 @@ RSpec.describe RuboCop::CodeReuseHelpers do end end - describe '#in_graphql_types?' do - %w[ - app/graphql/types - ee/app/graphql/ee/types - ee/app/graphql/types - ].each do |path| - it "returns true for a node in #{path}" do - node = build_and_parse_source('10', rails_root_join(path, 'foo.rb')) - - expect(cop.in_graphql_types?(node)).to eq(true) - end - end - - %w[ - app/graphql/resolvers - app/foo - ].each do |path| - it "returns true for a node in #{path}" do - node = build_and_parse_source('10', rails_root_join(path, 'foo.rb')) - - expect(cop.in_graphql_types?(node)).to eq(false) - end - end - end - describe '#in_api?' do it 'returns true for a node in the API directory' do node = build_and_parse_source('10', rails_root_join('lib', 'api', 'foo.rb')) @@ -189,67 +164,25 @@ RSpec.describe RuboCop::CodeReuseHelpers do end end - describe '#in_spec?' do - it 'returns true for a node in the spec directory' do - node = build_and_parse_source('10', rails_root_join('spec', 'foo.rb')) - - expect(cop.in_spec?(node)).to eq(true) - end - - it 'returns true for a node in the ee/spec directory' do - node = build_and_parse_source('10', rails_root_join('ee', 'spec', 'foo.rb')) - - expect(cop.in_spec?(node)).to eq(true) - end - - it 'returns false for a node outside the spec directory' do - node = build_and_parse_source('10', rails_root_join('lib', 'foo.rb')) - - expect(cop.in_spec?(node)).to eq(false) - end - end - - describe '#in_app_directory?' do + describe '#in_directory?' do it 'returns true for a directory in the CE app/ directory' do node = build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb')) - expect(cop.in_app_directory?(node, 'models')).to eq(true) + expect(cop.in_directory?(node, 'models')).to eq(true) end it 'returns true for a directory in the EE app/ directory' do node = build_and_parse_source('10', rails_root_join('ee', 'app', 'models', 'foo.rb')) - expect(cop.in_app_directory?(node, 'models')).to eq(true) + expect(cop.in_directory?(node, 'models')).to eq(true) end it 'returns false for a directory in the lib/ directory' do node = build_and_parse_source('10', rails_root_join('lib', 'models', 'foo.rb')) - expect(cop.in_app_directory?(node, 'models')).to eq(false) - end - end - - describe '#in_lib_directory?' do - it 'returns true for a directory in the CE lib/ directory' do - node = build_and_parse_source('10', rails_root_join('lib', 'models', 'foo.rb')) - - expect(cop.in_lib_directory?(node, 'models')).to eq(true) - end - - it 'returns true for a directory in the EE lib/ directory' do - node = - build_and_parse_source('10', rails_root_join('ee', 'lib', 'models', 'foo.rb')) - - expect(cop.in_lib_directory?(node, 'models')).to eq(true) - end - - it 'returns false for a directory in the app/ directory' do - node = - build_and_parse_source('10', rails_root_join('app', 'models', 'foo.rb')) - - expect(cop.in_lib_directory?(node, 'models')).to eq(false) + expect(cop.in_directory?(node, 'models')).to eq(false) end end diff --git a/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb b/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb deleted file mode 100644 index 063d9dc7017..00000000000 --- a/spec/rubocop/cop/gitlab/mark_used_feature_flags_spec.rb +++ /dev/null @@ -1,228 +0,0 @@ -# frozen_string_literal: true - -require 'fast_spec_helper' -require 'rubocop' -require 'rubocop/rspec/support' -require_relative '../../../../rubocop/cop/gitlab/mark_used_feature_flags' - -RSpec.describe RuboCop::Cop::Gitlab::MarkUsedFeatureFlags do - let(:defined_feature_flags) do - %w[a_feature_flag foo_hello foo_world baz_experiment_percentage bar_baz] - end - - subject(:cop) { described_class.new } - - before do - stub_const("#{described_class}::DYNAMIC_FEATURE_FLAGS", []) - allow(cop).to receive(:defined_feature_flags).and_return(defined_feature_flags) - allow(cop).to receive(:usage_data_counters_known_event_feature_flags).and_return([]) - end - - def feature_flag_path(feature_flag_name) - File.expand_path("../../../../tmp/feature_flags/#{feature_flag_name}.used", __dir__) - end - - shared_examples 'sets flag as used' do |method_call, flags_to_be_set| - it 'sets the flag as used' do - Array(flags_to_be_set).each do |flag_to_be_set| - expect(FileUtils).to receive(:touch).with(feature_flag_path(flag_to_be_set)) - end - - expect_no_offenses(<<~RUBY) - class Foo < ApplicationRecord - #{method_call} - end - RUBY - end - end - - shared_examples 'does not set any flags as used' do |method_call| - it 'sets the flag as used' do - expect(FileUtils).not_to receive(:touch) - - expect_no_offenses(method_call) - end - end - - %w[ - Feature.enabled? - Feature.disabled? - push_frontend_feature_flag - ].each do |feature_flag_method| - context "#{feature_flag_method} method" do - context 'a string feature flag' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}("foo")|, 'foo' - end - - context 'a symbol feature flag' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(:foo)|, 'foo' - end - - context 'an interpolated string feature flag with a string prefix' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}("foo_\#{bar}")|, %w[foo_hello foo_world] - end - - context 'an interpolated symbol feature flag with a string prefix' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(:"foo_\#{bar}")|, %w[foo_hello foo_world] - end - - context 'a string with a "/" in it' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}("bar/baz")|, 'bar_baz' - end - - context 'an interpolated string feature flag with a string prefix and suffix' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(:"foo_\#{bar}_baz")| - end - - context 'a dynamic string feature flag as a variable' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(a_variable, an_arg)| - end - - context 'an integer feature flag' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(123)| - end - end - end - - %w[ - Feature::Gitaly.enabled? - Feature::Gitaly.disabled? - ].each do |feature_flag_method| - context "#{feature_flag_method} method" do - context 'a string feature flag' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}("foo")|, 'gitaly_foo' - end - - context 'a symbol feature flag' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(:foo)|, 'gitaly_foo' - end - - context 'an interpolated string feature flag with a string prefix' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}("foo_\#{bar}")|, %w[foo_hello foo_world] - end - - context 'an interpolated symbol feature flag with a string prefix' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(:"foo_\#{bar}")|, %w[foo_hello foo_world] - end - - context 'an interpolated string feature flag with a string prefix and suffix' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(:"foo_\#{bar}_baz")| - end - - context 'a dynamic string feature flag as a variable' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(a_variable, an_arg)| - end - - context 'an integer feature flag' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(123)| - end - end - end - - %w[ - experiment - experiment_enabled? - push_frontend_experiment - Gitlab::Experimentation.active? - ].each do |feature_flag_method| - context "#{feature_flag_method} method" do - context 'a string feature flag' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}("baz")|, %w[baz baz_experiment_percentage] - end - - context 'a symbol feature flag' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(:baz)|, %w[baz baz_experiment_percentage] - end - - context 'an interpolated string feature flag with a string prefix' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}("foo_\#{bar}")|, %w[foo_hello foo_world] - end - - context 'an interpolated symbol feature flag with a string prefix' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(:"foo_\#{bar}")|, %w[foo_hello foo_world] - end - - context 'an interpolated string feature flag with a string prefix and suffix' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(:"foo_\#{bar}_baz")| - end - - context 'a dynamic string feature flag as a variable' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(a_variable, an_arg)| - end - - context 'an integer feature flag' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(123)| - end - end - end - - %w[ - use_rugged? - ].each do |feature_flag_method| - context "#{feature_flag_method} method" do - context 'a string feature flag' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(arg, "baz")|, 'baz' - end - - context 'a symbol feature flag' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(arg, :baz)|, 'baz' - end - - context 'an interpolated string feature flag with a string prefix' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(arg, "foo_\#{bar}")|, %w[foo_hello foo_world] - end - - context 'an interpolated symbol feature flag with a string prefix' do - include_examples 'sets flag as used', %Q|#{feature_flag_method}(arg, :"foo_\#{bar}")|, %w[foo_hello foo_world] - end - - context 'an interpolated string feature flag with a string prefix and suffix' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(arg, :"foo_\#{bar}_baz")| - end - - context 'a dynamic string feature flag as a variable' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(a_variable, an_arg)| - end - - context 'an integer feature flag' do - include_examples 'does not set any flags as used', %Q|#{feature_flag_method}(arg, 123)| - end - end - end - - describe 'self.limit_feature_flag = :foo' do - include_examples 'sets flag as used', 'self.limit_feature_flag = :foo', 'foo' - end - - describe 'FEATURE_FLAG = :foo' do - include_examples 'sets flag as used', 'FEATURE_FLAG = :foo', 'foo' - end - - describe 'Worker `data_consistency` method' do - include_examples 'sets flag as used', 'data_consistency :delayed, feature_flag: :foo', 'foo' - include_examples 'does not set any flags as used', 'data_consistency :delayed' - end - - describe 'GraphQL `field` method' do - before do - allow(cop).to receive(:in_graphql_types?).and_return(true) - end - - include_examples 'sets flag as used', 'field :runners, Types::Ci::RunnerType.connection_type, null: true, feature_flag: :foo', 'foo' - include_examples 'sets flag as used', 'field :runners, null: true, feature_flag: :foo', 'foo' - include_examples 'does not set any flags as used', 'field :solution' - include_examples 'does not set any flags as used', 'field :runners, Types::Ci::RunnerType.connection_type' - include_examples 'does not set any flags as used', 'field :runners, Types::Ci::RunnerType.connection_type, null: true, description: "hello world"' - include_examples 'does not set any flags as used', 'field :solution, type: GraphQL::STRING_TYPE, null: true, description: "URL to the vulnerabilitys details page."' - end - - describe "tracking of usage data metrics known events happens at the beginning of inspection" do - let(:usage_data_counters_known_event_feature_flags) { ['an_event_feature_flag'] } - - before do - allow(cop).to receive(:usage_data_counters_known_event_feature_flags).and_return(usage_data_counters_known_event_feature_flags) - end - - include_examples 'sets flag as used', "FEATURE_FLAG = :foo", %w[foo an_event_feature_flag] - end -end diff --git a/spec/support/gitlab_experiment.rb b/spec/support/gitlab_experiment.rb index 3d099dc689c..b84adf82d29 100644 --- a/spec/support/gitlab_experiment.rb +++ b/spec/support/gitlab_experiment.rb @@ -4,6 +4,16 @@ require 'gitlab/experiment/rspec' require_relative 'stub_snowplow' +# This is a temporary fix until we have a larger discussion around the +# challenges raised in https://gitlab.com/gitlab-org/gitlab/-/issues/300104 +require Rails.root.join('app', 'experiments', 'application_experiment') +class ApplicationExperiment # rubocop:disable Gitlab/NamespacedClass + def initialize(...) + super(...) + Feature.persist_used!(feature_flag_name) + end +end + RSpec.configure do |config| config.include StubSnowplow, :experiment diff --git a/spec/support/helpers/stub_experiments.rb b/spec/support/helpers/stub_experiments.rb index 8995b8f5f7b..408d16a7c08 100644 --- a/spec/support/helpers/stub_experiments.rb +++ b/spec/support/helpers/stub_experiments.rb @@ -11,6 +11,7 @@ module StubExperiments allow(Gitlab::Experimentation).to receive(:active?).and_call_original experiments.each do |experiment_key, enabled| + Feature.persist_used!("#{experiment_key}#{feature_flag_suffix}") allow(Gitlab::Experimentation).to receive(:active?).with(experiment_key) { enabled } end end @@ -25,6 +26,7 @@ module StubExperiments allow(Gitlab::Experimentation).to receive(:in_experiment_group?).and_call_original experiments.each do |experiment_key, enabled| + Feature.persist_used!("#{experiment_key}#{feature_flag_suffix}") allow(Gitlab::Experimentation).to receive(:in_experiment_group?).with(experiment_key, anything) { enabled } end end diff --git a/spec/support/helpers/stubbed_feature.rb b/spec/support/helpers/stubbed_feature.rb index 4113a28182b..67ceb7d9b35 100644 --- a/spec/support/helpers/stubbed_feature.rb +++ b/spec/support/helpers/stubbed_feature.rb @@ -4,6 +4,14 @@ module StubbedFeature extend ActiveSupport::Concern + prepended do + cattr_reader(:persist_used) do + # persist feature flags in CI + # nil: indicates that we do not want to persist used feature flags + Gitlab::Utils.to_boolean(ENV['CI']) ? {} : nil + end + end + class_methods do # Turn stubbed feature flags on or off. def stub=(stub) @@ -33,6 +41,8 @@ module StubbedFeature feature_flag = super return feature_flag unless stub? + persist_used!(args.first) + # If feature flag is not persisted we mark the feature flag as enabled # We do `m.call` as we want to validate the execution of method arguments # and a feature flag state if it is not persisted @@ -42,5 +52,17 @@ module StubbedFeature feature_flag end + + # This method creates a temporary file in `tmp/feature_flags` + # if feature flag was touched during execution + def persist_used!(name) + return unless persist_used + return if persist_used[name] + + persist_used[name] = true + FileUtils.touch( + Rails.root.join('tmp', 'feature_flags', name.to_s + ".used") + ) + end end end |