lix/tests/unit/libutil/paths-setting.cc
Alois Wohlschlager e3c289dbe9
libutil/config: unify path setting types
There have been multiple setting types for paths that are supposed to be
canonicalised, depending on whether zero or one, one, or any number of paths is
to be specified. Naturally, they behaved in slightly different ways in the
code. Simplify things by unifying them and removing special behaviour (mainly
the "multiple paths type can coerce to boolean" thing).

Change-Id: I7c1ce95e9c8e1829a866fb37d679e167811e9705
2024-08-21 17:57:23 +02:00

86 lines
1.8 KiB
C++

#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> 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, 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