From f9669605f43210dc5ceec5d85e0e34a2a0407747 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 31 May 2025 19:42:44 +0200 Subject: [PATCH] test/cli/lookup_tests.py: improved test coverage --- test/cli/lookup_test.py | 453 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 435 insertions(+), 18 deletions(-) diff --git a/test/cli/lookup_test.py b/test/cli/lookup_test.py index 341152bdb34..f22270b4cc0 100644 --- a/test/cli/lookup_test.py +++ b/test/cli/lookup_test.py @@ -11,7 +11,6 @@ def __remove_std_lookup_log(l : list, exepath): return l -# TODO: test with FILESDIR def test_lib_lookup(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -32,7 +31,25 @@ def test_lib_lookup(tmpdir): ] -# TODO: test with FILESDIR +def test_lib_lookup_ext(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=library', '--library=gnu.cfg', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 0, stdout if stdout else stderr + lines = __remove_std_lookup_log(stdout.splitlines(), exepath) + assert lines == [ + "looking for library 'gnu.cfg'", + "looking for library '{}/gnu.cfg'".format(exepath), + "looking for library '{}/cfg/gnu.cfg'".format(exepath), + 'Checking {} ...'.format(test_file) + ] + + def test_lib_lookup_notfound(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -57,6 +74,67 @@ def test_lib_lookup_notfound(tmpdir): ] +def test_lib_lookup_ext_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=library', '--library=none.cfg', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 1, stdout if stdout else stderr + lines = __remove_std_lookup_log(stdout.splitlines(), exepath) + assert lines == [ + "looking for library 'none.cfg'", + "looking for library '{}/none.cfg'".format(exepath), + "looking for library '{}/cfg/none.cfg'".format(exepath), + "library not found: 'none.cfg'", + "cppcheck: Failed to load library configuration file 'none.cfg'. File not found" + ] + + +def test_lib_lookup_relative_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=library', '--library=config/gnu.xml', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 1, stdout if stdout else stderr + lines = __remove_std_lookup_log(stdout.splitlines(), exepath) + assert lines == [ + "looking for library 'config/gnu.xml'", + "looking for library '{}/config/gnu.xml'".format(exepath), + "looking for library '{}/cfg/config/gnu.xml'".format(exepath), + "library not found: 'config/gnu.xml'", + "cppcheck: Failed to load library configuration file 'config/gnu.xml'. File not found" + ] + + +def test_lib_lookup_relative_noext_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=library', '--library=config/gnu', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 1, stdout if stdout else stderr + lines = __remove_std_lookup_log(stdout.splitlines(), exepath) + assert lines == [ + "looking for library 'config/gnu'", + "looking for library 'config/gnu.cfg'", + "looking for library '{}/config/gnu.cfg'".format(exepath), + "looking for library '{}/cfg/config/gnu.cfg'".format(exepath), + "library not found: 'config/gnu'", + "cppcheck: Failed to load library configuration file 'config/gnu'. File not found" + ] + + def test_lib_lookup_absolute(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -128,6 +206,31 @@ def test_lib_lookup_nofile(tmpdir): ] +# make sure we bail out when we encounter an invalid file +def test_lib_lookup_invalid(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + gnu_cfg_file = os.path.join(tmpdir, 'gnu.cfg') + with open(gnu_cfg_file, 'wt') as f: + f.write('''{}''') + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=library', '--library=gnu', test_file], cwd=tmpdir) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 1, stdout if stdout else stderr + lines = __remove_std_lookup_log(stdout.splitlines(), exepath) + assert lines == [ + "looking for library 'gnu'", + "looking for library 'gnu.cfg'", + "library not found: 'gnu'", + "Error=XML_ERROR_PARSING_TEXT ErrorID=8 (0x8) Line number=1", # TODO: log the failure before saying the library was not found + "cppcheck: Failed to load library configuration file 'gnu'. Bad XML" + ] + + def test_lib_lookup_multi(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -166,22 +269,21 @@ def test_platform_lookup_builtin(tmpdir): ] -# TODO: behaves differently when using a CMake build -# TODO: test with FILESDIR -@pytest.mark.skip -def test_platform_lookup_external(tmpdir): +@pytest.mark.skip # TODO: perform 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'): pass exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=avr8', test_file]) exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') if sys.platform == 'win32': - exepath = exepath.replace('\\', '/') + exepath_bin += '.exe' assert exitcode == 0, stdout if stdout else stderr lines = stdout.splitlines() assert lines == [ - "looking for platform 'avr8' in '{}'".format(os.path.join(exepath, 'cppcheck')), # TODO: this not not the path *of* the executable but the the path *to* the executable + "looking for platform 'avr8' in '{}'".format(exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable "try to load platform file 'avr8' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8", "try to load platform file 'avr8.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8.xml", "try to load platform file 'platforms/avr8' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/avr8", @@ -190,8 +292,29 @@ def test_platform_lookup_external(tmpdir): ] -# TODO: test with FILESDIR -def test_platform_lookup_external_notfound(tmpdir): +@pytest.mark.skip # TODO: perform 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'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=avr8.xml', test_file]) + exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') + if sys.platform == 'win32': + exepath_bin += '.exe' + assert exitcode == 0, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for platform 'avr8.xml' in '{}'".format(exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable + "try to load platform file 'avr8.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8.xml", + "try to load platform file 'avr8.xml.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8.xml.xml", # TODO: the extension is incorrectly appended + "try to load platform file 'platforms/avr8.xml' ... Success", + 'Checking {} ...'.format(test_file) + ] + + +def test_platform_lookup_notfound(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): pass @@ -218,7 +341,185 @@ def test_platform_lookup_external_notfound(tmpdir): ] -# TODO: test with FILESDIR +def test_platform_lookup_ext_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=none.xml', test_file]) + exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + exepath_bin += '.exe' + assert exitcode == 1, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for platform 'none.xml' in '{}'".format(exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable + "try to load platform file 'none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml", + "try to load platform file 'none.xml.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none.xml.xml", # TODO: the extension is incorrectly appended + "try to load platform file 'platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml", + "try to load platform file 'platforms/none.xml.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none.xml.xml", # TODO: the extension is incorrectly appended + "try to load platform file '{}/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/none.xml".format(exepath, exepath), + "try to load platform file '{}/platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/none.xml".format(exepath, exepath), + "try to load platform file '{}/platforms/none.xml.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/none.xml.xml".format(exepath, exepath), # TODO: the extension is incorrectly appended + "cppcheck: error: unrecognized platform: 'none.xml'." + ] + + +def test_platform_lookup_relative_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=platform/none.xml', test_file]) + exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + exepath_bin += '.exe' + assert exitcode == 1, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for platform 'platform/none.xml' in '{}'".format(exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable + "try to load platform file 'platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platform/none.xml", + "try to load platform file 'platform/none.xml.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platform/none.xml.xml", # TODO: the extension is incorrectly appended + "try to load platform file 'platforms/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/platform/none.xml", + "try to load platform file 'platforms/platform/none.xml.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/platform/none.xml.xml", # TODO: the extension is incorrectly appended + "try to load platform file '{}/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platform/none.xml".format(exepath, exepath), + "try to load platform file '{}/platforms/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/platform/none.xml".format(exepath, exepath), + "try to load platform file '{}/platforms/platform/none.xml.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/platform/none.xml.xml".format(exepath, exepath), # TODO: the extension is incorrectly appended + "cppcheck: error: unrecognized platform: 'platform/none.xml'." + ] + + +def test_platform_lookup_relative_noext_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=platform/none', test_file]) + exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + exepath_bin += '.exe' + assert exitcode == 1, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for platform 'platform/none' in '{}'".format(exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable + "try to load platform file 'platform/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platform/none", + "try to load platform file 'platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platform/none.xml", + "try to load platform file 'platforms/platform/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/platform/none", + "try to load platform file 'platforms/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/platform/none.xml", + "try to load platform file '{}/platform/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platform/none".format(exepath, exepath), + "try to load platform file '{}/platforms/platform/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/platform/none".format(exepath, exepath), + "try to load platform file '{}/platforms/platform/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename={}/platforms/platform/none.xml".format(exepath, exepath), + "cppcheck: error: unrecognized platform: 'platform/none'." + ] + + +def test_platform_lookup_absolute(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + platform_file = os.path.join(tmpdir, 'test.xml') + with open(platform_file, 'wt') as f: + f.write(''' + + ''') + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform={}'.format(platform_file), test_file]) + exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') + if sys.platform == 'win32': + exepath_bin += '.exe' + assert exitcode == 0, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for platform '{}' in '{}'".format(platform_file, exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable + "try to load platform file '{}' ... Success".format(platform_file), + 'Checking {} ...'.format(test_file) + ] + + +@pytest.mark.xfail(strict=True) # TODO: performs lots of unnecessary lookups +def test_platform_lookup_absolute_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + platform_file = os.path.join(tmpdir, 'test.xml') + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform={}'.format(platform_file), test_file]) + exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') + if sys.platform == 'win32': + exepath_bin += '.exe' + assert exitcode == 1, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for platform '{}' in '{}'".format(platform_file, exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable + "cppcheck: error: unrecognized platform: '{}'.".format(platform_file) + ] + + +@pytest.mark.skip # TODO: perform 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'): + pass + + # make sure we do not produce an error when the attempted lookup path is a directory and not a file + avr8_dir = os.path.join(tmpdir, 'avr8') + os.mkdir(avr8_dir) + avr8_cfg_dir = os.path.join(tmpdir, 'avr8.xml') + os.mkdir(avr8_cfg_dir) + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=avr8', test_file]) + exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') + if sys.platform == 'win32': + exepath_bin += '.exe' + assert exitcode == 0, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for platform 'avr8' in '{}'".format(exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable + "try to load platform file 'avr8' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8", + "try to load platform file 'avr8.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8.xml", + "try to load platform file 'platforms/avr8' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/avr8", + "try to load platform file 'platforms/avr8.xml' ... Success", + 'Checking {} ...'.format(test_file) + ] + + +# make sure we bail out when we encounter an invalid file +@pytest.mark.xfail(strict=True) # TODO: does not bail out after it found an invalid file +def test_platform_lookup_invalid(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + avr8_file = os.path.join(tmpdir, 'avr8.xml') + with open(avr8_file, 'wt') as f: + f.write('''{}''') + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=avr8', test_file], cwd=tmpdir) + exepath = os.path.dirname(exe) + exepath_bin = os.path.join(exepath, 'cppcheck') + if sys.platform == 'win32': + exepath_bin += '.exe' + assert exitcode == 1, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for platform 'avr8' in '{}'".format(exepath_bin), # TODO: this not not the path *of* the executable but the the path *to* the executable + "try to load platform file 'avr8' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=avr8", + "try to load platform file 'avr8.xml' ... Error=XML_ERROR_PARSING_TEXT ErrorID=8 (0x8) Line number=1", + "cppcheck: error: unrecognized platform: 'avr8'." + ] + + def test_addon_lookup(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -237,7 +538,6 @@ def test_addon_lookup(tmpdir): ] -# TODO: test with FILESDIR def test_addon_lookup_ext(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -256,7 +556,6 @@ def test_addon_lookup_ext(tmpdir): ] -# TODO: test with FILESDIR def test_addon_lookup_notfound(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -275,7 +574,6 @@ def test_addon_lookup_notfound(tmpdir): ] -# TODO: test with FILESDIR def test_addon_lookup_ext_notfound(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -294,8 +592,124 @@ def test_addon_lookup_ext_notfound(tmpdir): ] -# TODO: test with FILESDIR -@pytest.mark.skip +def test_addon_lookup_relative_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=addon/misra.py', test_file]) + exepath = os.path.dirname(exe) + exepath_sep = exepath + os.path.sep + assert exitcode == 1, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for addon 'addon/misra.py'", + "looking for addon '{}addon/misra.py'".format(exepath_sep), + "looking for addon '{}addons/addon/misra.py'".format(exepath_sep), # TODO: mixed separators + 'Did not find addon addon/misra.py' + ] + + +def test_addon_lookup_relative_noext_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=addon/misra', test_file]) + exepath = os.path.dirname(exe) + exepath_sep = exepath + os.path.sep + assert exitcode == 1, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for addon 'addon/misra.py'", + "looking for addon '{}addon/misra.py'".format(exepath_sep), + "looking for addon '{}addons/addon/misra.py'".format(exepath_sep), # TODO: mixed separators + 'Did not find addon addon/misra.py' + ] + + +def test_addon_lookup_absolute(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + addon_file = os.path.join(tmpdir, 'test.py') + with open(addon_file, 'wt') as f: + f.write('''''') + + exitcode, stdout, stderr = cppcheck(['--debug-lookup=addon', '--addon={}'.format(addon_file), test_file]) + assert exitcode == 0, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for addon '{}'".format(addon_file), + 'Checking {} ...'.format(test_file) + ] + + +def test_addon_lookup_absolute_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + addon_file = os.path.join(tmpdir, 'test.py') + + exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon={}'.format(addon_file), test_file]) + 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 addon '{}'".format(addon_file), + "looking for addon '{}{}'".format(exepath_sep, addon_file), # TODO: should not perform this lookup + "looking for addon '{}addons/{}'".format(exepath_sep, addon_file), # TODO: should not perform this lookup + 'Did not find addon {}'.format(addon_file) + ] + + +def test_addon_lookup_nofile(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + # make sure we do not produce an error when the attempted lookup path is a directory and not a file + misra_dir = os.path.join(tmpdir, 'misra') + os.mkdir(misra_dir) + misra_cfg_dir = os.path.join(tmpdir, 'misra.py') + os.mkdir(misra_cfg_dir) + + 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 + lines = stdout.splitlines() + assert lines == [ + "looking for addon 'misra.py'", + "looking for addon '{}misra.py'".format(exepath_sep), + "looking for addon '{}addons/misra.py'".format(exepath_sep), # TODO: mixed separators + 'Checking {} ...'.format(test_file) + ] + + +# make sure we bail out when we encounter an invalid file +def test_addon_lookup_invalid(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + misra_py_file = os.path.join(tmpdir, 'misra.py') + with open(misra_py_file, 'wt') as f: + f.write('''''') + + exitcode, stdout, stderr = cppcheck(['--debug-lookup=addon', '--addon=misra', test_file], cwd=tmpdir) + assert exitcode == 0, stdout if stdout else stderr + lines = stdout.splitlines() + assert lines == [ + "looking for addon 'misra.py'", + 'Checking {} ...'.format(test_file) # TODO: should bail out + ] + + +@pytest.mark.skip # TODO def test_config_lookup(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -318,7 +732,6 @@ def test_config_lookup(tmpdir): ] -# TODO: test with FILESDIR def test_config_lookup_notfound(tmpdir): test_file = os.path.join(tmpdir, 'test.c') with open(test_file, 'wt'): @@ -333,4 +746,8 @@ def test_config_lookup_notfound(tmpdir): "looking for '{}cppcheck.cfg'".format(exepath_sep), 'no configuration found', 'Checking {} ...'.format(test_file) - ] \ No newline at end of file + ] + +# TODO: test handling of invalid configuration + +# TODO: test with FILESDIR \ No newline at end of file