Skip to content

Commit 92ecd3e

Browse files
author
Chris Hulton
committed
Removes duplicate definition of skip? method
This fixes a bug where issues were being reported when the thresholds were not being met, due to a second definition of `skip?` replacing the correct one. This also improves the specs to properly cover the behavior.
1 parent 8ea6ef3 commit 92ecd3e

File tree

2 files changed

+81
-40
lines changed

2 files changed

+81
-40
lines changed

lib/cc/engine/analyzers/reporter.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,6 @@ def insufficient_occurrence?(violation)
152152
(violation.occurrences + 1) < language_strategy.count_threshold
153153
end
154154

155-
def skip?(violation)
156-
insufficient_occurrence?(violation) || check_disabled?(violation)
157-
end
158-
159-
def insufficient_occurrence?(violation)
160-
(violation.occurrences + 1) < language_strategy.count_threshold
161-
end
162-
163155
def check_disabled?(violation)
164156
!engine_config.check_enabled?(
165157
violation.fingerprint_check_name,

spec/cc/engine/analyzers/ruby/main_spec.rb

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -187,44 +187,93 @@ def self.from_remediation_amount(amount)
187187
expect(issues.length).to eq(0)
188188
end
189189

190-
it "respects the per-check mass thresholds" do
191-
create_source_file("foo.rb", <<-EORUBY)
192-
def identical
193-
puts "identical \#{thing.bar} \#{other.fun} \#{moo ? "moo" : "cluck"}"
194-
puts "identical \#{thing.bar} \#{other.fun} \#{moo ? "moo" : "cluck"}"
195-
puts "identical \#{thing.bar} \#{other.fun} \#{moo ? "moo" : "cluck"}"
196-
end
197-
describe 'similar1' do
198-
before { subject.type = 'js' }
199-
it 'returns true' do
200-
expect(subject.ruby?).to be true
190+
context "per-check mass thresholds" do
191+
before do
192+
create_source_file("foo.rb", <<-EORUBY)
193+
# identical mass of 15
194+
def identical
195+
puts "identical \#{thing.bar} \#{other.fun} \#{moo ? "moo" : "cluck"}"
196+
puts "identical \#{thing.bar} \#{other.fun} \#{moo ? "moo" : "cluck"}"
201197
end
202-
end
203-
describe 'similar2' do
204-
before { subject.type = 'js' }
205-
it 'returns true' do
206-
expect(subject.js?).to be true
198+
199+
# similar mass of 10
200+
def similar1
201+
foo.select { |f| if f.baz?; 10; end }
207202
end
203+
204+
def similar2
205+
foo.select { |f| if f.bar?; 15; end }
206+
end
207+
EORUBY
208+
end
209+
210+
it "reports identical issues when only that threshold exceeded" do
211+
config = CC::Engine::Analyzers::EngineConfig.new({
212+
"config" => {
213+
"languages" => %w[ruby],
214+
"checks" => {
215+
"identical-code" => { "config" => { "threshold" => 5 } },
216+
"similar-code" => { "config" => { "threshold" => 20 } },
217+
},
218+
},
219+
})
220+
221+
issues = run_engine(config).strip.split("\0")
222+
expect(issues.length).to eq(2)
223+
224+
issues.each do |issue|
225+
expect(JSON.parse(issue)["check_name"]).to eq("identical-code")
208226
end
209-
EORUBY
227+
end
210228

211-
config = CC::Engine::Analyzers::EngineConfig.new({
212-
"config" => {
213-
"languages" => %w[ruby],
214-
"checks" => {
215-
"identical-code" => { "config" => { "threshold" => 5 } },
216-
"similar-code" => { "config" => { "threshold" => 20 } },
229+
it "reports similar issues when only that threshold exceeded" do
230+
config = CC::Engine::Analyzers::EngineConfig.new({
231+
"config" => {
232+
"languages" => %w[ruby],
233+
"checks" => {
234+
"identical-code" => { "config" => { "threshold" => 20 } },
235+
"similar-code" => { "config" => { "threshold" => 5 } },
236+
},
217237
},
218-
},
219-
})
220-
output = run_engine(config).strip.split("\0").first.strip
221-
json = JSON.parse(output)
238+
})
222239

223-
expect(json["check_name"]).to eq "identical-code"
224-
expect(json["location"]).to eq({
225-
"path" => "foo.rb",
226-
"lines" => { "begin" => 2, "end" => 2 },
227-
})
240+
issues = run_engine(config).strip.split("\0")
241+
expect(issues.length).to eq(2)
242+
243+
issues.each do |issue|
244+
expect(JSON.parse(issue)["check_name"]).to eq("similar-code")
245+
end
246+
end
247+
248+
it "reports similar and identical issues when both thresholds exceeded" do
249+
config = CC::Engine::Analyzers::EngineConfig.new({
250+
"config" => {
251+
"languages" => %w[ruby],
252+
"checks" => {
253+
"identical-code" => { "config" => { "threshold" => 5 } },
254+
"similar-code" => { "config" => { "threshold" => 5 } },
255+
},
256+
},
257+
})
258+
259+
issues = run_engine(config).strip.split("\0")
260+
expect(issues.length).to eq(4)
261+
end
262+
263+
it "reports no issues when neither threshold exceeded" do
264+
config = CC::Engine::Analyzers::EngineConfig.new({
265+
"config" => {
266+
"languages" => %w[ruby],
267+
"checks" => {
268+
"identical-code" => { "config" => { "threshold" => 20 } },
269+
"similar-code" => { "config" => { "threshold" => 20 } },
270+
},
271+
},
272+
})
273+
274+
issues = run_engine(config).strip.split("\0")
275+
expect(issues.length).to eq(0)
276+
end
228277
end
229278
end
230279

0 commit comments

Comments
 (0)