Shared examples must not have a `:feature_category` metadata since that would automatically include these to all examples with the same `:feature_category`

Problem

In !109075 (merged), I noticed a very weird behavior that I can reproduce locally with the following:

$ bin/rspec -f doc 'spec/requests/api/projects_spec.rb:188' 'spec/requests/self_monitoring_project_spec.rb[1:1:1]'

Running via Spring preloader in process 77678
Run options: include {:focus=>true, :locations=>{"./spec/requests/api/projects_spec.rb"=>[188]}, :ids=>{"./spec/requests/self_monitoring_project_spec.rb"=>["1:1:1"]}}

Test environment set up in 10.170598 seconds

API::Projects
  GET /projects
    when unauthenticated
      behaves like projects response
Missing metadata feature_category: ./spec/requests/api/projects_spec.rb:153 See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#feature-category-metadata
        returns an array of projects
Missing metadata feature_category: ./spec/requests/api/projects_spec.rb:162 See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#feature-category-metadata
        returns the proper security headers

Self-Monitoring project requests
  when the languages haven't been detected yet
    returns expected language values (FAILED - 1)

Failures:

  1) Self-Monitoring project requests when the languages haven't been detected yet returns expected language values
     Failure/Error: return super unless match_data = method_name.match(HELPER_METHOD_PATTERN)
     
     NameError:
       undefined local variable or method `project' for #<RSpec::ExampleGroups::SelfMonitoringProjectRequests::WhenTheLanguagesHavenTBeenDetectedYet "returns expected language values" (./spec/requests/api/projects_spec.rb:18)>
       Did you mean?  project_title
     Shared Example Group: "languages and percentages JSON response" called from ./spec/requests/self_monitoring_project_spec.rb:5
     # ./spec/support/helpers/next_found_instance_of.rb:8:in `method_missing'
     # ./spec/requests/api/projects_spec.rb:9:in `block (2 levels) in <main>'
     # ./spec/spec_helper.rb:512:in `block (3 levels) in <main>'
     # ./spec/support/sidekiq_middleware.rb:18:in `with_sidekiq_server_middleware'
     # ./spec/spec_helper.rb:504:in `block (2 levels) in <main>'
     # ./spec/spec_helper.rb:500:in `block (3 levels) in <main>'
     # ./lib/gitlab/application_context.rb:59:in `with_raw_context'
     # ./spec/spec_helper.rb:500:in `block (2 levels) in <main>'
     # ./spec/spec_helper.rb:239:in `block (2 levels) in <main>'
     # ./spec/support/sidekiq.rb:17:in `block (3 levels) in <main>'
     # ./spec/support/sidekiq.rb:8:in `gitlab_sidekiq_inline'
     # ./spec/support/sidekiq.rb:17:in `block (2 levels) in <main>'
     # ./spec/support/system_exit_detected.rb:7:in `block (2 levels) in <main>'
     # ./spec/support/database/prevent_cross_joins.rb:106:in `block (3 levels) in <main>'
     # ./spec/support/database/prevent_cross_joins.rb:60:in `with_cross_joins_prevented'
     # ./spec/support/database/prevent_cross_joins.rb:106:in `block (2 levels) in <main>'
     # -e:1:in `<main>'

Finished in 27.07 seconds (files took 3.34 seconds to load)
3 examples, 1 failure

Failed examples:

rspec './spec/requests/self_monitoring_project_spec.rb[1:1:1]' # Self-Monitoring project requests when the languages haven't been detected yet returns expected language values

The when the languages haven't been detected yet returns expected language values shared example is defined as a top-level shared example in spec/requests/api/projects_spec.rb.

Debugging steps

  1. If I move it to be a nested shared example inside the RSpec.describe block in that file, then bin/rspec -f doc 'spec/requests/api/projects_spec.rb:188' 'spec/requests/self_monitoring_project_spec.rb[1:1:1]' passes.
  2. If I remove the feature_category: :projects metadata from spec/requests/self_monitoring_project_spec.rb, then bin/rspec -f doc 'spec/requests/api/projects_spec.rb:188' 'spec/requests/self_monitoring_project_spec.rb[1:1:1]' passes.
  3. If I remove the feature_category: :projects metadata from the shared example in spec/requests/api/projects_spec.rb, then bin/rspec -f doc 'spec/requests/api/projects_spec.rb:188' 'spec/requests/self_monitoring_project_spec.rb[1:1:1]' passes.

Cause

I actually found the cause at https://relishapp.com/rspec/rspec-core/v/3-2/docs/example-groups/shared-examples#sharing-metadata-automatically-includes-shared-example-groups:

Screenshot_2023-01-25_at_15.52.12

Long-term fix

We should ensure with a new cop shared examples don't have a :feature_category metadata!