Skip to content

Commit db70247

Browse files
author
ABaldwinHunter
committed
add tests - wip - one error
1 parent a659e97 commit db70247

File tree

3 files changed

+130
-2
lines changed

3 files changed

+130
-2
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'cc/engine/analyzers/javascript/main'
2+
require 'cc/engine/analyzers/reporter'
3+
require 'cc/engine/analyzers/engine_config'
4+
require 'cc/engine/analyzers/file_list'
5+
require 'flay'
6+
require 'tmpdir'
7+
8+
RSpec.describe CC::Engine::Analyzers::Violation, in_tmpdir: true do
9+
include AnalyzerSpecHelpers
10+
11+
before do
12+
create_source_file("foo.js", <<-EOJS)
13+
console.log("hello JS!");
14+
console.log("hello JS!");
15+
console.log("hello JS!");
16+
EOJS
17+
end
18+
19+
describe "#report" do
20+
it "passes the correct parameters to Violation" do
21+
expected_base_points = CC::Engine::Analyzers::Javascript::Main::BASE_POINTS
22+
expected_per_points = CC::Engine::Analyzers::Javascript::Main::POINTS_PER
23+
expected_threshold = 1
24+
expected_issue = sample_flay_issue
25+
expected_hashes = sample_hashes
26+
27+
expect(CC::Engine::Analyzers::Violation).to receive(:new).with(
28+
expected_base_points,
29+
expected_per_points,
30+
expected_threshold,
31+
expected_issue,
32+
expected_hashes,
33+
)
34+
35+
result = run_engine(engine_conf, CC::Engine::Analyzers::Javascript::Main).strip
36+
end
37+
end
38+
39+
def sample_hashes
40+
[s(:ExpressionStatement, s(:expression, s(:CallExpression, s(:callee, s(:MemberExpression, s(:object, s(:Identifier, :console)), s(:property, s(:Identifier, :log)))), s(:arguments, s(:StringLiteral, :"hello JS!"))))), s(:ExpressionStatement, s(:expression, s(:CallExpression, s(:callee, s(:MemberExpression, s(:object, s(:Identifier, :console)), s(:property, s(:Identifier, :log)))), s(:arguments, s(:StringLiteral, :"hello JS!"))))), s(:ExpressionStatement, s(:expression, s(:CallExpression, s(:callee, s(:MemberExpression, s(:object, s(:Identifier, :console)), s(:property, s(:Identifier, :log)))), s(:arguments, s(:StringLiteral, :"hello JS!")))))]
41+
end
42+
43+
def engine_conf
44+
CC::Engine::Analyzers::EngineConfig.new({
45+
'config' => {
46+
'languages' => {
47+
'javascript' => {
48+
'mass_threshold' => 1
49+
}
50+
}
51+
}
52+
})
53+
end
54+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'cc/engine/analyzers/javascript/main'
2+
require 'cc/engine/analyzers/reporter'
3+
require 'cc/engine/analyzers/engine_config'
4+
require 'cc/engine/analyzers/file_list'
5+
require 'flay'
6+
require 'tmpdir'
7+
8+
RSpec.describe CC::Engine::Analyzers::Violation, in_tmpdir: true do
9+
include AnalyzerSpecHelpers
10+
11+
describe "#format" do
12+
it "returns correctly formatted issue" do
13+
14+
create_source_file("foo.js", <<-EOJS)
15+
console.log("hello JS!");
16+
console.log("hello JS!");
17+
console.log("hello JS!");
18+
EOJS
19+
20+
result = run_engine(engine_conf, CC::Engine::Analyzers::Javascript::Main).strip
21+
json = JSON.parse(result)
22+
23+
expect(json["type"]).to eq("issue")
24+
expect(json["check_name"]).to eq("Identical code")
25+
expect(json["description"]).to eq("Similar code found in 2 other locations")
26+
expect(json["categories"]).to eq(["Duplication"])
27+
expect(json["location"]).to eq({
28+
"path" => "foo.js",
29+
"lines" => { "begin" => 1, "end" => 1 },
30+
})
31+
expect(json["remediation_points"]).to eq(6400000)
32+
expect(json["other_locations"]).to eq([
33+
{"path" => "foo.js", "lines" => { "begin" => 2, "end" => 2} },
34+
{"path" => "foo.js", "lines" => { "begin" => 3, "end" => 3} }
35+
])
36+
expect(json["content"]["body"]).to match /This issue has a mass of `99`/
37+
expect(json["fingerprint"]).to eq("55ae5d0990647ef496e9e0d315f9727d")
38+
end
39+
end
40+
41+
def engine_conf
42+
CC::Engine::Analyzers::EngineConfig.new({
43+
'config' => {
44+
'languages' => {
45+
'javascript' => {
46+
'mass_threshold' => 1
47+
}
48+
}
49+
}
50+
})
51+
end
52+
end

spec/support/helpers/analyzer_spec_helpers.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,36 @@ def create_source_file(path, content)
33
File.write(File.join(@code, path), content)
44
end
55

6-
def run_engine(config = nil)
6+
def run_engine(config = nil, engine_class = described_class)
77
io = StringIO.new
88

9-
engine = described_class.new(engine_config: config)
9+
engine = engine_class.new(engine_config: config)
1010
reporter = ::CC::Engine::Analyzers::Reporter.new(double(concurrency: 2), engine, io)
1111

1212
reporter.run
1313

1414
io.string
1515
end
16+
17+
def sample_flay_issue(
18+
structural_hash = "bad8b06679434a129fbfc4034d9ddad7",
19+
name = :ExpressionStatement,
20+
bonus = "*3",
21+
mass = 99,
22+
locations = sample_locations
23+
)
24+
flay = Flay::Item.new(structural_hash, name, bonus, mass, locations)
25+
end
26+
27+
def sample_locations
28+
[
29+
sample_flay_location("./foo.js", 1, nil),
30+
sample_flay_location("./foo.js", 2, nil),
31+
sample_flay_location("./foo.js", 3, nil),
32+
]
33+
end
34+
35+
def sample_flay_location(file = "./foo.js", line = 1, fuzzy = nil)
36+
Flay::Location.new(file, line, fuzzy)
37+
end
1638
end

0 commit comments

Comments
 (0)