From 12f2565f91a82ec3c99ee7a422f69efaf284353d Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Mon, 8 Apr 2024 12:43:35 -0700 Subject: [PATCH] Don't run libstore unit tests in project root This keeps the libstore unit tests from writing `libstore-unit-tests.xml` to the project root. Change-Id: I0d9909aabf9f3574cc1e72a5ae81daefba9a394b --- .../unit/libstore-support/tests/test-data.hh | 25 +++++++++++++++++++ .../{test-data => data}/machines.bad_format | 0 .../{test-data => data}/machines.valid | 0 tests/unit/libstore/machines.cc | 6 ++--- tests/unit/meson.build | 2 +- 5 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 tests/unit/libstore-support/tests/test-data.hh rename tests/unit/libstore/{test-data => data}/machines.bad_format (100%) rename tests/unit/libstore/{test-data => data}/machines.valid (100%) 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, )