From 9141b8c62d1cb2fba7ffa2439cd653145925df68 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 16 Jul 2024 20:49:07 +0200 Subject: [PATCH] added lookup debug logging for the remaining configuration files --- cli/cmdlineparser.cpp | 33 ++++-- lib/addoninfo.cpp | 18 +++- lib/addoninfo.h | 2 +- lib/platform.cpp | 14 +-- lib/platform.h | 6 +- lib/settings.cpp | 17 +++- lib/settings.h | 16 ++- test/cli/other_test.py | 203 +++++++++++++++++++++++++++++++++++-- test/testcmdlineparser.cpp | 48 +++++++++ 9 files changed, 323 insertions(+), 34 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index ae39951d8554..6e5747fa3c69 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -556,6 +556,25 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a else if (std::strcmp(argv[i], "--debug-lookup") == 0) mSettings.debuglookup = true; + else if (std::strncmp(argv[i], "--debug-lookup=", 15) == 0) { + const std::string lookup = argv[i] + 15; + if (lookup == "all") + mSettings.debuglookup = true; + else if (lookup == "addon") + mSettings.debuglookupAddon = true; + else if (lookup == "config") + mSettings.debuglookupConfig = true; + else if (lookup == "library") + mSettings.debuglookupLibrary = true; + else if (lookup == "platform") + mSettings.debuglookupPlatform = true; + else + { + mLogger.printError("unknown lookup '" + lookup + "'"); + return Result::Fail; + } + } + // Flag used for various purposes during debugging else if (std::strcmp(argv[i], "--debug-simplified") == 0) mSettings.debugSimplified = true; @@ -927,7 +946,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a std::string errstr; const std::vector paths = {argv[0]}; - if (!mSettings.platform.set(platform, errstr, paths)) { + if (!mSettings.platform.set(platform, errstr, paths, mSettings.debuglookup || mSettings.debuglookupPlatform)) { mLogger.printError(errstr); return Result::Fail; } @@ -1021,7 +1040,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a if (!platform.empty()) { std::string errstr; const std::vector paths = {projectFile, argv[0]}; - if (!mSettings.platform.set(platform, errstr, paths)) { + if (!mSettings.platform.set(platform, errstr, paths, mSettings.debuglookup || mSettings.debuglookupPlatform)) { mLogger.printError(errstr); return Result::Fail; } @@ -1816,7 +1835,7 @@ std::string CmdLineParser::getVersion() const { bool CmdLineParser::isCppcheckPremium() const { if (mSettings.cppcheckCfgProductName.empty()) - Settings::loadCppcheckCfg(mSettings, mSettings.supprs); + Settings::loadCppcheckCfg(mSettings, mSettings.supprs, mSettings.debuglookup || mSettings.debuglookupConfig); return startsWith(mSettings.cppcheckCfgProductName, "Cppcheck Premium"); } @@ -1869,7 +1888,7 @@ bool CmdLineParser::tryLoadLibrary(Library& destination, const std::string& base bool CmdLineParser::loadLibraries(Settings& settings) { - if (!tryLoadLibrary(settings.library, settings.exename, "std.cfg", settings.debuglookup)) { + if (!tryLoadLibrary(settings.library, settings.exename, "std.cfg", settings.debuglookup || settings.debuglookupLibrary)) { const std::string msg("Failed to load std.cfg. Your Cppcheck installation is broken, please re-install."); #ifdef FILESDIR const std::string details("The Cppcheck binary was compiled with FILESDIR set to \"" @@ -1887,7 +1906,7 @@ bool CmdLineParser::loadLibraries(Settings& settings) bool result = true; for (const auto& lib : settings.libraries) { - if (!tryLoadLibrary(settings.library, settings.exename, lib.c_str(), settings.debuglookup)) { + if (!tryLoadLibrary(settings.library, settings.exename, lib.c_str(), settings.debuglookup || settings.debuglookupLibrary)) { result = false; } } @@ -1899,7 +1918,7 @@ bool CmdLineParser::loadAddons(Settings& settings) bool result = true; for (const std::string &addon: settings.addons) { AddonInfo addonInfo; - const std::string failedToGetAddonInfo = addonInfo.getAddonInfo(addon, settings.exename); + const std::string failedToGetAddonInfo = addonInfo.getAddonInfo(addon, settings.exename, settings.debuglookup || settings.debuglookupAddon); if (!failedToGetAddonInfo.empty()) { mLogger.printRaw(failedToGetAddonInfo); // TODO: do not print as raw result = false; @@ -1912,7 +1931,7 @@ bool CmdLineParser::loadAddons(Settings& settings) bool CmdLineParser::loadCppcheckCfg() { - const std::string cfgErr = Settings::loadCppcheckCfg(mSettings, mSuppressions); + const std::string cfgErr = Settings::loadCppcheckCfg(mSettings, mSuppressions, mSettings.debuglookup || mSettings.debuglookupConfig); if (!cfgErr.empty()) { mLogger.printError("could not load cppcheck.cfg - " + cfgErr); return false; diff --git a/lib/addoninfo.cpp b/lib/addoninfo.cpp index f0b15eac7a6d..2c1bb08a9f0b 100644 --- a/lib/addoninfo.cpp +++ b/lib/addoninfo.cpp @@ -27,19 +27,29 @@ #include "json.h" -static std::string getFullPath(const std::string &fileName, const std::string &exename) { +static std::string getFullPath(const std::string &fileName, const std::string &exename, bool debug = false) { + if (debug) + std::cout << "looking for addon '" << fileName << "'" << std::endl; if (Path::isFile(fileName)) return fileName; const std::string exepath = Path::getPathFromFilename(exename); + if (debug) + std::cout << "looking for addon '" << (exepath + fileName) << "'" << std::endl; if (Path::isFile(exepath + fileName)) return exepath + fileName; + if (debug) + std::cout << "looking for addon '" << (exepath + "addons/" + fileName) << "'" << std::endl; if (Path::isFile(exepath + "addons/" + fileName)) return exepath + "addons/" + fileName; #ifdef FILESDIR + if (debug) + std::cout << "looking for addon '" << (FILESDIR + ("/" + fileName)) << "'" << std::endl; if (Path::isFile(FILESDIR + ("/" + fileName))) return FILESDIR + ("/" + fileName); + if (debug) + std::cout << "looking for addon '" << ( FILESDIR + ("/addons/" + fileName)) << "'" << std::endl; if (Path::isFile(FILESDIR + ("/addons/" + fileName))) return FILESDIR + ("/addons/" + fileName); #endif @@ -124,7 +134,7 @@ static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &j return addoninfo.getAddonInfo(val.get(), exename); } -std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::string &exename) { +std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::string &exename, bool debug) { if (fileName[0] == '{') { picojson::value json; const std::string err = picojson::parse(json, fileName); @@ -132,10 +142,10 @@ std::string AddonInfo::getAddonInfo(const std::string &fileName, const std::stri return parseAddonInfo(*this, json, fileName, exename); } if (fileName.find('.') == std::string::npos) - return getAddonInfo(fileName + ".py", exename); + return getAddonInfo(fileName + ".py", exename, debug); if (endsWith(fileName, ".py")) { - scriptFile = Path::fromNativeSeparators(getFullPath(fileName, exename)); + scriptFile = Path::fromNativeSeparators(getFullPath(fileName, exename, debug)); if (scriptFile.empty()) return "Did not find addon " + fileName; diff --git a/lib/addoninfo.h b/lib/addoninfo.h index 5e4443eb8bd3..3ebbcdf13b58 100644 --- a/lib/addoninfo.h +++ b/lib/addoninfo.h @@ -32,7 +32,7 @@ struct CPPCHECKLIB AddonInfo { bool ctu = false; std::string runScript; - std::string getAddonInfo(const std::string &fileName, const std::string &exename); + std::string getAddonInfo(const std::string &fileName, const std::string &exename, bool debug = false); }; #endif // addonInfoH diff --git a/lib/platform.cpp b/lib/platform.cpp index f54b9110afe7..cab87d61d790 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -150,7 +150,7 @@ bool Platform::set(Type t) return false; } -bool Platform::set(const std::string& platformstr, std::string& errstr, const std::vector& paths, bool verbose) +bool Platform::set(const std::string& platformstr, std::string& errstr, const std::vector& paths, bool debug) { if (platformstr == "win32A") set(Type::Win32A); @@ -173,9 +173,9 @@ bool Platform::set(const std::string& platformstr, std::string& errstr, const st else { bool found = false; for (const std::string& path : paths) { - if (verbose) + if (debug) std::cout << "looking for platform '" + platformstr + "' in '" + path + "'" << std::endl; - if (loadFromFile(path.c_str(), platformstr, verbose)) { + if (loadFromFile(path.c_str(), platformstr, debug)) { found = true; break; } @@ -189,7 +189,7 @@ bool Platform::set(const std::string& platformstr, std::string& errstr, const st return true; } -bool Platform::loadFromFile(const char exename[], const std::string &filename, bool verbose) +bool Platform::loadFromFile(const char exename[], const std::string &filename, bool debug) { // TODO: only append .xml if missing // TODO: use native separators @@ -216,15 +216,15 @@ bool Platform::loadFromFile(const char exename[], const std::string &filename, b tinyxml2::XMLDocument doc; bool success = false; for (const std::string & f : filenames) { - if (verbose) + if (debug) std::cout << "try to load platform file '" << f << "' ... "; if (doc.LoadFile(f.c_str()) == tinyxml2::XML_SUCCESS) { - if (verbose) + if (debug) std::cout << "Success" << std::endl; success = true; break; } - if (verbose) + if (debug) std::cout << doc.ErrorStr() << std::endl; } if (!success) diff --git a/lib/platform.h b/lib/platform.h index df9ad01e4d05..48eb7e42c399 100644 --- a/lib/platform.h +++ b/lib/platform.h @@ -122,16 +122,16 @@ class CPPCHECKLIB Platform { bool set(Type t); /** set the platform type */ - bool set(const std::string& platformstr, std::string& errstr, const std::vector& paths = {}, bool verbose = false); + bool set(const std::string& platformstr, std::string& errstr, const std::vector& paths = {}, bool debug = false); /** * load platform file * @param exename application path * @param filename platform filename - * @param verbose log verbose information about the lookup + * @param debug log verbose information about the lookup * @return returns true if file was loaded successfully */ - bool loadFromFile(const char exename[], const std::string &filename, bool verbose = false); + bool loadFromFile(const char exename[], const std::string &filename, bool debug = false); /** load platform from xml document, primarily for testing */ bool loadFromXmlDocument(const tinyxml2::XMLDocument *doc); diff --git a/lib/settings.cpp b/lib/settings.cpp index 5a06beaef55a..e132ebcd00e2 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -62,21 +62,32 @@ Settings::Settings() pid = getPid(); } -std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppressions) +std::string Settings::loadCppcheckCfg(Settings& settings, Suppressions& suppressions, bool debug) { // TODO: this always needs to be run *after* the Settings has been filled static const std::string cfgFilename = "cppcheck.cfg"; std::string fileName; #ifdef FILESDIR - if (Path::isFile(Path::join(FILESDIR, cfgFilename))) - fileName = Path::join(FILESDIR, cfgFilename); + { + const std::string filesdirCfg = Path::join(FILESDIR, cfgFilename); + if (debug) + std::cout << "looking for '" << filesdirCfg << "'" << std::endl; + if (Path::isFile(filesdirCfg)) + fileName = filesdirCfg; + } #endif // cppcheck-suppress knownConditionTrueFalse if (fileName.empty()) { // TODO: make sure that exename is set fileName = Path::getPathFromFilename(settings.exename) + cfgFilename; + if (debug) + std::cout << "looking for '" << fileName << "'" << std::endl; if (!Path::isFile(fileName)) + { + if (debug) + std::cout << "no configuration found" << std::endl; return ""; + } } std::ifstream fin(fileName); diff --git a/lib/settings.h b/lib/settings.h index 7fa2918a15f4..4f301e7dfd6c 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -101,7 +101,7 @@ class CPPCHECKLIB WARN_UNUSED Settings { public: Settings(); - static std::string loadCppcheckCfg(Settings& settings, Suppressions& suppressions); + static std::string loadCppcheckCfg(Settings& settings, Suppressions& suppressions, bool debug = false); static std::pair getNameAndVersion(const std::string& productName); @@ -173,9 +173,21 @@ class CPPCHECKLIB WARN_UNUSED Settings { /** @brief Are we running from DACA script? */ bool daca{}; - /** @brief Internal: Is --debug-lookup given? */ + /** @brief Internal: Is --debug-lookup or --debug-lookup=all given? */ bool debuglookup{}; + /** @brief Internal: Is --debug-lookup=addon given? */ + bool debuglookupAddon{}; + + /** @brief Internal: Is --debug-lookup=config given? */ + bool debuglookupConfig{}; + + /** @brief Internal: Is --debug-lookup=library given? */ + bool debuglookupLibrary{}; + + /** @brief Internal: Is --debug-lookup=platform given? */ + bool debuglookupPlatform{}; + /** @brief Is --debug-normal given? */ bool debugnormal{}; diff --git a/test/cli/other_test.py b/test/cli/other_test.py index 1206c45c57d9..535dc27d0d7d 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -1622,7 +1622,7 @@ def test_lib_lookup(tmpdir): with open(test_file, 'wt'): pass - exitcode, stdout, _, exe = cppcheck_ex(['--library=gnu', '--debug-lookup', test_file]) + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=library', '--library=gnu', test_file]) exepath = os.path.dirname(exe) if sys.platform == 'win32': exepath = exepath.replace('\\', '/') @@ -1643,16 +1643,19 @@ def test_lib_lookup_notfound(tmpdir): with open(test_file, 'wt'): pass - exitcode, stdout, _, exe = cppcheck_ex(['--library=none', '--debug-lookup', test_file]) + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=library', '--library=none', test_file]) exepath = os.path.dirname(exe) if sys.platform == 'win32': exepath = exepath.replace('\\', '/') assert exitcode == 1, stdout lines = __remove_std_lookup_log(stdout.splitlines(), exepath) assert lines == [ - "looking for library 'none'", + # TODO: specify which folder is actually used for lookup here + "looking for library 'none'", # TODO: this could conflict with the platform lookup "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" @@ -1672,7 +1675,7 @@ def test_lib_lookup_absolute(tmpdir): ''') - exitcode, stdout, _, exe = cppcheck_ex(['--library={}'.format(cfg_file), '--debug-lookup', test_file]) + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=library', '--library={}'.format(cfg_file), test_file]) exepath = os.path.dirname(exe) if sys.platform == 'win32': exepath = exepath.replace('\\', '/') @@ -1691,7 +1694,7 @@ def test_lib_lookup_absolute_notfound(tmpdir): cfg_file = os.path.join(tmpdir, 'test.cfg') - exitcode, stdout, _, exe = cppcheck_ex(['--library={}'.format(cfg_file), '--debug-lookup', test_file]) + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=library', '--library={}'.format(cfg_file), test_file]) exepath = os.path.dirname(exe) if sys.platform == 'win32': exepath = exepath.replace('\\', '/') @@ -1715,7 +1718,7 @@ def test_lib_lookup_nofile(tmpdir): gtk_cfg_dir = os.path.join(tmpdir, 'gtk.cfg') os.mkdir(gtk_cfg_dir) - exitcode, stdout, _, exe = cppcheck_ex(['--library=gtk', '--debug-lookup', test_file], cwd=tmpdir) + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=library', '--library=gtk', test_file], cwd=tmpdir) exepath = os.path.dirname(exe) if sys.platform == 'win32': exepath = exepath.replace('\\', '/') @@ -1735,7 +1738,7 @@ def test_lib_lookup_multi(tmpdir): with open(test_file, 'wt'): pass - exitcode, stdout, _, exe = cppcheck_ex(['--library=posix,gnu', '--debug-lookup', test_file]) + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=library', '--library=posix,gnu', test_file]) exepath = os.path.dirname(exe) if sys.platform == 'win32': exepath = exepath.replace('\\', '/') @@ -1752,3 +1755,189 @@ def test_lib_lookup_multi(tmpdir): "looking for library '{}/cfg/gnu.cfg'".format(exepath), 'Checking {} ...'.format(test_file) ] + + +def test_platform_lookup_builtin(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, _ = cppcheck(['--debug-lookup=platform', '--platform=unix64', test_file]) + assert exitcode == 0, stdout + lines = stdout.splitlines() + # built-in platform are not being looked up + assert lines == [ + 'Checking {} ...'.format(test_file) + ] + + +# TODO: test with FILESDIR +def test_platform_lookup_external(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=avr8', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 0, stdout + 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 + "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) + ] + + +# TODO: test with FILESDIR +def test_platform_lookup_external_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=platform', '--platform=none', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 1, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for platform 'none' in '{}'".format(os.path.join(exepath, 'cppcheck')), # TODO: this not not the path *of* the executable but the the path *to* the executable + "try to load platform file 'none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=none", + "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 'platforms/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=platforms/none", + "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 '{}/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=/mnt/s/GitHub/cppcheck-fw/none".format(exepath), + # TODO: lookup of '{exepath}/none.xml' missing + "try to load platform file '{}/platforms/none' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=/mnt/s/GitHub/cppcheck-fw/platforms/none".format(exepath), + "try to load platform file '{}/platforms/none.xml' ... Error=XML_ERROR_FILE_NOT_FOUND ErrorID=3 (0x3) Line number=0: filename=/mnt/s/GitHub/cppcheck-fw/platforms/none.xml".format(exepath), + "cppcheck: error: unrecognized platform: 'none'." + ] + + +# TODO: test with FILESDIR +def test_addon_lookup(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 0, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for addon 'misra.py'", + "looking for addon '{}/misra.py'".format(exepath), + "looking for addon '{}/addons/misra.py'".format(exepath), + 'Checking {} ...'.format(test_file) + ] + + +# TODO: test with FILESDIR +def test_addon_lookup_ext(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra.py', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 0, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for addon 'misra.py'", + "looking for addon '{}/misra.py'".format(exepath), + "looking for addon '{}/addons/misra.py'".format(exepath), + 'Checking {} ...'.format(test_file) + ] + + +# TODO: test with FILESDIR +def test_addon_lookup_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=none', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 1, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for addon 'none.py'", + "looking for addon '{}/none.py'".format(exepath), + "looking for addon '{}/addons/none.py'".format(exepath), + 'Did not find addon none.py' + ] + + +# TODO: test with FILESDIR +def test_addon_lookup_ext_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=none.py', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 1, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for addon 'none.py'", + "looking for addon '{}/none.py'".format(exepath), + "looking for addon '{}/addons/none.py'".format(exepath), + 'Did not find addon none.py' + ] + + +# TODO: test with FILESDIR +@pytest.mark.skip +def test_config_lookup(tmpdir): + 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 + + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=config', '--addon=misra', test_file], cwd=tmpdir) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 0, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for '{}/cppcheck.cfg'".format(exepath), + 'no configuration found', + 'Checking {} ...'.format(test_file) + ] + + +# TODO: test with FILESDIR +def test_config_lookup_notfound(tmpdir): + test_file = os.path.join(tmpdir, 'test.c') + with open(test_file, 'wt'): + pass + + exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=config', test_file]) + exepath = os.path.dirname(exe) + if sys.platform == 'win32': + exepath = exepath.replace('\\', '/') + assert exitcode == 0, stdout + lines = stdout.splitlines() + assert lines == [ + "looking for '{}/cppcheck.cfg'".format(exepath), + 'no configuration found', + 'Checking {} ...'.format(test_file) + ] diff --git a/test/testcmdlineparser.cpp b/test/testcmdlineparser.cpp index 21700461f1e3..80db7eff8238 100644 --- a/test/testcmdlineparser.cpp +++ b/test/testcmdlineparser.cpp @@ -395,6 +395,12 @@ class TestCmdlineParser : public TestFixture { TEST_CASE(cppHeaderProbe2); TEST_CASE(noCppHeaderProbe); TEST_CASE(noCppHeaderProbe2); + TEST_CASE(debugLookup); + TEST_CASE(debugLookupAll); + TEST_CASE(debugLookupAddon); + TEST_CASE(debugLookupConfig); + TEST_CASE(debugLookupLibrary); + TEST_CASE(debugLookupPlatform); TEST_CASE(ignorepaths1); TEST_CASE(ignorepaths2); @@ -2670,6 +2676,48 @@ class TestCmdlineParser : public TestFixture { ASSERT_EQUALS(false, settings->cppHeaderProbe); } + void debugLookup() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--debug-lookup", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv)); + ASSERT_EQUALS(true, settings->debuglookup); + } + + void debugLookupAll() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--debug-lookup=all", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv)); + ASSERT_EQUALS(true, settings->debuglookup); + } + + void debugLookupAddon() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--debug-lookup=addon", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv)); + ASSERT_EQUALS(true, settings->debuglookupAddon); + } + + void debugLookupConfig() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--debug-lookup=config", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv)); + ASSERT_EQUALS(true, settings->debuglookupConfig); + } + + void debugLookupLibrary() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--debug-lookup=library", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv)); + ASSERT_EQUALS(true, settings->debuglookupLibrary); + } + + void debugLookupPlatform() { + REDIRECT; + const char * const argv[] = {"cppcheck", "--debug-lookup=platform", "file.cpp"}; + ASSERT_EQUALS_ENUM(CmdLineParser::Result::Success, parser->parseFromArgs(3, argv)); + ASSERT_EQUALS(true, settings->debuglookupPlatform); + } + void ignorepaths1() { REDIRECT; const char * const argv[] = {"cppcheck", "-isrc", "file.cpp"};