From 51868aec060337a640e1faf9bac7efe60d71ba15 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 6 Jun 2025 14:20:34 +0200 Subject: [PATCH 1/2] test/cli/lookup_test.py: improved testing of config lookup --- test/cli/lookup_test.py | 45 +++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/test/cli/lookup_test.py b/test/cli/lookup_test.py index dc28c6af35e..a7f1941d783 100644 --- a/test/cli/lookup_test.py +++ b/test/cli/lookup_test.py @@ -1,8 +1,9 @@ import os import sys import pytest +import shutil -from testutils import cppcheck_ex, cppcheck +from testutils import cppcheck_ex, cppcheck, __lookup_cppcheck_exe def __remove_std_lookup_log(l : list, exepath): l.remove("looking for library 'std.cfg'") @@ -679,25 +680,29 @@ def test_addon_lookup_invalid(tmpdir): ] -@pytest.mark.skip # TODO def test_config_lookup(tmpdir): + cppcheck_exe = __lookup_cppcheck_exe() + bin_dir = os.path.dirname(cppcheck_exe) + tmp_cppcheck_exe = shutil.copy2(cppcheck_exe, tmpdir) + if sys.platform == 'win32': + shutil.copy2(os.path.join(bin_dir, 'cppcheck-core.dll'), tmpdir) + shutil.copytree(os.path.join(bin_dir, 'cfg'), os.path.join(tmpdir, 'cfg')) + test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): pass - # TODO: needs to be in exepath so this is found config_file = os.path.join(tmpdir, 'cppcheck.cfg') - with open(config_file, 'wt'): - pass + with open(config_file, 'wt') as f: + f.write('{}') - exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=config', '--addon=misra', test_file], cwd=tmpdir) + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=config', test_file], cwd=tmpdir, cppcheck_exe=tmp_cppcheck_exe) exepath = os.path.dirname(exe) exepath_sep = exepath + os.path.sep assert exitcode == 0, stdout if stdout else stderr lines = stdout.splitlines() assert lines == [ "looking for '{}cppcheck.cfg'".format(exepath_sep), - 'no configuration found', 'Checking {} ...'.format(test_file) ] @@ -718,6 +723,30 @@ def test_config_lookup_notfound(tmpdir): 'Checking {} ...'.format(test_file) ] -# TODO: test handling of invalid configuration +def test_config_invalid(tmpdir): + cppcheck_exe = __lookup_cppcheck_exe() + bin_dir = os.path.dirname(cppcheck_exe) + tmp_cppcheck_exe = shutil.copy2(cppcheck_exe, tmpdir) + if sys.platform == 'win32': + shutil.copy2(os.path.join(bin_dir, 'cppcheck-core.dll'), tmpdir) + shutil.copytree(os.path.join(bin_dir, 'cfg'), os.path.join(tmpdir, 'cfg')) + + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + config_file = os.path.join(tmpdir, 'cppcheck.cfg') + with open(config_file, 'wt'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=config', test_file], cwd=tmpdir, cppcheck_exe=tmp_cppcheck_exe) + exepath = os.path.dirname(exe) + exepath_sep = exepath + os.path.sep + assert exitcode == 1, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for '{}cppcheck.cfg'".format(exepath_sep), + 'cppcheck: error: could not load cppcheck.cfg - not a valid JSON - syntax error at line 1 near: ' + ] # TODO: test with FILESDIR \ No newline at end of file From 0c52e8b88bf80841e34765aad4a13ccac69a3e15 Mon Sep 17 00:00:00 2001 From: firewave Date: Fri, 6 Jun 2025 14:29:20 +0200 Subject: [PATCH 2/2] test/cli/lookup_test.py: small TODO cleanup --- test/cli/lookup_test.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/test/cli/lookup_test.py b/test/cli/lookup_test.py index a7f1941d783..ce3d02e600d 100644 --- a/test/cli/lookup_test.py +++ b/test/cli/lookup_test.py @@ -64,9 +64,7 @@ def test_lib_lookup_notfound(tmpdir): assert lines == [ # TODO: specify which folder is actually used for lookup here "looking for library 'none.cfg'", - # TODO: lookup of '{exepath}/none' missing - could conflict with the platform lookup though "looking for library '{}/none.cfg'".format(exepath), - # TODO: lookup of '{exepath}/cfg/none' missing "looking for library '{}/cfg/none.cfg'".format(exepath), "library not found: 'none'", "cppcheck: Failed to load library configuration file 'none'. File not found" @@ -261,7 +259,7 @@ def test_platform_lookup_builtin(tmpdir): ] -@pytest.mark.skip # TODO: perform additional lookups when run via symlink in CI +@pytest.mark.skip # TODO: performs additional lookups when run via symlink in CI def test_platform_lookup(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -282,7 +280,7 @@ def test_platform_lookup(tmpdir): ] -@pytest.mark.skip # TODO: perform additional lookups when run via symlink in CI +@pytest.mark.skip # TODO: performs additional lookups when run via symlink in CI def test_platform_lookup_ext(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -441,7 +439,7 @@ def test_platform_lookup_absolute_notfound(tmpdir): ] -@pytest.mark.skip # TODO: perform additional lookups when run via symlink in CI +@pytest.mark.skip # TODO: performs additional lookups when run via symlink in CI def test_platform_lookup_nofile(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -651,7 +649,7 @@ def test_addon_lookup_nofile(tmpdir): exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra', test_file]) exepath = os.path.dirname(exe) exepath_sep = exepath + os.path.sep - assert exitcode == 0, stdout if stdout else stderr # TODO. should fail when addon is not found + assert exitcode == 0, stdout if stdout else stderr lines = stdout.splitlines() assert lines == [ "looking for addon 'misra.py'",