This repository was archived by the owner on Jul 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathmain_spec.rb
More file actions
385 lines (325 loc) · 11.9 KB
/
main_spec.rb
File metadata and controls
385 lines (325 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
require 'spec_helper'
require 'cc/engine/analyzers/engine_config'
require 'cc/engine/analyzers/ruby/main'
require 'cc/engine/parse_metrics'
module CC::Engine::Analyzers
RSpec.describe Ruby::Main, in_tmpdir: true do
include AnalyzerSpecHelpers
describe "#run" do
it "handles escaped multibyte characters in regular expressions" do
create_source_file("foo.rb", <<-EORUBY)
class Helper
def self.sub_degrees(str)
str.gsub(/\\d+\\°\\s/, "")
end
end
EORUBY
pending "Potential lexing bug. Ask customer to remove escaping."
expect(CC.logger).to receive(:info).with(/Skipping file/)
expect(run_engine(engine_conf)).to eq("")
end
it "calculates locations correctly for conditional statements" do
create_source_file("foo.rb", <<-EORUBY)
def self.from_level(level)
if level >= 4
new("A")
elsif level >= 2
new("E")
elsif level >= 1
new("I")
elsif level >= 0
new("O")
else
new("U")
end
end
def self.from_remediation_amount(amount)
if amount.nil?
NULL_RATING
elsif amount <= 20
new("A")
elsif amount <= 40
new("E")
elsif amount <= 80
new("I")
elsif amount <= 160
new("O")
else
new("U")
end
end
EORUBY
issues = run_engine(engine_conf).strip.split("\0")
result = issues.first.strip
json = JSON.parse(result)
expect(json["location"]).to eq({
"path" => "foo.rb",
"lines" => { "begin" => 2, "end" => 12 },
})
expect(json["other_locations"]).to eq([
{"path" => "foo.rb", "lines" => { "begin" => 18, "end" => 28} },
])
end
it "prints an issue" do
create_source_file("foo.rb", <<-EORUBY)
describe '#ruby?' do
before { subject.type = 'ruby' }
it 'returns true' do
10.times { |i| if i < 5; if i % 2 == 0; subject.increase_mass!; end; end }; expect(subject.ruby?).to be true
end
end
describe '#js?' do
before { subject.type = 'js' }
it 'returns true' do
10.times { |i| if i < 5; if i % 2 == 0; subject.increase_mass!; end; end }; expect(subject.js?).to be true
end
end
EORUBY
issues = run_engine(engine_conf).strip.split("\0")
result = issues.first.strip
json = JSON.parse(result)
expect(json["type"]).to eq("issue")
expect(json["check_name"]).to eq("similar-code")
expect(json["description"]).to eq("Similar blocks of code found in 2 locations. Consider refactoring.")
expect(json["categories"]).to eq(["Duplication"])
expect(json["location"]).to eq({
"path" => "foo.rb",
"lines" => { "begin" => 1, "end" => 5 },
})
expect(json["remediation_points"]).to eq(350_000)
expect(json["other_locations"]).to eq([
{"path" => "foo.rb", "lines" => { "begin" => 9, "end" => 13} },
])
expect(json["content"]["body"]).to match /This issue has a mass of 35/
expect(json["fingerprint"]).to eq("fb28e849f22fbabf946d1afdeaa84c5b")
expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MINOR)
end
it "creates an issue for each occurrence of the duplicated code" do
create_source_file("foo.rb", <<-EORUBY)
describe '#ruby?' do
before { subject.type = 'ruby' }
it 'returns true' do
10.times { |i| if i < 5; if i % 2 == 0; subject.increase_mass!; end; end }; expect(subject.ruby?).to be true
end
end
describe '#js?' do
before { subject.type = 'js' }
it 'returns true' do
10.times { |i| if i < 5; if i % 2 == 0; subject.increase_mass!; end; end }; expect(subject.js?).to be true
end
end
describe '#whaddup?' do
before { subject.type = 'js' }
it 'returns true' do
10.times { |i| if i < 5; if i % 2 == 0; subject.increase_mass!; end; end }; expect(subject.js?).to be true
end
end
EORUBY
issues = run_engine(engine_conf).strip.split("\0")
expect(issues.length).to eq(3)
end
it "skips unparsable files" do
create_source_file("foo.rb", <<-EORUBY)
---
EORUBY
expect(CC.logger).to receive(:info).with(/Skipping file/)
expect(run_engine(engine_conf)).to eq("")
end
it "does not see hashes as similar" do
create_source_file("foo.rb", <<-EORUBY)
ANIMALS = {
bat: "Bat",
bee: "Bee",
cat: "Cat",
cow: "Cow",
dog: "Dog",
fly: "Fly",
human: "Human",
lizard: "Lizard",
owl: "Owl",
ringworm: "Ringworm",
salmon: "Salmon",
whale: "Whale",
}.freeze
TRANSPORT = {
airplane: "Airplane",
bicycle: "Bicycle",
bus: "Bus",
car: "Car",
escalator: "Escalator",
helicopter: "Helicopter",
lift: "Lift",
motorcycle: "Motorcycle",
rocket: "Rocket",
scooter: "Scooter",
skateboard: "Skateboard",
truck: "Truck",
}.freeze
EORUBY
issues = run_engine(filtered_engine_conf("(hash ___)")).strip.split("\0")
expect(issues.length).to eq(0)
end
context "per-check mass thresholds" do
before do
create_source_file("foo.rb", <<-EORUBY)
# identical mass of 15
def identical
puts "identical \#{thing.bar} \#{other.fun} \#{moo ? "moo" : "cluck"}"
puts "identical \#{thing.bar} \#{other.fun} \#{moo ? "moo" : "cluck"}"
end
# similar mass of 10
def similar1
foo.select { |f| if f.baz?; 10; end }
end
def similar2
foo.select { |f| if f.bar?; 15; end }
end
EORUBY
end
it "reports identical issues when only that threshold exceeded" do
config = CC::Engine::Analyzers::EngineConfig.new({
"config" => {
"languages" => %w[ruby],
"checks" => {
"identical-code" => { "config" => { "threshold" => 5 } },
"similar-code" => { "config" => { "threshold" => 20 } },
},
},
})
issues = run_engine(config).strip.split("\0")
expect(issues.length).to eq(2)
issues.each do |issue|
expect(JSON.parse(issue)["check_name"]).to eq("identical-code")
end
end
it "reports similar issues when only that threshold exceeded" do
config = CC::Engine::Analyzers::EngineConfig.new({
"config" => {
"languages" => %w[ruby],
"checks" => {
"identical-code" => { "config" => { "threshold" => 20 } },
"similar-code" => { "config" => { "threshold" => 5 } },
},
},
})
issues = run_engine(config).strip.split("\0")
expect(issues.length).to eq(2)
issues.each do |issue|
expect(JSON.parse(issue)["check_name"]).to eq("similar-code")
end
end
it "reports similar and identical issues when both thresholds exceeded" do
config = CC::Engine::Analyzers::EngineConfig.new({
"config" => {
"languages" => %w[ruby],
"checks" => {
"identical-code" => { "config" => { "threshold" => 5 } },
"similar-code" => { "config" => { "threshold" => 5 } },
},
},
})
issues = run_engine(config).strip.split("\0")
expect(issues.length).to eq(4)
end
it "reports no issues when neither threshold exceeded" do
config = CC::Engine::Analyzers::EngineConfig.new({
"config" => {
"languages" => %w[ruby],
"checks" => {
"identical-code" => { "config" => { "threshold" => 20 } },
"similar-code" => { "config" => { "threshold" => 20 } },
},
},
})
issues = run_engine(config).strip.split("\0")
expect(issues.length).to eq(0)
end
end
end
describe "#calculate_points" do
let(:analyzer) { new_analyzer }
let(:base_points) { Ruby::Main::BASE_POINTS }
let(:points_per) { Ruby::Main::POINTS_PER_OVERAGE }
let(:threshold) { Ruby::Main::DEFAULT_MASS_THRESHOLD }
context "when mass exceeds threshold" do
it "calculates mass overage points" do
mass = threshold + 10
overage = mass - threshold
violation = OpenStruct.new(mass: mass, check_name: "identical-code")
expected_points = base_points + overage * points_per
points = analyzer.calculate_points(violation)
expect(points).to eq(expected_points)
end
end
context "when mass is less than threshold" do
it "calculates mass overage points" do
mass = threshold - 5
overage = mass - threshold
violation = OpenStruct.new(mass: mass, check_name: "identical-code")
expected_points = base_points + overage * points_per
points = analyzer.calculate_points(violation)
expect(points).to eq(expected_points)
end
end
context "when mass equals threshold" do
it "calculates mass overage points" do
mass = threshold
overage = mass - threshold
violation = OpenStruct.new(mass: mass, check_name: "identical-code")
expected_points = base_points + overage * points_per
points = analyzer.calculate_points(violation)
expect(points).to eq(expected_points)
end
end
end
describe "#calculate_severity(points)" do
let(:analyzer) { new_analyzer }
let(:base_points) { Ruby::Main::BASE_POINTS }
let(:points_per) { Ruby::Main::POINTS_PER_OVERAGE }
let(:threshold) { Ruby::Main::DEFAULT_MASS_THRESHOLD }
context "when points exceed threshold" do
it "assigns a severity of major" do
total_points = Base::MAJOR_SEVERITY_THRESHOLD + 1
severity = analyzer.calculate_severity(total_points)
expect(severity).to eq(Base::MAJOR)
end
end
context "when points equal threshold" do
it "assigns a severity of major" do
total_points = Base::MAJOR_SEVERITY_THRESHOLD
severity = analyzer.calculate_severity(total_points)
expect(severity).to eq(Base::MAJOR)
end
end
context "when points are below threshold" do
it "assigns a severity of minor" do
total_points = Base::MAJOR_SEVERITY_THRESHOLD - 10
severity = analyzer.calculate_severity(total_points)
expect(severity).to eq(Base::MINOR)
end
end
end
def new_analyzer
Ruby::Main.new(
engine_config: engine_conf,
parse_metrics: CC::Engine::ParseMetrics.new(
language: "ruby",
io: StringIO.new,
),
)
end
def engine_conf
EngineConfig.new({})
end
def filtered_engine_conf *patterns
EngineConfig.new({ "config" => {
"languages" => {
"ruby" => {
"filters" => patterns
}
}
}
})
end
end
end