mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-28 00:26:15 +02:00
91b6833686
Today, with the tests inside a `tests` intermingled with the corresponding library's source code, we have a few problems: - We have to be careful that wildcards don't end up with tests being built as part of Nix proper, or test headers being installed as part of Nix proper. - Tests in libraries but not executables is not right: - It means each executable runs the previous unit tests again, because it needs the libraries. - It doesn't work right on Windows, which doesn't want you to load a DLL just for the side global variable . It could be made to work with the dlopen equivalent, but that's gross! This reorg solves these problems. There is a remaining problem which is that sibbling headers (like `hash.hh` the test header vs `hash.hh` the main `libnixutil` header) end up shadowing each other. This PR doesn't solve that. That is left as future work for a future PR. Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
162 lines
5.3 KiB
C++
162 lines
5.3 KiB
C++
#include "canon-path.hh"
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
namespace nix {
|
|
|
|
TEST(CanonPath, basic) {
|
|
{
|
|
CanonPath p("/");
|
|
ASSERT_EQ(p.abs(), "/");
|
|
ASSERT_EQ(p.rel(), "");
|
|
ASSERT_EQ(p.baseName(), std::nullopt);
|
|
ASSERT_EQ(p.dirOf(), std::nullopt);
|
|
ASSERT_FALSE(p.parent());
|
|
}
|
|
|
|
{
|
|
CanonPath p("/foo//");
|
|
ASSERT_EQ(p.abs(), "/foo");
|
|
ASSERT_EQ(p.rel(), "foo");
|
|
ASSERT_EQ(*p.baseName(), "foo");
|
|
ASSERT_EQ(*p.dirOf(), ""); // FIXME: do we want this?
|
|
ASSERT_EQ(p.parent()->abs(), "/");
|
|
}
|
|
|
|
{
|
|
CanonPath p("foo/bar");
|
|
ASSERT_EQ(p.abs(), "/foo/bar");
|
|
ASSERT_EQ(p.rel(), "foo/bar");
|
|
ASSERT_EQ(*p.baseName(), "bar");
|
|
ASSERT_EQ(*p.dirOf(), "/foo");
|
|
ASSERT_EQ(p.parent()->abs(), "/foo");
|
|
}
|
|
|
|
{
|
|
CanonPath p("foo//bar/");
|
|
ASSERT_EQ(p.abs(), "/foo/bar");
|
|
ASSERT_EQ(p.rel(), "foo/bar");
|
|
ASSERT_EQ(*p.baseName(), "bar");
|
|
ASSERT_EQ(*p.dirOf(), "/foo");
|
|
}
|
|
}
|
|
|
|
TEST(CanonPath, pop) {
|
|
CanonPath p("foo/bar/x");
|
|
ASSERT_EQ(p.abs(), "/foo/bar/x");
|
|
p.pop();
|
|
ASSERT_EQ(p.abs(), "/foo/bar");
|
|
p.pop();
|
|
ASSERT_EQ(p.abs(), "/foo");
|
|
p.pop();
|
|
ASSERT_EQ(p.abs(), "/");
|
|
}
|
|
|
|
TEST(CanonPath, removePrefix) {
|
|
CanonPath p1("foo/bar");
|
|
CanonPath p2("foo/bar/a/b/c");
|
|
ASSERT_EQ(p2.removePrefix(p1).abs(), "/a/b/c");
|
|
ASSERT_EQ(p1.removePrefix(p1).abs(), "/");
|
|
ASSERT_EQ(p1.removePrefix(CanonPath("/")).abs(), "/foo/bar");
|
|
}
|
|
|
|
TEST(CanonPath, iter) {
|
|
{
|
|
CanonPath p("a//foo/bar//");
|
|
std::vector<std::string_view> ss;
|
|
for (auto & c : p) ss.push_back(c);
|
|
ASSERT_EQ(ss, std::vector<std::string_view>({"a", "foo", "bar"}));
|
|
}
|
|
|
|
{
|
|
CanonPath p("/");
|
|
std::vector<std::string_view> ss;
|
|
for (auto & c : p) ss.push_back(c);
|
|
ASSERT_EQ(ss, std::vector<std::string_view>());
|
|
}
|
|
}
|
|
|
|
TEST(CanonPath, concat) {
|
|
{
|
|
CanonPath p1("a//foo/bar//");
|
|
CanonPath p2("xyzzy/bla");
|
|
ASSERT_EQ((p1 + p2).abs(), "/a/foo/bar/xyzzy/bla");
|
|
}
|
|
|
|
{
|
|
CanonPath p1("/");
|
|
CanonPath p2("/a/b");
|
|
ASSERT_EQ((p1 + p2).abs(), "/a/b");
|
|
}
|
|
|
|
{
|
|
CanonPath p1("/a/b");
|
|
CanonPath p2("/");
|
|
ASSERT_EQ((p1 + p2).abs(), "/a/b");
|
|
}
|
|
|
|
{
|
|
CanonPath p("/foo/bar");
|
|
ASSERT_EQ((p + "x").abs(), "/foo/bar/x");
|
|
}
|
|
|
|
{
|
|
CanonPath p("/");
|
|
ASSERT_EQ((p + "foo" + "bar").abs(), "/foo/bar");
|
|
}
|
|
}
|
|
|
|
TEST(CanonPath, within) {
|
|
ASSERT_TRUE(CanonPath("foo").isWithin(CanonPath("foo")));
|
|
ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("bar")));
|
|
ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("fo")));
|
|
ASSERT_TRUE(CanonPath("foo/bar").isWithin(CanonPath("foo")));
|
|
ASSERT_FALSE(CanonPath("foo").isWithin(CanonPath("foo/bar")));
|
|
ASSERT_TRUE(CanonPath("/foo/bar/default.nix").isWithin(CanonPath("/")));
|
|
ASSERT_TRUE(CanonPath("/").isWithin(CanonPath("/")));
|
|
}
|
|
|
|
TEST(CanonPath, sort) {
|
|
ASSERT_FALSE(CanonPath("foo") < CanonPath("foo"));
|
|
ASSERT_TRUE (CanonPath("foo") < CanonPath("foo/bar"));
|
|
ASSERT_TRUE (CanonPath("foo/bar") < CanonPath("foo!"));
|
|
ASSERT_FALSE(CanonPath("foo!") < CanonPath("foo"));
|
|
ASSERT_TRUE (CanonPath("foo") < CanonPath("foo!"));
|
|
}
|
|
|
|
TEST(CanonPath, allowed) {
|
|
std::set<CanonPath> allowed {
|
|
CanonPath("foo/bar"),
|
|
CanonPath("foo!"),
|
|
CanonPath("xyzzy"),
|
|
CanonPath("a/b/c"),
|
|
};
|
|
|
|
ASSERT_TRUE (CanonPath("foo/bar").isAllowed(allowed));
|
|
ASSERT_TRUE (CanonPath("foo/bar/bla").isAllowed(allowed));
|
|
ASSERT_TRUE (CanonPath("foo").isAllowed(allowed));
|
|
ASSERT_FALSE(CanonPath("bar").isAllowed(allowed));
|
|
ASSERT_FALSE(CanonPath("bar/a").isAllowed(allowed));
|
|
ASSERT_TRUE (CanonPath("a").isAllowed(allowed));
|
|
ASSERT_TRUE (CanonPath("a/b").isAllowed(allowed));
|
|
ASSERT_TRUE (CanonPath("a/b/c").isAllowed(allowed));
|
|
ASSERT_TRUE (CanonPath("a/b/c/d").isAllowed(allowed));
|
|
ASSERT_TRUE (CanonPath("a/b/c/d/e").isAllowed(allowed));
|
|
ASSERT_FALSE(CanonPath("a/b/a").isAllowed(allowed));
|
|
ASSERT_FALSE(CanonPath("a/b/d").isAllowed(allowed));
|
|
ASSERT_FALSE(CanonPath("aaa").isAllowed(allowed));
|
|
ASSERT_FALSE(CanonPath("zzz").isAllowed(allowed));
|
|
ASSERT_TRUE (CanonPath("/").isAllowed(allowed));
|
|
}
|
|
|
|
TEST(CanonPath, makeRelative) {
|
|
CanonPath d("/foo/bar");
|
|
ASSERT_EQ(d.makeRelative(CanonPath("/foo/bar")), ".");
|
|
ASSERT_EQ(d.makeRelative(CanonPath("/foo")), "..");
|
|
ASSERT_EQ(d.makeRelative(CanonPath("/")), "../..");
|
|
ASSERT_EQ(d.makeRelative(CanonPath("/foo/bar/xyzzy")), "xyzzy");
|
|
ASSERT_EQ(d.makeRelative(CanonPath("/foo/bar/xyzzy/bla")), "xyzzy/bla");
|
|
ASSERT_EQ(d.makeRelative(CanonPath("/foo/xyzzy/bla")), "../xyzzy/bla");
|
|
ASSERT_EQ(d.makeRelative(CanonPath("/xyzzy/bla")), "../../xyzzy/bla");
|
|
}
|
|
}
|