Fix CanonPath::parent()

This also fixes flake.lock loading.
This commit is contained in:
Eelco Dolstra 2022-05-19 00:08:51 +02:00
parent c80b942c6e
commit d7d93ebdc4
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
2 changed files with 5 additions and 3 deletions

View file

@ -16,14 +16,13 @@ CanonPath::CanonPath(std::string_view raw, const CanonPath & root)
std::optional<CanonPath> CanonPath::parent() const
{
if (isRoot()) return std::nullopt;
return CanonPath(unchecked_t(), path.substr(0, path.rfind('/')));
return CanonPath(unchecked_t(), path.substr(0, std::max((size_t) 1, path.rfind('/'))));
}
void CanonPath::pop()
{
assert(!isRoot());
auto slash = path.rfind('/');
path.resize(std::max((size_t) 1, slash));
path.resize(std::max((size_t) 1, path.rfind('/')));
}
bool CanonPath::isWithin(const CanonPath & parent) const

View file

@ -11,6 +11,7 @@ namespace nix {
ASSERT_EQ(p.rel(), "");
ASSERT_EQ(p.baseName(), std::nullopt);
ASSERT_EQ(p.dirOf(), std::nullopt);
ASSERT_FALSE(p.parent());
}
{
@ -19,6 +20,7 @@ namespace nix {
ASSERT_EQ(p.rel(), "foo");
ASSERT_EQ(*p.baseName(), "foo");
ASSERT_EQ(*p.dirOf(), ""); // FIXME: do we want this?
ASSERT_EQ(p.parent()->abs(), "/");
}
{
@ -27,6 +29,7 @@ namespace nix {
ASSERT_EQ(p.rel(), "foo/bar");
ASSERT_EQ(*p.baseName(), "bar");
ASSERT_EQ(*p.dirOf(), "/foo");
ASSERT_EQ(p.parent()->abs(), "/foo");
}
{