AK: Make LexicalPath handle relative paths correctly

Previously LexicalPath would consider "." and ".." as equivalent to
"/". This is not true though.
This commit is contained in:
Gunnar Beutner 2021-05-18 01:59:13 +02:00 committed by Andreas Kling
parent ebb1d9740e
commit c99fd217e2
Notes: sideshowbarker 2024-07-18 17:53:03 +09:00
2 changed files with 5 additions and 1 deletions

View File

@ -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;
}

View File

@ -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)