Skip to content

Commit 22b92cf

Browse files
committed
test/cli/unused_function_test.py: added more testing with --project= [skip ci]
1 parent 620b52a commit 22b92cf

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

test/cli/unused_function_test.py

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,39 @@
22
# python3 -m pytest test-unused_function_test.py
33

44
import os
5+
import json
56
from testutils import cppcheck
67

78
__script_dir = os.path.dirname(os.path.abspath(__file__))
89

910
PROJECT_DIR = os.path.join(__script_dir, 'unusedFunction')
1011

1112

13+
# TODO: make this a generic helper function
14+
def __create_compdb(tmpdir, projpath):
15+
compile_commands = os.path.join(tmpdir, 'compile_commands.json')
16+
j = [
17+
{
18+
'directory': projpath,
19+
'file': os.path.join(projpath, '1.c'),
20+
'command': 'gcc -c 1.c'
21+
},
22+
{
23+
'directory': projpath,
24+
'file': os.path.join(projpath, '2.c'),
25+
'command': 'gcc -c 2.c'
26+
},
27+
{
28+
'directory': projpath,
29+
'file': os.path.join(projpath, '3.c'),
30+
'command': 'gcc -c 3.c'
31+
}
32+
]
33+
with open(compile_commands, 'wt') as f:
34+
f.write(json.dumps(j, indent=4))
35+
return compile_commands
36+
37+
1238
def test_unused_functions():
1339
ret, stdout, stderr = cppcheck(['-q', '--template=simple', '--enable=unusedFunction', '--inline-suppr', PROJECT_DIR])
1440
assert stdout.splitlines() == []
@@ -54,6 +80,37 @@ def test_unused_functions_project_j():
5480
assert ret == 0, stdout
5581

5682

83+
def test_unused_functions_compdb(tmpdir):
84+
compdb_file = __create_compdb(tmpdir, PROJECT_DIR)
85+
ret, stdout, stderr = cppcheck(['-q',
86+
'--template=simple',
87+
'--enable=unusedFunction',
88+
'--inline-suppr',
89+
'--project={}'.format(compdb_file)
90+
])
91+
assert stdout.splitlines() == []
92+
assert stderr.splitlines() == [
93+
"{}/3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(PROJECT_DIR)
94+
]
95+
assert ret == 0, stdout
96+
97+
98+
def test_unused_functions_compdb_j(tmpdir):
99+
compdb_file = __create_compdb(tmpdir, PROJECT_DIR)
100+
ret, stdout, stderr = cppcheck(['-q',
101+
'--template=simple',
102+
'--enable=unusedFunction',
103+
'--inline-suppr',
104+
'--project={}'.format(compdb_file),
105+
'-j2'
106+
])
107+
assert stdout.splitlines() == [
108+
"cppcheck: unusedFunction check can't be used with '-j' option. Disabling unusedFunction check."
109+
]
110+
assert stderr.splitlines() == []
111+
assert ret == 0, stdout
112+
113+
57114
def test_unused_functions_builddir(tmpdir):
58115
build_dir = os.path.join(tmpdir, 'b1')
59116
os.mkdir(build_dir)
@@ -114,4 +171,42 @@ def test_unused_functions_builddir_project_j(tmpdir):
114171
]
115172
assert ret == 0, stdout
116173

117-
# TODO: test with FileSettings
174+
175+
def test_unused_functions_builddir_compdb(tmpdir):
176+
compdb_file = __create_compdb(tmpdir, PROJECT_DIR)
177+
build_dir = os.path.join(tmpdir, 'b1')
178+
os.mkdir(build_dir)
179+
ret, stdout, stderr = cppcheck(['-q',
180+
'--template=simple',
181+
'--enable=unusedFunction',
182+
'--inline-suppr',
183+
'--project={}'.format(compdb_file),
184+
'--cppcheck-build-dir={}'.format(build_dir)
185+
])
186+
assert stdout.splitlines() == []
187+
assert stderr.splitlines() == [
188+
"{}/3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(PROJECT_DIR)
189+
]
190+
assert ret == 0, stdout
191+
192+
193+
# TODO: only f3_3 is unused
194+
def test_unused_functions_builddir_compdb_j(tmpdir):
195+
compdb_file = __create_compdb(tmpdir, PROJECT_DIR)
196+
build_dir = os.path.join(tmpdir, 'b1')
197+
os.mkdir(build_dir)
198+
ret, stdout, stderr = cppcheck(['-q',
199+
'--template=simple',
200+
'--enable=unusedFunction',
201+
'--inline-suppr',
202+
'--project={}'.format(compdb_file),
203+
'--cppcheck-build-dir={}'.format(build_dir),
204+
'-j2'
205+
])
206+
assert stdout.splitlines() == []
207+
assert stderr.splitlines() == [
208+
"{}/1.c:4:0: style: The function 'f1' is never used. [unusedFunction]".format(PROJECT_DIR),
209+
"{}/2.c:4:0: style: The function 'f2' is never used. [unusedFunction]".format(PROJECT_DIR),
210+
"{}/3.c:3:0: style: The function 'f3_3' is never used. [unusedFunction]".format(PROJECT_DIR)
211+
]
212+
assert ret == 0, stdout

0 commit comments

Comments
 (0)