1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-14 14:32:42 +01:00

Merge pull request #14057 from obsidiansystems/derived-path-json

Modernize and test derived path JSON
This commit is contained in:
John Ericson 2025-09-24 18:08:06 -04:00 committed by GitHub
commit 9f26c20ebd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 300 additions and 69 deletions

View file

@ -0,0 +1,10 @@
{
"drvPath": {
"drvPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv",
"output": "bar"
},
"outputs": [
"baz",
"quux"
]
}

View file

@ -0,0 +1,9 @@
{
"drvPath": {
"drvPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv",
"output": "bar"
},
"outputs": [
"*"
]
}

View file

@ -0,0 +1 @@
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"

View file

@ -0,0 +1,7 @@
{
"drvPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv",
"outputs": [
"bar",
"baz"
]
}

View file

@ -0,0 +1,4 @@
{
"drvPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv",
"output": "bar"
}

View file

@ -0,0 +1,7 @@
{
"drvPath": {
"drvPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv",
"output": "bar"
},
"output": "baz"
}

View file

@ -0,0 +1 @@
"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"

View file

@ -3,13 +3,23 @@
#include <gtest/gtest.h>
#include <rapidcheck/gtest.h>
#include "nix/util/tests/characterization.hh"
#include "nix/store/tests/derived-path.hh"
#include "nix/store/tests/libstore.hh"
namespace nix {
class DerivedPathTest : public LibStoreTest
{};
class DerivedPathTest : public CharacterizationTest, public LibStoreTest
{
std::filesystem::path unitTestData = getUnitTestData() / "derived-path";
public:
std::filesystem::path goldenMaster(std::string_view testStem) const override
{
return unitTestData / testStem;
}
};
/**
* Round trip (string <-> data structure) test for
@ -107,4 +117,90 @@ RC_GTEST_FIXTURE_PROP(DerivedPathTest, prop_round_rip, (const DerivedPath & o))
#endif
/* ----------------------------------------------------------------------------
* JSON
* --------------------------------------------------------------------------*/
using nlohmann::json;
#define TEST_JSON(TYPE, NAME, VAL) \
static const TYPE NAME = VAL; \
\
TEST_F(DerivedPathTest, NAME##_from_json) \
{ \
readTest(#NAME ".json", [&](const auto & encoded_) { \
auto encoded = json::parse(encoded_); \
TYPE got = static_cast<TYPE>(encoded); \
ASSERT_EQ(got, NAME); \
}); \
} \
\
TEST_F(DerivedPathTest, NAME##_to_json) \
{ \
writeTest( \
#NAME ".json", \
[&]() -> json { return static_cast<json>(NAME); }, \
[](const auto & file) { return json::parse(readFile(file)); }, \
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
}
TEST_JSON(
SingleDerivedPath, single_opaque, SingleDerivedPath::Opaque{StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}});
TEST_JSON(
SingleDerivedPath,
single_built,
(SingleDerivedPath::Built{
.drvPath = make_ref<const SingleDerivedPath>(SingleDerivedPath::Opaque{
StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}}),
.output = "bar",
}));
TEST_JSON(
SingleDerivedPath,
single_built_built,
(SingleDerivedPath::Built{
.drvPath = make_ref<const SingleDerivedPath>(SingleDerivedPath::Built{
.drvPath = make_ref<const SingleDerivedPath>(SingleDerivedPath::Opaque{
StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}}),
.output = "bar",
}),
.output = "baz",
}));
TEST_JSON(DerivedPath, multi_opaque, DerivedPath::Opaque{StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}});
TEST_JSON(
DerivedPath,
mutli_built,
(DerivedPath::Built{
.drvPath = make_ref<const SingleDerivedPath>(SingleDerivedPath::Opaque{
StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}}),
.outputs = OutputsSpec::Names{"bar", "baz"},
}));
TEST_JSON(
DerivedPath,
multi_built_built,
(DerivedPath::Built{
.drvPath = make_ref<const SingleDerivedPath>(SingleDerivedPath::Built{
.drvPath = make_ref<const SingleDerivedPath>(SingleDerivedPath::Opaque{
StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}}),
.output = "bar",
}),
.outputs = OutputsSpec::Names{"baz", "quux"},
}));
TEST_JSON(
DerivedPath,
multi_built_built_wildcard,
(DerivedPath::Built{
.drvPath = make_ref<const SingleDerivedPath>(SingleDerivedPath::Built{
.drvPath = make_ref<const SingleDerivedPath>(SingleDerivedPath::Opaque{
StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"}}),
.output = "bar",
}),
.outputs = OutputsSpec::All{},
}));
} // namespace nix