Skip to content

Commit f05d240

Browse files
authored
Merge pull request #14278 from obsidiansystems/adl-serializer-xp
Cleanup and JSON serializer and XP feature interations
2 parents ddf7de0 + 1177d65 commit f05d240

File tree

11 files changed

+166
-164
lines changed

11 files changed

+166
-164
lines changed

src/libstore-tests/derivation-advanced-attrs.cc

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace nix {
1616

17-
using nlohmann::json;
17+
using namespace nlohmann;
1818

1919
class DerivationAdvancedAttrsTest : public CharacterizationTest, public LibStoreTest
2020
{
@@ -51,44 +51,44 @@ using BothFixtures = ::testing::Types<DerivationAdvancedAttrsTest, CaDerivationA
5151

5252
TYPED_TEST_SUITE(DerivationAdvancedAttrsBothTest, BothFixtures);
5353

54-
#define TEST_ATERM_JSON(STEM, NAME) \
55-
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_from_json) \
56-
{ \
57-
this->readTest(NAME ".json", [&](const auto & encoded_) { \
58-
auto encoded = json::parse(encoded_); \
59-
/* Use DRV file instead of C++ literal as source of truth. */ \
60-
auto aterm = readFile(this->goldenMaster(NAME ".drv")); \
61-
auto expected = parseDerivation(*this->store, std::move(aterm), NAME, this->mockXpSettings); \
62-
Derivation got = Derivation::fromJSON(encoded, this->mockXpSettings); \
63-
EXPECT_EQ(got, expected); \
64-
}); \
65-
} \
66-
\
67-
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_to_json) \
68-
{ \
69-
this->writeTest( \
70-
NAME ".json", \
71-
[&]() -> json { \
72-
/* Use DRV file instead of C++ literal as source of truth. */ \
73-
auto aterm = readFile(this->goldenMaster(NAME ".drv")); \
74-
return parseDerivation(*this->store, std::move(aterm), NAME, this->mockXpSettings).toJSON(); \
75-
}, \
76-
[](const auto & file) { return json::parse(readFile(file)); }, \
77-
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
78-
} \
79-
\
80-
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_from_aterm) \
81-
{ \
82-
this->readTest(NAME ".drv", [&](auto encoded) { \
83-
/* Use JSON file instead of C++ literal as source of truth. */ \
84-
auto json = json::parse(readFile(this->goldenMaster(NAME ".json"))); \
85-
auto expected = Derivation::fromJSON(json, this->mockXpSettings); \
86-
auto got = parseDerivation(*this->store, std::move(encoded), NAME, this->mockXpSettings); \
87-
EXPECT_EQ(got.toJSON(), expected.toJSON()); \
88-
EXPECT_EQ(got, expected); \
89-
}); \
90-
} \
91-
\
54+
#define TEST_ATERM_JSON(STEM, NAME) \
55+
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_from_json) \
56+
{ \
57+
this->readTest(NAME ".json", [&](const auto & encoded_) { \
58+
auto encoded = json::parse(encoded_); \
59+
/* Use DRV file instead of C++ literal as source of truth. */ \
60+
auto aterm = readFile(this->goldenMaster(NAME ".drv")); \
61+
auto expected = parseDerivation(*this->store, std::move(aterm), NAME, this->mockXpSettings); \
62+
Derivation got = adl_serializer<Derivation>::from_json(encoded, this->mockXpSettings); \
63+
EXPECT_EQ(got, expected); \
64+
}); \
65+
} \
66+
\
67+
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_to_json) \
68+
{ \
69+
this->writeTest( \
70+
NAME ".json", \
71+
[&]() -> json { \
72+
/* Use DRV file instead of C++ literal as source of truth. */ \
73+
auto aterm = readFile(this->goldenMaster(NAME ".drv")); \
74+
return parseDerivation(*this->store, std::move(aterm), NAME, this->mockXpSettings); \
75+
}, \
76+
[](const auto & file) { return json::parse(readFile(file)); }, \
77+
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
78+
} \
79+
\
80+
TYPED_TEST(DerivationAdvancedAttrsBothTest, Derivation_##STEM##_from_aterm) \
81+
{ \
82+
this->readTest(NAME ".drv", [&](auto encoded) { \
83+
/* Use JSON file instead of C++ literal as source of truth. */ \
84+
auto j = json::parse(readFile(this->goldenMaster(NAME ".json"))); \
85+
auto expected = adl_serializer<Derivation>::from_json(j, this->mockXpSettings); \
86+
auto got = parseDerivation(*this->store, std::move(encoded), NAME, this->mockXpSettings); \
87+
EXPECT_EQ(static_cast<json>(got), static_cast<json>(expected)); \
88+
EXPECT_EQ(got, expected); \
89+
}); \
90+
} \
91+
\
9292
/* No corresponding write test, because we need to read the drv to write the json file */
9393

9494
TEST_ATERM_JSON(advancedAttributes, "advanced-attributes-defaults");

src/libstore-tests/derivation.cc

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,17 @@ TEST_F(DynDerivationTest, BadATerm_oldVersionDynDeps)
6666
FormatError);
6767
}
6868

69-
#define MAKE_OUTPUT_JSON_TEST_P(FIXTURE) \
70-
TEST_P(FIXTURE, from_json) \
71-
{ \
72-
const auto & [name, expected] = GetParam(); \
73-
/* Don't use readJsonTest because we want to check experimental \
74-
features. */ \
75-
readTest(Path{"output-"} + name + ".json", [&](const auto & encoded_) { \
76-
json j = json::parse(encoded_); \
77-
DerivationOutput got = DerivationOutput::fromJSON(j, mockXpSettings); \
78-
ASSERT_EQ(got, expected); \
79-
}); \
80-
} \
81-
\
82-
TEST_P(FIXTURE, to_json) \
83-
{ \
84-
const auto & [name, value] = GetParam(); \
85-
writeJsonTest("output-" + name, value); \
69+
#define MAKE_OUTPUT_JSON_TEST_P(FIXTURE) \
70+
TEST_P(FIXTURE, from_json) \
71+
{ \
72+
const auto & [name, expected] = GetParam(); \
73+
readJsonTest(Path{"output-"} + name, expected, mockXpSettings); \
74+
} \
75+
\
76+
TEST_P(FIXTURE, to_json) \
77+
{ \
78+
const auto & [name, value] = GetParam(); \
79+
writeJsonTest("output-" + name, value); \
8680
}
8781

8882
struct DerivationOutputJsonTest : DerivationTest,
@@ -193,13 +187,7 @@ INSTANTIATE_TEST_SUITE_P(
193187
TEST_P(FIXTURE, from_json) \
194188
{ \
195189
const auto & drv = GetParam(); \
196-
/* Don't use readJsonTest because we want to check experimental \
197-
features. */ \
198-
readTest(drv.name + ".json", [&](const auto & encoded_) { \
199-
auto encoded = json::parse(encoded_); \
200-
Derivation got = Derivation::fromJSON(encoded, mockXpSettings); \
201-
ASSERT_EQ(got, drv); \
202-
}); \
190+
readJsonTest(drv.name, drv, mockXpSettings); \
203191
} \
204192
\
205193
TEST_P(FIXTURE, to_json) \
@@ -213,7 +201,8 @@ INSTANTIATE_TEST_SUITE_P(
213201
const auto & drv = GetParam(); \
214202
readTest(drv.name + ".drv", [&](auto encoded) { \
215203
auto got = parseDerivation(*store, std::move(encoded), drv.name, mockXpSettings); \
216-
ASSERT_EQ(got.toJSON(), drv.toJSON()); \
204+
using nlohmann::json; \
205+
ASSERT_EQ(static_cast<json>(got), static_cast<json>(drv)); \
217206
ASSERT_EQ(got, drv); \
218207
}); \
219208
} \

src/libstore-tests/derived-path.cc

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#include <gtest/gtest.h>
44
#include <rapidcheck/gtest.h>
55

6-
#include "nix/util/tests/characterization.hh"
6+
#include "nix/util/tests/json-characterization.hh"
77
#include "nix/store/tests/derived-path.hh"
88
#include "nix/store/tests/libstore.hh"
99

1010
namespace nix {
1111

12-
class DerivedPathTest : public CharacterizationTest, public LibStoreTest
12+
class DerivedPathTest : public virtual CharacterizationTest, public LibStoreTest
1313
{
1414
std::filesystem::path unitTestData = getUnitTestData() / "derived-path";
1515

@@ -123,25 +123,51 @@ RC_GTEST_FIXTURE_PROP(DerivedPathTest, prop_round_rip, (const DerivedPath & o))
123123

124124
using nlohmann::json;
125125

126-
#define TEST_JSON(TYPE, NAME, VAL) \
127-
static const TYPE NAME = VAL; \
128-
\
129-
TEST_F(DerivedPathTest, NAME##_from_json) \
130-
{ \
131-
readTest(#NAME ".json", [&](const auto & encoded_) { \
132-
auto encoded = json::parse(encoded_); \
133-
TYPE got = static_cast<TYPE>(encoded); \
134-
ASSERT_EQ(got, NAME); \
135-
}); \
136-
} \
137-
\
138-
TEST_F(DerivedPathTest, NAME##_to_json) \
139-
{ \
140-
writeTest( \
141-
#NAME ".json", \
142-
[&]() -> json { return static_cast<json>(NAME); }, \
143-
[](const auto & file) { return json::parse(readFile(file)); }, \
144-
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
126+
struct SingleDerivedPathJsonTest : DerivedPathTest,
127+
JsonCharacterizationTest<SingleDerivedPath>,
128+
::testing::WithParamInterface<SingleDerivedPath>
129+
{};
130+
131+
struct DerivedPathJsonTest : DerivedPathTest,
132+
JsonCharacterizationTest<DerivedPath>,
133+
::testing::WithParamInterface<DerivedPath>
134+
{};
135+
136+
#define TEST_JSON(TYPE, NAME, VAL) \
137+
static const TYPE NAME = VAL; \
138+
\
139+
TEST_F(TYPE##JsonTest, NAME##_from_json) \
140+
{ \
141+
readJsonTest(#NAME, NAME); \
142+
} \
143+
\
144+
TEST_F(TYPE##JsonTest, NAME##_to_json) \
145+
{ \
146+
writeJsonTest(#NAME, NAME); \
147+
}
148+
149+
#define TEST_JSON_XP_DYN(TYPE, NAME, VAL) \
150+
static const TYPE NAME = VAL; \
151+
\
152+
TEST_F(TYPE##JsonTest, NAME##_from_json_throws_without_xp) \
153+
{ \
154+
std::optional<json> ret; \
155+
readTest(#NAME ".json", [&](const auto & encoded_) { ret = json::parse(encoded_); }); \
156+
if (ret) { \
157+
EXPECT_THROW(nlohmann::adl_serializer<TYPE>::from_json(*ret), MissingExperimentalFeature); \
158+
} \
159+
} \
160+
\
161+
TEST_F(TYPE##JsonTest, NAME##_from_json) \
162+
{ \
163+
ExperimentalFeatureSettings xpSettings; \
164+
xpSettings.set("experimental-features", "dynamic-derivations"); \
165+
readJsonTest(#NAME, NAME, xpSettings); \
166+
} \
167+
\
168+
TEST_F(TYPE##JsonTest, NAME##_to_json) \
169+
{ \
170+
writeJsonTest(#NAME, NAME); \
145171
}
146172

147173
TEST_JSON(
@@ -156,7 +182,7 @@ TEST_JSON(
156182
.output = "bar",
157183
}));
158184

159-
TEST_JSON(
185+
TEST_JSON_XP_DYN(
160186
SingleDerivedPath,
161187
single_built_built,
162188
(SingleDerivedPath::Built{
@@ -179,7 +205,7 @@ TEST_JSON(
179205
.outputs = OutputsSpec::Names{"bar", "baz"},
180206
}));
181207

182-
TEST_JSON(
208+
TEST_JSON_XP_DYN(
183209
DerivedPath,
184210
multi_built_built,
185211
(DerivedPath::Built{
@@ -191,7 +217,7 @@ TEST_JSON(
191217
.outputs = OutputsSpec::Names{"baz", "quux"},
192218
}));
193219

194-
TEST_JSON(
220+
TEST_JSON_XP_DYN(
195221
DerivedPath,
196222
multi_built_built_wildcard,
197223
(DerivedPath::Built{

0 commit comments

Comments
 (0)