Skip to content

Commit d33ccc6

Browse files
committed
added (undocumented) --no-cppcheck-build-dir command-line option to indicate that tests should not inject a build dir
1 parent 7a5d5fc commit d33ccc6

4 files changed

Lines changed: 80 additions & 118 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,11 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
913913
mSettings.maxCtuDepth = temp;
914914
}
915915

916+
// undocumented option for usage in Python tests to indicate that no build dir should be injected
917+
else if (std::strcmp(argv[i], "--no-cppcheck-build-dir") == 0) {
918+
mSettings.buildDir.clear();
919+
}
920+
916921
else if (std::strcmp(argv[i], "--no-cpp-header-probe") == 0) {
917922
mSettings.cppHeaderProbe = false;
918923
}

test/cli/testutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def cppcheck_ex(args, env=None, remove_checkers_report=True, cwd=None, cppcheck_
194194
if 'TEST_CPPCHECK_INJECT_BUILDDIR' in os.environ:
195195
found_builddir = False
196196
for arg in args:
197-
if arg.startswith('--cppcheck-build-dir='):
197+
if arg.startswith('--cppcheck-build-dir=') or arg == '--no-cppcheck-build-dir':
198198
found_builddir = True
199199
break
200200
if not found_builddir:

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/testcmdlineparser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ class TestCmdlineParser : public TestFixture {
436436
TEST_CASE(cppcheckBuildDirExistent);
437437
TEST_CASE(cppcheckBuildDirNonExistent);
438438
TEST_CASE(cppcheckBuildDirEmpty);
439+
// TODO: test --no-cppcheck-build-dir
439440

440441
TEST_CASE(invalidCppcheckCfg);
441442
}

0 commit comments

Comments
 (0)