Create RuboCop rule to enforce shared_examples file placement standards
Background & Context
This follow-up issue was created from a discussion in !201411 (merged) where @dstull suggested creating a RuboCop rule to programmatically enforce GitLab's shared examples best practices.
The original merge request involved moving shared examples files from spec/requests/api/graphql/projects/shared_spec.rb to the proper location at spec/support/shared_examples/requests/api/graphql/projects_examples.rb, highlighting the need for automated enforcement of this standard.
Current State
GitLab has established best practices for shared examples placement:
- Inline shared examples: Can be declared inline if only used in one spec file
-
Reusable shared examples: Should be placed under
spec/support/shared_examples/ - Subfolder organization: Can be placed in subfolders if they apply to specific spec types (features, requests, etc.)
-
Naming convention: Files should have descriptive names ending with
_examples.rb
However, there are currently violations of this standard throughout the codebase:
Examples of current violations:
-
spec/lib/feature/shared_spec.rb- Contains regular specs, not shared examples -
spec/lib/gitlab/health_checks/simple_check_shared.rb- Contains shared context that should be inspec/support/ - Various
*_shared_spec.rbfiles in different locations
Requested Changes
Create a new RuboCop rule that automatically detects and flags violations of the shared examples placement standards:
-
Detect misplaced shared examples: Files containing
RSpec.shared_examplesoutside ofspec/support/shared_examples/ -
Detect incorrect naming: Files with shared examples that don't follow the
*_examples.rbnaming convention - Provide auto-correction: Where possible, suggest the correct file path and name
- Handle edge cases: Account for inline shared examples that are acceptable
Detection Logic
The cop should:
-
Identify shared examples declarations: Look for
RSpec.shared_examples,RSpec.shared_examples_for, andRSpec.shared_context(maybe this one) -
Check file location: Verify if the file is under
spec/support/shared_examples/or theeedirectory for it or if it's inline (single file usage) -
Validate naming: Ensure files with shared examples follow the
*_examples.rbconvention - Cross-reference usage: Determine if shared examples are used across multiple files
Auto-correction Strategy
For files that can be auto-corrected:
- Suggest moving to
spec/support/shared_examples/with appropriate subfolder structure - Recommend proper naming convention
Acceptance Criteria
-
RuboCop cop detects shared examples files in incorrect locations -
Cop flags files with incorrect naming conventions ( *_shared.rbvs*_examples.rb) -
Cop provides clear, actionable error messages with links to documentation -
Auto-correction suggests proper file paths when possible -
Cop distinguishes between inline shared examples (acceptable) and reusable ones (must be in spec/support/) -
Integration with existing GitLab RuboCop configuration -
Comprehensive test coverage for the new cop -
Documentation updates explaining the new rule
Technical Context
Affected Files:
-
rubocop/cop/gitlab/rspec/shared_examples_placement.rb(new) -
spec/rubocop/cop/gitlab/rspec/shared_examples_placement_spec.rb(new) -
.rubocop.yml(configuration) - Various existing spec files that violate the standard
Implementation Considerations:
- development guides: https://docs.gitlab.com/development/rubocop_development_guide/
- similarly recent developed cop: https://gitlab.com/gitlab-org/gitlab/blob/e3633e34994cde83493583ec092bcad928cb09ad/rubocop/cop/gitlab/rspec/misplaced_ee_spec_file.rb#L23-23
Related Issues:
- Original discussion: !201411 (merged)