From a75f9eee7f9e2e476c41f200010d88a6f33a3bd7 Mon Sep 17 00:00:00 2001 From: John Pignata Date: Mon, 26 Oct 2015 17:07:37 -0400 Subject: [PATCH] Mimic descriptions emitted by classic Previously Code Climate would show a description that indicated how many instances of duplication were found within a given issue. This seems more intelligble than what this engine does by pointing to data from the s-expression reported by flay. --- .ruby-version | 1 + lib/cc/engine/analyzers/violation.rb | 12 +++++++++++- spec/cc/engine/analyzers/javascript/main_spec.rb | 2 +- spec/cc/engine/analyzers/php/main_spec.rb | 2 +- spec/cc/engine/analyzers/python/main_spec.rb | 2 +- spec/cc/engine/analyzers/ruby/main_spec.rb | 2 +- 6 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..58594069 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.2.3 diff --git a/lib/cc/engine/analyzers/violation.rb b/lib/cc/engine/analyzers/violation.rb index e6d8b92b..85e61eac 100644 --- a/lib/cc/engine/analyzers/violation.rb +++ b/lib/cc/engine/analyzers/violation.rb @@ -15,7 +15,7 @@ def format { "type": "issue", "check_name": name, - "description": "Duplication found in #{issue.name}", + "description": description, "categories": ["Duplication"], "location": format_location, "remediation_points": calculate_points, @@ -83,6 +83,16 @@ def content_body read_up = File.read('config/contents/duplicated_code.md') { "body": read_up } end + + def description + description = "*Similar code* found in #{occurrences} other location" + description += "s" if occurrences > 1 + description + end + + def occurrences + other_sexps.count + end end end end diff --git a/spec/cc/engine/analyzers/javascript/main_spec.rb b/spec/cc/engine/analyzers/javascript/main_spec.rb index 95ea22b5..df975faa 100644 --- a/spec/cc/engine/analyzers/javascript/main_spec.rb +++ b/spec/cc/engine/analyzers/javascript/main_spec.rb @@ -37,7 +37,7 @@ def run_engine(config = nil) end def printed_issue - issue = {"type":"issue","check_name":"Identical code","description":"Duplication found in ExpressionStatement","categories":["Duplication"],"location":{"path":"foo.js","lines":{"begin":1,"end":1}},"remediation_points":378000, "other_locations":[{"path":"foo.js","lines":{"begin":2,"end":2}},{"path":"foo.js","lines":{"begin":3,"end":3}}], "content":{"body": read_up}} + issue = {"type":"issue","check_name":"Identical code","description":"*Similar code* found in 2 other locations","categories":["Duplication"],"location":{"path":"foo.js","lines":{"begin":1,"end":1}},"remediation_points":378000, "other_locations":[{"path":"foo.js","lines":{"begin":2,"end":2}},{"path":"foo.js","lines":{"begin":3,"end":3}}], "content":{"body": read_up}} issue.to_json + "\0\n" end diff --git a/spec/cc/engine/analyzers/php/main_spec.rb b/spec/cc/engine/analyzers/php/main_spec.rb index a5b50d30..08788bf3 100644 --- a/spec/cc/engine/analyzers/php/main_spec.rb +++ b/spec/cc/engine/analyzers/php/main_spec.rb @@ -50,7 +50,7 @@ def run_engine(config = nil) end def printed_issue - issue = {"type":"issue","check_name":"Identical code","description":"Duplication found in function","categories":["Duplication"],"location":{"path":"foo.php","lines":{"begin":2,"end":6}},"remediation_points":176000,"other_locations":[{"path":"foo.php","lines":{"begin":10,"end":14}}],"content":{"body": read_up}} + issue = {"type":"issue","check_name":"Identical code","description":"*Similar code* found in 1 other location","categories":["Duplication"],"location":{"path":"foo.php","lines":{"begin":2,"end":6}},"remediation_points":176000,"other_locations":[{"path":"foo.php","lines":{"begin":10,"end":14}}],"content":{"body": read_up}} issue.to_json + "\0\n" end diff --git a/spec/cc/engine/analyzers/python/main_spec.rb b/spec/cc/engine/analyzers/python/main_spec.rb index fce6e2e4..fb2e309a 100644 --- a/spec/cc/engine/analyzers/python/main_spec.rb +++ b/spec/cc/engine/analyzers/python/main_spec.rb @@ -35,7 +35,7 @@ def run_engine(config = nil) end def printed_issue - issue = {"type":"issue","check_name":"Identical code","description":"Duplication found in Print","categories":["Duplication"],"location":{"path":"foo.py","lines":{"begin":1,"end":1}},"remediation_points":81000, "other_locations":[{"path":"foo.py","lines":{"begin":2,"end":2}},{"path":"foo.py","lines":{"begin":3,"end":3}}], "content":{"body": read_up}} + issue = {"type":"issue","check_name":"Identical code","description":"*Similar code* found in 2 other locations","categories":["Duplication"],"location":{"path":"foo.py","lines":{"begin":1,"end":1}},"remediation_points":81000, "other_locations":[{"path":"foo.py","lines":{"begin":2,"end":2}},{"path":"foo.py","lines":{"begin":3,"end":3}}], "content":{"body": read_up}} issue.to_json + "\0\n" end diff --git a/spec/cc/engine/analyzers/ruby/main_spec.rb b/spec/cc/engine/analyzers/ruby/main_spec.rb index e9f33372..869eb961 100644 --- a/spec/cc/engine/analyzers/ruby/main_spec.rb +++ b/spec/cc/engine/analyzers/ruby/main_spec.rb @@ -47,7 +47,7 @@ def run_engine(config = {}) end def first_issue - {"type":"issue","check_name":"Similar code","description":"Duplication found in iter","categories":["Duplication"],"location":{"path":"foo.rb","lines":{"begin":1,"end":5}},"remediation_points": 360000, "other_locations":[{"path":"foo.rb","lines":{"begin":9,"end":13}}], "content": {"body": read_up}} + {"type":"issue","check_name":"Similar code","description":"*Similar code* found in 1 other location","categories":["Duplication"],"location":{"path":"foo.rb","lines":{"begin":1,"end":5}},"remediation_points": 360000, "other_locations":[{"path":"foo.rb","lines":{"begin":9,"end":13}}], "content": {"body": read_up}} end def printed_issues