Skip to content

Commit 67b9919

Browse files
author
Ashley Baldwin-Hunter
committed
Merge pull request #67 from codeclimate/abh-points-ruby-only
Tune RUBY remediation points to match Classic
2 parents e2e8034 + 5a77e88 commit 67b9919

File tree

5 files changed

+107
-57
lines changed

5 files changed

+107
-57
lines changed

lib/cc/engine/analyzers/analyzer_base.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ def mass_threshold
3535
engine_config.mass_threshold_for(self.class::LANGUAGE) || self.class::DEFAULT_MASS_THRESHOLD
3636
end
3737

38-
def base_points
39-
self.class::BASE_POINTS
38+
def calculate_points(issue)
39+
self.class::BASE_POINTS * issue.mass
4040
end
4141

4242
private

lib/cc/engine/analyzers/reporter.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,15 @@ def flay
6060

6161
attr_reader :engine_config, :language_strategy, :io
6262

63-
def mass_threshold
64-
@mass_threshold ||= language_strategy.mass_threshold
65-
end
66-
6763
def new_violation(issue)
6864
hashes = flay.hashes[issue.structural_hash]
69-
Violation.new(language_strategy.base_points, issue, hashes)
65+
Violation.new(language_strategy, issue, hashes)
7066
end
7167

7268
def flay_options
7369
{
7470
diff: false,
75-
mass: mass_threshold,
71+
mass: language_strategy.mass_threshold,
7672
summary: false,
7773
verbose: false,
7874
number: true,

lib/cc/engine/analyzers/ruby/main.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ class Main < CC::Engine::Analyzers::Base
1818

1919
]
2020
DEFAULT_MASS_THRESHOLD = 18
21-
BASE_POINTS = 10_000
21+
BASE_POINTS = 1_500_000
22+
POINTS_PER_OVERAGE = 100_000
2223
TIMEOUT = 300
2324

25+
def calculate_points(issue)
26+
BASE_POINTS + (overage(issue) * POINTS_PER_OVERAGE)
27+
end
28+
2429
private
2530

31+
def overage(issue)
32+
issue.mass - mass_threshold
33+
end
34+
2635
def process_file(file)
2736
RubyParser.new.process(File.binread(file), file, TIMEOUT)
2837
end

lib/cc/engine/analyzers/violation.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module Analyzers
88
class Violation
99
attr_reader :issue
1010

11-
def initialize(base_points, issue, hashes)
12-
@base_points = base_points
11+
def initialize(language_strategy, issue, hashes)
12+
@language_strategy = language_strategy
1313
@issue = issue
1414
@hashes = hashes
1515
end
@@ -34,7 +34,7 @@ def report_name
3434

3535
private
3636

37-
attr_reader :base_points, :hashes
37+
attr_reader :language_strategy, :hashes
3838

3939
def current_sexp
4040
@location ||= sorted_hashes.first
@@ -57,7 +57,7 @@ def name
5757
end
5858

5959
def calculate_points
60-
base_points * issue.mass
60+
language_strategy.calculate_points(issue)
6161
end
6262

6363
def format_location

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

+89-44
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,105 @@
44
require 'flay'
55
require 'tmpdir'
66

7-
RSpec.describe CC::Engine::Analyzers::Ruby::Main, in_tmpdir: true do
8-
include AnalyzerSpecHelpers
7+
module CC::Engine::Analyzers
8+
RSpec.describe Ruby::Main, in_tmpdir: true do
9+
include AnalyzerSpecHelpers
910

10-
describe "#run" do
11-
it "prints an issue" do
12-
create_source_file("foo.rb", <<-EORUBY)
13-
describe '#ruby?' do
14-
before { subject.type = 'ruby' }
11+
describe "#run" do
12+
it "prints an issue" do
13+
create_source_file("foo.rb", <<-EORUBY)
14+
describe '#ruby?' do
15+
before { subject.type = 'ruby' }
1516
16-
it 'returns true' do
17-
expect(subject.ruby?).to be true
17+
it 'returns true' do
18+
expect(subject.ruby?).to be true
19+
end
1820
end
19-
end
2021
21-
describe '#js?' do
22-
before { subject.type = 'js' }
22+
describe '#js?' do
23+
before { subject.type = 'js' }
2324
24-
it 'returns true' do
25-
expect(subject.js?).to be true
25+
it 'returns true' do
26+
expect(subject.js?).to be true
27+
end
2628
end
27-
end
28-
EORUBY
29-
30-
result = run_engine(engine_conf).strip
31-
json = JSON.parse(result)
32-
33-
expect(json["type"]).to eq("issue")
34-
expect(json["check_name"]).to eq("Similar code")
35-
expect(json["description"]).to eq("Similar code found in 1 other location")
36-
expect(json["categories"]).to eq(["Duplication"])
37-
expect(json["location"]).to eq({
38-
"path" => "foo.rb",
39-
"lines" => { "begin" => 1, "end" => 5 },
40-
})
41-
expect(json["remediation_points"]).to eq(360000)
42-
expect(json["other_locations"]).to eq([
43-
{"path" => "foo.rb", "lines" => { "begin" => 9, "end" => 13} },
44-
])
45-
expect(json["content"]["body"]).to match /This issue has a mass of `36`/
46-
expect(json["fingerprint"]).to eq("f21b75bbd135ec3ae6638364d5c73762")
29+
EORUBY
30+
31+
result = run_engine(engine_conf).strip
32+
json = JSON.parse(result)
33+
34+
expect(json["type"]).to eq("issue")
35+
expect(json["check_name"]).to eq("Similar code")
36+
expect(json["description"]).to eq("Similar code found in 1 other location")
37+
expect(json["categories"]).to eq(["Duplication"])
38+
expect(json["location"]).to eq({
39+
"path" => "foo.rb",
40+
"lines" => { "begin" => 1, "end" => 5 },
41+
})
42+
expect(json["remediation_points"]).to eq(3300000)
43+
expect(json["other_locations"]).to eq([
44+
{"path" => "foo.rb", "lines" => { "begin" => 9, "end" => 13} },
45+
])
46+
expect(json["content"]["body"]).to match /This issue has a mass of `36`/
47+
expect(json["fingerprint"]).to eq("f21b75bbd135ec3ae6638364d5c73762")
48+
end
49+
50+
it "skips unparsable files" do
51+
create_source_file("foo.rb", <<-EORUBY)
52+
---
53+
EORUBY
54+
55+
expect {
56+
expect(run_engine(engine_conf)).to eq("")
57+
}.to output(/Skipping file/).to_stderr
58+
end
4759
end
4860

49-
it "skips unparsable files" do
50-
create_source_file("foo.rb", <<-EORUBY)
51-
---
52-
EORUBY
61+
describe "#calculate_points(issue)" do
62+
let(:analyzer) { Ruby::Main.new(engine_config: engine_conf) }
63+
let(:base_points) { Ruby::Main::BASE_POINTS }
64+
let(:points_per) { Ruby::Main::POINTS_PER_OVERAGE }
65+
let(:threshold) { Ruby::Main::DEFAULT_MASS_THRESHOLD }
66+
67+
context "when issue mass exceeds threshold" do
68+
it "calculates mass overage points" do
69+
issue = double(:issue, mass: threshold + 10)
70+
overage = issue.mass - threshold
71+
72+
expected_points = base_points + overage * points_per
73+
points = analyzer.calculate_points(issue)
74+
75+
expect(points).to eq(expected_points)
76+
end
77+
end
5378

54-
expect {
55-
expect(run_engine(engine_conf)).to eq("")
56-
}.to output(/Skipping file/).to_stderr
79+
context "when issue mass is less than threshold" do
80+
it "calculates mass overage points" do
81+
issue = double(:issue, mass: threshold - 5)
82+
overage = issue.mass - threshold
83+
84+
expected_points = base_points + overage * points_per
85+
points = analyzer.calculate_points(issue)
86+
87+
expect(points).to eq(expected_points)
88+
end
89+
end
90+
91+
context "when issue mass equals threshold" do
92+
it "calculates mass overage points" do
93+
issue = double(:issue, mass: threshold)
94+
overage = issue.mass - threshold
95+
96+
expected_points = base_points + overage * points_per
97+
points = analyzer.calculate_points(issue)
98+
99+
expect(points).to eq(expected_points)
100+
end
101+
end
57102
end
58-
end
59103

60-
def engine_conf
61-
CC::Engine::Analyzers::EngineConfig.new({})
104+
def engine_conf
105+
EngineConfig.new({})
106+
end
62107
end
63108
end

0 commit comments

Comments
 (0)