Skip to content

Commit ca8f6d0

Browse files
author
Ashley Baldwin-Hunter
committed
Merge pull request #76 from codeclimate/abh-identical-code-description
Specify similar vs identical code in description
2 parents 3a87fe6 + f86cf21 commit ca8f6d0

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

lib/cc/engine/analyzers/violation.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def initialize(language_strategy, issue, hashes)
1717
def format
1818
{
1919
"type": "issue",
20-
"check_name": name,
20+
"check_name": check_name,
2121
"description": description,
2222
"categories": ["Duplication"],
2323
"location": format_location,
@@ -48,7 +48,7 @@ def other_sexps
4848
@other_locations ||= sorted_hashes.drop(1)
4949
end
5050

51-
def name
51+
def check_name
5252
if issue.identical?
5353
"Identical code"
5454
else
@@ -96,7 +96,7 @@ def fingerprint
9696
end
9797

9898
def description
99-
description = "Similar code found in #{occurrences} other location"
99+
description = "#{check_name} found in #{occurrences} other location"
100100
description += "s" if occurrences > 1
101101
description
102102
end

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

+29-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
include AnalyzerSpecHelpers
1010

1111
describe "#run" do
12-
it "prints an issue" do
12+
it "prints an issue for identical code" do
1313
create_source_file("foo.js", <<-EOJS)
1414
console.log("hello JS!");
1515
console.log("hello JS!");
@@ -21,7 +21,7 @@
2121

2222
expect(json["type"]).to eq("issue")
2323
expect(json["check_name"]).to eq("Identical code")
24-
expect(json["description"]).to eq("Similar code found in 2 other locations")
24+
expect(json["description"]).to eq("Identical code found in 2 other locations")
2525
expect(json["categories"]).to eq(["Duplication"])
2626
expect(json["location"]).to eq({
2727
"path" => "foo.js",
@@ -36,6 +36,33 @@
3636
expect(json["fingerprint"]).to eq("55ae5d0990647ef496e9e0d315f9727d")
3737
end
3838

39+
it "prints an issue for similar code" do
40+
create_source_file("foo.js", <<-EOJS)
41+
console.log("hello JS!");
42+
console.log("hellllllo JS!");
43+
console.log("helllllllllllllllllo JS!");
44+
EOJS
45+
46+
result = run_engine(engine_conf).strip
47+
json = JSON.parse(result)
48+
49+
expect(json["type"]).to eq("issue")
50+
expect(json["check_name"]).to eq("Similar code")
51+
expect(json["description"]).to eq("Similar code found in 2 other locations")
52+
expect(json["categories"]).to eq(["Duplication"])
53+
expect(json["location"]).to eq({
54+
"path" => "foo.js",
55+
"lines" => { "begin" => 1, "end" => 1 },
56+
})
57+
expect(json["remediation_points"]).to eq(99000)
58+
expect(json["other_locations"]).to eq([
59+
{"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} },
60+
{"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} }
61+
])
62+
expect(json["content"]["body"]).to match /This issue has a mass of `33`/
63+
expect(json["fingerprint"]).to eq("55ae5d0990647ef496e9e0d315f9727d")
64+
end
65+
3966
it "skips unparsable files" do
4067
create_source_file("foo.js", <<-EOJS)
4168
function () { do(); // missing closing brace

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
include AnalyzerSpecHelpers
1010

1111
describe "#run" do
12-
it "prints an issue" do
12+
it "prints an issue for identical code" do
1313
create_source_file("foo.php", <<-EOPHP)
1414
<?php
1515
function hello($name) {
@@ -34,7 +34,7 @@
3434

3535
expect(json["type"]).to eq("issue")
3636
expect(json["check_name"]).to eq("Identical code")
37-
expect(json["description"]).to eq("Similar code found in 1 other location")
37+
expect(json["description"]).to eq("Identical code found in 1 other location")
3838
expect(json["categories"]).to eq(["Duplication"])
3939
expect(json["location"]).to eq({
4040
"path" => "foo.php",

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
include AnalyzerSpecHelpers
1010

1111
describe "#run" do
12-
it "prints an issue" do
12+
it "prints an issue for identical code" do
1313
create_source_file("foo.py", <<-EOJS)
1414
print("Hello", "python")
1515
print("Hello", "python")
@@ -21,7 +21,7 @@
2121

2222
expect(json["type"]).to eq("issue")
2323
expect(json["check_name"]).to eq("Identical code")
24-
expect(json["description"]).to eq("Similar code found in 2 other locations")
24+
expect(json["description"]).to eq("Identical code found in 2 other locations")
2525
expect(json["categories"]).to eq(["Duplication"])
2626
expect(json["location"]).to eq({
2727
"path" => "foo.py",
@@ -36,6 +36,34 @@
3636
expect(json["fingerprint"]).to eq("42b832387c997f54a2012efb2159aefc")
3737
end
3838

39+
it "prints an issue for similar code" do
40+
create_source_file("foo.py", <<-EOJS)
41+
print("Hello", "python")
42+
print("Hello It's me", "python")
43+
print("Hello from the other side", "python")
44+
EOJS
45+
46+
result = run_engine(engine_conf).strip
47+
json = JSON.parse(result)
48+
49+
expect(json["type"]).to eq("issue")
50+
expect(json["check_name"]).to eq("Similar code")
51+
expect(json["description"]).to eq("Similar code found in 2 other locations")
52+
expect(json["categories"]).to eq(["Duplication"])
53+
expect(json["location"]).to eq({
54+
"path" => "foo.py",
55+
"lines" => { "begin" => 1, "end" => 1 },
56+
})
57+
expect(json["remediation_points"]).to eq(18000)
58+
expect(json["other_locations"]).to eq([
59+
{"path" => "foo.py", "lines" => { "begin" => 2, "end" => 2} },
60+
{"path" => "foo.py", "lines" => { "begin" => 3, "end" => 3} }
61+
])
62+
expect(json["content"]["body"]).to match /This issue has a mass of `18`/
63+
expect(json["fingerprint"]).to eq("42b832387c997f54a2012efb2159aefc")
64+
end
65+
66+
3967
it "skips unparsable files" do
4068
create_source_file("foo.py", <<-EOPY)
4169
---

0 commit comments

Comments
 (0)