From 9c1b6432874fabed9b3979892c1477f0e13f6da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20St=C3=B6neberg?= Date: Wed, 17 Jan 2024 22:45:42 +0100 Subject: [PATCH] refs #12274 - made `test/cli/inline-suppress_test.py` location-independent (#5876) --- test/cli/inline-suppress_test.py | 102 +++++++++++++++++-------------- test/cli/testutils.py | 5 +- 2 files changed, 60 insertions(+), 47 deletions(-) diff --git a/test/cli/inline-suppress_test.py b/test/cli/inline-suppress_test.py index f443896321c..8d9d18fc722 100644 --- a/test/cli/inline-suppress_test.py +++ b/test/cli/inline-suppress_test.py @@ -3,98 +3,110 @@ import json import os -import tempfile from testutils import cppcheck -def create_unused_function_compile_commands(): - prjpath = os.path.realpath('proj-inline-suppress-unusedFunction') - compile_commands = os.path.join(prjpath, 'compile_commands.json') +__script_dir = os.path.dirname(os.path.abspath(__file__)) + + +def create_unused_function_compile_commands(tmpdir): + prjpath = os.path.realpath(os.path.join(__script_dir, 'proj-inline-suppress-unusedFunction')) j = [{'directory': prjpath, 'command': '/usr/bin/c++ -I"' + prjpath + '" -o "' + os.path.join(prjpath, 'B.cpp.o') + '" -c "' + os.path.join(prjpath, 'B.cpp') + '"', 'file': os.path.join(prjpath, 'B.cpp')}, {'directory': prjpath, 'command': '/usr/bin/c++ -I"' + prjpath + '" -o "' + os.path.join(prjpath, 'A.cpp.o') + '" -c "' + os.path.join(prjpath, 'A.cpp') + '"', 'file': os.path.join(prjpath, 'A.cpp')}] + compdb_path = os.path.join(tmpdir, 'proj-inline-suppress-unusedFunction') + os.makedirs(compdb_path) + compile_commands = os.path.join(compdb_path, 'compile_commands.json') with open(compile_commands, 'wt') as f: f.write(json.dumps(j, indent=4)) + return compile_commands + def test1(): - ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress']) - assert ret == 0, stdout + ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress'], cwd=__script_dir) assert stderr == '' + assert ret == 0, stdout + def test2(): - ret, stdout, stderr = cppcheck(['proj-inline-suppress']) + ret, stdout, stderr = cppcheck(['proj-inline-suppress'], cwd=__script_dir) + assert len(stderr) > 0, stderr assert ret == 0, stdout - assert len(stderr) > 0 + def test_unmatched_suppression(): - ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', 'proj-inline-suppress/2.c']) - assert ret == 1 + ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', 'proj-inline-suppress/2.c'], cwd=__script_dir) assert 'Unmatched suppression: some_warning_id' in stderr + assert ret == 1, stdout -def test_unmatched_suppression_path_with_extra_stuf(): - ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', './proj-inline-suppress/2.c']) - assert ret == 1 + +def test_unmatched_suppression_path_with_extra_stuff(): + ret, stdout, stderr = cppcheck(['--inline-suppr', '--enable=information', '--error-exitcode=1', './proj-inline-suppress/2.c'], cwd=__script_dir) assert 'Unmatched suppression: some_warning_id' in stderr + assert ret == 1, stdout + def test_backwards_compatibility(): - ret, stdout, stderr = cppcheck(['proj-inline-suppress/3.cpp']) + ret, stdout, stderr = cppcheck(['proj-inline-suppress/3.cpp'], cwd=__script_dir) assert '[zerodiv]' in stderr - - ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress/3.cpp']) assert ret == 0, stdout + + ret, stdout, stderr = cppcheck(['--inline-suppr', 'proj-inline-suppress/3.cpp'], cwd=__script_dir) assert stderr == '' + assert ret == 0, stdout + -def test_compile_commands_unused_function(): - create_unused_function_compile_commands() - ret, stdout, stderr = cppcheck(['--enable=all', '--error-exitcode=1', '--project=./proj-inline-suppress-unusedFunction/compile_commands.json']) - assert ret == 1 +def test_compile_commands_unused_function(tmpdir): + compdb_file = create_unused_function_compile_commands(tmpdir) + ret, stdout, stderr = cppcheck(['--enable=all', '--error-exitcode=1', '--project={}'.format(compdb_file)]) assert 'unusedFunction' in stderr + assert ret == 1, stdout -def test_compile_commands_unused_function_suppression(): - create_unused_function_compile_commands() - ret, stdout, stderr = cppcheck(['--enable=all', '--inline-suppr', '--error-exitcode=1', '--project=./proj-inline-suppress-unusedFunction/compile_commands.json']) - assert ret == 0, stdout + +def test_compile_commands_unused_function_suppression(tmpdir): + compdb_file = create_unused_function_compile_commands(tmpdir) + ret, stdout, stderr = cppcheck(['--enable=all', '--inline-suppr', '--error-exitcode=1', '--project={}'.format(compdb_file)]) assert 'unusedFunction' not in stderr + assert ret == 0, stdout def test_unmatched_suppression_ifdef(): - ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=missingIncludeSystem', '--inline-suppr', '-DNO_ZERO_DIV', 'trac5704/trac5704a.c']) - assert ret == 0, stdout + ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=missingIncludeSystem', '--inline-suppr', '-DNO_ZERO_DIV', 'trac5704/trac5704a.c'], cwd=__script_dir) assert 'unmatchedSuppression' not in stderr + assert ret == 0, stdout def test_unmatched_suppression_ifdef_0(): - ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=missingIncludeSystem', '--inline-suppr', 'trac5704/trac5704b.c']) - assert ret == 0, stdout + ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=missingIncludeSystem', '--inline-suppr', 'trac5704/trac5704b.c'], cwd=__script_dir) assert 'unmatchedSuppression' not in stderr + assert ret == 0, stdout -def test_build_dir(): - with tempfile.TemporaryDirectory() as tempdir: - args = f'--cppcheck-build-dir={tempdir} --enable=all --inline-suppr proj-inline-suppress/4.c'.split() +def test_build_dir(tmpdir): + args = f'--cppcheck-build-dir={tmpdir} --enable=all --inline-suppr proj-inline-suppress/4.c'.split() - ret, stdout, stderr = cppcheck(args) - assert ret == 0, stdout - assert len(stderr) == 0 + ret, stdout, stderr = cppcheck(args, cwd=__script_dir) + assert len(stderr) == 0, stderr + assert ret == 0, stdout + + ret, stdout, stderr = cppcheck(args, cwd=__script_dir) + assert len(stderr) == 0, stderr + assert ret == 0, stdout - ret, stdout, stderr = cppcheck(args) - assert ret == 0, stdout - assert len(stderr) == 0 -def test_build_dir_unused_template(): - with tempfile.TemporaryDirectory() as tempdir: - args = f'--cppcheck-build-dir={tempdir} --enable=all --inline-suppr proj-inline-suppress/template.cpp'.split() +def test_build_dir_unused_template(tmpdir): + args = f'--cppcheck-build-dir={tmpdir} --enable=all --inline-suppr proj-inline-suppress/template.cpp'.split() - ret, stdout, stderr = cppcheck(args) - assert ret == 0, stdout - assert len(stderr) == 0 + ret, stdout, stderr = cppcheck(args, cwd=__script_dir) + assert len(stderr) == 0, stderr + assert ret == 0, stdout def test_suppress_unmatched_inline_suppression(): # 11172 - ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=unmatchedSuppression', '--inline-suppr', 'proj-inline-suppress/2.c']) - assert ret == 0, stdout + ret, stdout, stderr = cppcheck(['--enable=all', '--suppress=unmatchedSuppression', '--inline-suppr', 'proj-inline-suppress/2.c'], cwd=__script_dir) assert 'unmatchedSuppression' not in stderr + assert ret == 0, stdout diff --git a/test/cli/testutils.py b/test/cli/testutils.py index a27900a0e87..25712346aef 100644 --- a/test/cli/testutils.py +++ b/test/cli/testutils.py @@ -66,17 +66,18 @@ def __lookup_cppcheck_exe(): break if exe_path: + exe_path = os.path.abspath(exe_path) print("using '{}'".format(exe_path)) return exe_path # Run Cppcheck with args -def cppcheck(args, env=None, remove_checkers_report=True): +def cppcheck(args, env=None, remove_checkers_report=True, cwd=None): exe = __lookup_cppcheck_exe() assert exe is not None, 'no cppcheck binary found' logging.info(exe + ' ' + ' '.join(args)) - p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) + p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env, cwd=cwd) comm = p.communicate() stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n') stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')