From 5fc6fcb31035f79a8e590f07d73dc6cc592e9e29 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Sun, 25 Aug 2024 11:58:10 -0700 Subject: Thread `ApplyConfigOptions` through config parsing This makes no changes to logic but makes the `ApplyConfigOptions` value available to consumers. Change-Id: I88cf53d38faac8472c556aee55c13d0acbd1e5db --- tests/unit/libutil/config.cc | 2 +- tests/unit/libutil/paths-setting.cc | 42 ++++++++++++------------------------- 2 files changed, 14 insertions(+), 30 deletions(-) (limited to 'tests/unit/libutil') 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..c198b25e0 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{this, Paths(), "paths", "documentation"}; }; -struct PathsSettingTest : public ::testing::Test { +struct PathsSettingTest : public ::testing::Test +{ public: PathsSettingTestConfig mkConfig() { @@ -26,33 +25,27 @@ 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({"/puppy.nix"}) - ); + ASSERT_THAT(config.paths.parse("/puppy.nix", {}), Eq({"/puppy.nix"})); // Splits on whitespace: ASSERT_THAT( - config.paths.parse("/puppy.nix /doggy.nix"), - Eq({"/puppy.nix", "/doggy.nix"}) + config.paths.parse("/puppy.nix /doggy.nix", {}), Eq({"/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({"/puppy.nix", "/doggy.nix", "/borzoi.nix", "/goldie.nix"}) ); // Canonicizes paths: - ASSERT_THAT( - config.paths.parse("/puppy/../doggy.nix"), - Eq({"/doggy.nix"}) - ); + ASSERT_THAT(config.paths.parse("/puppy/../doggy.nix", {}), Eq({"/doggy.nix"})); } TEST_F(PathsSettingTest, append) { @@ -61,26 +54,17 @@ TEST_F(PathsSettingTest, append) { ASSERT_TRUE(config.paths.isAppendable()); // Starts with no paths: - ASSERT_THAT( - config.paths.get(), - Eq({}) - ); + ASSERT_THAT(config.paths.get(), Eq({})); // Can append a path: config.paths.set("/puppy.nix", true); - ASSERT_THAT( - config.paths.get(), - Eq({"/puppy.nix"}) - ); + ASSERT_THAT(config.paths.get(), Eq({"/puppy.nix"})); // Can append multiple paths: config.paths.set("/silly.nix /doggy.nix", true); - ASSERT_THAT( - config.paths.get(), - Eq({"/puppy.nix", "/silly.nix", "/doggy.nix"}) - ); + ASSERT_THAT(config.paths.get(), Eq({"/puppy.nix", "/silly.nix", "/doggy.nix"})); } } // namespace nix -- cgit v1.2.3 From 690f07272e58bfe86d12adb0bd6c81c031f930fd Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Sun, 25 Aug 2024 11:58:55 -0700 Subject: Support relative and `~/` paths in config settings Change-Id: I5566a9858ba255f4ac5051d1368c7dfb24460f0a --- tests/unit/libutil/paths-setting.cc | 55 ++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) (limited to 'tests/unit/libutil') diff --git a/tests/unit/libutil/paths-setting.cc b/tests/unit/libutil/paths-setting.cc index c198b25e0..2d37ad525 100644 --- a/tests/unit/libutil/paths-setting.cc +++ b/tests/unit/libutil/paths-setting.cc @@ -48,7 +48,60 @@ TEST_F(PathsSettingTest, parse) ASSERT_THAT(config.paths.parse("/puppy/../doggy.nix", {}), Eq({"/doggy.nix"})); } -TEST_F(PathsSettingTest, append) { +TEST_F(PathsSettingTest, parseRelative) +{ + auto options = ApplyConfigOptions{.path = "/doggy/kinds/config.nix"}; + auto config = mkConfig(); + ASSERT_THAT( + config.paths.parse("puppy.nix", options), + Eq({"/doggy/kinds/puppy.nix"}) + ); + + // Splits on whitespace: + ASSERT_THAT( + config.paths.parse("puppy.nix /doggy.nix", options), Eq({"/doggy/kinds/puppy.nix", "/doggy.nix"}) + ); + + // Canonicizes paths: + ASSERT_THAT(config.paths.parse("../soft.nix", options), Eq({"/doggy/soft.nix"})); + + // Canonicizes paths: + ASSERT_THAT(config.paths.parse("./soft.nix", options), Eq({"/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({"/doggy/kinds/puppy.nix"}) + ); + + ASSERT_THAT( + config.paths.parse("~/.config/nix/puppy.nix", options), + Eq({"/home/puppy/.config/nix/puppy.nix"}) + ); + + // Splits on whitespace: + ASSERT_THAT( + config.paths.parse("~/puppy.nix ~/doggy.nix", options), + Eq({"/home/puppy/puppy.nix", "/home/puppy/doggy.nix"}) + ); + + // Canonicizes paths: + ASSERT_THAT(config.paths.parse("~/../why.nix", options), Eq({"/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()); -- cgit v1.2.3