Test including relative paths in configuration

Change-Id: If6c69a5e16d1ccd223fba392890f08f0032fb754
This commit is contained in:
Rebecca Turner 2024-08-26 09:28:14 -07:00
parent 02eb07cfd5
commit 75c0de3e3c
No known key found for this signature in database
6 changed files with 56 additions and 18 deletions

View file

@ -9,17 +9,10 @@
#include <filesystem>
#include "types.hh"
#include "test-data.hh"
namespace nix {
/**
* The path to the unit test data directory. See the contributing guide
* in the manual for further details.
*/
static Path getUnitTestData() {
return getEnv("_NIX_TEST_UNIT_DATA").value();
}
/**
* Whether we should update "golden masters" instead of running tests
* against them. See the contributing guide in the manual for further

View file

@ -0,0 +1,16 @@
#include "test-data.hh"
#include "strings.hh"
namespace nix {
Path getUnitTestData()
{
return getEnv("_NIX_TEST_UNIT_DATA").value();
}
Path getUnitTestDataPath(std::string_view path)
{
return absPath(getUnitTestData() + "/" + path);
}
}

View file

@ -1,25 +1,20 @@
#pragma once
#include "environment-variables.hh"
#include "types.hh"
#include "environment-variables.hh"
#include "file-system.hh"
namespace nix {
// TODO: These helpers should be available in all unit tests.
/**
* The path to the unit test data directory. See the contributing guide
* in the manual for further details.
*/
static Path getUnitTestData() {
return getEnv("_NIX_TEST_UNIT_DATA").value();
}
Path getUnitTestData();
/**
* Resolve a path under the unit test data directory to an absolute path.
*/
static Path getUnitTestDataPath(std::string_view path) {
return absPath(getUnitTestData() + "/" + path);
}
Path getUnitTestDataPath(std::string_view path);
}

View file

@ -1,5 +1,9 @@
#include "config.hh"
#include "args.hh"
#include "file-system.hh"
#include "environment-variables.hh"
#include "logging.hh"
#include "tests/test-data.hh"
#include <sstream>
#include <gtest/gtest.h>
@ -287,6 +291,35 @@ namespace nix {
), Error);
}
TEST(Config, includeRelativePath) {
Config config;
Setting<std::string> setting{&config, "", "puppy", "description"};
config.applyConfig("include puppy.conf", {
.path = getUnitTestDataPath("nix.conf")
});
std::map<std::string, Config::SettingInfo> settings;
config.getSettings(settings);
ASSERT_FALSE(settings.empty());
ASSERT_EQ(settings["puppy"].value, "doggy");
}
TEST(Config, includeTildePath) {
Config config;
Setting<std::string> setting{&config, "", "puppy", "description"};
config.applyConfig("include ~/puppy.conf", {
.path = "/doesnt-exist",
.home = getUnitTestData()
});
std::map<std::string, Config::SettingInfo> settings;
config.getSettings(settings);
ASSERT_FALSE(settings.empty());
ASSERT_EQ(settings["puppy"].value, "doggy");
}
TEST(Config, applyConfigInvalidThrows) {
Config config;
ASSERT_THROW(config.applyConfig("value == key"), UsageError);

View file

@ -0,0 +1 @@
puppy = doggy

View file

@ -19,6 +19,7 @@ libutil_test_support_sources = files(
'libutil-support/tests/cli-literate-parser.cc',
'libutil-support/tests/hash.cc',
'libutil-support/tests/terminal-code-eater.cc',
'libutil-support/tests/test-data.cc',
)
libutil_test_support = library(
'lixutil-test-support',
@ -95,7 +96,6 @@ libstore_test_support_sources = files(
'libstore-support/tests/derived-path.cc',
'libstore-support/tests/outputs-spec.cc',
'libstore-support/tests/path.cc',
'libstore-support/tests/test-data.hh',
)
libstore_test_support = library(