From 726c3a2d1306385d19c3cb02e0362088d0877dff Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Sun, 3 Oct 2021 11:18:11 +0300 Subject: [PATCH] Fix inherit_mode for third-party gems fixes #1126 See https://github.com/palkan/action_policy/issues/103#issuecomment-932886450 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`](https://github.com/rubocop/rubocop/pull/9952) and we've [bumped to use `rubocop` version that includes the fix](https://github.com/rubocop/rubocop-rspec/pull/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`. --- CHANGELOG.md | 2 ++ config/default.yml | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11d7bd71..94a13961 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) diff --git a/config/default.yml b/config/default.yml index 5f2dd7f9..1f3c5e94 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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 @@ -20,6 +33,12 @@ RSpec: - fcontext - ffeature Examples: + inherit_mode: + merge: + - Regular + - Skipped + - Focused + - Pending Regular: - it - specify @@ -62,6 +81,10 @@ RSpec: - all - suite Includes: + inherit_mode: + merge: + - Examples + - Context Examples: - it_behaves_like - it_should_behave_like @@ -73,6 +96,10 @@ RSpec: - to_not - not_to SharedGroups: + inherit_mode: + merge: + - Examples + - Context Examples: - shared_examples - shared_examples_for