Skip to content

RSpec/InvalidFeatureCategory: Validate feature categories in specs

Peter Leitzen requested to merge pl-rubocop-invalid-feature-category into master

What does this MR do and why?

This MR adds a new 👮 rule RSpec/InvalidFeatureCategory to validate feature_category in spec files.

# bad
RSpec.describe 'foo', feature_category: :invalid do
end
                                                      
RSpec.describe 'foo', feature_category: :not_owned do
end
                                                      
# good                                                   
RSpec.describe 'foo', feature_category: :wiki do
end
      
# good because custom                                                
RSpec.describe 'foo', feature_category: :tooling do
end

Valid feature categories are:

  • Anything defied in config/feature_categories.json
  • Custom definitions like tooling in the cop itself

"Grace period" enabled as per https://docs.gitlab.com/ee/development/rubocop_development_guide.html#enabling-a-new-cop.

Fixes #381882 (closed).

Out of scope

The presence of feature_category in spec files is checked via RSpec/MissingFeatureCategory. See !109481 (merged).

How to set up and validate locally

  1. bundle exec rubocop --only RSpec/InvalidFeatureCategory --parallel -> no offenses
  2. bundle exec rake rubocop:todo:generate[RSpec/InvalidFeatureCategory] -> no changes

Caching

Because this cop depends on the contents config/feature_categories.json we need to make sure RuboCop's cache is invalidated properly if the file changes. This is done by defining external_dependency_checksum menthod in the cop which returns a SHA256 digest used by RuboCop to determine the final cache directory. If this value changes re-inspecting the same spec won't use any cached results.

For example:

# Initial run which caches the results.
bundle exec rubocop spec/finders/members_finder_spec.rb
Inspecting 1 file
.

1 file inspected, no offenses detected


# Make a change
sed -i 's/subgroups/foo/' config/feature_categories.yml

# Run again to see it fail.
bundle exec rubocop spec/finders/members_finder_spec.rb
Inspecting 1 file
C

Offenses:

spec/finders/members_finder_spec.rb:5:49: C: RSpec/InvalidFeatureCategory: Please use a valid feature category. See https://docs.gitlab.com/ee/development/feature_categorization/#rspec-examples.
RSpec.describe MembersFinder, feature_category: :subgroups do
                                                ^^^^^^^^^^

1 file inspected, 1 offense detected

MR acceptance checklist

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

Edited by Peter Leitzen

Merge request reports