diff options
-rw-r--r-- | src/libutil/config.cc | 28 | ||||
-rw-r--r-- | src/libutil/config.hh | 20 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-eol-1.err.exp (renamed from tests/functional/lang/eval-fail-eol-1.err.exp) | 2 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-eol-1.nix (renamed from tests/functional/lang/eval-fail-eol-1.nix) | 0 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-eol-2.err.exp (renamed from tests/functional/lang/eval-fail-eol-3.err.exp) | 2 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-eol-2.nix (renamed from tests/functional/lang/eval-fail-eol-2.nix) | 0 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-eol-3.err.exp (renamed from tests/functional/lang/eval-fail-eol-2.err.exp) | 2 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-eol-3.nix (renamed from tests/functional/lang/eval-fail-eol-3.nix) | 0 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-path-slash.err.exp (renamed from tests/functional/lang/eval-fail-path-slash.err.exp) | 2 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-path-slash.nix (renamed from tests/functional/lang/eval-fail-path-slash.nix) | 0 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-set.err.exp (renamed from tests/functional/lang/eval-fail-set.err.exp) | 2 | ||||
-rw-r--r-- | tests/functional/lang/parse-fail-set.nix (renamed from tests/functional/lang/eval-fail-set.nix) | 0 | ||||
-rw-r--r-- | tests/unit/libutil/paths-setting.cc | 100 | ||||
-rw-r--r-- | tests/unit/meson.build | 1 |
14 files changed, 154 insertions, 5 deletions
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<Path> & v) this->assign(v); } +PathsSetting::PathsSetting(Config * options, + const Paths & def, + const std::string & name, + const std::string & description, + const std::set<std::string> & aliases) + : BaseSetting<Paths>(def, true, name, description, aliases) +{ + options->addSetting(this); +} + + +Paths PathsSetting::parse(const std::string & str) const +{ + auto strings = tokenizeString<Strings>(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<Path> & v); }; +/** + * Like `OptionalPathSetting`, but allows multiple paths. + */ +class PathsSetting : public BaseSetting<Paths> +{ +public: + + PathsSetting(Config * options, + const Paths & def, + const std::string & name, + const std::string & description, + const std::set<std::string> & 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<Config*> ConfigRegistrations; diff --git a/tests/functional/lang/eval-fail-eol-1.err.exp b/tests/functional/lang/parse-fail-eol-1.err.exp index 3f5a5c22c..f7f79b460 100644 --- a/tests/functional/lang/eval-fail-eol-1.err.exp +++ b/tests/functional/lang/parse-fail-eol-1.err.exp @@ -1,5 +1,5 @@ error: undefined variable 'invalid' - at /pwd/lang/eval-fail-eol-1.nix:2:1: + at «stdin»:2:1: 1| # foo 2| invalid | ^ diff --git a/tests/functional/lang/eval-fail-eol-1.nix b/tests/functional/lang/parse-fail-eol-1.nix index 476223919..476223919 100644 --- a/tests/functional/lang/eval-fail-eol-1.nix +++ b/tests/functional/lang/parse-fail-eol-1.nix diff --git a/tests/functional/lang/eval-fail-eol-3.err.exp b/tests/functional/lang/parse-fail-eol-2.err.exp index ada3c5ecd..f7f79b460 100644 --- a/tests/functional/lang/eval-fail-eol-3.err.exp +++ b/tests/functional/lang/parse-fail-eol-2.err.exp @@ -1,5 +1,5 @@ error: undefined variable 'invalid' - at /pwd/lang/eval-fail-eol-3.nix:2:1: + at «stdin»:2:1: 1| # foo 2| invalid | ^ diff --git a/tests/functional/lang/eval-fail-eol-2.nix b/tests/functional/lang/parse-fail-eol-2.nix index 0cf92a425..0cf92a425 100644 --- a/tests/functional/lang/eval-fail-eol-2.nix +++ b/tests/functional/lang/parse-fail-eol-2.nix diff --git a/tests/functional/lang/eval-fail-eol-2.err.exp b/tests/functional/lang/parse-fail-eol-3.err.exp index ff13e2d55..f7f79b460 100644 --- a/tests/functional/lang/eval-fail-eol-2.err.exp +++ b/tests/functional/lang/parse-fail-eol-3.err.exp @@ -1,5 +1,5 @@ error: undefined variable 'invalid' - at /pwd/lang/eval-fail-eol-2.nix:2:1: + at «stdin»:2:1: 1| # foo 2| invalid | ^ diff --git a/tests/functional/lang/eval-fail-eol-3.nix b/tests/functional/lang/parse-fail-eol-3.nix index 33422452d..33422452d 100644 --- a/tests/functional/lang/eval-fail-eol-3.nix +++ b/tests/functional/lang/parse-fail-eol-3.nix diff --git a/tests/functional/lang/eval-fail-path-slash.err.exp b/tests/functional/lang/parse-fail-path-slash.err.exp index e3531d352..0407543a1 100644 --- a/tests/functional/lang/eval-fail-path-slash.err.exp +++ b/tests/functional/lang/parse-fail-path-slash.err.exp @@ -1,5 +1,5 @@ error: path has a trailing slash - at /pwd/lang/eval-fail-path-slash.nix:6:12: + at «stdin»:6:12: 5| # and https://nixos.org/nix-dev/2016-June/020829.html 6| /nix/store/ | ^ diff --git a/tests/functional/lang/eval-fail-path-slash.nix b/tests/functional/lang/parse-fail-path-slash.nix index 8c2e104c7..8c2e104c7 100644 --- a/tests/functional/lang/eval-fail-path-slash.nix +++ b/tests/functional/lang/parse-fail-path-slash.nix diff --git a/tests/functional/lang/eval-fail-set.err.exp b/tests/functional/lang/parse-fail-set.err.exp index 6dd646e11..2f77406a7 100644 --- a/tests/functional/lang/eval-fail-set.err.exp +++ b/tests/functional/lang/parse-fail-set.err.exp @@ -1,5 +1,5 @@ error: undefined variable 'x' - at /pwd/lang/eval-fail-set.nix:1:3: + at «stdin»:1:3: 1| 8.x | ^ 2| diff --git a/tests/functional/lang/eval-fail-set.nix b/tests/functional/lang/parse-fail-set.nix index c6b7980b6..c6b7980b6 100644 --- a/tests/functional/lang/eval-fail-set.nix +++ b/tests/functional/lang/parse-fail-set.nix diff --git a/tests/unit/libutil/paths-setting.cc b/tests/unit/libutil/paths-setting.cc new file mode 100644 index 000000000..b450b0cf9 --- /dev/null +++ b/tests/unit/libutil/paths-setting.cc @@ -0,0 +1,100 @@ +#include "config.hh" + +#include <gtest/gtest.h> +#include <gmock/gmock.h> +#include <sstream> + +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<Paths>({"/puppy.nix"}) + ); + + // Splits on whitespace: + ASSERT_THAT( + 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"), + 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, 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); +} + +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"}) + ); + + // 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"}) + ); +} + +} // 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', |