diff options
author | rebecca “wiggles” turner <rbt@sent.as> | 2024-04-08 06:31:11 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@lix> | 2024-04-08 06:31:11 +0000 |
commit | 8ed8b9aef08949105fd7a2c4ded71d005e35cec7 (patch) | |
tree | 62f43e2cdc6d28b977761a93842fbdf3a9b6460e /tests/unit | |
parent | b995c17f0eb8d9598f339c080c467101c1f55feb (diff) | |
parent | 6f863e8ccd44342e7650f612b46893b605755000 (diff) |
Merge "Add `PathsSetting`" into main
Diffstat (limited to 'tests/unit')
-rw-r--r-- | tests/unit/libutil/paths-setting.cc | 100 | ||||
-rw-r--r-- | tests/unit/meson.build | 1 |
2 files changed, 101 insertions, 0 deletions
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', |