Skip to content

Commit 0a67d9b

Browse files
authored
added environment variable TEST_CPPCHECK_INJECT_BUILDDIR to inject --cppcheck-build-dir into the cppcheck invocation of Python tests (#6876)
1 parent 205f6c0 commit 0a67d9b

11 files changed

+149
-136
lines changed

.github/workflows/CI-unixish.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ jobs:
355355
env:
356356
TEST_CPPCHECK_INJECT_CLANG: clang
357357

358+
- name: Run test/cli (--cppcheck-build-dir)
359+
run: |
360+
python3 -m pytest -Werror --strict-markers -vv test/cli
361+
env:
362+
TEST_CPPCHECK_INJECT_BUILDDIR: injected
363+
358364
- name: Run cfg tests
359365
if: matrix.os != 'ubuntu-22.04'
360366
run: |

.github/workflows/CI-windows.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ jobs:
193193
env:
194194
TEST_CPPCHECK_INJECT_CLANG: clang
195195

196+
- name: Run test/cli (--cppcheck-build-dir)
197+
if: matrix.config == 'release'
198+
run: |
199+
python -m pytest -Werror --strict-markers -vv test/cli || exit /b !errorlevel!
200+
env:
201+
TEST_CPPCHECK_INJECT_BUILDDIR: injected
202+
196203
- name: Test addons
197204
if: matrix.config == 'release'
198205
run: |

.github/workflows/asan.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ jobs:
120120
env:
121121
TEST_CPPCHECK_INJECT_CLANG: clang
122122

123+
- name: Run test/cli (--cppcheck-build-dir)
124+
run: |
125+
pwd=$(pwd)
126+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
127+
env:
128+
TEST_CPPCHECK_INJECT_BUILDDIR: injected
129+
123130
- name: Generate dependencies
124131
if: false
125132
run: |

.github/workflows/tsan.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ jobs:
122122
env:
123123
TEST_CPPCHECK_INJECT_CLANG: clang
124124

125+
- name: Run test/cli (--cppcheck-build-dir)
126+
run: |
127+
pwd=$(pwd)
128+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
129+
env:
130+
TEST_CPPCHECK_INJECT_BUILDDIR: injected
131+
125132
- name: Generate dependencies
126133
if: false
127134
run: |

.github/workflows/ubsan.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ jobs:
120120
env:
121121
TEST_CPPCHECK_INJECT_CLANG: clang
122122

123+
- name: Run test/cli (--cppcheck-build-dir)
124+
run: |
125+
pwd=$(pwd)
126+
TEST_CPPCHECK_EXE_LOOKUP_PATH="$pwd/cmake.output" python3 -m pytest -Werror --strict-markers -vv test/cli
127+
env:
128+
TEST_CPPCHECK_INJECT_BUILDDIR: injected
129+
123130
- name: Generate dependencies
124131
run: |
125132
# make sure auto-generated GUI files exist

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/testutils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import select
55
import subprocess
66
import time
7+
import tempfile
78

89
# Create Cppcheck project file
910
import sys
@@ -187,6 +188,19 @@ def cppcheck_ex(args, env=None, remove_checkers_report=True, cwd=None, cppcheck_
187188
arg_executor = '--executor=' + str(os.environ['TEST_CPPCHECK_INJECT_EXECUTOR'])
188189
args.append(arg_executor)
189190

191+
builddir_tmp = None
192+
193+
if 'TEST_CPPCHECK_INJECT_BUILDDIR' in os.environ:
194+
found_builddir = False
195+
for arg in args:
196+
if arg.startswith('--cppcheck-build-dir=') or arg == '--no-cppcheck-build-dir':
197+
found_builddir = True
198+
break
199+
if not found_builddir:
200+
builddir_tmp = tempfile.TemporaryDirectory(prefix=str(os.environ['TEST_CPPCHECK_INJECT_BUILDDIR']))
201+
arg_clang = '--cppcheck-build-dir=' + builddir_tmp.name
202+
args.append(arg_clang)
203+
190204
logging.info(exe + ' ' + ' '.join(args))
191205

192206
run_subprocess = __run_subprocess_tty if tty else __run_subprocess
@@ -195,6 +209,9 @@ def cppcheck_ex(args, env=None, remove_checkers_report=True, cwd=None, cppcheck_
195209
stdout = stdout.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
196210
stderr = stderr.decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
197211

212+
if builddir_tmp:
213+
builddir_tmp.cleanup()
214+
198215
if remove_checkers_report:
199216
if stderr.find('[checkersReport]\n') > 0:
200217
start_id = stderr.find('[checkersReport]\n')

0 commit comments

Comments
 (0)