Skip to content

Commit

Permalink
Improve spec branch coverage for ConfigFormatter
Browse files Browse the repository at this point in the history
`AMENDMENTS` was defined as a String, not an Array. And splatting the
array into `#gsub` doesn't work if there is more than one element - we
need to use `Regexp.union` instead.
  • Loading branch information
bquorning committed Feb 9, 2025
1 parent aec1799 commit 73bc726
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/rubocop/rspec/config_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module RSpec
# Builds a YAML config file from two config hashes
class ConfigFormatter
EXTENSION_ROOT_DEPARTMENT = %r{^(RSpec/)}.freeze
AMENDMENTS = %(Metrics/BlockLength)
AMENDMENTS = %w[Metrics/BlockLength].freeze
COP_DOC_BASE_URL = 'https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/'

def initialize(config, descriptions)
Expand All @@ -17,8 +17,8 @@ def initialize(config, descriptions)

def dump
YAML.dump(unified_config)
.gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\1")
.gsub(*AMENDMENTS, "\n\\0")
.gsub(EXTENSION_ROOT_DEPARTMENT, "\n\\0")
.gsub(Regexp.union(AMENDMENTS), "\n\\0")
.gsub(/^(\s+)- /, '\1 - ')
.gsub('"~"', '~')
end
Expand All @@ -27,8 +27,6 @@ def dump

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

replace_nil(unified[cop])
unified[cop].merge!(descriptions.fetch(cop))
unified[cop]['Reference'] = reference(cop)
Expand Down
11 changes: 11 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 Down Expand Up @@ -45,6 +51,11 @@
AllCops:
Setting: forty two
Metrics/BlockLength:
Exclude:
- "**/*_spec.rb"
- "**/spec/**/*"
RSpec/Foo:
Config: 2
Enabled: true
Expand Down

0 comments on commit 73bc726

Please sign in to comment.