Skip to content

Commit

Permalink
Path: fixed result from getAbsolutePath() on Linux if parts discard…
Browse files Browse the repository at this point in the history
…ed by `..` do not exist
  • Loading branch information
firewave committed Aug 14, 2024
1 parent 45219c0 commit f229fde
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ std::string Path::getAbsoluteFilePath(const std::string& filePath)
if (_fullpath(absolute, filePath.c_str(), _MAX_PATH))
absolute_path = absolute;
#elif defined(__linux__) || defined(__sun) || defined(__hpux) || defined(__GNUC__) || defined(__CPPCHECK__)
char * absolute = realpath(filePath.c_str(), nullptr);
// simplify the path since any non-existent part has to exist even if discarded by ".."
std::string spath = Path::simplifyPath(filePath);
// TODO: assert if path exists?
char * absolute = realpath(spath.c_str(), nullptr);
if (absolute)
absolute_path = absolute;
free(absolute);
Expand Down
39 changes: 39 additions & 0 deletions test/testpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TestPath : public TestFixture {
TEST_CASE(identifyWithCppProbe);
TEST_CASE(is_header);
TEST_CASE(simplifyPath);
TEST_CASE(getAbsolutePath);
}

void removeQuotationMarks() const {
Expand Down Expand Up @@ -437,6 +438,44 @@ class TestPath : public TestFixture {
ASSERT_EQUALS("//home/file.cpp", Path::simplifyPath("\\\\home\\test\\..\\file.cpp"));
ASSERT_EQUALS("//file.cpp", Path::simplifyPath("\\\\home\\..\\test\\..\\file.cpp"));
}

void getAbsolutePath() const {
const std::string cwd = Path::getCurrentPath();

ScopedFile file("testabspath.txt", "");
std::string expected = Path::toNativeSeparators(Path::join(cwd, "testabspath.txt"));

ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("testabspath.txt"));
ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("./testabspath.txt"));
ASSERT_EQUALS(expected, Path::getAbsoluteFilePath(".\\testabspath.txt"));
ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("test/../testabspath.txt"));
ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("test\\..\\testabspath.txt"));
ASSERT_EQUALS(expected, Path::getAbsoluteFilePath("./test/../testabspath.txt"));
ASSERT_EQUALS(expected, Path::getAbsoluteFilePath(".\\test\\../testabspath.txt"));

ASSERT_EQUALS(expected, Path::getAbsoluteFilePath(Path::join(cwd, "testabspath.txt")));

std::string cwd_up = Path::getPathFromFilename(cwd);
cwd_up.pop_back(); // remove trailing slash
ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, "..")));
ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, "../")));
ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, "..\\")));
ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, "./../")));
ASSERT_EQUALS(cwd_up, Path::getAbsoluteFilePath(Path::join(cwd, ".\\..\\")));

ASSERT_EQUALS(cwd, Path::getAbsoluteFilePath("."));
TODO_ASSERT_EQUALS(cwd, "", Path::getAbsoluteFilePath("./"));
TODO_ASSERT_EQUALS(cwd, "", Path::getAbsoluteFilePath(".\\"));

ASSERT_EQUALS("", Path::getAbsoluteFilePath(""));

#ifndef _WIN32
// the underlying realpath() call only returns something if the path actually exists
ASSERT_EQUALS("", Path::getAbsoluteFilePath("testabspath2.txt"));
#else
ASSERT_EQUALS(Path::toNativeSeparators(Path::join(cwd, "testabspath2.txt")), Path::getAbsoluteFilePath("testabspath2.txt"));
#endif
}
};

REGISTER_TEST(TestPath)

0 comments on commit f229fde

Please sign in to comment.