Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config formatter coverage #2031

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions lib/rubocop/rspec/config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ module RuboCop
module RSpec
# Builds a YAML config file from two config hashes
class ConfigFormatter
EXTENSION_ROOT_DEPARTMENT = %r{^(RSpec/)}.freeze
SUBDEPARTMENTS = [].freeze
AMENDMENTS = %(Metrics/BlockLength)
EXTENSION_ROOT_DEPARTMENT = %r{^RSpec/}.freeze
COP_DOC_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/'

def initialize(config, descriptions)
Expand All @@ -18,18 +16,15 @@ def initialize(config, descriptions)

def dump
YAML.dump(unified_config)
.gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\1")
.gsub(*AMENDMENTS, "\n\\0")
.gsub(/^(\s+)- /, '\1 - ')
.gsub('"~"', '~')
.gsub(%r{^\w+/}, "\n\\0") # Add an extra newline before each cop.
.gsub(/^(\s+)- /, '\1 - ') # Add 2 spaces before each array element.
.gsub('"~"', '~') # Don't quote tilde, YAML's null value.
end

private

def unified_config
cops.each_with_object(config.dup) do |cop, unified|
next if SUBDEPARTMENTS.include?(cop) || AMENDMENTS.include?(cop)

replace_nil(unified[cop])
unified[cop].merge!(descriptions.fetch(cop))
unified[cop]['Reference'] = reference(cop)
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/rspec/config_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
'AllCops' => {
'Setting' => 'forty two'
},
'Metrics/BlockLength' => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice the spec explains the behavior now!

'Exclude' => [
'**/*_spec.rb',
'**/spec/**/*'
]
},
'RSpec/Foo' => {
'Config' => 2,
'Enabled' => true
Expand All @@ -18,6 +24,7 @@
},
'RSpec/Baz' => {
'Enabled' => true,
'NegatedMatcher' => '~',
'StyleGuide' => '#buzz'
}
}
Expand Down Expand Up @@ -45,6 +52,11 @@
AllCops:
Setting: forty two

Metrics/BlockLength:
Exclude:
- "**/*_spec.rb"
- "**/spec/**/*"

RSpec/Foo:
Config: 2
Enabled: true
Expand All @@ -59,6 +71,7 @@

RSpec/Baz:
Enabled: true
NegatedMatcher: ~
StyleGuide: "#buzz"
Description: Woof
Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Baz
Expand Down