From 17e472820752b3510e337ef63a881590f7e57c41 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 +++++++++++++++++++ .../third_party_rspec_syntax_extensions.adoc | 2 ++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11d7bd710..94a13961a 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 5f2dd7f99..1f3c5e94f 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 diff --git a/docs/modules/ROOT/pages/third_party_rspec_syntax_extensions.adoc b/docs/modules/ROOT/pages/third_party_rspec_syntax_extensions.adoc index 960a600b1..1b78f6d9d 100644 --- a/docs/modules/ROOT/pages/third_party_rspec_syntax_extensions.adoc +++ b/docs/modules/ROOT/pages/third_party_rspec_syntax_extensions.adoc @@ -8,6 +8,8 @@ RuboCop https://docs.rubocop.org/rubocop/configuration.html#inheriting-configura == Packaging configuration for RuboCop RSpec +NOTE: Due to a [bug](https://github.com/rubocop/rubocop-rspec/issues/1126), this feature doesn't work properly for `rubocop-rspec` 2.5.0 and earlier versions. + For a third-party gem, it's sufficient to follow three steps: 1. Provide a configuration file (e.g. `.rubocop_rspec_alias_config.yml` or `config/rubocop-rspec.yml`).