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:
parent
a2c6f38e1f
commit
1177d65094
3 changed files with 69 additions and 36 deletions
|
|
@ -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;
|
||||||
|
|
||||||
|
struct SingleDerivedPathJsonTest : DerivedPathTest,
|
||||||
|
JsonCharacterizationTest<SingleDerivedPath>,
|
||||||
|
::testing::WithParamInterface<SingleDerivedPath>
|
||||||
|
{};
|
||||||
|
|
||||||
|
struct DerivedPathJsonTest : DerivedPathTest,
|
||||||
|
JsonCharacterizationTest<DerivedPath>,
|
||||||
|
::testing::WithParamInterface<DerivedPath>
|
||||||
|
{};
|
||||||
|
|
||||||
#define TEST_JSON(TYPE, NAME, VAL) \
|
#define TEST_JSON(TYPE, NAME, VAL) \
|
||||||
static const TYPE NAME = VAL; \
|
static const TYPE NAME = VAL; \
|
||||||
\
|
\
|
||||||
TEST_F(DerivedPathTest, NAME##_from_json) \
|
TEST_F(TYPE##JsonTest, NAME##_from_json) \
|
||||||
{ \
|
{ \
|
||||||
readTest(#NAME ".json", [&](const auto & encoded_) { \
|
readJsonTest(#NAME, NAME); \
|
||||||
auto encoded = json::parse(encoded_); \
|
|
||||||
TYPE got = static_cast<TYPE>(encoded); \
|
|
||||||
ASSERT_EQ(got, NAME); \
|
|
||||||
}); \
|
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
TEST_F(DerivedPathTest, NAME##_to_json) \
|
TEST_F(TYPE##JsonTest, NAME##_to_json) \
|
||||||
{ \
|
{ \
|
||||||
writeTest( \
|
writeJsonTest(#NAME, NAME); \
|
||||||
#NAME ".json", \
|
}
|
||||||
[&]() -> json { return static_cast<json>(NAME); }, \
|
|
||||||
[](const auto & file) { return json::parse(readFile(file)); }, \
|
#define TEST_JSON_XP_DYN(TYPE, NAME, VAL) \
|
||||||
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
|
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{
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue