diff options
Diffstat (limited to 'spec/rubocop/cop/rspec')
-rw-r--r-- | spec/rubocop/cop/rspec/env_mocking_spec.rb | 61 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/invalid_feature_category_spec.rb | 120 | ||||
-rw-r--r-- | spec/rubocop/cop/rspec/missing_feature_category_spec.rb | 31 |
3 files changed, 212 insertions, 0 deletions
diff --git a/spec/rubocop/cop/rspec/env_mocking_spec.rb b/spec/rubocop/cop/rspec/env_mocking_spec.rb new file mode 100644 index 00000000000..189fccf483a --- /dev/null +++ b/spec/rubocop/cop/rspec/env_mocking_spec.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' + +require_relative '../../../../rubocop/cop/rspec/env_mocking' + +RSpec.describe RuboCop::Cop::RSpec::EnvMocking, feature_category: :tooling do + offense_call_brackets_string_quotes = %(allow(ENV).to receive(:[]).with('FOO').and_return('bar')) + offense_call_brackets_variables = %(allow(ENV).to receive(:[]).with(key).and_return(value)) + + offense_call_fetch_string_quotes = %(allow(ENV).to receive(:fetch).with('FOO').and_return('bar')) + offense_call_fetch_variables = %(allow(ENV).to receive(:fetch).with(key).and_return(value)) + + offense_call_root_env_variables = %(allow(::ENV).to receive(:[]).with(key).and_return(value)) + offense_call_key_value_method_calls = + %(allow(ENV).to receive(:[]).with(fetch_key(object)).and_return(fetch_value(object))) + + acceptable_mocking_other_methods = %(allow(ENV).to receive(:foo).with("key").and_return("value")) + + let(:source_file) { 'spec/foo_spec.rb' } + + shared_examples 'cop offense mocking the ENV constant correctable with stub_env' do |content, autocorrected_content| + it "registers an offense for `#{content}` and corrects", :aggregate_failures do + expect_offense(<<~CODE, content: content) + %{content} + ^{content} Don't mock the ENV, use `stub_env` instead. + CODE + + expect_correction(<<~CODE) + #{autocorrected_content} + CODE + end + end + + context 'with mocking bracket calls ' do + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_brackets_string_quotes, %(stub_env('FOO', 'bar')) + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_brackets_variables, %(stub_env(key, value)) + end + + context 'with mocking fetch calls' do + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_fetch_string_quotes, %(stub_env('FOO', 'bar')) + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_fetch_variables, %(stub_env(key, value)) + end + + context 'with other special cases and variations' do + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_root_env_variables, %(stub_env(key, value)) + it_behaves_like 'cop offense mocking the ENV constant correctable with stub_env', + offense_call_key_value_method_calls, %(stub_env(fetch_key(object), fetch_value(object))) + end + + context 'with acceptable cases' do + it 'does not register an offense for mocking other methods' do + expect_no_offenses(acceptable_mocking_other_methods) + end + end +end diff --git a/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb new file mode 100644 index 00000000000..0d2fd029a13 --- /dev/null +++ b/spec/rubocop/cop/rspec/invalid_feature_category_spec.rb @@ -0,0 +1,120 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require 'rspec-parameterized' + +require_relative '../../../../rubocop/cop/rspec/invalid_feature_category' + +RSpec.describe RuboCop::Cop::RSpec::InvalidFeatureCategory, feature_category: :tooling do + shared_examples 'feature category validation' do |valid_category| + it 'flags invalid feature category in top level example group' do + expect_offense(<<~RUBY, invalid: invalid_category) + RSpec.describe 'foo', feature_category: :%{invalid}, foo: :bar do + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. + end + RUBY + end + + it 'flags invalid feature category in nested context' do + expect_offense(<<~RUBY, valid: valid_category, invalid: invalid_category) + RSpec.describe 'foo', feature_category: :%{valid} do + context 'bar', foo: :bar, feature_category: :%{invalid} do + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. + end + end + RUBY + end + + it 'flags invalid feature category in examples' do + expect_offense(<<~RUBY, valid: valid_category, invalid: invalid_category) + RSpec.describe 'foo', feature_category: :%{valid} do + it 'bar', feature_category: :%{invalid} do + ^^{invalid} Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. + end + end + RUBY + end + + it 'does not flag if feature category is valid' do + expect_no_offenses(<<~RUBY) + RSpec.describe 'foo', feature_category: :#{valid_category} do + context 'bar', feature_category: :#{valid_category} do + it 'baz', feature_category: :#{valid_category} do + end + end + end + RUBY + end + + it 'suggests an alternative' do + mistyped = make_typo(valid_category) + + expect_offense(<<~RUBY, invalid: mistyped, valid: valid_category) + RSpec.describe 'foo', feature_category: :%{invalid} do + ^^{invalid} Please use a valid feature category. Did you mean `:%{valid}`? See [...] + end + RUBY + end + + def make_typo(string) + "#{string}#{string[-1]}" + end + end + + let(:invalid_category) { :invalid_category } + + context 'with categories defined in config/feature_categories.yml' do + where(:valid_category) do + YAML.load_file(rails_root_join('config/feature_categories.yml')) + end + + with_them do + it_behaves_like 'feature category validation', params[:valid_category] + end + end + + context 'with custom categories' do + it_behaves_like 'feature category validation', 'tooling' + it_behaves_like 'feature category validation', 'shared' + end + + it 'flags invalid feature category for non-symbols' do + expect_offense(<<~RUBY, invalid: invalid_category) + RSpec.describe 'foo', feature_category: "%{invalid}" do + ^^^{invalid} Please use a symbol as value. + end + + RSpec.describe 'foo', feature_category: 42 do + ^^ Please use a symbol as value. + end + RUBY + end + + it 'does not flag use of invalid categories in non-example code' do + # See https://gitlab.com/gitlab-org/gitlab/-/issues/381882#note_1265865125 + expect_no_offenses(<<~RUBY) + RSpec.describe 'A spec' do + let(:api_handler) do + Class.new(described_class) do + namespace '/test' do + get 'hello', feature_category: :foo, urgency: :#{invalid_category} do + end + end + end + end + + it 'tests something' do + Gitlab::ApplicationContext.with_context(feature_category: :#{invalid_category}) do + payload = generator.generate(exception, extra) + end + end + end + RUBY + end + + describe '#external_dependency_checksum' do + it 'returns a SHA256 digest used by RuboCop to invalid cache' do + expect(cop.external_dependency_checksum).to match(/^\h{64}$/) + end + end +end diff --git a/spec/rubocop/cop/rspec/missing_feature_category_spec.rb b/spec/rubocop/cop/rspec/missing_feature_category_spec.rb new file mode 100644 index 00000000000..41b1d2b8580 --- /dev/null +++ b/spec/rubocop/cop/rspec/missing_feature_category_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'rubocop_spec_helper' +require_relative '../../../../rubocop/cop/rspec/missing_feature_category' + +RSpec.describe RuboCop::Cop::RSpec::MissingFeatureCategory, feature_category: :tooling do + it 'flags missing feature category in top level example group' do + expect_offense(<<~RUBY) + RSpec.describe 'foo' do + ^^^^^^^^^^^^^^^^^^^^ Please add missing feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. + end + + RSpec.describe 'foo', some: :tag do + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Please add missing feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples. + end + RUBY + end + + it 'does not flag if feature category is defined' do + expect_no_offenses(<<~RUBY) + RSpec.describe 'foo', feature_category: :foo do + end + + RSpec.describe 'foo', some: :tag, feature_category: :foo do + end + + RSpec.describe 'foo', feature_category: :foo, some: :tag do + end + RUBY + end +end |