Skip to content

Commit ecaf84c

Browse files
authored
Merge pull request #24 from rubocop/add-spec
Add specs for unsafe cops in default_config_spec.rb
2 parents 4b1dd95 + 6cb9f55 commit ecaf84c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

spec/project/default_config_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@
2727
cop_names + namespaces.values
2828
end
2929

30+
let(:unsafe_cops) do
31+
require 'yard'
32+
YARD::Registry.load!
33+
YARD::Registry.all(:class).select do |example|
34+
example.tags.any? { |tag| tag.tag_name == 'safety' }
35+
end
36+
end
37+
38+
let(:unsafe_cop_names) do
39+
unsafe_cops.map do |cop|
40+
dept_and_cop_names =
41+
cop.path.split('::')[2..] # Drop `RuboCop::Cop` from class name.
42+
dept_and_cop_names.join('/')
43+
end
44+
end
45+
3046
def cop_configuration(config_key)
3147
cop_names.map do |cop_name|
3248
cop_config = default_config[cop_name]
@@ -76,4 +92,31 @@ def cop_configuration(config_key)
7692
expect(cop_configuration('Enabled'))
7793
.to all be(true).or(be(false)).or(eq('pending'))
7894
end
95+
96+
it 'does not include unnecessary `SafeAutoCorrect: false`' do
97+
cop_names.each do |cop_name|
98+
next unless default_config.dig(cop_name, 'Safe') == false
99+
100+
safe_autocorrect = default_config.dig(cop_name, 'SafeAutoCorrect')
101+
102+
expect(safe_autocorrect).not_to(
103+
be(false),
104+
"`#{cop_name}` has unnecessary `SafeAutoCorrect: false` config."
105+
)
106+
end
107+
end
108+
109+
it 'is expected that all cops documented with `@safety` are `Safe: false`' \
110+
' or `SafeAutoCorrect: false`' do
111+
unsafe_cop_names.each do |cop_name|
112+
unsafe = default_config[cop_name]['Safe'] == false ||
113+
default_config[cop_name]['SafeAutoCorrect'] == false
114+
expect(unsafe).to(
115+
be(true),
116+
"`#{cop_name}` cop should be set `Safe: false` or " \
117+
'`SafeAutoCorrect: false` because `@safety` YARD tag exists.'
118+
)
119+
YARD::Registry.clear
120+
end
121+
end
79122
end

0 commit comments

Comments
 (0)