Repository navigation
Expand file tree
/
Copy pathdumpfile_test.py
More file actions
167 lines (115 loc) · 4.65 KB
/
Copy pathdumpfile_test.py
File metadata and controls
167 lines (115 loc) · 4.65 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
# python -m pytest dumpfile_test.py
import json
import os
import pathlib
from testutils import cppcheck
import xml.etree.ElementTree as ET
def test_libraries(tmpdir): #13701
test_file = str(tmpdir / 'test.c')
with open(test_file, 'wt') as f:
f.write('x=1;\n')
args = ['--library=posix', '--dump', test_file]
_, _, _ = cppcheck(args)
dumpfile = test_file + '.dump'
assert os.path.isfile(dumpfile)
with open(dumpfile, 'rt') as f:
dump = f.read()
assert '<library lib="posix"/>' in dump
assert dump.find('<library lib="posix"/>') < dump.find('<dump cfg=')
def __test_language(tmp_path, file_ext, exp_lang, force_lang=None):
test_file = tmp_path / ('test.' + file_ext)
with open(test_file, 'wt'):
pass
args = [
'--dump',
str(test_file)
]
if force_lang:
args += ['--language=' + force_lang]
exitcode, stdout, stderr = cppcheck(args)
assert exitcode == 0, stdout if stdout else stderr
dump_s = pathlib.Path(str(test_file) + '.dump').read_text()
dump_xml = ET.fromstring(dump_s)
assert 'language' in dump_xml.attrib
assert dump_xml.attrib['language'] == exp_lang
def test_language_c(tmp_path):
__test_language(tmp_path, 'c', exp_lang='c')
def test_language_c_force_c(tmp_path):
__test_language(tmp_path, 'c', force_lang='c', exp_lang='c')
def test_language_c_force_cpp(tmp_path):
__test_language(tmp_path, 'c', force_lang='c++', exp_lang='cpp')
def test_language_cpp(tmp_path):
__test_language(tmp_path, 'cpp', exp_lang='cpp')
def test_language_cpp_force_cpp(tmp_path):
__test_language(tmp_path, 'cpp', force_lang='c++', exp_lang='cpp')
def test_language_cpp_force_c(tmp_path):
__test_language(tmp_path, 'cpp', force_lang='c', exp_lang='c')
# headers default to C
def test_language_h(tmp_path):
__test_language(tmp_path, 'h', exp_lang='c')
def test_language_h_force_c(tmp_path):
__test_language(tmp_path, 'h', force_lang='c', exp_lang='c')
def test_language_h_force_cpp(tmp_path):
__test_language(tmp_path, 'h', force_lang='c++', exp_lang='cpp')
# files with unknown extensions default to C++
def test_language_unk(tmp_path):
__test_language(tmp_path, 'src', exp_lang='cpp')
def test_language_unk_force_c(tmp_path):
__test_language(tmp_path, 'src', force_lang='c', exp_lang='c')
def test_language_unk_force_cpp(tmp_path):
__test_language(tmp_path, 'src', force_lang='c++', exp_lang='cpp')
def test_duplicate_file_entries(tmpdir): #13333
test_file = str(tmpdir / 'test.c')
with open(test_file, 'wt') as f:
f.write('x=1;\n')
project_file = str(tmpdir / 'compile_commands.json')
with open(project_file, 'wt') as f:
f.write(json.dumps([{
"file": test_file,
"directory": str(tmpdir),
"command": "cc -c test.c"
},{
"file": test_file,
"directory": str(tmpdir),
"command": "cc -c test.c"
}]))
args = ['--project=compile_commands.json', '--dump']
_, _, _ = cppcheck(args, cwd=str(tmpdir))
assert os.path.isfile(test_file + '.dump')
assert os.path.isfile(test_file + '.1.dump')
def test_container_methods(tmpdir):
test_file = str(tmpdir / 'test.cpp')
with open(test_file, 'wt') as f:
f.write('std::string s;\n')
exitcode, _, stderr = cppcheck(['--dump', '.'], cwd=str(tmpdir))
assert exitcode == 0, stderr
dumpfile = test_file + '.dump'
with open(dumpfile, 'rt') as f:
dump = f.read()
assert '<f name="emplace" action="push" yield="iterator"/>' in dump
def test_line_macro(tmpdir):
test_file = str(tmpdir / 'test.cpp')
with open(test_file, 'wt') as f:
f.write('#define MACRO() __LINE__\n')
f.write('int x = MACRO();\n')
exitcode, _, stderr = cppcheck(['--dump', test_file])
assert exitcode == 0, stderr
dumpfile = test_file + '.dump'
with open(dumpfile, 'rt') as f:
dump = f.read()
assert 'type="number" isInt="true" macroName="__LINE__"' in dump
def test_suppression_this_and_next_line(tmpdir):
test_file = str(tmpdir / 'test.c')
with open(test_file, 'wt') as f:
f.write('void f(void)\n')
f.write('{ // cppcheck-suppress id1\n')
f.write(' // cppcheck-suppress id2\n')
f.write(' x = 0;\n')
f.write('}\n')
exitcode, _, stderr = cppcheck(['--dump', '--inline-suppr', test_file])
assert exitcode == 0, stderr
dumpfile = test_file + '.dump'
with open(dumpfile, 'rt') as f:
dump = f.read()
assert 'lineNumber="2" thisAndNextLine="true" inline="true"' in dump
assert 'lineNumber="4" inline="true"' in dump