Skip to content

Commit 7e36471

Browse files
authored
addoninfo.cpp: use Path::join() in getFullPath() (#8433)
1 parent d4f3b30 commit 7e36471

5 files changed

Lines changed: 69 additions & 25 deletions

File tree

lib/addoninfo.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,36 @@ static std::string getFullPath(const std::string &fileName, const std::string &e
3939
return "";
4040

4141
const std::string exepath = Path::getPathFromFilename(exename);
42-
if (debug)
43-
std::cout << "looking for addon '" << (exepath + fileName) << "'" << std::endl;
44-
if (Path::isFile(exepath + fileName))
45-
return exepath + fileName;
46-
if (debug)
47-
std::cout << "looking for addon '" << (exepath + "addons/" + fileName) << "'" << std::endl;
48-
if (Path::isFile(exepath + "addons/" + fileName))
49-
return exepath + "addons/" + fileName;
42+
{
43+
std::string p = Path::join(exepath, fileName);
44+
if (debug)
45+
std::cout << "looking for addon '" << p << "'" << std::endl;
46+
if (Path::isFile(p))
47+
return p;
48+
}
49+
{
50+
std::string p = Path::join(exepath, "addons", fileName);
51+
if (debug)
52+
std::cout << "looking for addon '" << p << "'" << std::endl;
53+
if (Path::isFile(p))
54+
return p;
55+
}
5056

5157
#ifdef FILESDIR
52-
if (debug)
53-
std::cout << "looking for addon '" << (FILESDIR + ("/" + fileName)) << "'" << std::endl;
54-
if (Path::isFile(FILESDIR + ("/" + fileName)))
55-
return FILESDIR + ("/" + fileName);
56-
if (debug)
57-
std::cout << "looking for addon '" << (FILESDIR + ("/addons/" + fileName)) << "'" << std::endl;
58-
if (Path::isFile(FILESDIR + ("/addons/" + fileName)))
59-
return FILESDIR + ("/addons/" + fileName);
58+
{
59+
std::string p = Path::join(FILESDIR, fileName);
60+
if (debug)
61+
std::cout << "looking for addon '" << p << "'" << std::endl;
62+
if (Path::isFile(p))
63+
return p;
64+
}
65+
{
66+
std::string p = Path::join(FILESDIR, "addons", fileName);
67+
if (debug)
68+
std::cout << "looking for addon '" << p << "'" << std::endl;
69+
if (Path::isFile(p))
70+
return p;
71+
}
6072
#endif
6173
return "";
6274
}

lib/path.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,8 @@ std::string Path::join(std::string path1, std::string path2)
464464
return path2;
465465
return ((path1.back() == '/') ? path1 : (path1 + "/")) + path2;
466466
}
467+
468+
std::string Path::join(std::string path1, std::string path2, std::string path3)
469+
{
470+
return Path::join(Path::join(std::move(path1), std::move(path2)), std::move(path3));
471+
}

lib/path.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ class CPPCHECKLIB Path {
213213
* @return the joined path with normalized slashes
214214
*/
215215
static std::string join(std::string path1, std::string path2);
216+
217+
/**
218+
* @brief join 3 paths with '/' separators
219+
* if path2 is an absolute path path1 will be dismissed.
220+
* @return the joined path with normalized slashes
221+
*/
222+
static std::string join(std::string path1, std::string path2, std::string path3);
216223
};
217224

218225
/// @}

test/cli/lookup_test.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -767,12 +767,14 @@ def test_addon_lookup(tmpdir):
767767
exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra', test_file])
768768
exepath = os.path.dirname(exe)
769769
exepath_sep = exepath + os.path.sep
770+
if sys.platform == 'win32':
771+
exepath_sep = exepath_sep.replace('\\', '/')
770772
assert exitcode == 0, stdout if stdout else stderr
771773
lines = stdout.splitlines()
772774
assert lines == [
773775
"looking for addon 'misra.py'",
774776
"looking for addon '{}misra.py'".format(exepath_sep),
775-
"looking for addon '{}addons/misra.py'".format(exepath_sep), # TODO: mixed separators
777+
"looking for addon '{}addons/misra.py'".format(exepath_sep),
776778
'Checking {} ...'.format(test_file)
777779
]
778780

@@ -785,12 +787,14 @@ def test_addon_lookup_ext(tmpdir):
785787
exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra.py', test_file])
786788
exepath = os.path.dirname(exe)
787789
exepath_sep = exepath + os.path.sep
790+
if sys.platform == 'win32':
791+
exepath_sep = exepath_sep.replace('\\', '/')
788792
assert exitcode == 0, stdout if stdout else stderr
789793
lines = stdout.splitlines()
790794
assert lines == [
791795
"looking for addon 'misra.py'",
792796
"looking for addon '{}misra.py'".format(exepath_sep),
793-
"looking for addon '{}addons/misra.py'".format(exepath_sep), # TODO: mixed separators
797+
"looking for addon '{}addons/misra.py'".format(exepath_sep),
794798
'Checking {} ...'.format(test_file)
795799
]
796800

@@ -803,12 +807,14 @@ def test_addon_lookup_notfound(tmpdir):
803807
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', test_file])
804808
exepath = os.path.dirname(exe)
805809
exepath_sep = exepath + os.path.sep
810+
if sys.platform == 'win32':
811+
exepath_sep = exepath_sep.replace('\\', '/')
806812
assert exitcode == 1, stdout
807813
lines = stdout.splitlines()
808814
assert lines == [
809815
"looking for addon 'none.py'",
810816
"looking for addon '{}none.py'".format(exepath_sep),
811-
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators
817+
"looking for addon '{}addons/none.py'".format(exepath_sep),
812818
'Did not find addon none.py'
813819
]
814820

@@ -819,13 +825,15 @@ def test_addon_lookup_notfound_project(tmpdir): # #13940 / #13941
819825
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', '--project={}'.format(project_file)])
820826
exepath = os.path.dirname(exe)
821827
exepath_sep = exepath + os.path.sep
828+
if sys.platform == 'win32':
829+
exepath_sep = exepath_sep.replace('\\', '/')
822830
assert exitcode == 1, stdout
823831
lines = stdout.splitlines()
824832
assert lines == [
825833
# TODO: needs to look relative to the project file first
826834
"looking for addon 'none.py'",
827835
"looking for addon '{}none.py'".format(exepath_sep),
828-
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators
836+
"looking for addon '{}addons/none.py'".format(exepath_sep),
829837
'Did not find addon none.py'
830838
]
831839

@@ -836,12 +844,14 @@ def test_addon_lookup_notfound_compdb(tmpdir):
836844
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none', '--project={}'.format(compdb_file)])
837845
exepath = os.path.dirname(exe)
838846
exepath_sep = exepath + os.path.sep
847+
if sys.platform == 'win32':
848+
exepath_sep = exepath_sep.replace('\\', '/')
839849
assert exitcode == 1, stdout
840850
lines = stdout.splitlines()
841851
assert lines == [
842852
"looking for addon 'none.py'",
843853
"looking for addon '{}none.py'".format(exepath_sep),
844-
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators
854+
"looking for addon '{}addons/none.py'".format(exepath_sep),
845855
'Did not find addon none.py'
846856
]
847857

@@ -854,12 +864,14 @@ def test_addon_lookup_ext_notfound(tmpdir):
854864
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=none.py', test_file])
855865
exepath = os.path.dirname(exe)
856866
exepath_sep = exepath + os.path.sep
867+
if sys.platform == 'win32':
868+
exepath_sep = exepath_sep.replace('\\', '/')
857869
assert exitcode == 1, stdout
858870
lines = stdout.splitlines()
859871
assert lines == [
860872
"looking for addon 'none.py'",
861873
"looking for addon '{}none.py'".format(exepath_sep),
862-
"looking for addon '{}addons/none.py'".format(exepath_sep), # TODO: mixed separators
874+
"looking for addon '{}addons/none.py'".format(exepath_sep),
863875
'Did not find addon none.py'
864876
]
865877

@@ -872,12 +884,14 @@ def test_addon_lookup_relative_notfound(tmpdir):
872884
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=addon/misra.py', test_file])
873885
exepath = os.path.dirname(exe)
874886
exepath_sep = exepath + os.path.sep
887+
if sys.platform == 'win32':
888+
exepath_sep = exepath_sep.replace('\\', '/')
875889
assert exitcode == 1, stdout
876890
lines = stdout.splitlines()
877891
assert lines == [
878892
"looking for addon 'addon/misra.py'",
879893
"looking for addon '{}addon/misra.py'".format(exepath_sep),
880-
"looking for addon '{}addons/addon/misra.py'".format(exepath_sep), # TODO: mixed separators
894+
"looking for addon '{}addons/addon/misra.py'".format(exepath_sep),
881895
'Did not find addon addon/misra.py'
882896
]
883897

@@ -891,12 +905,14 @@ def test_addon_lookup_relative_noext_notfound(tmpdir):
891905
exitcode, stdout, _, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=addon/misra', test_file])
892906
exepath = os.path.dirname(exe)
893907
exepath_sep = exepath + os.path.sep
908+
if sys.platform == 'win32':
909+
exepath_sep = exepath_sep.replace('\\', '/')
894910
assert exitcode == 1, stdout
895911
lines = stdout.splitlines()
896912
assert lines == [
897913
"looking for addon 'addon/misra.py'",
898914
"looking for addon '{}addon/misra.py'".format(exepath_sep),
899-
"looking for addon '{}addons/addon/misra.py'".format(exepath_sep), # TODO: mixed separators
915+
"looking for addon '{}addons/addon/misra.py'".format(exepath_sep),
900916
'Did not find addon addon/misra.py'
901917
]
902918

@@ -1006,12 +1022,14 @@ def test_addon_lookup_nofile(tmpdir):
10061022
exitcode, stdout, stderr, exe = cppcheck_ex(['--debug-lookup=addon', '--addon=misra', test_file])
10071023
exepath = os.path.dirname(exe)
10081024
exepath_sep = exepath + os.path.sep
1025+
if sys.platform == 'win32':
1026+
exepath_sep = exepath_sep.replace('\\', '/')
10091027
assert exitcode == 0, stdout if stdout else stderr
10101028
lines = stdout.splitlines()
10111029
assert lines == [
10121030
"looking for addon 'misra.py'",
10131031
"looking for addon '{}misra.py'".format(exepath_sep),
1014-
"looking for addon '{}addons/misra.py'".format(exepath_sep), # TODO: mixed separators
1032+
"looking for addon '{}addons/misra.py'".format(exepath_sep),
10151033
'Checking {} ...'.format(test_file)
10161034
]
10171035

test/testpath.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ class TestPath : public TestFixture {
203203
//ASSERT_EQUALS("", Path::join("S:/a", "S:/b"));
204204
//ASSERT_EQUALS("", Path::join("S:/a", "S:\\b"));
205205
//ASSERT_EQUALS("", Path::join("S:/a", "/b"));
206+
207+
ASSERT_EQUALS("a/b/c", Path::join("a", "b", "c"));
206208
}
207209

208210
void isDirectory() const {

0 commit comments

Comments
 (0)