From 2e20ce18021fe9c6e0887b07dcd82fbf8b2020a5 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Wed, 30 Jun 2021 19:06:41 +0300 Subject: [PATCH] Fix default config for sub-departments It turns out that the default config does not apply for sub-departments. It is weird on many levels: - RSpec DSL configuration didn't work - - RSpec DSL doesn't work as intended anyway #1126 - if users would re-define RSpec DSL, they would have to do it for all sub-departments, as YAML defaults are only for the default config - if users would re-define RSpec DSL and use YAML defaults, too, the merging of the resulting config is undetermined Still, I believe having `Include` to work correctly is the most important. fixes #1160 --- CHANGELOG.md | 1 + config/default.yml | 19 +++++++++++++++++-- lib/rubocop/rspec/config_formatter.rb | 3 +++ spec/project/default_config_spec.rb | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62f2100e4..a4ed3a451 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ * Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][]) * Add `RSpec/Rails/AvoidSetupHook` cop. ([@paydaylight][]) * Fix false negative in `RSpec/ExpectChange` cop with block style and chained method call. ([@tejasbubane][]) +* Fix `Include` configuration for sub-departments. ([@pirj][]) ## 2.3.0 (2021-04-28) diff --git a/config/default.yml b/config/default.yml index 1f3c5e94f..e52ba7411 100644 --- a/config/default.yml +++ b/config/default.yml @@ -1,10 +1,10 @@ --- RSpec: Enabled: true - Include: + Include: &include - "**/*_spec.rb" - "**/spec/**/*" - Language: + Language: &language inherit_mode: merge: - Expectations @@ -745,6 +745,11 @@ RSpec/Yield: VersionAdded: '1.32' StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Yield +RSpec/Capybara: + Enabled: true + Include: *include + Language: *language + RSpec/Capybara/CurrentPathExpectation: Description: Checks that no expectations are set on Capybara's `current_path`. Enabled: true @@ -767,6 +772,11 @@ RSpec/Capybara/VisibilityMatcher: VersionChanged: '2.0' StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Capybara/VisibilityMatcher +RSpec/FactoryBot: + Enabled: true + Include: *include + Language: *language + RSpec/FactoryBot/AttributeDefinedStatically: Description: Always declare attribute values as blocks. Enabled: true @@ -806,6 +816,11 @@ RSpec/FactoryBot/FactoryClassName: VersionChanged: '2.0' StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName +RSpec/Rails: + Enabled: true + Include: *include + Language: *language + RSpec/Rails/AvoidSetupHook: Description: Checks that tests use RSpec `before` hook over Rails `setup` method. Enabled: pending diff --git a/lib/rubocop/rspec/config_formatter.rb b/lib/rubocop/rspec/config_formatter.rb index bf8c0f2a3..11447917a 100644 --- a/lib/rubocop/rspec/config_formatter.rb +++ b/lib/rubocop/rspec/config_formatter.rb @@ -7,6 +7,7 @@ module RSpec # Builds a YAML config file from two config hashes class ConfigFormatter EXTENSION_ROOT_DEPARTMENT = %r{^(RSpec/)}.freeze + SUBDEPARTMENTS = %(RSpec/Capybara RSpec/FactoryBot RSpec/Rails) STYLE_GUIDE_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/' def initialize(config, descriptions) @@ -24,6 +25,8 @@ def dump def unified_config cops.each_with_object(config.dup) do |cop, unified| + next if SUBDEPARTMENTS.include?(cop) + unified[cop] = config.fetch(cop) .merge(descriptions.fetch(cop)) .merge('StyleGuide' => STYLE_GUIDE_BASE_URL + cop.sub('RSpec/', '')) diff --git a/spec/project/default_config_spec.rb b/spec/project/default_config_spec.rb index 2a349f881..e5414e0f2 100644 --- a/spec/project/default_config_spec.rb +++ b/spec/project/default_config_spec.rb @@ -29,7 +29,7 @@ end let(:config_keys) do - cop_names + %w[RSpec] + cop_names + %w[RSpec RSpec/Capybara RSpec/FactoryBot RSpec/Rails] end def cop_configuration(config_key)