diff --git a/tests/unit/libutil-support/tests/characterization.hh b/tests/unit/libutil-support/tests/characterization.hh index db82476da..2bf606bd8 100644 --- a/tests/unit/libutil-support/tests/characterization.hh +++ b/tests/unit/libutil-support/tests/characterization.hh @@ -9,17 +9,10 @@ #include #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 diff --git a/tests/unit/libutil-support/tests/test-data.cc b/tests/unit/libutil-support/tests/test-data.cc new file mode 100644 index 000000000..016bd5e78 --- /dev/null +++ b/tests/unit/libutil-support/tests/test-data.cc @@ -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); +} + +} diff --git a/tests/unit/libstore-support/tests/test-data.hh b/tests/unit/libutil-support/tests/test-data.hh similarity index 53% rename from tests/unit/libstore-support/tests/test-data.hh rename to tests/unit/libutil-support/tests/test-data.hh index 1fec6f912..794ec6265 100644 --- a/tests/unit/libstore-support/tests/test-data.hh +++ b/tests/unit/libutil-support/tests/test-data.hh @@ -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); } diff --git a/tests/unit/libutil/config.cc b/tests/unit/libutil/config.cc index 1629969ba..1600f4ff0 100644 --- a/tests/unit/libutil/config.cc +++ b/tests/unit/libutil/config.cc @@ -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 #include @@ -287,6 +291,35 @@ namespace nix { ), Error); } + TEST(Config, includeRelativePath) { + Config config; + Setting setting{&config, "", "puppy", "description"}; + + config.applyConfig("include puppy.conf", { + .path = getUnitTestDataPath("nix.conf") + }); + + std::map settings; + config.getSettings(settings); + ASSERT_FALSE(settings.empty()); + ASSERT_EQ(settings["puppy"].value, "doggy"); + } + + TEST(Config, includeTildePath) { + Config config; + Setting setting{&config, "", "puppy", "description"}; + + config.applyConfig("include ~/puppy.conf", { + .path = "/doesnt-exist", + .home = getUnitTestData() + }); + + std::map 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); diff --git a/tests/unit/libutil/data/puppy.conf b/tests/unit/libutil/data/puppy.conf new file mode 100644 index 000000000..805e484ef --- /dev/null +++ b/tests/unit/libutil/data/puppy.conf @@ -0,0 +1 @@ +puppy = doggy diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 55c7566bd..8ff0b5ec5 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -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(