diff --git a/tests/unit/libstore-support/tests/test-data.hh b/tests/unit/libstore-support/tests/test-data.hh new file mode 100644 index 000000000..7aa1dd90d --- /dev/null +++ b/tests/unit/libstore-support/tests/test-data.hh @@ -0,0 +1,25 @@ +#pragma once + +#include "util.hh" +#include "types.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(); +} + +/** + * Resolve a path under the unit test data directory to an absolute path. + */ +static Path getUnitTestDataPath(std::string_view path) { + return absPath(getUnitTestData() + "/" + path); +} + +} diff --git a/tests/unit/libstore/test-data/machines.bad_format b/tests/unit/libstore/data/machines.bad_format similarity index 100% rename from tests/unit/libstore/test-data/machines.bad_format rename to tests/unit/libstore/data/machines.bad_format diff --git a/tests/unit/libstore/test-data/machines.valid b/tests/unit/libstore/data/machines.valid similarity index 100% rename from tests/unit/libstore/test-data/machines.valid rename to tests/unit/libstore/data/machines.valid diff --git a/tests/unit/libstore/machines.cc b/tests/unit/libstore/machines.cc index 702461926..0277237bb 100644 --- a/tests/unit/libstore/machines.cc +++ b/tests/unit/libstore/machines.cc @@ -1,5 +1,6 @@ #include "machines.hh" #include "globals.hh" +#include "tests/test-data.hh" #include @@ -17,7 +18,6 @@ using nix::getMachines; using nix::Machine; using nix::Machines; using nix::pathExists; -using nix::Settings; using nix::settings; class Environment : public ::testing::Environment { @@ -138,7 +138,7 @@ TEST(machines, getMachinesWithIncorrectFormat) { } TEST(machines, getMachinesWithCorrectFileReference) { - auto path = absPath("tests/unit/libstore/test-data/machines.valid"); + auto path = nix::getUnitTestDataPath("machines.valid"); ASSERT_TRUE(pathExists(path)); settings.builders = std::string("@") + path; @@ -165,6 +165,6 @@ TEST(machines, getMachinesWithIncorrectFileReference) { } TEST(machines, getMachinesWithCorrectFileReferenceToIncorrectFile) { - settings.builders = std::string("@") + absPath("tests/unit/libstore/test-data/machines.bad_format"); + settings.builders = std::string("@") + nix::getUnitTestDataPath("machines.bad_format"); EXPECT_THROW(getMachines(), FormatError); } diff --git a/tests/unit/meson.build b/tests/unit/meson.build index a05776eca..ae850df47 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -85,6 +85,7 @@ 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( @@ -142,7 +143,6 @@ test( '_NIX_TEST_UNIT_DATA': meson.project_source_root() / 'tests/unit/libstore/data', }, suite : 'check', - workdir : meson.project_source_root(), protocol : 'gtest', verbose : true, )