Skip to content

Commit

Permalink
Add specs for unsafe cops in default_config_spec.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
ydah committed Apr 10, 2024
1 parent 3ee2486 commit 66fe5a3
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions spec/project/default_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@
cop_names + namespaces.values
end

let(:unsafe_cops) do
require 'yard'
YARD::Registry.load!
YARD::Registry.all(:class).select do |example|
example.tags.any? { |tag| tag.tag_name == 'safety' }
end
end

let(:unsafe_cop_names) do
unsafe_cops.map do |cop|
dept_and_cop_names =
cop.path.split('::')[2..] # Drop `RuboCop::Cop` from class name.
dept_and_cop_names.join('/')
end
end

def cop_configuration(config_key)
cop_names.map do |cop_name|
cop_config = default_config[cop_name]
Expand Down Expand Up @@ -74,4 +90,31 @@ def cop_configuration(config_key)
expect(cop_configuration('Enabled'))
.to all be(true).or(be(false)).or(eq('pending'))
end

it 'does not include unnecessary `SafeAutoCorrect: false`' do
cop_names.each do |cop_name|
next unless default_config.dig(cop_name, 'Safe') == false

safe_autocorrect = default_config.dig(cop_name, 'SafeAutoCorrect')

expect(safe_autocorrect).not_to(
be(false),
"`#{cop_name}` has unnecessary `SafeAutoCorrect: false` config."
)
end
end

it 'is expected that all cops documented with `@safety` are `Safe: false`' \
' or `SafeAutoCorrect: false`' do
unsafe_cop_names.each do |cop_name|
unsafe = default_config[cop_name]['Safe'] == false ||
default_config[cop_name]['SafeAutoCorrect'] == false
expect(unsafe).to(
be(true),
"`#{cop_name}` cop should be set `Safe: false` or " \
'`SafeAutoCorrect: false` because `@safety` YARD tag exists.'
)
YARD::Registry.clear
end
end
end

0 comments on commit 66fe5a3

Please sign in to comment.