Skip to content

Commit 9f60237

Browse files
authored
Bump cc-parser to b378
Pulls in https://github.com/codeclimate/codeclimate-parser/pull/89 Paired with @larkinscott on this
1 parent 3f8de97 commit 9f60237

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM codeclimate/codeclimate-parser:b375
1+
FROM codeclimate/codeclimate-parser:b378
22
LABEL maintainer="Code Climate <hello@codeclimate.com>"
33

44
# Reset from base image

spec/cc/engine/analyzers/java/java_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,76 @@ module CC::Engine::Analyzers
142142
issues = run_engine(engine_conf).strip.split("\0")
143143
expect(issues).to be_empty
144144
end
145+
146+
it "prints an issue for similar code when the only difference is the value of a literal" do
147+
create_source_file("foo.java", <<-EOF)
148+
public class ArrayDemo {
149+
public static void foo() {
150+
int[] scott;
151+
scott = new int[] {
152+
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F
153+
};
154+
155+
int[] anArray;
156+
157+
anArray = new int[10];
158+
159+
for (int i = 0; i < anArray.length; i++) {
160+
anArray[i] = i;
161+
}
162+
163+
for (int i = 0; i < anArray.length; i++) {
164+
System.out.print(anArray[i] + " ");
165+
}
166+
167+
System.out.println();
168+
}
169+
170+
public static void foo() {
171+
int[] scott;
172+
scott = new int[] {
173+
0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7
174+
};
175+
176+
int[] anArray;
177+
178+
anArray = new int[10];
179+
180+
for (int i = 0; i < anArray.length; i++) {
181+
anArray[i] = i;
182+
}
183+
184+
for (int i = 0; i < anArray.length; i++) {
185+
System.out.print(anArray[i] + " ");
186+
}
187+
188+
System.out.println();
189+
}
190+
}
191+
EOF
192+
193+
issues = run_engine(engine_conf).strip.split("\0")
194+
expect(issues.length).to be > 0
195+
result = issues.first.strip
196+
json = JSON.parse(result)
197+
198+
expect(json["type"]).to eq("issue")
199+
expect(json["check_name"]).to eq("similar-code")
200+
201+
expect(json["description"]).to eq("Similar blocks of code found in 2 locations. Consider refactoring.")
202+
expect(json["categories"]).to eq(["Duplication"])
203+
expect(json["location"]).to eq({
204+
"path" => "foo.java",
205+
"lines" => { "begin" => 2, "end" => 21 },
206+
})
207+
expect(json["remediation_points"]).to eq(1_230_000)
208+
expect(json["other_locations"]).to eq([
209+
{"path" => "foo.java", "lines" => { "begin" => 23, "end" => 42 } },
210+
])
211+
expect(json["content"]["body"]).to match /This issue has a mass of 133/
212+
expect(json["fingerprint"]).to eq("9abf88bac3a56bf708a5c4ceaf251d98")
213+
expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MAJOR)
214+
end
145215
end
146216
end
147217
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "spec_helper"
2+
require "cc/engine/processed_source"
3+
4+
RSpec.describe CC::Engine::ProcessedSource, in_tmpdir: true do
5+
include AnalyzerSpecHelpers
6+
7+
describe "#ast" do
8+
it "returns an AST" do
9+
10+
create_source_file("foo.java", <<-EOF)
11+
public class Carousel {
12+
public int fav_num = 3;
13+
public int least_fav_num = 0x0000000;
14+
}
15+
EOF
16+
17+
path = "foo.java"
18+
request_path = "/java"
19+
processed_source = described_class.new(path, request_path)
20+
ast = processed_source.ast
21+
22+
expect(ast).to be_a CC::Parser::Node
23+
expect(ast.type).to eq("CompilationUnit")
24+
numbers = with_type("IntegerLiteralExpr", ast)
25+
expect(numbers.length).to eq(2)
26+
expect(numbers.first.properties.fetch("value")).to eq("3")
27+
expect(numbers.last.properties.fetch("value")).to eq("0x0000000")
28+
end
29+
30+
def with_type(type, node)
31+
flattened = flatten(node)
32+
flattened.select { |child| child.type == type }
33+
end
34+
35+
def flatten(node)
36+
([node] + node.properties.fetch("body").map { |child| flatten(child) }).flatten
37+
end
38+
end
39+
end

0 commit comments

Comments
 (0)