Skip to content

Commit

Permalink
Fix inherit_mode for third-party gems
Browse files Browse the repository at this point in the history
fixes #1126

See palkan/action_policy#103 (comment)

If a third-party gem, e.g. `action_policy`, defines their
`config/default.yml` and supplements to the default RSpec DSL that we
provide in our `config/default.yml`, `rubocop`'s behaviour is to
actually override the configuration, as `inherit_mode` is `override` for
lists by default.

RuboCop has recently [fixed the problem with ignored
`inherit_mode`](rubocop/rubocop#9952) and we've
[bumped to use `rubocop` version that includes the
fix](rubocop/rubocop-rspec#1181), but we haven't
adjusted our `config/default.yml` to merge by default.

This is causing both user project RSpec DSL configuration and
third-party gem RSpec DSL configuration to override our default,
rendering our cops blind.

### Example

A new project

```ruby
# Gemfile
source 'https://rubygems.org'

gem 'action_policy'
gem 'rubocop-rspec'
```

```yml
# .rubocop.yml
require:
  - rubocop-rspec

inherit_gem:
  action_policy: config/rubocop-rspec.yml
```

```ruby
# spec/a_spec.rb
RSpec.describe 'A' do
  it_behaves_like 'a'
  it_behaves_like 'a'

  describe_rule :show? do
    succeed 'when post is published'
    succeed 'when post is published'
  end
end
```

Ideally, both the duplicated `it_behaves_like` and `succeed` should be
detected. However, `action_policy`'s `Includes/Examples` setting
overrides ours, and `it_behaves_like` disappears from this list. As a
result, `rubocop` only detects the duplication of `succeed`, but not of
`it_behaves_like`.
  • Loading branch information
pirj committed Oct 5, 2021
1 parent 291c5d7 commit 3679c8b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

* Fix merging RSpec DSL configuration from third-party gems. ([@pirj][])

## 2.5.0 (2021-09-21)

* Declare autocorrect as unsafe for `ExpectChange`. ([@francois-ferrandis][])
Expand Down
27 changes: 27 additions & 0 deletions config/default.yml
Expand Up @@ -5,7 +5,20 @@ RSpec:
- "**/*_spec.rb"
- "**/spec/**/*"
Language:
inherit_mode:
merge:
- Expectations
- Helpers
- Hooks
- HookScopes
- Runners
- Subjects
ExampleGroups:
inherit_mode:
merge:
- Regular
- Skipped
- Focused
Regular:
- describe
- context
Expand All @@ -20,6 +33,12 @@ RSpec:
- fcontext
- ffeature
Examples:
inherit_mode:
merge:
- Regular
- Skipped
- Focused
- Pending
Regular:
- it
- specify
Expand Down Expand Up @@ -62,6 +81,10 @@ RSpec:
- all
- suite
Includes:
inherit_mode:
merge:
- Examples
- Context
Examples:
- it_behaves_like
- it_should_behave_like
Expand All @@ -73,6 +96,10 @@ RSpec:
- to_not
- not_to
SharedGroups:
inherit_mode:
merge:
- Examples
- Context
Examples:
- shared_examples
- shared_examples_for
Expand Down

0 comments on commit 3679c8b

Please sign in to comment.