2024-03-09 22:00:43 +00:00
|
|
|
#include "config.hh"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using testing::Eq;
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
class PathsSettingTestConfig : public Config
|
|
|
|
{
|
|
|
|
public:
|
2024-08-25 18:58:10 +00:00
|
|
|
PathsSettingTestConfig() : Config() {}
|
2024-03-09 22:00:43 +00:00
|
|
|
|
2024-08-17 18:55:41 +00:00
|
|
|
PathsSetting<Paths> paths{this, Paths(), "paths", "documentation"};
|
2024-03-09 22:00:43 +00:00
|
|
|
};
|
|
|
|
|
2024-08-25 18:58:10 +00:00
|
|
|
struct PathsSettingTest : public ::testing::Test
|
|
|
|
{
|
2024-03-09 22:00:43 +00:00
|
|
|
public:
|
|
|
|
PathsSettingTestConfig mkConfig()
|
|
|
|
{
|
|
|
|
return PathsSettingTestConfig();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-08-25 18:58:10 +00:00
|
|
|
TEST_F(PathsSettingTest, parse)
|
|
|
|
{
|
2024-03-09 22:00:43 +00:00
|
|
|
auto config = mkConfig();
|
|
|
|
// Not an absolute path:
|
2024-08-25 18:58:10 +00:00
|
|
|
ASSERT_THROW(config.paths.parse("puppy.nix", {}), Error);
|
2024-03-09 22:00:43 +00:00
|
|
|
|
2024-08-25 18:58:10 +00:00
|
|
|
ASSERT_THAT(config.paths.parse("/puppy.nix", {}), Eq<Paths>({"/puppy.nix"}));
|
2024-03-09 22:00:43 +00:00
|
|
|
|
|
|
|
// Splits on whitespace:
|
|
|
|
ASSERT_THAT(
|
2024-08-25 18:58:10 +00:00
|
|
|
config.paths.parse("/puppy.nix /doggy.nix", {}), Eq<Paths>({"/puppy.nix", "/doggy.nix"})
|
2024-03-09 22:00:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Splits on _any_ whitespace:
|
|
|
|
ASSERT_THAT(
|
2024-08-25 18:58:10 +00:00
|
|
|
config.paths.parse("/puppy.nix \t /doggy.nix\n\n\n/borzoi.nix\r/goldie.nix", {}),
|
2024-03-09 22:00:43 +00:00
|
|
|
Eq<Paths>({"/puppy.nix", "/doggy.nix", "/borzoi.nix", "/goldie.nix"})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Canonicizes paths:
|
2024-08-25 18:58:10 +00:00
|
|
|
ASSERT_THAT(config.paths.parse("/puppy/../doggy.nix", {}), Eq<Paths>({"/doggy.nix"}));
|
2024-03-09 22:00:43 +00:00
|
|
|
}
|
|
|
|
|
2024-08-25 18:58:55 +00:00
|
|
|
TEST_F(PathsSettingTest, parseRelative)
|
|
|
|
{
|
|
|
|
auto options = ApplyConfigOptions{.path = "/doggy/kinds/config.nix"};
|
|
|
|
auto config = mkConfig();
|
|
|
|
ASSERT_THAT(
|
|
|
|
config.paths.parse("puppy.nix", options),
|
|
|
|
Eq<Paths>({"/doggy/kinds/puppy.nix"})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Splits on whitespace:
|
|
|
|
ASSERT_THAT(
|
|
|
|
config.paths.parse("puppy.nix /doggy.nix", options), Eq<Paths>({"/doggy/kinds/puppy.nix", "/doggy.nix"})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Canonicizes paths:
|
|
|
|
ASSERT_THAT(config.paths.parse("../soft.nix", options), Eq<Paths>({"/doggy/soft.nix"}));
|
|
|
|
|
|
|
|
// Canonicizes paths:
|
|
|
|
ASSERT_THAT(config.paths.parse("./soft.nix", options), Eq<Paths>({"/doggy/kinds/soft.nix"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PathsSettingTest, parseHome)
|
|
|
|
{
|
|
|
|
auto options = ApplyConfigOptions{
|
|
|
|
.path = "/doggy/kinds/config.nix",
|
|
|
|
.home = "/home/puppy"
|
|
|
|
};
|
|
|
|
auto config = mkConfig();
|
|
|
|
|
|
|
|
ASSERT_THAT(
|
|
|
|
config.paths.parse("puppy.nix", options),
|
|
|
|
Eq<Paths>({"/doggy/kinds/puppy.nix"})
|
|
|
|
);
|
|
|
|
|
|
|
|
ASSERT_THAT(
|
|
|
|
config.paths.parse("~/.config/nix/puppy.nix", options),
|
|
|
|
Eq<Paths>({"/home/puppy/.config/nix/puppy.nix"})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Splits on whitespace:
|
|
|
|
ASSERT_THAT(
|
|
|
|
config.paths.parse("~/puppy.nix ~/doggy.nix", options),
|
|
|
|
Eq<Paths>({"/home/puppy/puppy.nix", "/home/puppy/doggy.nix"})
|
|
|
|
);
|
|
|
|
|
|
|
|
// Canonicizes paths:
|
|
|
|
ASSERT_THAT(config.paths.parse("~/../why.nix", options), Eq<Paths>({"/home/why.nix"}));
|
|
|
|
|
|
|
|
// Home paths for other users not allowed. Needs to start with `~/`.
|
|
|
|
ASSERT_THROW(config.paths.parse("~root/config.nix", options), Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(PathsSettingTest, append)
|
|
|
|
{
|
2024-03-09 22:00:43 +00:00
|
|
|
auto config = mkConfig();
|
|
|
|
|
|
|
|
ASSERT_TRUE(config.paths.isAppendable());
|
|
|
|
|
|
|
|
// Starts with no paths:
|
2024-08-25 18:58:10 +00:00
|
|
|
ASSERT_THAT(config.paths.get(), Eq<Paths>({}));
|
2024-03-09 22:00:43 +00:00
|
|
|
|
|
|
|
// Can append a path:
|
|
|
|
config.paths.set("/puppy.nix", true);
|
|
|
|
|
2024-08-25 18:58:10 +00:00
|
|
|
ASSERT_THAT(config.paths.get(), Eq<Paths>({"/puppy.nix"}));
|
2024-03-09 22:00:43 +00:00
|
|
|
|
|
|
|
// Can append multiple paths:
|
|
|
|
config.paths.set("/silly.nix /doggy.nix", true);
|
|
|
|
|
2024-08-25 18:58:10 +00:00
|
|
|
ASSERT_THAT(config.paths.get(), Eq<Paths>({"/puppy.nix", "/silly.nix", "/doggy.nix"}));
|
2024-03-09 22:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace nix
|