Skip to content

Commit 4f8040e

Browse files
committed
adjusted several Python tests to accommodate for the build dir usage
1 parent 856282d commit 4f8040e

5 files changed

Lines changed: 98 additions & 136 deletions

File tree

test/cli/helloworld_test.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,24 +291,25 @@ def test_checkers_report(tmpdir):
291291
filename = os.path.join(tmpdir, '1.txt')
292292
args = [
293293
f'--checkers-report={filename}',
294+
'--no-cppcheck-build-dir', # TODO: remove this
294295
'helloworld'
295296
]
296297

297298
cppcheck(args, cwd=__script_dir)
298299

299300
with open(filename, 'rt') as f:
300-
data = f.read()
301-
assert 'No CheckAutoVariables::assignFunctionArg' in data
302-
assert 'Yes CheckAutoVariables::autoVariables' in data
301+
data = f.read().splitlines()
302+
assert 'No CheckAutoVariables::assignFunctionArg require:style,warning' in data, json.dumps(data, indent=4)
303+
assert 'Yes CheckAutoVariables::autoVariables' in data, json.dumps(data, indent=4)
303304

304305
args += [
305306
'--enable=style'
306307
]
307308
cppcheck(args, cwd=__script_dir)
308309
with open(filename, 'rt') as f:
309-
data = f.read()
310+
data = f.read().splitlines()
310311
# checker has been activated by --enable=style
311-
assert 'Yes CheckAutoVariables::assignFunctionArg' in data
312+
assert 'Yes CheckAutoVariables::assignFunctionArg' in data, json.dumps(data, indent=4)
312313

313314

314315
def test_missing_include_system(): # #11283

test/cli/other_test.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -820,13 +820,6 @@ def test_unused_function_include(tmpdir):
820820
__test_unused_function_include(tmpdir, [])
821821

822822

823-
# TODO: remove when we inject builddir
824-
def test_unused_function_include_builddir(tmpdir):
825-
builddir = os.path.join(tmpdir, 'injected')
826-
os.makedirs(builddir)
827-
__test_unused_function_include(tmpdir, ['--cppcheck-build-dir={}'.format(builddir)])
828-
829-
830823
# TODO: test with all other types
831824
def test_showtime_top5_file(tmpdir):
832825
test_file = os.path.join(tmpdir, 'test.cpp')

test/cli/qml_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __test_unused_functions(extra_args):
3535

3636

3737
def test_unused_functions():
38-
__test_unused_functions(['-j1'])
38+
__test_unused_functions(['-j1', '--no-cppcheck-build-dir'])
3939

4040

4141
def test_unused_functions_j():
@@ -45,6 +45,7 @@ def test_unused_functions_j():
4545
'--library=qt',
4646
'--enable=unusedFunction',
4747
'-j2',
48+
'--no-cppcheck-build-dir',
4849
__project_dir
4950
]
5051
ret, stdout, stderr = cppcheck(args)

test/cli/unused_function_test.py

Lines changed: 73 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import json
6+
import pytest
67
from testutils import cppcheck
78

89
__script_dir = os.path.dirname(os.path.abspath(__file__))
@@ -30,185 +31,140 @@ def __create_compdb(tmpdir, projpath):
3031
return compile_commands
3132

3233

33-
def test_unused_functions():
34-
ret, stdout, stderr = cppcheck(['-q', '--template=simple', '--enable=unusedFunction', '--inline-suppr', '-j1', __project_dir])
34+
def __test_unused_functions(extra_args):
35+
args = ['-q', '--template=simple', '--enable=unusedFunction', '--inline-suppr', __project_dir]
36+
args += extra_args
37+
ret, stdout, stderr = cppcheck(args)
3538
assert stdout.splitlines() == []
3639
assert stderr.splitlines() == [
3740
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep)
3841
]
3942
assert ret == 0, stdout
4043

4144

45+
def test_unused_functions():
46+
__test_unused_functions(['-j1', '--no-cppcheck-build-dir'])
47+
48+
4249
def test_unused_functions_j():
43-
ret, stdout, stderr = cppcheck(['-q', '--template=simple', '--enable=unusedFunction', '--inline-suppr', '-j2', __project_dir])
50+
ret, stdout, stderr = cppcheck(['-q', '--template=simple', '--enable=unusedFunction', '--inline-suppr', '-j2', '--no-cppcheck-build-dir', __project_dir])
4451
assert stdout.splitlines() == [
4552
"cppcheck: unusedFunction check requires --cppcheck-build-dir to be active with -j."
4653
]
4754
assert stderr.splitlines() == []
4855
assert ret == 0, stdout
4956

5057

51-
def test_unused_functions_project():
52-
ret, stdout, stderr = cppcheck(['-q',
53-
'--template=simple',
54-
'--enable=unusedFunction',
55-
'--inline-suppr',
56-
'--project={}'.format(os.path.join(__project_dir, 'unusedFunction.cppcheck')),
57-
'-j1'])
58+
def test_unused_functions_builddir(tmpdir):
59+
build_dir = os.path.join(tmpdir, 'b1')
60+
os.mkdir(build_dir)
61+
__test_unused_functions(['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
62+
63+
64+
@pytest.mark.xfail(strict=True)
65+
def test_unused_functions_builddir_j(tmpdir):
66+
build_dir = os.path.join(tmpdir, 'b1')
67+
os.mkdir(build_dir)
68+
__test_unused_functions(['-j2', '--cppcheck-build-dir={}'.format(build_dir)])
69+
70+
def __test_unused_functions_project(extra_args):
71+
project_file = os.path.join(__project_dir, 'unusedFunction.cppcheck')
72+
args = ['-q',
73+
'--template=simple',
74+
'--enable=unusedFunction',
75+
'--inline-suppr',
76+
'--project={}'.format(project_file),
77+
]
78+
args += extra_args
79+
ret, stdout, stderr = cppcheck(args)
5880
assert stdout.splitlines() == []
5981
assert [
6082
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep)
6183
] == stderr.splitlines()
6284
assert ret == 0, stdout
6385

6486

65-
def test_unused_functions_project_j():
66-
ret, stdout, stderr = cppcheck(['-q',
67-
'--template=simple',
68-
'--enable=unusedFunction',
69-
'--inline-suppr',
70-
'--project={}'.format(os.path.join(__project_dir, 'unusedFunction.cppcheck')),
71-
'-j2'])
72-
assert stdout.splitlines() == [
73-
"cppcheck: unusedFunction check requires --cppcheck-build-dir to be active with -j."
74-
]
75-
assert [] == stderr.splitlines()
76-
assert ret == 0, stdout
77-
78-
79-
def test_unused_functions_compdb(tmpdir):
80-
compdb_file = __create_compdb(tmpdir, __project_dir)
81-
ret, stdout, stderr = cppcheck(['-q',
82-
'--template=simple',
83-
'--enable=unusedFunction',
84-
'--inline-suppr',
85-
'--project={}'.format(compdb_file),
86-
'-j1'
87-
])
88-
assert stdout.splitlines() == []
89-
assert stderr.splitlines() == [
90-
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep)
91-
]
92-
assert ret == 0, stdout
87+
def test_unused_functions_project():
88+
__test_unused_functions_project(['-j1', '--no-cppcheck-build-dir'])
9389

9490

95-
def test_unused_functions_compdb_j(tmpdir):
96-
compdb_file = __create_compdb(tmpdir, __project_dir)
91+
def test_unused_functions_project_j():
92+
project_file = os.path.join(__project_dir, 'unusedFunction.cppcheck')
9793
ret, stdout, stderr = cppcheck(['-q',
9894
'--template=simple',
9995
'--enable=unusedFunction',
10096
'--inline-suppr',
101-
'--project={}'.format(compdb_file),
102-
'-j2'
97+
'--project={}'.format(project_file),
98+
'-j2',
99+
'--no-cppcheck-build-dir'
103100
])
104101
assert stdout.splitlines() == [
105102
"cppcheck: unusedFunction check requires --cppcheck-build-dir to be active with -j."
106103
]
107-
assert stderr.splitlines() == []
104+
assert [] == stderr.splitlines()
108105
assert ret == 0, stdout
109106

110107

111-
def test_unused_functions_builddir(tmpdir):
108+
def test_unused_functions_project_builddir(tmpdir):
112109
build_dir = os.path.join(tmpdir, 'b1')
113110
os.mkdir(build_dir)
114-
ret, stdout, stderr = cppcheck(['-q', '--template=simple', '--enable=unusedFunction', '--inline-suppr', '-j1', '--cppcheck-build-dir={}'.format(build_dir), __project_dir])
115-
assert stdout.splitlines() == []
116-
assert stderr.splitlines() == [
117-
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep)
118-
]
119-
assert ret == 0, stdout
111+
__test_unused_functions_project(['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
120112

121113

122-
# TODO: only f3_3 is unused
123-
def test_unused_functions_builddir_j(tmpdir):
114+
@pytest.mark.xfail(strict=True)
115+
def test_unused_functions_project_builddir_j(tmpdir):
124116
build_dir = os.path.join(tmpdir, 'b1')
125117
os.mkdir(build_dir)
126-
ret, stdout, stderr = cppcheck(['-q', '--template=simple', '--enable=unusedFunction', '--inline-suppr', '-j2', '--cppcheck-build-dir={}'.format(build_dir), __project_dir])
127-
assert stdout.splitlines() == []
128-
assert stderr.splitlines() == [
129-
"{}1.c:4:0: style: The function 'f1' is never used. [unusedFunction]".format(__project_dir_sep),
130-
"{}2.c:4:0: style: The function 'f2' is never used. [unusedFunction]".format(__project_dir_sep),
131-
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep),
132-
"{}4.c:4:0: style: The function 'f4_1' is never used. [unusedFunction]".format(__project_dir_sep)
133-
]
134-
assert ret == 0, stdout
118+
__test_unused_functions_project(['-j2', '--cppcheck-build-dir={}'.format(build_dir)])
135119

136120

137-
def test_unused_functions_builddir_project(tmpdir):
138-
build_dir = os.path.join(tmpdir, 'b1')
139-
os.mkdir(build_dir)
140-
ret, stdout, stderr = cppcheck(['-q',
141-
'--template=simple',
142-
'--enable=unusedFunction',
143-
'--inline-suppr',
144-
'--project={}'.format(os.path.join(__project_dir, 'unusedFunction.cppcheck')),
145-
'--cppcheck-build-dir={}'.format(build_dir),
146-
'-j1'])
121+
def __test_unused_functions_compdb(tmpdir, extra_args):
122+
compdb_file = __create_compdb(tmpdir, __project_dir)
123+
args = ['-q',
124+
'--template=simple',
125+
'--enable=unusedFunction',
126+
'--inline-suppr',
127+
'--project={}'.format(compdb_file),
128+
'-j1'
129+
]
130+
args += extra_args
131+
ret, stdout, stderr = cppcheck(args)
147132
assert stdout.splitlines() == []
148133
assert stderr.splitlines() == [
149134
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep)
150135
]
151136
assert ret == 0, stdout
152137

153138

154-
# TODO: only f3_3 is unused
155-
def test_unused_functions_builddir_project_j(tmpdir):
156-
build_dir = os.path.join(tmpdir, 'b1')
157-
os.mkdir(build_dir)
158-
ret, stdout, stderr = cppcheck(['-q',
159-
'--template=simple',
160-
'--enable=unusedFunction',
161-
'--inline-suppr',
162-
'--project={}'.format(os.path.join(__project_dir, 'unusedFunction.cppcheck')),
163-
'--cppcheck-build-dir={}'.format(build_dir),
164-
'-j2'])
165-
assert stdout.splitlines() == []
166-
assert stderr.splitlines() == [
167-
"{}1.c:4:0: style: The function 'f1' is never used. [unusedFunction]".format(__project_dir_sep),
168-
"{}2.c:4:0: style: The function 'f2' is never used. [unusedFunction]".format(__project_dir_sep),
169-
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep),
170-
"{}4.c:4:0: style: The function 'f4_1' is never used. [unusedFunction]".format(__project_dir_sep)
171-
]
172-
assert ret == 0, stdout
139+
def test_unused_functions_compdb(tmpdir):
140+
__test_unused_functions_compdb(tmpdir, ['-j1', '--no-cppcheck-build-dir'])
173141

174142

175-
def test_unused_functions_builddir_compdb(tmpdir):
143+
def test_unused_functions_compdb_j(tmpdir):
176144
compdb_file = __create_compdb(tmpdir, __project_dir)
177-
build_dir = os.path.join(tmpdir, 'b1')
178-
os.mkdir(build_dir)
179145
ret, stdout, stderr = cppcheck(['-q',
180146
'--template=simple',
181147
'--enable=unusedFunction',
182148
'--inline-suppr',
183149
'--project={}'.format(compdb_file),
184-
'--cppcheck-build-dir={}'.format(build_dir),
185-
'-j1'
150+
'-j2',
151+
'--no-cppcheck-build-dir'
186152
])
187-
assert stdout.splitlines() == []
188-
assert stderr.splitlines() == [
189-
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep)
153+
assert stdout.splitlines() == [
154+
"cppcheck: unusedFunction check requires --cppcheck-build-dir to be active with -j."
190155
]
156+
assert stderr.splitlines() == []
191157
assert ret == 0, stdout
192158

193159

194-
# TODO: only f3_3 is unused
195-
def test_unused_functions_builddir_compdb_j(tmpdir):
196-
compdb_file = __create_compdb(tmpdir, __project_dir)
160+
def test_unused_functions_compdb_builddir(tmpdir):
197161
build_dir = os.path.join(tmpdir, 'b1')
198162
os.mkdir(build_dir)
199-
ret, stdout, stderr = cppcheck(['-q',
200-
'--template=simple',
201-
'--enable=unusedFunction',
202-
'--inline-suppr',
203-
'--project={}'.format(compdb_file),
204-
'--cppcheck-build-dir={}'.format(build_dir),
205-
'-j2'
206-
])
207-
assert stdout.splitlines() == []
208-
assert stderr.splitlines() == [
209-
"{}1.c:4:0: style: The function 'f1' is never used. [unusedFunction]".format(__project_dir_sep),
210-
"{}2.c:4:0: style: The function 'f2' is never used. [unusedFunction]".format(__project_dir_sep),
211-
"{}3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(__project_dir_sep),
212-
"{}4.c:4:0: style: The function 'f4_1' is never used. [unusedFunction]".format(__project_dir_sep)
213-
]
214-
assert ret == 0, stdout
163+
__test_unused_functions_compdb(tmpdir, ['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
164+
165+
166+
@pytest.mark.xfail(strict=True)
167+
def test_unused_functions_compdb_buildir_j(tmpdir):
168+
build_dir = os.path.join(tmpdir, 'b1')
169+
os.mkdir(build_dir)
170+
__test_unused_functions_compdb(tmpdir, ['-j2', '--cppcheck-build-dir={}'.format(build_dir)])

test/cli/whole-program_test.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,11 @@ def test_addon_rerun(tmp_path, builddir):
220220
'--enable=style',
221221
'--template={id}',
222222
'whole-program']
223+
# do not use the injection because the directory needs to survive runs
223224
if builddir:
224225
args.append('--cppcheck-build-dir=' + str(tmp_path))
226+
else:
227+
args.append('--no-cppcheck-build-dir')
225228
_, _, stderr = cppcheck(args, cwd=__script_dir)
226229
assert 'misra-c2012-5.8' in stderr
227230
_, _, stderr = cppcheck(args, cwd=__script_dir)
@@ -293,10 +296,9 @@ def test_checkclass():
293296
__test_checkclass(['-j1'])
294297

295298

296-
# whole program analysis requires a build dir with -j
297-
@pytest.mark.xfail(strict=True)
299+
@pytest.mark.xfail(strict=True) # TODO: check error
298300
def test_checkclass_j():
299-
__test_checkclass(['-j2'])
301+
__test_checkclass(['-j2', '--no-cppcheck-build-dir'])
300302

301303

302304
def test_checkclass_builddir(tmpdir):
@@ -305,6 +307,11 @@ def test_checkclass_builddir(tmpdir):
305307
__test_checkclass(['--cppcheck-build-dir={}'.format(build_dir)])
306308

307309

310+
def test_checkclass_builddir_j(tmpdir):
311+
build_dir = os.path.join(tmpdir, 'b1')
312+
os.mkdir(build_dir)
313+
__test_checkclass(['-j2', '--cppcheck-build-dir={}'.format(build_dir)])
314+
308315
def __test_checkclass_project(tmpdir, extra_args):
309316
odr_file_1 = os.path.join(__script_dir, 'whole-program', 'odr1.cpp')
310317

@@ -336,14 +343,18 @@ def test_checkclass_project(tmpdir):
336343
__test_checkclass_project(tmpdir, ['-j1'])
337344

338345

339-
# whole program analysis requires a build dir with -j
340-
@pytest.mark.xfail(strict=True)
346+
@pytest.mark.xfail(strict=True) # TODO: check error
341347
def test_checkclass_project_j(tmpdir):
342-
__test_checkclass_project(tmpdir, ['-j2'])
348+
__test_checkclass_project(tmpdir, ['-j2', '--no-cppcheck-build-dir'])
343349

344350

345351
def test_checkclass_project_builddir(tmpdir):
346352
build_dir = os.path.join(tmpdir, 'b1')
347353
os.mkdir(build_dir)
348354
__test_checkclass_project(tmpdir, ['-j1', '--cppcheck-build-dir={}'.format(build_dir)])
349355

356+
357+
def test_checkclass_project_builddir_j(tmpdir):
358+
build_dir = os.path.join(tmpdir, 'b1')
359+
os.mkdir(build_dir)
360+
__test_checkclass_project(tmpdir, ['-j2', '--cppcheck-build-dir={}'.format(build_dir)])

0 commit comments

Comments
 (0)