Skip to content

Specify similar vs identical code in description #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/cc/engine/analyzers/violation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -48,7 +48,7 @@ def other_sexps
@other_locations ||= sorted_hashes.drop(1)
end

def name
def check_name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should identical vs. similar factor into the fingerprint? Maybe out of scope for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I don't think so.

Issues that get better or worse don't change fingerprints. I think a duplication issue moving from Similar to Identical or back is related to, and part of, that issue getting better or worse.

if issue.identical?
"Identical code"
else
Expand Down Expand Up @@ -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
Expand Down
31 changes: 29 additions & 2 deletions spec/cc/engine/analyzers/javascript/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand All @@ -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",
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/cc/engine/analyzers/php/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<?php
function hello($name) {
Expand All @@ -34,7 +34,7 @@

expect(json["type"]).to eq("issue")
expect(json["check_name"]).to eq("Identical code")
expect(json["description"]).to eq("Similar code found in 1 other location")
expect(json["description"]).to eq("Identical code found in 1 other location")
expect(json["categories"]).to eq(["Duplication"])
expect(json["location"]).to eq({
"path" => "foo.php",
Expand Down
32 changes: 30 additions & 2 deletions spec/cc/engine/analyzers/python/main_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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",
Expand All @@ -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)
---
Expand Down