Skip to content

Commit

Permalink
Merge pull request #2031 from rubocop/config-formatter-coverage
Browse files Browse the repository at this point in the history
Config formatter coverage
  • Loading branch information
bquorning authored Feb 10, 2025
2 parents 01e1f9b + e79ec49 commit 285d92b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
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' => {
'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

0 comments on commit 285d92b

Please sign in to comment.