Skip to content

Commit b7226a2

Browse files
committed
Use individual check thresholds to filter results
Previous commits enabled config to pick up this config appropiately. This commit adjusts the reporter pipeline to filter out reported violations that don't meet the mass threshold. Flay just takes on threshold in its config, so we calculate the minimumt threshold of both checks & give that to Flay, then filter the results afterwords based on check type.
1 parent f2c3fb9 commit b7226a2

File tree

4 files changed

+69
-8
lines changed

4 files changed

+69
-8
lines changed

lib/cc/engine/analyzers/analyzer_base.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def language
5858
self.class::LANGUAGE
5959
end
6060

61+
def check_mass_threshold(check)
62+
engine_config.mass_threshold_for(language, check) || self.class::DEFAULT_MASS_THRESHOLD
63+
end
64+
6165
def mass_threshold
6266
engine_config.minimum_mass_threshold_for(language) || self.class::DEFAULT_MASS_THRESHOLD
6367
end
@@ -66,8 +70,8 @@ def count_threshold
6670
engine_config.count_threshold_for(language)
6771
end
6872

69-
def calculate_points(mass)
70-
overage = mass - mass_threshold
73+
def calculate_points(violation)
74+
overage = violation.mass - check_mass_threshold(violation.inner_check_name)
7175
base_points + (overage * points_per_overage)
7276
end
7377

lib/cc/engine/analyzers/reporter.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ def flay_options
136136
end
137137

138138
def skip?(violation)
139-
insufficient_occurrence?(violation) || check_disabled?(violation)
139+
below_threshold?(violation) ||
140+
insufficient_occurrence?(violation) ||
141+
check_disabled?(violation)
142+
end
143+
144+
def below_threshold?(violation)
145+
violation.mass < language_strategy.check_mass_threshold(violation.inner_check_name)
140146
end
141147

142148
def insufficient_occurrence?(violation)

lib/cc/engine/analyzers/violation.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ def identical?
5050
@identical
5151
end
5252

53+
def inner_check_name
54+
if identical?
55+
EngineConfig::IDENTICAL_CODE_CHECK
56+
else
57+
EngineConfig::SIMILAR_CODE_CHECK
58+
end
59+
end
60+
5361
private
5462

5563
attr_reader :language_strategy, :other_sexps, :current_sexp
@@ -59,7 +67,7 @@ def check_name
5967
end
6068

6169
def calculate_points
62-
@calculate_points ||= language_strategy.calculate_points(mass)
70+
@calculate_points ||= language_strategy.calculate_points(self)
6371
end
6472

6573
def points_across_occurrences

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

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,49 @@ def self.from_remediation_amount(amount)
186186

187187
expect(issues.length).to eq(0)
188188
end
189+
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
201+
end
202+
end
203+
describe 'similar2' do
204+
before { subject.type = 'js' }
205+
it 'returns true' do
206+
expect(subject.js?).to be true
207+
end
208+
end
209+
EORUBY
210+
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+
output = run_engine(config).strip.split("\0").first.strip
221+
json = JSON.parse(output)
222+
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+
})
228+
end
189229
end
190230

191-
describe "#calculate_points(mass)" do
231+
describe "#calculate_points" do
192232
let(:analyzer) { Ruby::Main.new(engine_config: engine_conf) }
193233
let(:base_points) { Ruby::Main::BASE_POINTS }
194234
let(:points_per) { Ruby::Main::POINTS_PER_OVERAGE }
@@ -198,9 +238,10 @@ def self.from_remediation_amount(amount)
198238
it "calculates mass overage points" do
199239
mass = threshold + 10
200240
overage = mass - threshold
241+
violation = OpenStruct.new(mass: mass, inner_check_name: "identical-code")
201242

202243
expected_points = base_points + overage * points_per
203-
points = analyzer.calculate_points(mass)
244+
points = analyzer.calculate_points(violation)
204245

205246
expect(points).to eq(expected_points)
206247
end
@@ -210,9 +251,10 @@ def self.from_remediation_amount(amount)
210251
it "calculates mass overage points" do
211252
mass = threshold - 5
212253
overage = mass - threshold
254+
violation = OpenStruct.new(mass: mass, inner_check_name: "identical-code")
213255

214256
expected_points = base_points + overage * points_per
215-
points = analyzer.calculate_points(mass)
257+
points = analyzer.calculate_points(violation)
216258

217259
expect(points).to eq(expected_points)
218260
end
@@ -222,9 +264,10 @@ def self.from_remediation_amount(amount)
222264
it "calculates mass overage points" do
223265
mass = threshold
224266
overage = mass - threshold
267+
violation = OpenStruct.new(mass: mass, inner_check_name: "identical-code")
225268

226269
expected_points = base_points + overage * points_per
227-
points = analyzer.calculate_points(mass)
270+
points = analyzer.calculate_points(violation)
228271

229272
expect(points).to eq(expected_points)
230273
end

0 commit comments

Comments
 (0)