|
2 | 2 | # python3 -m pytest test-unused_function_test.py |
3 | 3 |
|
4 | 4 | import os |
| 5 | +import json |
5 | 6 | from testutils import cppcheck |
6 | 7 |
|
7 | 8 | __script_dir = os.path.dirname(os.path.abspath(__file__)) |
8 | 9 |
|
9 | 10 | PROJECT_DIR = os.path.join(__script_dir, 'unusedFunction') |
10 | 11 |
|
11 | 12 |
|
| 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 | + |
12 | 38 | def test_unused_functions(): |
13 | 39 | ret, stdout, stderr = cppcheck(['-q', '--template=simple', '--enable=unusedFunction', '--inline-suppr', PROJECT_DIR]) |
14 | 40 | assert stdout.splitlines() == [] |
@@ -54,6 +80,37 @@ def test_unused_functions_project_j(): |
54 | 80 | assert ret == 0, stdout |
55 | 81 |
|
56 | 82 |
|
| 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 | + |
57 | 114 | def test_unused_functions_builddir(tmpdir): |
58 | 115 | build_dir = os.path.join(tmpdir, 'b1') |
59 | 116 | os.mkdir(build_dir) |
@@ -114,4 +171,42 @@ def test_unused_functions_builddir_project_j(tmpdir): |
114 | 171 | ] |
115 | 172 | assert ret == 0, stdout |
116 | 173 |
|
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