summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGunnar Beutner <gbeutner@serenityos.org>2021-05-18 01:59:13 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-18 08:11:21 +0200
commitc99fd217e230fbeef07e8bbda2c0a21fb3b0169d (patch)
tree5ea16f71de63872ce0356f2ec40b399cf3ce9a82
parentebb1d9740e063baa5d22bcdca58d08354141aa1c (diff)
downloadserenity-c99fd217e230fbeef07e8bbda2c0a21fb3b0169d.zip
AK: Make LexicalPath handle relative paths correctly
Previously LexicalPath would consider "." and ".." as equivalent to "/". This is not true though.
-rw-r--r--AK/LexicalPath.cpp2
-rw-r--r--Tests/AK/TestLexicalPath.cpp4
2 files changed, 5 insertions, 1 deletions
diff --git a/AK/LexicalPath.cpp b/AK/LexicalPath.cpp
index 0e18d00365..b810ba2462 100644
--- a/AK/LexicalPath.cpp
+++ b/AK/LexicalPath.cpp
@@ -55,7 +55,7 @@ void LexicalPath::canonicalize()
}
}
if (canonical_parts.is_empty()) {
- m_string = m_basename = m_dirname = "/";
+ m_string = m_basename = m_dirname = m_is_absolute ? "/" : ".";
return;
}
diff --git a/Tests/AK/TestLexicalPath.cpp b/Tests/AK/TestLexicalPath.cpp
index 044f5f157a..0aa9986c9c 100644
--- a/Tests/AK/TestLexicalPath.cpp
+++ b/Tests/AK/TestLexicalPath.cpp
@@ -24,12 +24,16 @@ TEST_CASE(basic)
EXPECT_EQ(path.parts().size(), 3u);
EXPECT_EQ(path.parts(), Vector<String>({ "abc", "def", "ghi.txt" }));
EXPECT_EQ(path.string(), "/abc/def/ghi.txt");
+ EXPECT_EQ(LexicalPath(".").string(), ".");
+ EXPECT_EQ(LexicalPath("..").string(), "..");
}
TEST_CASE(dotdot_coalescing)
{
EXPECT_EQ(LexicalPath("/home/user/../../not/home").string(), "/not/home");
EXPECT_EQ(LexicalPath("/../../../../").string(), "/");
+ EXPECT_EQ(LexicalPath("./../../../../").string(), "../../../..");
+ EXPECT_EQ(LexicalPath("../../../../../").string(), "../../../../..");
}
TEST_CASE(has_extension)