From abbd855e93e6abcb75da93855d94da38d55935c7 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Sat, 9 Mar 2024 14:00:43 -0800 Subject: [PATCH] Add `PathsSetting` Change-Id: I1165f6ef033a5f757ca3716d3f8008ba36b01fd0 --- src/libutil/config.cc | 28 +++++++++++ src/libutil/config.hh | 20 ++++++++ tests/unit/libutil/paths-setting.cc | 72 +++++++++++++++++++++++++++++ tests/unit/meson.build | 1 + 4 files changed, 121 insertions(+) create mode 100644 tests/unit/libutil/paths-setting.cc diff --git a/src/libutil/config.cc b/src/libutil/config.cc index 8e76d6d66..81efcd507 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -437,6 +437,34 @@ void OptionalPathSetting::operator =(const std::optional & v) this->assign(v); } +PathsSetting::PathsSetting(Config * options, + const Paths & def, + const std::string & name, + const std::string & description, + const std::set & aliases) + : BaseSetting(def, true, name, description, aliases) +{ + options->addSetting(this); +} + + +Paths PathsSetting::parse(const std::string & str) const +{ + auto strings = tokenizeString(str); + Paths parsed; + + for (auto str : strings) { + parsed.push_back(canonPath(str)); + } + + return parsed; +} + +PathsSetting::operator bool() const noexcept +{ + return !get().empty(); +} + bool GlobalConfig::set(const std::string & name, const std::string & value) { for (auto & config : *configRegistrations) diff --git a/src/libutil/config.hh b/src/libutil/config.hh index 3e232d224..01e1239b3 100644 --- a/src/libutil/config.hh +++ b/src/libutil/config.hh @@ -375,6 +375,26 @@ public: void operator =(const std::optional & v); }; +/** + * Like `OptionalPathSetting`, but allows multiple paths. + */ +class PathsSetting : public BaseSetting +{ +public: + + PathsSetting(Config * options, + const Paths & def, + const std::string & name, + const std::string & description, + const std::set & aliases = {}); + + Paths parse(const std::string & str) const override; + + void operator =(const Paths & v); + + operator bool() const noexcept; +}; + struct GlobalConfig : public AbstractConfig { typedef std::vector ConfigRegistrations; diff --git a/tests/unit/libutil/paths-setting.cc b/tests/unit/libutil/paths-setting.cc new file mode 100644 index 000000000..56739e0cc --- /dev/null +++ b/tests/unit/libutil/paths-setting.cc @@ -0,0 +1,72 @@ +#include "config.hh" + +#include +#include +#include + +using testing::Eq; + +namespace nix { + +class PathsSettingTestConfig : public Config +{ +public: + PathsSettingTestConfig() + : Config() + { } + + PathsSetting paths{this, Paths(), "paths", "documentation"}; +}; + +struct PathsSettingTest : public ::testing::Test { +public: + PathsSettingTestConfig mkConfig() + { + return PathsSettingTestConfig(); + } +}; + +TEST_F(PathsSettingTest, parse) { + auto config = mkConfig(); + // Not an absolute path: + ASSERT_THROW(config.paths.parse("puppy.nix"), Error); + + 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"}) + ); + + // Splits on _any_ whitespace: + ASSERT_THAT( + 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"}) + ); +} + +TEST_F(PathsSettingTest, bool) { + auto config = mkConfig(); + // No paths: + ASSERT_FALSE(config.paths); + // Set a path: + config.set("paths", "/puppy.nix"); + // Now there are paths: + ASSERT_TRUE(config.paths); + + // Multiple paths count too: + config.set("paths", "/puppy.nix /doggy.nix"); + ASSERT_TRUE(config.paths); +} + +} // namespace nix diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 60bb2de89..a05776eca 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -46,6 +46,7 @@ libutil_tests_sources = files( 'libutil/json-utils.cc', 'libutil/logging.cc', 'libutil/lru-cache.cc', + 'libutil/paths-setting.cc', 'libutil/pool.cc', 'libutil/references.cc', 'libutil/suggestions.cc',