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

Add specs for unsafe cops in default_config_spec.rb #112

Merged
merged 1 commit into from
Apr 10, 2024
Merged
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
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