From 75c0de3e3cbd972328c7d0c6fdea79abbfa204df Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Mon, 26 Aug 2024 09:28:14 -0700 Subject: [PATCH] Test including relative paths in configuration Change-Id: If6c69a5e16d1ccd223fba392890f08f0032fb754 --- .../libutil-support/tests/characterization.hh | 9 +---- tests/unit/libutil-support/tests/test-data.cc | 16 +++++++++ .../tests/test-data.hh | 13 +++----- tests/unit/libutil/config.cc | 33 +++++++++++++++++++ tests/unit/libutil/data/puppy.conf | 1 + tests/unit/meson.build | 2 +- 6 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 tests/unit/libutil-support/tests/test-data.cc rename tests/unit/{libstore-support => libutil-support}/tests/test-data.hh (53%) create mode 100644 tests/unit/libutil/data/puppy.conf 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(