Skip to content

RSpec/InvalidFeatureCategory: Fix false positives

What does this MR do and why?

Only look in example groups and examples for feature_category: and skip normal test code.

See #381882 (comment 1265865125).

Found while fixing offenses in !111117 (merged).

How to set up and validate locally

rake rubocop:todo:generate[RSpec/InvalidFeatureCategory]

Revert production code and run new examples:

git checkout master -- rubocop/cop/rspec/invalid_feature_category.rb

bin/rspec spec/rubocop/cop/rspec/invalid_feature_category_spec.rb
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

Randomized with seed 8344
F..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................

Failures:

  1) RuboCop::Cop::RSpec::InvalidFeatureCategory does not flag use of invalid categories in non-example code
     Failure/Error:
       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

       expected: "RSpec.describe 'A spec' do\n  let(:api_handler) do\n    Class.new(described_class) do\n      namespa... :invalid_category) do\n      payload = generator.generate(exception, extra)\n    end\n  end\nend\n"
            got: "RSpec.describe 'A spec' do\n  let(:api_handler) do\n    Class.new(described_class) do\n      namespa...ation/#rspec-examples.\n      payload = generator.generate(exception, extra)\n    end\n  end\nend\n"

       (compared using ==)

       Diff:
       @@ -3,6 +3,7 @@
            Class.new(described_class) do
              namespace '/test' do
                get 'hello', feature_category: :foo, urgency: :invalid_category do
       +                                       ^^^^ Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples.
                end
              end
            end
       @@ -10,6 +11,7 @@

          it 'tests something' do
            Gitlab::ApplicationContext.with_context(feature_category: :invalid_category) do
       +                                                              ^^^^^^^^^^^^^^^^^ Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples.
              payload = generator.generate(exception, extra)
            end
          end

     # ./spec/rubocop/cop/rspec/invalid_feature_category_spec.rb:81:in `block (2 levels) in <top (required)>'
     # ./spec/support/system_exit_detected.rb:7:in `block (2 levels) in <top (required)>'

Finished in 0.92194 seconds (files took 1.7 seconds to load)
547 examples, 1 failure

Failed examples:

rspec ./spec/rubocop/cop/rspec/invalid_feature_category_spec.rb:79 # RuboCop::Cop::RSpec::InvalidFeatureCategory does not flag use of invalid categories in non-example code

Randomized with seed 8344
`` `

## MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

* [x] I have evaluated the [MR acceptance checklist](https://docs.gitlab.com/ee/development/code_review.html#acceptance-checklist) for this MR.

<!-- template sourced from https://gitlab.com/gitlab-org/gitlab/-/blob/master/.gitlab/merge_request_templates/Default.md -->
Edited by Peter Leitzen

Merge request reports