aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/libutil
diff options
context:
space:
mode:
authorrebecca “wiggles” turner <rbt@sent.as>2024-09-01 22:06:36 +0000
committerGerrit Code Review <gerrit@localhost>2024-09-01 22:06:36 +0000
commit02eb07cfd539c34c080cb1baf042e5e780c1fcc2 (patch)
tree0397ab434cce8b284a8d447594e2e1f3f35a1470 /tests/unit/libutil
parentd75df91f74b6819f674f0733143fdf32580af183 (diff)
parent690f07272e58bfe86d12adb0bd6c81c031f930fd (diff)
Merge changes I5566a985,I88cf53d3 into main
* changes: Support relative and `~/` paths in config settings Thread `ApplyConfigOptions` through config parsing
Diffstat (limited to 'tests/unit/libutil')
-rw-r--r--tests/unit/libutil/config.cc2
-rw-r--r--tests/unit/libutil/paths-setting.cc93
2 files changed, 66 insertions, 29 deletions
diff --git a/tests/unit/libutil/config.cc b/tests/unit/libutil/config.cc
index 886e70da5..1629969ba 100644
--- a/tests/unit/libutil/config.cc
+++ b/tests/unit/libutil/config.cc
@@ -80,7 +80,7 @@ namespace nix {
class TestSetting : public AbstractSetting {
public:
TestSetting() : AbstractSetting("test", "test", {}) {}
- void set(const std::string & value, bool append) override {}
+ void set(const std::string & value, bool append, const ApplyConfigOptions & options) override {}
std::string to_string() const override { return {}; }
bool isAppendable() override { return false; }
};
diff --git a/tests/unit/libutil/paths-setting.cc b/tests/unit/libutil/paths-setting.cc
index 17cb125c8..2d37ad525 100644
--- a/tests/unit/libutil/paths-setting.cc
+++ b/tests/unit/libutil/paths-setting.cc
@@ -11,14 +11,13 @@ namespace nix {
class PathsSettingTestConfig : public Config
{
public:
- PathsSettingTestConfig()
- : Config()
- { }
+ PathsSettingTestConfig() : Config() {}
PathsSetting<Paths> paths{this, Paths(), "paths", "documentation"};
};
-struct PathsSettingTest : public ::testing::Test {
+struct PathsSettingTest : public ::testing::Test
+{
public:
PathsSettingTestConfig mkConfig()
{
@@ -26,61 +25,99 @@ public:
}
};
-TEST_F(PathsSettingTest, parse) {
+TEST_F(PathsSettingTest, parse)
+{
auto config = mkConfig();
// Not an absolute path:
- ASSERT_THROW(config.paths.parse("puppy.nix"), Error);
+ ASSERT_THROW(config.paths.parse("puppy.nix", {}), Error);
- ASSERT_THAT(
- config.paths.parse("/puppy.nix"),
- Eq<Paths>({"/puppy.nix"})
- );
+ ASSERT_THAT(config.paths.parse("/puppy.nix", {}), Eq<Paths>({"/puppy.nix"}));
// Splits on whitespace:
ASSERT_THAT(
- config.paths.parse("/puppy.nix /doggy.nix"),
- Eq<Paths>({"/puppy.nix", "/doggy.nix"})
+ config.paths.parse("/puppy.nix /doggy.nix", {}), Eq<Paths>({"/puppy.nix", "/doggy.nix"})
);
// Splits on _any_ whitespace:
ASSERT_THAT(
- config.paths.parse("/puppy.nix \t /doggy.nix\n\n\n/borzoi.nix\r/goldie.nix"),
+ config.paths.parse("/puppy.nix \t /doggy.nix\n\n\n/borzoi.nix\r/goldie.nix", {}),
Eq<Paths>({"/puppy.nix", "/doggy.nix", "/borzoi.nix", "/goldie.nix"})
);
// Canonicizes paths:
+ ASSERT_THAT(config.paths.parse("/puppy/../doggy.nix", {}), Eq<Paths>({"/doggy.nix"}));
+}
+
+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/../doggy.nix"),
- Eq<Paths>({"/doggy.nix"})
+ 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, append) {
+TEST_F(PathsSettingTest, parseHome)
+{
+ auto options = ApplyConfigOptions{
+ .path = "/doggy/kinds/config.nix",
+ .home = "/home/puppy"
+ };
auto config = mkConfig();
- ASSERT_TRUE(config.paths.isAppendable());
+ ASSERT_THAT(
+ config.paths.parse("puppy.nix", options),
+ Eq<Paths>({"/doggy/kinds/puppy.nix"})
+ );
- // Starts with no paths:
ASSERT_THAT(
- config.paths.get(),
- Eq<Paths>({})
+ 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)
+{
+ auto config = mkConfig();
+
+ ASSERT_TRUE(config.paths.isAppendable());
+
+ // Starts with no paths:
+ ASSERT_THAT(config.paths.get(), Eq<Paths>({}));
+
// Can append a path:
config.paths.set("/puppy.nix", true);
- ASSERT_THAT(
- config.paths.get(),
- Eq<Paths>({"/puppy.nix"})
- );
+ ASSERT_THAT(config.paths.get(), Eq<Paths>({"/puppy.nix"}));
// Can append multiple paths:
config.paths.set("/silly.nix /doggy.nix", true);
- ASSERT_THAT(
- config.paths.get(),
- Eq<Paths>({"/puppy.nix", "/silly.nix", "/doggy.nix"})
- );
+ ASSERT_THAT(config.paths.get(), Eq<Paths>({"/puppy.nix", "/silly.nix", "/doggy.nix"}));
}
} // namespace nix