From f86cf21b763f11aaad64a0fe5ce057b344089079 Mon Sep 17 00:00:00 2001 From: ABaldwinHunter Date: Fri, 15 Jan 2016 14:40:15 -0500 Subject: [PATCH] Specify similar vs identical code in description This information is already present on issue check_name, but the description is more visible. --- lib/cc/engine/analyzers/violation.rb | 6 ++-- .../engine/analyzers/javascript/main_spec.rb | 31 ++++++++++++++++-- spec/cc/engine/analyzers/php/main_spec.rb | 4 +-- spec/cc/engine/analyzers/python/main_spec.rb | 32 +++++++++++++++++-- 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/lib/cc/engine/analyzers/violation.rb b/lib/cc/engine/analyzers/violation.rb index 7f0ff2d0..4f39fbf1 100644 --- a/lib/cc/engine/analyzers/violation.rb +++ b/lib/cc/engine/analyzers/violation.rb @@ -17,7 +17,7 @@ def initialize(language_strategy, issue, hashes) def format { "type": "issue", - "check_name": name, + "check_name": check_name, "description": description, "categories": ["Duplication"], "location": format_location, @@ -48,7 +48,7 @@ def other_sexps @other_locations ||= sorted_hashes.drop(1) end - def name + def check_name if issue.identical? "Identical code" else @@ -96,7 +96,7 @@ def fingerprint end def description - description = "Similar code found in #{occurrences} other location" + description = "#{check_name} found in #{occurrences} other location" description += "s" if occurrences > 1 description end diff --git a/spec/cc/engine/analyzers/javascript/main_spec.rb b/spec/cc/engine/analyzers/javascript/main_spec.rb index c0ff1a8d..3fa0517b 100644 --- a/spec/cc/engine/analyzers/javascript/main_spec.rb +++ b/spec/cc/engine/analyzers/javascript/main_spec.rb @@ -9,7 +9,7 @@ include AnalyzerSpecHelpers describe "#run" do - it "prints an issue" do + it "prints an issue for identical code" do create_source_file("foo.js", <<-EOJS) console.log("hello JS!"); console.log("hello JS!"); @@ -21,7 +21,7 @@ expect(json["type"]).to eq("issue") expect(json["check_name"]).to eq("Identical code") - expect(json["description"]).to eq("Similar code found in 2 other locations") + expect(json["description"]).to eq("Identical code found in 2 other locations") expect(json["categories"]).to eq(["Duplication"]) expect(json["location"]).to eq({ "path" => "foo.js", @@ -36,6 +36,33 @@ expect(json["fingerprint"]).to eq("55ae5d0990647ef496e9e0d315f9727d") end + it "prints an issue for similar code" do + create_source_file("foo.js", <<-EOJS) + console.log("hello JS!"); + console.log("hellllllo JS!"); + console.log("helllllllllllllllllo JS!"); + EOJS + + result = run_engine(engine_conf).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 code found in 2 other locations") + expect(json["categories"]).to eq(["Duplication"]) + expect(json["location"]).to eq({ + "path" => "foo.js", + "lines" => { "begin" => 1, "end" => 1 }, + }) + expect(json["remediation_points"]).to eq(99000) + expect(json["other_locations"]).to eq([ + {"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} }, + {"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} } + ]) + expect(json["content"]["body"]).to match /This issue has a mass of `33`/ + expect(json["fingerprint"]).to eq("55ae5d0990647ef496e9e0d315f9727d") + end + it "skips unparsable files" do create_source_file("foo.js", <<-EOJS) function () { do(); // missing closing brace diff --git a/spec/cc/engine/analyzers/php/main_spec.rb b/spec/cc/engine/analyzers/php/main_spec.rb index 2158b9b7..c6898473 100644 --- a/spec/cc/engine/analyzers/php/main_spec.rb +++ b/spec/cc/engine/analyzers/php/main_spec.rb @@ -9,7 +9,7 @@ include AnalyzerSpecHelpers describe "#run" do - it "prints an issue" do + it "prints an issue for identical code" do create_source_file("foo.php", <<-EOPHP) "foo.php", diff --git a/spec/cc/engine/analyzers/python/main_spec.rb b/spec/cc/engine/analyzers/python/main_spec.rb index bd44022e..9c3da701 100644 --- a/spec/cc/engine/analyzers/python/main_spec.rb +++ b/spec/cc/engine/analyzers/python/main_spec.rb @@ -9,7 +9,7 @@ include AnalyzerSpecHelpers describe "#run" do - it "prints an issue" do + it "prints an issue for identical code" do create_source_file("foo.py", <<-EOJS) print("Hello", "python") print("Hello", "python") @@ -21,7 +21,7 @@ expect(json["type"]).to eq("issue") expect(json["check_name"]).to eq("Identical code") - expect(json["description"]).to eq("Similar code found in 2 other locations") + expect(json["description"]).to eq("Identical code found in 2 other locations") expect(json["categories"]).to eq(["Duplication"]) expect(json["location"]).to eq({ "path" => "foo.py", @@ -36,6 +36,34 @@ expect(json["fingerprint"]).to eq("42b832387c997f54a2012efb2159aefc") end + it "prints an issue for similar code" do + create_source_file("foo.py", <<-EOJS) +print("Hello", "python") +print("Hello It's me", "python") +print("Hello from the other side", "python") + EOJS + + result = run_engine(engine_conf).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 code found in 2 other locations") + expect(json["categories"]).to eq(["Duplication"]) + expect(json["location"]).to eq({ + "path" => "foo.py", + "lines" => { "begin" => 1, "end" => 1 }, + }) + expect(json["remediation_points"]).to eq(18000) + expect(json["other_locations"]).to eq([ + {"path" => "foo.py", "lines" => { "begin" => 2, "end" => 2} }, + {"path" => "foo.py", "lines" => { "begin" => 3, "end" => 3} } + ]) + expect(json["content"]["body"]).to match /This issue has a mass of `18`/ + expect(json["fingerprint"]).to eq("42b832387c997f54a2012efb2159aefc") + end + + it "skips unparsable files" do create_source_file("foo.py", <<-EOPY) ---