forked from cppcheck-opensource/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproj2_test.py
More file actions
213 lines (189 loc) · 9.66 KB
/
proj2_test.py
File metadata and controls
213 lines (189 loc) · 9.66 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
# python -m pytest test-proj2.py
import json
import os
import shutil
from testutils import create_gui_project_file, cppcheck
__script_dir = os.path.dirname(os.path.abspath(__file__))
__proj_dir = os.path.join(__script_dir, 'proj2')
__COMPILE_COMMANDS_JSON = 'compile_commands.json'
__ERR_A = ('%s:1:7: error: Division by zero. [zerodiv]\n' +
'x = 3 / 0;\n' +
' ^\n') % os.path.join('a', 'a.c')
__ERR_B = ('%s:1:7: error: Division by zero. [zerodiv]\n' +
'x = 3 / 0;\n' +
' ^\n') % os.path.join('b', 'b.c')
def __create_compile_commands(proj_dir):
proj_dir = str(proj_dir)
j = [{'directory': os.path.join(proj_dir, 'a'), 'command': 'gcc -c a.c', 'file': 'a.c'},
{'directory': proj_dir, 'command': 'gcc -c b/b.c', 'file': 'b/b.c'}]
with open(os.path.join(proj_dir, __COMPILE_COMMANDS_JSON), 'wt') as f:
f.write(json.dumps(j))
def test_file_filter():
ret, stdout, _ = cppcheck(['proj2/','--file-filter=proj2/a/*'], cwd=__script_dir)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
ret, stdout, _ = cppcheck(['proj2/','--file-filter=proj2/b*'], cwd=__script_dir)
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file2) >= 0
def test_local_path(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
__create_compile_commands(proj_dir)
ret, stdout, _ = cppcheck(['--project=compile_commands.json'], cwd=proj_dir)
file1 = os.path.join('a', 'a.c')
file2 = os.path.join('b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0
def test_local_path_force(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
__create_compile_commands(proj_dir)
ret, stdout, _ = cppcheck(['--project=compile_commands.json', '--force'], cwd=proj_dir)
assert ret == 0, stdout
assert stdout.find('AAA') >= 0
def test_local_path_maxconfigs(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
__create_compile_commands(proj_dir)
ret, stdout, _ = cppcheck(['--project=compile_commands.json', '--max-configs=2'], cwd=proj_dir)
assert ret == 0, stdout
assert stdout.find('AAA') >= 0
def test_relative_path(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
__create_compile_commands(proj_dir)
ret, stdout, _ = cppcheck(['--project=proj2/' + __COMPILE_COMMANDS_JSON], cwd=tmp_path)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0
def test_absolute_path(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
__create_compile_commands(proj_dir)
ret, stdout, _ = cppcheck(['--project=' + os.path.join(proj_dir, __COMPILE_COMMANDS_JSON)], cwd=tmp_path)
file1 = os.path.join(proj_dir, 'a', 'a.c')
file2 = os.path.join(proj_dir, 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0
def test_progress_json(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
size1 = os.path.getsize(os.path.join(proj_dir, 'a', 'a.c'))
size2 = os.path.getsize(os.path.join(proj_dir, 'b', 'b.c'))
perc1 = 100 * size1 // (size1 + size2)
perc2 = 100 * size2 // (size1 + size2)
__create_compile_commands(proj_dir)
ret, stdout, _ = cppcheck(['--project=' + os.path.join(proj_dir, __COMPILE_COMMANDS_JSON)], cwd=tmp_path)
assert ret == 0, stdout
assert stdout.find('1/2 files checked %d%% done' % perc1) >= 0 or stdout.find('1/2 files checked %d%% done' % perc2) >= 0
assert stdout.find('2/2 files checked 100% done') >= 0
def test_progress_cppcheck(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
size1 = os.path.getsize(os.path.join(proj_dir, 'a', 'a.c'))
size2 = os.path.getsize(os.path.join(proj_dir, 'b', 'b.c'))
perc1 = 100 * size1 // (size1 + size2)
perc2 = 100 * size2 // (size1 + size2)
__create_compile_commands(proj_dir)
ret, stdout, _ = cppcheck(['--project=proj2/proj2.cppcheck'], cwd=tmp_path)
assert ret == 0, stdout
assert stdout.find('1/2 files checked %d%% done' % perc1) >= 0 or stdout.find('1/2 files checked %d%% done' % perc2) >= 0
assert stdout.find('2/2 files checked 100% done') >= 0
def test_progress_cli(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
path1 = os.path.join(proj_dir, 'a', 'a.c')
path2 = os.path.join(proj_dir, 'b', 'b.c')
size1 = os.path.getsize(path1)
size2 = os.path.getsize(path2)
perc1 = 100 * size1 // (size1 + size2)
perc2 = 100 * size2 // (size1 + size2)
ret, stdout, _ = cppcheck([path1, path2], cwd=tmp_path)
assert ret == 0, stdout
assert stdout.find('1/2 files checked %d%% done' % perc1) >= 0 or stdout.find('1/2 files checked %d%% done' % perc2) >= 0
assert stdout.find('2/2 files checked 100% done') >= 0
def test_gui_project_loads_compile_commands_1(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
__create_compile_commands(proj_dir)
ret, stdout, _ = cppcheck(['--project=proj2/proj2.cppcheck'], cwd=tmp_path)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) >= 0
def test_gui_project_loads_compile_commands_2(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
__create_compile_commands(proj_dir)
exclude_path_1 = 'proj2/b'
create_gui_project_file(os.path.join(proj_dir, 'test.cppcheck'),
import_project='compile_commands.json',
exclude_paths=[exclude_path_1])
ret, stdout, _ = cppcheck(['--project=' + os.path.join('proj2','test.cppcheck')], cwd=tmp_path)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c') # Excluded by test.cppcheck
assert ret == 0, stdout
assert stdout.find('Checking %s ...' % file1) >= 0
assert stdout.find('Checking %s ...' % file2) < 0
def test_gui_project_loads_relative_vs_solution(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
create_gui_project_file(os.path.join(tmp_path, 'test.cppcheck'), import_project='proj2/proj2.sln')
ret, stdout, _ = cppcheck(['--project=test.cppcheck'], cwd=tmp_path)
file1 = os.path.join('proj2', 'a', 'a.c')
file2 = os.path.join('proj2', 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s Debug|Win32...' % file1) >= 0
assert stdout.find('Checking %s Debug|x64...' % file1) >= 0
assert stdout.find('Checking %s Release|Win32...' % file1) >= 0
assert stdout.find('Checking %s Release|x64...' % file1) >= 0
assert stdout.find('Checking %s Debug|Win32...' % file2) >= 0
assert stdout.find('Checking %s Debug|x64...' % file2) >= 0
assert stdout.find('Checking %s Release|Win32...' % file2) >= 0
assert stdout.find('Checking %s Release|x64...' % file2) >= 0
def test_gui_project_loads_absolute_vs_solution(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
create_gui_project_file(os.path.join(tmp_path, 'test.cppcheck'), import_project=os.path.join(proj_dir, 'proj2.sln'))
ret, stdout, _ = cppcheck(['--project=test.cppcheck'], cwd=tmp_path)
file1 = os.path.join(proj_dir, 'a', 'a.c')
file2 = os.path.join(proj_dir, 'b', 'b.c')
assert ret == 0, stdout
assert stdout.find('Checking %s Debug|Win32...' % file1) >= 0
assert stdout.find('Checking %s Debug|x64...' % file1) >= 0
assert stdout.find('Checking %s Release|Win32...' % file1) >= 0
assert stdout.find('Checking %s Release|x64...' % file1) >= 0
assert stdout.find('Checking %s Debug|Win32...' % file2) >= 0
assert stdout.find('Checking %s Debug|x64...' % file2) >= 0
assert stdout.find('Checking %s Release|Win32...' % file2) >= 0
assert stdout.find('Checking %s Release|x64...' % file2) >= 0
def test_gui_project_loads_relative_vs_solution_2(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
create_gui_project_file(os.path.join(tmp_path, 'test.cppcheck'), root_path='proj2', import_project='proj2/proj2.sln')
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'], cwd=tmp_path)
assert ret == 0, stdout
assert stderr == __ERR_A + __ERR_B
def test_gui_project_loads_relative_vs_solution_with_exclude(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
create_gui_project_file(os.path.join(tmp_path, 'test.cppcheck'), root_path='proj2', import_project='proj2/proj2.sln', exclude_paths=['b'])
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'], cwd=tmp_path)
assert ret == 0, stdout
assert stderr == __ERR_A
def test_gui_project_loads_absolute_vs_solution_2(tmp_path):
proj_dir = tmp_path / 'proj2'
shutil.copytree(__proj_dir, proj_dir)
create_gui_project_file(os.path.join(tmp_path, 'test.cppcheck'),
root_path=str(proj_dir),
import_project=os.path.join(proj_dir, 'proj2.sln'))
ret, stdout, stderr = cppcheck(['--project=test.cppcheck'], cwd=tmp_path)
assert ret == 0, stdout
assert stderr == __ERR_A + __ERR_B