Merge "Add PathsSetting
" into main
This commit is contained in:
commit
8ed8b9aef0
4 changed files with 149 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
|
100
tests/unit/libutil/paths-setting.cc
Normal file
100
tests/unit/libutil/paths-setting.cc
Normal file
|
@ -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
|
|
@ -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',
|
||||
|
|
Loading…
Reference in a new issue