-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain_spec.rb
295 lines (254 loc) · 8.57 KB
/
main_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require 'spec_helper'
require 'cc/engine/analyzers/php/main'
require 'cc/engine/analyzers/reporter'
require 'cc/engine/analyzers/engine_config'
RSpec.describe CC::Engine::Analyzers::Php::Main, in_tmpdir: true do
include AnalyzerSpecHelpers
describe "#run" do
it "prints an issue for identical code" do
create_source_file("foo.php", <<-EOPHP)
<?php
function hello($name) {
if (empty($name)) {
echo "Hello World!";
} else {
echo "Hello $name!";
}
}
function hello($name) {
if (empty($name)) {
echo "Hello World!";
} else {
echo "Hello $name!";
}
}
EOPHP
issues = run_engine(engine_conf).strip.split("\0")
expect(issues.length).to be > 0
result = issues.first.strip
json = JSON.parse(result)
expect(json["type"]).to eq("issue")
expect(json["check_name"]).to eq("identical-code")
expect(json["description"]).to eq("Identical blocks of code found in 2 locations. Consider refactoring.")
expect(json["categories"]).to eq(["Duplication"])
expect(json["location"]).to eq({
"path" => "foo.php",
"lines" => { "begin" => 2, "end" => 8 },
})
expect(json["remediation_points"]).to eq(965_000)
expect(json["other_locations"]).to eq([
{"path" => "foo.php", "lines" => { "begin" => 10, "end" => 16} },
])
expect(json["content"]["body"]).to match(/This issue has a mass of 24/)
expect(json["fingerprint"]).to eq("367e371730140daeda61ab577c617236")
expect(json["severity"]).to eq(CC::Engine::Analyzers::Base::MAJOR)
end
it "prints an issue for similar code" do
create_source_file("foo.php", <<-EOPHP)
<?php
function hello($name) {
if (empty($name)) {
echo "Hello World!";
} else {
echo "Hello $name!";
}
}
function hi($nickname) {
if (empty($nickname)) {
echo "Hi World!";
} else {
echo "Hi $nickname!";
}
}
EOPHP
issues = run_engine(engine_conf).strip.split("\0")
expect(issues.length).to be > 0
result = issues.first.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 blocks of code found in 2 locations. Consider refactoring.")
expect(json["categories"]).to eq(["Duplication"])
expect(json["location"]).to eq({
"path" => "foo.php",
"lines" => { "begin" => 2, "end" => 8 },
})
expect(json["remediation_points"]).to eq(965_000)
expect(json["other_locations"]).to eq([
{"path" => "foo.php", "lines" => { "begin" => 10, "end" => 16} },
])
expect(json["content"]["body"]).to match(/This issue has a mass of 24/)
expect(json["fingerprint"]).to eq("8fe741c1e4cccc7226bbf6f0244fc49d")
end
it "runs against complex files" do
FileUtils.cp(fixture_path("symfony_configuration.php"), File.join(@code, "configuration.php"))
issues = run_engine(engine_conf).strip.split("\0")
expect(issues.length).to be > 0
result = issues.first.strip
expect(result).to match "\"type\":\"issue\""
end
it "handles INF & NAN constants" do
create_source_file("foo.php", <<-EOPHP)
<?php
function f1($name) {
// the php-parser lib turns this into INF, but writing INF directly gets emitted differently
if (empty($name)) {
return 646e444;
} else {
return 0;
}
}
function f2($name) {
if (empty($name)) {
return 646e444;
} else {
return 0;
}
}
EOPHP
result = run_engine(engine_conf).strip
expect(result).to match "\"type\":\"issue\""
end
it "skips unparsable files" do
create_source_file("foo.php", <<-EOPHP)
<?php blorb &; "fee
EOPHP
expect(CC.logger).to receive(:warn).with(/Skipping \.\/foo.php/)
expect(CC.logger).to receive(:warn).with(/Response status: 422/)
expect(run_engine(engine_conf)).to eq("")
end
it "can parse php 7 code" do
create_source_file("foo.php", File.read(fixture_path("from_phan_php7.php")))
issues = run_engine(engine_conf).strip.split("\0")
expect(issues.length).to be > 0
result = issues.first.strip
json = JSON.parse(result)
expect(json["location"]).to eq({
"path" => "foo.php",
"lines" => { "begin" => 190, "end" => 190 },
})
end
it "can parse php 7.1 code" do
create_source_file("foo.php", File.read(fixture_path("php_71_sample.php")))
issues = run_engine(engine_conf).strip.split("\0")
expect(issues.length).to eq(2)
expect(JSON.parse(issues.first.strip)["location"]).to eq({
"path" => "foo.php",
"lines" => { "begin" => 2, "end" => 9 },
})
expect(JSON.parse(issues.last.strip)["location"]).to eq({
"path" => "foo.php",
"lines" => { "begin" => 11, "end" => 18 },
})
end
it "ignores namespace and use declarations" do
create_source_file("foo.php", <<~EOPHP)
<?php
namespace KeepClear\\Http\\Controllers\\API\\V1;
use Illuminate\\Http\\Request;
use KeepClear\\Http\\Controllers\\Controller;
use KeepClear\\Models\\Comment;
use KeepClear\\Models\\User;
use KeepClear\\Models\\Asset;
use KeepClear\\Traits\\Controllers\\ApiFilter;
use KeepClear\\Traits\\Controllers\\ApiParseBody;
use KeepClear\\Traits\\Controllers\\ApiException;
a / b;
EOPHP
create_source_file("bar.php", <<~EOPHP)
<?php
namespace KeepClear\\Http\\Controllers\\API\\V1;
use Illuminate\\Http\\Request;
use KeepClear\\Http\\Controllers\\Controller;
use KeepClear\\Models\\Comment;
use KeepClear\\Models\\User;
use KeepClear\\Models\\Asset;
use KeepClear\\Traits\\Controllers\\ApiFilter;
use KeepClear\\Traits\\Controllers\\ApiParseBody;
use KeepClear\\Traits\\Controllers\\ApiException;
a + b;
EOPHP
issues = run_engine(engine_conf 6).strip.split("\0")
expect(issues).to be_empty
end
context "comments" do
it "ignores PHPDoc comments" do
create_source_file("foo.php", <<-EOPHP)
<?php
/**
* Says "hello"
*
* @param string $name
*/
function hello($name) {
if (empty($name)) {
echo "Hello World!";
} else {
echo "Hello $name!";
}
}
/**
* Says "hi"
*
* @param string $nickname
*/
function hi($nickname) {
if (empty($nickname)) {
echo "Hi World!";
} else {
echo "Hi $nickname!";
}
}
EOPHP
issues = run_engine(engine_conf).strip.split("\0")
expect(issues.length).to be > 0
issue = JSON.parse(issues.first.strip)
expect(issue["location"]).to eq(
"path" => "foo.php",
"lines" => { "begin" => 8, "end" => 14 },
)
expect(issue["content"]["body"]).to match(/This issue has a mass of 24/)
end
it "ignores one-line comments" do
create_source_file("foo.php", <<-EOPHP)
<?php
// Says "hello"
function hello($name) {
if (empty($name)) {
echo "Hello World!";
} else {
echo "Hello $name!";
}
}
// Says "hi"
function hi($nickname) {
if (empty($nickname)) {
echo "Hi World!";
} else {
echo "Hi $nickname!";
}
}
EOPHP
issues = run_engine(engine_conf).strip.split("\0")
expect(issues.length).to be > 0
issue = JSON.parse(issues.first.strip)
expect(issue["location"]).to eq(
"path" => "foo.php",
"lines" => { "begin" => 4, "end" => 10 },
)
expect(issue["content"]["body"]).to match(/This issue has a mass of 24/)
end
end
end
def engine_conf mass = 5
CC::Engine::Analyzers::EngineConfig.new({
'config' => {
'languages' => {
'php' => {
'mass_threshold' => mass,
},
},
},
})
end
end