diff --git a/lib/addoninfo.cpp b/lib/addoninfo.cpp index c0139320d7e..388484916ea 100644 --- a/lib/addoninfo.cpp +++ b/lib/addoninfo.cpp @@ -39,24 +39,36 @@ static std::string getFullPath(const std::string &fileName, const std::string &e return ""; 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; + { + std::string p = Path::join(exepath, fileName); + if (debug) + std::cout << "looking for addon '" << p << "'" << std::endl; + if (Path::isFile(p)) + return p; + } + { + std::string p = Path::join(exepath, "addons", fileName); + if (debug) + std::cout << "looking for addon '" << p << "'" << std::endl; + if (Path::isFile(p)) + return p; + } #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); + { + std::string p = Path::join(FILESDIR, fileName); + if (debug) + std::cout << "looking for addon '" << p << "'" << std::endl; + if (Path::isFile(p)) + return p; + } + { + std::string p = Path::join(FILESDIR, "addons", fileName); + if (debug) + std::cout << "looking for addon '" << p << "'" << std::endl; + if (Path::isFile(p)) + return p; + } #endif return ""; } diff --git a/lib/path.cpp b/lib/path.cpp index 0e6e5e8ae9c..d679f3beceb 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -458,3 +458,8 @@ std::string Path::join(std::string path1, std::string path2) return path2; return ((path1.back() == '/') ? path1 : (path1 + "/")) + path2; } + +std::string Path::join(std::string path1, std::string path2, std::string path3) +{ + return Path::join(Path::join(std::move(path1), std::move(path2)), std::move(path3)); +} diff --git a/lib/path.h b/lib/path.h index 23db06c4e50..fcc46b48b5b 100644 --- a/lib/path.h +++ b/lib/path.h @@ -206,6 +206,13 @@ class CPPCHECKLIB Path { * @return the joined path with normalized slashes */ static std::string join(std::string path1, std::string path2); + + /** + * @brief join 3 paths with '/' separators + * if path2 is an absolute path path1 will be dismissed. + * @return the joined path with normalized slashes + */ + static std::string join(std::string path1, std::string path2, std::string path3); }; /// @} diff --git a/test/cli/lookup_test.py b/test/cli/lookup_test.py index cab96a7f6bb..f8c9c8e65ec 100644 --- a/test/cli/lookup_test.py +++ b/test/cli/lookup_test.py @@ -644,12 +644,14 @@ def test_addon_lookup(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 + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') assert exitcode == 0, stdout if stdout else stderr 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 + "looking for addon '{}addons/misra.py'".format(exepath_sep), 'Checking {} ...'.format(test_file) ] @@ -662,12 +664,14 @@ def test_addon_lookup_ext(tmpdir): exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra.py', test_file]) exepath = os.path.dirname(exe) exepath_sep = exepath + os.path.sep + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') assert exitcode == 0, stdout if stdout else stderr 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 + "looking for addon '{}addons/misra.py'".format(exepath_sep), 'Checking {} ...'.format(test_file) ] @@ -680,12 +684,14 @@ def test_addon_lookup_notfound(tmpdir): exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', test_file]) exepath = os.path.dirname(exe) exepath_sep = exepath + os.path.sep + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') assert exitcode == 1, stdout lines = stdout.splitlines() assert lines == [ "looking for addon 'none.py'", "looking for addon '{}none.py'".format(exepath_sep), - "looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators + "looking for addon '{}addons/none.py'".format(exepath_sep), 'Did not find addon none.py' ] @@ -696,13 +702,15 @@ def test_addon_lookup_notfound_project(tmpdir): # #13940 / #13941 exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', '--project={}'.format(project_file)]) exepath = os.path.dirname(exe) exepath_sep = exepath + os.path.sep + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') assert exitcode == 1, stdout lines = stdout.splitlines() assert lines == [ # TODO: needs to look relative to the project file first "looking for addon 'none.py'", "looking for addon '{}none.py'".format(exepath_sep), - "looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators + "looking for addon '{}addons/none.py'".format(exepath_sep), 'Did not find addon none.py' ] @@ -713,12 +721,14 @@ def test_addon_lookup_notfound_compdb(tmpdir): exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', '--project={}'.format(compdb_file)]) exepath = os.path.dirname(exe) exepath_sep = exepath + os.path.sep + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') assert exitcode == 1, stdout lines = stdout.splitlines() assert lines == [ "looking for addon 'none.py'", "looking for addon '{}none.py'".format(exepath_sep), - "looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators + "looking for addon '{}addons/none.py'".format(exepath_sep), 'Did not find addon none.py' ] @@ -731,12 +741,14 @@ def test_addon_lookup_ext_notfound(tmpdir): exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none.py', test_file]) exepath = os.path.dirname(exe) exepath_sep = exepath + os.path.sep + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') assert exitcode == 1, stdout lines = stdout.splitlines() assert lines == [ "looking for addon 'none.py'", "looking for addon '{}none.py'".format(exepath_sep), - "looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators + "looking for addon '{}addons/none.py'".format(exepath_sep), 'Did not find addon none.py' ] @@ -749,12 +761,14 @@ def test_addon_lookup_relative_notfound(tmpdir): 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 + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') 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 + "looking for addon '{}addons/addon/misra.py'".format(exepath_sep), 'Did not find addon addon/misra.py' ] @@ -767,12 +781,14 @@ def test_addon_lookup_relative_noext_notfound(tmpdir): exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=addon/misra', test_file]) exepath = os.path.dirname(exe) exepath_sep = exepath + os.path.sep + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') 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 + "looking for addon '{}addons/addon/misra.py'".format(exepath_sep), 'Did not find addon addon/misra.py' ] @@ -825,12 +841,14 @@ 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 + if sys.platform == 'win32': + exepath_sep = exepath_sep.replace('\\', '/') assert exitcode == 0, stdout if stdout else stderr 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 + "looking for addon '{}addons/misra.py'".format(exepath_sep), 'Checking {} ...'.format(test_file) ] diff --git a/test/testpath.cpp b/test/testpath.cpp index 44dea302762..f48a2fe53ab 100644 --- a/test/testpath.cpp +++ b/test/testpath.cpp @@ -202,6 +202,8 @@ class TestPath : public TestFixture { //ASSERT_EQUALS("", Path::join("S:/a", "S:/b")); //ASSERT_EQUALS("", Path::join("S:/a", "S:\\b")); //ASSERT_EQUALS("", Path::join("S:/a", "/b")); + + ASSERT_EQUALS("a/b/c", Path::join("a", "b", "c")); } void isDirectory() const {