1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 03:56:01 +01:00

Properly check xp features when deserializing deriving paths

This commit is contained in:
John Ericson 2025-10-16 16:44:27 -04:00
parent a2c6f38e1f
commit 1177d65094
3 changed files with 69 additions and 36 deletions

View file

@ -3,13 +3,13 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <rapidcheck/gtest.h> #include <rapidcheck/gtest.h>
#include "nix/util/tests/characterization.hh" #include "nix/util/tests/json-characterization.hh"
#include "nix/store/tests/derived-path.hh" #include "nix/store/tests/derived-path.hh"
#include "nix/store/tests/libstore.hh" #include "nix/store/tests/libstore.hh"
namespace nix { namespace nix {
class DerivedPathTest : public CharacterizationTest, public LibStoreTest class DerivedPathTest : public virtual CharacterizationTest, public LibStoreTest
{ {
std::filesystem::path unitTestData = getUnitTestData() / "derived-path"; std::filesystem::path unitTestData = getUnitTestData() / "derived-path";
@ -123,25 +123,51 @@ RC_GTEST_FIXTURE_PROP(DerivedPathTest, prop_round_rip, (const DerivedPath & o))
using nlohmann::json; using nlohmann::json;
#define TEST_JSON(TYPE, NAME, VAL) \ struct SingleDerivedPathJsonTest : DerivedPathTest,
static const TYPE NAME = VAL; \ JsonCharacterizationTest<SingleDerivedPath>,
\ ::testing::WithParamInterface<SingleDerivedPath>
TEST_F(DerivedPathTest, NAME##_from_json) \ {};
{ \
readTest(#NAME ".json", [&](const auto & encoded_) { \ struct DerivedPathJsonTest : DerivedPathTest,
auto encoded = json::parse(encoded_); \ JsonCharacterizationTest<DerivedPath>,
TYPE got = static_cast<TYPE>(encoded); \ ::testing::WithParamInterface<DerivedPath>
ASSERT_EQ(got, NAME); \ {};
}); \
} \ #define TEST_JSON(TYPE, NAME, VAL) \
\ static const TYPE NAME = VAL; \
TEST_F(DerivedPathTest, NAME##_to_json) \ \
{ \ TEST_F(TYPE##JsonTest, NAME##_from_json) \
writeTest( \ { \
#NAME ".json", \ readJsonTest(#NAME, NAME); \
[&]() -> 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_F(TYPE##JsonTest, NAME##_to_json) \
{ \
writeJsonTest(#NAME, NAME); \
}
#define TEST_JSON_XP_DYN(TYPE, NAME, VAL) \
static const TYPE NAME = VAL; \
\
TEST_F(TYPE##JsonTest, NAME##_from_json_throws_without_xp) \
{ \
std::optional<json> ret; \
readTest(#NAME ".json", [&](const auto & encoded_) { ret = json::parse(encoded_); }); \
if (ret) { \
EXPECT_THROW(nlohmann::adl_serializer<TYPE>::from_json(*ret), MissingExperimentalFeature); \
} \
} \
\
TEST_F(TYPE##JsonTest, NAME##_from_json) \
{ \
ExperimentalFeatureSettings xpSettings; \
xpSettings.set("experimental-features", "dynamic-derivations"); \
readJsonTest(#NAME, NAME, xpSettings); \
} \
\
TEST_F(TYPE##JsonTest, NAME##_to_json) \
{ \
writeJsonTest(#NAME, NAME); \
} }
TEST_JSON( TEST_JSON(
@ -156,7 +182,7 @@ TEST_JSON(
.output = "bar", .output = "bar",
})); }));
TEST_JSON( TEST_JSON_XP_DYN(
SingleDerivedPath, SingleDerivedPath,
single_built_built, single_built_built,
(SingleDerivedPath::Built{ (SingleDerivedPath::Built{
@ -179,7 +205,7 @@ TEST_JSON(
.outputs = OutputsSpec::Names{"bar", "baz"}, .outputs = OutputsSpec::Names{"bar", "baz"},
})); }));
TEST_JSON( TEST_JSON_XP_DYN(
DerivedPath, DerivedPath,
multi_built_built, multi_built_built,
(DerivedPath::Built{ (DerivedPath::Built{
@ -191,7 +217,7 @@ TEST_JSON(
.outputs = OutputsSpec::Names{"baz", "quux"}, .outputs = OutputsSpec::Names{"baz", "quux"},
})); }));
TEST_JSON( TEST_JSON_XP_DYN(
DerivedPath, DerivedPath,
multi_built_built_wildcard, multi_built_built_wildcard,
(DerivedPath::Built{ (DerivedPath::Built{

View file

@ -252,20 +252,26 @@ void adl_serializer<DerivedPath::Built>::to_json(json & json, const DerivedPath:
}; };
} }
SingleDerivedPath::Built adl_serializer<SingleDerivedPath::Built>::from_json(const json & json0) SingleDerivedPath::Built
adl_serializer<SingleDerivedPath::Built>::from_json(const json & json0, const ExperimentalFeatureSettings & xpSettings)
{ {
auto & json = getObject(json0); auto & json = getObject(json0);
auto drvPath = make_ref<SingleDerivedPath>(static_cast<SingleDerivedPath>(valueAt(json, "drvPath")));
drvRequireExperiment(*drvPath, xpSettings);
return { return {
.drvPath = make_ref<SingleDerivedPath>(static_cast<SingleDerivedPath>(valueAt(json, "drvPath"))), .drvPath = std::move(drvPath),
.output = getString(valueAt(json, "output")), .output = getString(valueAt(json, "output")),
}; };
} }
DerivedPath::Built adl_serializer<DerivedPath::Built>::from_json(const json & json0) DerivedPath::Built
adl_serializer<DerivedPath::Built>::from_json(const json & json0, const ExperimentalFeatureSettings & xpSettings)
{ {
auto & json = getObject(json0); auto & json = getObject(json0);
auto drvPath = make_ref<SingleDerivedPath>(static_cast<SingleDerivedPath>(valueAt(json, "drvPath")));
drvRequireExperiment(*drvPath, xpSettings);
return { return {
.drvPath = make_ref<SingleDerivedPath>(static_cast<SingleDerivedPath>(valueAt(json, "drvPath"))), .drvPath = std::move(drvPath),
.outputs = adl_serializer<OutputsSpec>::from_json(valueAt(json, "outputs")), .outputs = adl_serializer<OutputsSpec>::from_json(valueAt(json, "outputs")),
}; };
} }
@ -280,20 +286,21 @@ void adl_serializer<DerivedPath>::to_json(json & json, const DerivedPath & sdp)
std::visit([&](const auto & buildable) { json = buildable; }, sdp.raw()); std::visit([&](const auto & buildable) { json = buildable; }, sdp.raw());
} }
SingleDerivedPath adl_serializer<SingleDerivedPath>::from_json(const json & json) SingleDerivedPath
adl_serializer<SingleDerivedPath>::from_json(const json & json, const ExperimentalFeatureSettings & xpSettings)
{ {
if (json.is_string()) if (json.is_string())
return static_cast<SingleDerivedPath::Opaque>(json); return static_cast<SingleDerivedPath::Opaque>(json);
else else
return static_cast<SingleDerivedPath::Built>(json); return adl_serializer<SingleDerivedPath::Built>::from_json(json, xpSettings);
} }
DerivedPath adl_serializer<DerivedPath>::from_json(const json & json) DerivedPath adl_serializer<DerivedPath>::from_json(const json & json, const ExperimentalFeatureSettings & xpSettings)
{ {
if (json.is_string()) if (json.is_string())
return static_cast<DerivedPath::Opaque>(json); return static_cast<DerivedPath::Opaque>(json);
else else
return static_cast<DerivedPath::Built>(json); return adl_serializer<DerivedPath::Built>::from_json(json, xpSettings);
} }
} // namespace nlohmann } // namespace nlohmann

View file

@ -299,7 +299,7 @@ void drvRequireExperiment(
} // namespace nix } // namespace nix
JSON_IMPL(nix::SingleDerivedPath::Opaque) JSON_IMPL(nix::SingleDerivedPath::Opaque)
JSON_IMPL(nix::SingleDerivedPath::Built) JSON_IMPL_WITH_XP_FEATURES(nix::SingleDerivedPath::Built)
JSON_IMPL(nix::SingleDerivedPath) JSON_IMPL_WITH_XP_FEATURES(nix::SingleDerivedPath)
JSON_IMPL(nix::DerivedPath::Built) JSON_IMPL_WITH_XP_FEATURES(nix::DerivedPath::Built)
JSON_IMPL(nix::DerivedPath) JSON_IMPL_WITH_XP_FEATURES(nix::DerivedPath)