-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathhelloworld_test.py
More file actions
357 lines (307 loc) · 11.9 KB
/
helloworld_test.py
File metadata and controls
357 lines (307 loc) · 11.9 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# python -m pytest test-helloworld.py
import os
import re
import glob
import json
import xml.etree.ElementTree as ET
from testutils import create_gui_project_file, cppcheck
__script_dir = os.path.dirname(os.path.abspath(__file__))
__proj_dir = os.path.join(__script_dir, 'helloworld')
# Get Visual Studio configurations checking a file
# Checking {file} {config}...
def __getVsConfigs(stdout, filename):
ret = []
for line in stdout.split('\n'):
if not line.startswith('Checking %s ' % filename):
continue
if not line.endswith('...'):
continue
res = re.match(r'.* ([A-Za-z0-9|]+)...', line)
if res:
ret.append(res.group(1))
ret.sort()
return ' '.join(ret)
def test_relative_path():
args = [
'--template=cppcheck1',
'helloworld'
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
filename = os.path.join('helloworld', 'main.c')
assert ret == 0, stdout
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_local_path():
args = [
'--template=cppcheck1',
'.'
]
ret, stdout, stderr = cppcheck(args, cwd=__proj_dir)
assert ret == 0, stdout
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_absolute_path():
args = [
'--template=cppcheck1',
__proj_dir
]
ret, stdout, stderr = cppcheck(args)
filename = os.path.join(__proj_dir, 'main.c')
assert ret == 0, stdout
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_addon_local_path():
args = [
'--addon=misra',
'--enable=style',
'--template=cppcheck1',
'.'
]
ret, stdout, stderr = cppcheck(args, cwd=__proj_dir)
assert ret == 0, stdout
assert stderr == ('[main.c:5]: (error) Division by zero.\n'
'[main.c:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n')
def test_addon_local_path_not_enable():
args = [
'--addon=misra',
'--template=cppcheck1',
'.'
]
ret, stdout, stderr = cppcheck(args, cwd=__proj_dir)
assert ret == 0, stdout
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_addon_absolute_path():
args = [
'--addon=misra',
'--enable=style',
'--template=cppcheck1',
__proj_dir
]
ret, stdout, stderr = cppcheck(args)
filename = os.path.join(__proj_dir, 'main.c')
assert ret == 0, stdout
assert stderr == ('[%s:5]: (error) Division by zero.\n'
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
def test_addon_relative_path():
args = [
'--addon=misra',
'--enable=style',
'--template=cppcheck1',
'helloworld'
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
filename = os.path.join('helloworld', 'main.c')
assert ret == 0, stdout
assert stdout == ('Checking %s ...\n'
'Checking %s: SOME_CONFIG...\n' % (filename, filename))
assert stderr == ('[%s:5]: (error) Division by zero.\n'
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
def test_addon_with_gui_project():
project_file = os.path.join('helloworld', 'test.cppcheck')
# TODO: generate in temporary folder
create_gui_project_file(os.path.join(__script_dir, project_file), paths=['.'], addon='misra')
args = [
'--template=cppcheck1',
'--enable=style',
'--project=' + project_file
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
os.remove(os.path.join(__script_dir, project_file)) # TODO: do not remove explicitly
filename = os.path.join('helloworld', 'main.c')
assert ret == 0, stdout
assert stdout == 'Checking %s ...\n' % filename
assert stderr == ('[%s:5]: (error) Division by zero.\n'
'[%s:1]: (style) misra violation (use --rule-texts=<file> to get proper output)\n' % (filename, filename))
def test_basepath_relative_path():
args = [
'helloworld',
'--template=cppcheck1',
'-rp=helloworld'
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
assert ret == 0, stdout
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_basepath_absolute_path():
args = [
'--template=cppcheck1',
__proj_dir,
'-rp=' + __proj_dir
]
ret, stdout, stderr = cppcheck(args)
assert ret == 0, stdout
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_vs_project_local_path():
args = [
'--template=cppcheck1',
'--project=helloworld.vcxproj'
]
ret, stdout, stderr = cppcheck(args, cwd=__proj_dir)
assert ret == 0, stdout
assert __getVsConfigs(stdout, 'main.c') == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_vs_project_relative_path():
args = [
'--template=cppcheck1',
'--project=' + os.path.join(__proj_dir, 'helloworld.vcxproj')
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
filename = os.path.join(__proj_dir, 'main.c')
assert ret == 0, stdout
assert __getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_vs_project_absolute_path():
args = [
'--template=cppcheck1',
'--project=' + os.path.join(__proj_dir, 'helloworld.vcxproj')
]
ret, stdout, stderr = cppcheck(args)
filename = os.path.join(__proj_dir, 'main.c')
assert ret == 0, stdout
assert __getVsConfigs(stdout, filename) == 'Debug|Win32 Debug|x64 Release|Win32 Release|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_cppcheck_project_local_path():
args = [
'--template=cppcheck1',
'--platform=win64',
'--project=helloworld.cppcheck'
]
ret, stdout, stderr = cppcheck(args, cwd=__proj_dir)
assert ret == 0, stdout
assert __getVsConfigs(stdout, 'main.c') == 'Debug|x64'
assert stderr == '[main.c:5]: (error) Division by zero.\n'
def test_cppcheck_project_relative_path():
args = [
'--template=cppcheck1',
'--platform=win64',
'--project=' + os.path.join('helloworld', 'helloworld.cppcheck')
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
filename = os.path.join('helloworld', 'main.c')
assert ret == 0, stdout
assert __getVsConfigs(stdout, filename) == 'Debug|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_cppcheck_project_absolute_path():
args = [
'--template=cppcheck1',
'--platform=win64',
'--project=' + os.path.join(__proj_dir, 'helloworld.cppcheck')
]
ret, stdout, stderr = cppcheck(args)
filename = os.path.join(__proj_dir, 'main.c')
assert ret == 0, stdout
assert __getVsConfigs(stdout, filename) == 'Debug|x64'
assert stderr == '[%s:5]: (error) Division by zero.\n' % filename
def test_suppress_command_line_related():
args = [
'--suppress=zerodiv:' + os.path.join('helloworld', 'main.c'),
'helloworld'
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
assert ret == 0, stdout
assert stderr == ''
def test_suppress_command_line_absolute():
args = [
'--suppress=zerodiv:' + os.path.join(__proj_dir, 'main.c'),
__proj_dir
]
ret, stdout, stderr = cppcheck(args)
assert ret == 0, stdout
assert stderr == ''
def test_suppress_project_relative():
project_file = os.path.join('helloworld', 'test.cppcheck')
# TODO: generate in temporary folder
create_gui_project_file(os.path.join(__script_dir, project_file),
paths=['.'],
suppressions=[{'fileName':'main.c', 'id':'zerodiv'}])
args = [
'--project=' + project_file
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
os.remove(os.path.join(__script_dir, project_file)) # TODO: do not remove explicitly
assert ret == 0, stdout
assert stderr == ''
def test_suppress_project_absolute():
project_file = os.path.join('helloworld', 'test.cppcheck')
# TODO: generate in temporary folder
create_gui_project_file(os.path.join(__script_dir, project_file),
paths=['.'],
suppressions=[{'fileName':'main.c', 'id':'zerodiv'}])
args = [
'--project=' + os.path.join(__script_dir, 'helloworld', 'test.cppcheck')
]
ret, stdout, stderr = cppcheck(args)
os.remove(os.path.join(__script_dir, project_file)) # TODO: do not remove explicitly
assert ret == 0, stdout
assert stderr == ''
def test_exclude():
args = [
'-i' + 'helloworld',
'--platform=win64',
'--project=' + os.path.join('helloworld', 'helloworld.cppcheck')
]
ret, stdout, _ = cppcheck(args, cwd=__script_dir)
assert ret == 1
lines = stdout.splitlines()
assert lines == [
'cppcheck: error: no C or C++ source files found.',
'cppcheck: all paths were ignored'
]
def test_build_dir_dump_output(tmpdir):
args = [
f'--cppcheck-build-dir={tmpdir}',
'--addon=misra',
'helloworld'
]
cppcheck(args)
cppcheck(args)
filename = f'{tmpdir}/main.a1.*.dump'
filelist = glob.glob(filename)
assert(len(filelist) == 0)
def test_checkers_report(tmpdir):
filename = os.path.join(tmpdir, '1.txt')
args = [
f'--checkers-report={filename}',
'--no-cppcheck-build-dir', # TODO: remove this
'helloworld'
]
cppcheck(args, cwd=__script_dir)
with open(filename, 'rt') as f:
data = f.read().splitlines()
assert 'No CheckAutoVariables::assignFunctionArg require:style,warning' in data, json.dumps(data, indent=4)
assert 'Yes CheckAutoVariables::autoVariables' in data, json.dumps(data, indent=4)
args += [
'--enable=style'
]
cppcheck(args, cwd=__script_dir)
with open(filename, 'rt') as f:
data = f.read().splitlines()
# checker has been activated by --enable=style
assert 'Yes CheckAutoVariables::assignFunctionArg' in data, json.dumps(data, indent=4)
def test_missing_include_system(): # #11283
args = [
'--enable=missingInclude',
'--suppress=zerodiv',
'--template=simple',
'helloworld'
]
_, _, stderr = cppcheck(args, cwd=__script_dir)
assert stderr.replace('\\', '/') == 'helloworld/main.c:1:0: information: Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]\n'
def test_sarif():
args = [
'--output-format=sarif',
'helloworld'
]
ret, stdout, stderr = cppcheck(args, cwd=__script_dir)
assert ret == 0, stdout
res = json.loads(stderr)
assert res['version'] == '2.1.0'
assert res['runs'][0]['results'][0]['locations'][0]['physicalLocation']['artifactLocation']['uri'] == 'helloworld/main.c'
assert res['runs'][0]['results'][0]['ruleId'] == 'zerodiv'
assert res['runs'][0]['tool']['driver']['rules'][0]['id'] == 'zerodiv'
assert res['runs'][0]['tool']['driver']['rules'][0]['properties']['precision'] == 'high'
assert res['runs'][0]['tool']['driver']['rules'][0]['properties']['security-severity'] > 9.5
assert 'security' in res['runs'][0]['tool']['driver']['rules'][0]['properties']['tags']
assert re.match(r'[0-9]+(.[0-9]+)+', res['runs'][0]['tool']['driver']['semanticVersion'])
def test_xml_checkers_report():
test_file = os.path.join(__proj_dir, 'main.c')
args = ['--xml-version=3', '--enable=all', test_file]
exitcode, _, stderr = cppcheck(args)
assert exitcode == 0
assert ET.fromstring(stderr) is not None