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

Minimize the use of C Macros for characterization tests

Fewer macros is better!

Introduce a new `JsonChacterizationTest` mixin class to help with this.

Also, avoid some needless copies with `GetParam`.

Part of my effort shoring up the JSON formats with #13570.
This commit is contained in:
John Ericson 2025-09-24 11:55:21 -04:00
parent 3bf1268ac6
commit 01b2037bc0
15 changed files with 364 additions and 252 deletions

View file

@ -5,13 +5,13 @@
#include "nix/store/derivations.hh"
#include "nix/store/tests/libstore.hh"
#include "nix/util/tests/characterization.hh"
#include "nix/util/tests/json-characterization.hh"
namespace nix {
using nlohmann::json;
class DerivationTest : public CharacterizationTest, public LibStoreTest
class DerivationTest : public virtual CharacterizationTest, public LibStoreTest
{
std::filesystem::path unitTestData = getUnitTestData() / "derivation";
@ -66,146 +66,183 @@ TEST_F(DynDerivationTest, BadATerm_oldVersionDynDeps)
FormatError);
}
#define TEST_JSON(FIXTURE, NAME, VAL, DRV_NAME, OUTPUT_NAME) \
TEST_F(FIXTURE, DerivationOutput_##NAME##_from_json) \
{ \
readTest("output-" #NAME ".json", [&](const auto & encoded_) { \
auto encoded = json::parse(encoded_); \
DerivationOutput got = DerivationOutput::fromJSON(DRV_NAME, OUTPUT_NAME, encoded, mockXpSettings); \
DerivationOutput expected{VAL}; \
ASSERT_EQ(got, expected); \
}); \
} \
\
TEST_F(FIXTURE, DerivationOutput_##NAME##_to_json) \
{ \
writeTest( \
"output-" #NAME ".json", \
[&]() -> json { return DerivationOutput{(VAL)}.toJSON((DRV_NAME), (OUTPUT_NAME)); }, \
[](const auto & file) { return json::parse(readFile(file)); }, \
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
#define MAKE_OUTPUT_JSON_TEST_P(FIXTURE) \
TEST_P(FIXTURE, from_json) \
{ \
const auto & [name, expected] = GetParam(); \
/* Don't use readJsonTest because we want to check experimental \
features. */ \
readTest(Path{"output-"} + name + ".json", [&](const auto & encoded_) { \
json j = json::parse(encoded_); \
DerivationOutput got = DerivationOutput::fromJSON(j, mockXpSettings); \
ASSERT_EQ(got, expected); \
}); \
} \
\
TEST_P(FIXTURE, to_json) \
{ \
const auto & [name, value] = GetParam(); \
writeJsonTest("output-" + name, value); \
}
TEST_JSON(
DerivationTest,
inputAddressed,
(DerivationOutput::InputAddressed{
.path = store->parseStorePath("/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-drv-name-output-name"),
}),
"drv-name",
"output-name")
struct DerivationOutputJsonTest : DerivationTest,
JsonCharacterizationTest<DerivationOutput>,
::testing::WithParamInterface<std::pair<std::string_view, DerivationOutput>>
{};
TEST_JSON(
DerivationTest,
caFixedFlat,
(DerivationOutput::CAFixed{
.ca =
{
.method = ContentAddressMethod::Raw::Flat,
.hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
},
}),
"drv-name",
"output-name")
MAKE_OUTPUT_JSON_TEST_P(DerivationOutputJsonTest)
TEST_JSON(
DerivationTest,
caFixedNAR,
(DerivationOutput::CAFixed{
.ca =
{
INSTANTIATE_TEST_SUITE_P(
DerivationOutputJSON,
DerivationOutputJsonTest,
::testing::Values(
std::pair{
"inputAddressed",
DerivationOutput{DerivationOutput::InputAddressed{
.path = StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-drv-name-output-name"},
}},
},
std::pair{
"caFixedFlat",
DerivationOutput{DerivationOutput::CAFixed{
.ca =
{
.method = ContentAddressMethod::Raw::Flat,
.hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
},
}},
},
std::pair{
"caFixedNAR",
DerivationOutput{DerivationOutput::CAFixed{
.ca =
{
.method = ContentAddressMethod::Raw::NixArchive,
.hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
},
}},
},
std::pair{
"deferred",
DerivationOutput{DerivationOutput::Deferred{}},
}));
struct DynDerivationOutputJsonTest : DynDerivationTest,
JsonCharacterizationTest<DerivationOutput>,
::testing::WithParamInterface<std::pair<std::string_view, DerivationOutput>>
{};
MAKE_OUTPUT_JSON_TEST_P(DynDerivationOutputJsonTest);
INSTANTIATE_TEST_SUITE_P(
DynDerivationOutputJSON,
DynDerivationOutputJsonTest,
::testing::Values(
std::pair{
"caFixedText",
DerivationOutput{DerivationOutput::CAFixed{
.ca =
{
.method = ContentAddressMethod::Raw::Text,
.hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
},
}},
}));
struct CaDerivationOutputJsonTest : CaDerivationTest,
JsonCharacterizationTest<DerivationOutput>,
::testing::WithParamInterface<std::pair<std::string_view, DerivationOutput>>
{};
MAKE_OUTPUT_JSON_TEST_P(CaDerivationOutputJsonTest);
INSTANTIATE_TEST_SUITE_P(
CaDerivationOutputJSON,
CaDerivationOutputJsonTest,
::testing::Values(
std::pair{
"caFloating",
DerivationOutput{DerivationOutput::CAFloating{
.method = ContentAddressMethod::Raw::NixArchive,
.hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
},
}),
"drv-name",
"output-name")
.hashAlgo = HashAlgorithm::SHA256,
}},
}));
TEST_JSON(
DynDerivationTest,
caFixedText,
(DerivationOutput::CAFixed{
.ca =
{
.method = ContentAddressMethod::Raw::Text,
.hash = Hash::parseAnyPrefixed("sha256-iUUXyRY8iW7DGirb0zwGgf1fRbLA7wimTJKgP7l/OQ8="),
},
}),
"drv-name",
"output-name")
struct ImpureDerivationOutputJsonTest : ImpureDerivationTest,
JsonCharacterizationTest<DerivationOutput>,
::testing::WithParamInterface<std::pair<std::string_view, DerivationOutput>>
{};
TEST_JSON(
CaDerivationTest,
caFloating,
(DerivationOutput::CAFloating{
.method = ContentAddressMethod::Raw::NixArchive,
.hashAlgo = HashAlgorithm::SHA256,
}),
"drv-name",
"output-name")
MAKE_OUTPUT_JSON_TEST_P(ImpureDerivationOutputJsonTest);
TEST_JSON(DerivationTest, deferred, DerivationOutput::Deferred{}, "drv-name", "output-name")
INSTANTIATE_TEST_SUITE_P(
ImpureDerivationOutputJSON,
ImpureDerivationOutputJsonTest,
::testing::Values(
std::pair{
"impure",
DerivationOutput{DerivationOutput::Impure{
.method = ContentAddressMethod::Raw::NixArchive,
.hashAlgo = HashAlgorithm::SHA256,
}},
}));
TEST_JSON(
ImpureDerivationTest,
impure,
(DerivationOutput::Impure{
.method = ContentAddressMethod::Raw::NixArchive,
.hashAlgo = HashAlgorithm::SHA256,
}),
"drv-name",
"output-name")
#undef MAKE_OUTPUT_JSON_TEST_P
#undef TEST_JSON
#define TEST_JSON(FIXTURE, NAME, VAL) \
TEST_F(FIXTURE, Derivation_##NAME##_from_json) \
{ \
readTest(#NAME ".json", [&](const auto & encoded_) { \
auto encoded = json::parse(encoded_); \
Derivation expected{VAL}; \
Derivation got = Derivation::fromJSON(encoded, mockXpSettings); \
ASSERT_EQ(got, expected); \
}); \
} \
\
TEST_F(FIXTURE, Derivation_##NAME##_to_json) \
{ \
writeTest( \
#NAME ".json", \
[&]() -> json { return Derivation{VAL}.toJSON(); }, \
[](const auto & file) { return json::parse(readFile(file)); }, \
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
#define MAKE_TEST_P(FIXTURE) \
TEST_P(FIXTURE, from_json) \
{ \
const auto & drv = GetParam(); \
/* Don't use readJsonTest because we want to check experimental \
features. */ \
readTest(drv.name + ".json", [&](const auto & encoded_) { \
auto encoded = json::parse(encoded_); \
Derivation got = Derivation::fromJSON(encoded, mockXpSettings); \
ASSERT_EQ(got, drv); \
}); \
} \
\
TEST_P(FIXTURE, to_json) \
{ \
const auto & drv = GetParam(); \
writeJsonTest(drv.name, drv); \
} \
\
TEST_P(FIXTURE, from_aterm) \
{ \
const auto & drv = GetParam(); \
readTest(drv.name + ".drv", [&](auto encoded) { \
auto got = parseDerivation(*store, std::move(encoded), drv.name, mockXpSettings); \
ASSERT_EQ(got.toJSON(), drv.toJSON()); \
ASSERT_EQ(got, drv); \
}); \
} \
\
TEST_P(FIXTURE, to_aterm) \
{ \
const auto & drv = GetParam(); \
writeTest(drv.name + ".drv", [&]() -> std::string { return drv.unparse(*store, false); }); \
}
#define TEST_ATERM(FIXTURE, NAME, VAL, DRV_NAME) \
TEST_F(FIXTURE, Derivation_##NAME##_from_aterm) \
{ \
readTest(#NAME ".drv", [&](auto encoded) { \
Derivation expected{VAL}; \
auto got = parseDerivation(*store, std::move(encoded), DRV_NAME, mockXpSettings); \
ASSERT_EQ(got.toJSON(), expected.toJSON()); \
ASSERT_EQ(got, expected); \
}); \
} \
\
TEST_F(FIXTURE, Derivation_##NAME##_to_aterm) \
{ \
writeTest(#NAME ".drv", [&]() -> std::string { return (VAL).unparse(*store, false); }); \
}
struct DerivationJsonAtermTest : DerivationTest,
JsonCharacterizationTest<Derivation>,
::testing::WithParamInterface<Derivation>
{};
Derivation makeSimpleDrv(const Store & store)
MAKE_TEST_P(DerivationJsonAtermTest);
Derivation makeSimpleDrv()
{
Derivation drv;
drv.name = "simple-derivation";
drv.inputSrcs = {
store.parseStorePath("/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"),
StorePath("c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"),
};
drv.inputDrvs = {
.map =
{
{
store.parseStorePath("/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv"),
StorePath("c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv"),
{
.value =
{
@ -231,22 +268,27 @@ Derivation makeSimpleDrv(const Store & store)
return drv;
}
TEST_JSON(DerivationTest, simple, makeSimpleDrv(*store))
INSTANTIATE_TEST_SUITE_P(DerivationJSONATerm, DerivationJsonAtermTest, ::testing::Values(makeSimpleDrv()));
TEST_ATERM(DerivationTest, simple, makeSimpleDrv(*store), "simple-derivation")
struct DynDerivationJsonAtermTest : DynDerivationTest,
JsonCharacterizationTest<Derivation>,
::testing::WithParamInterface<Derivation>
{};
Derivation makeDynDepDerivation(const Store & store)
MAKE_TEST_P(DynDerivationJsonAtermTest);
Derivation makeDynDepDerivation()
{
Derivation drv;
drv.name = "dyn-dep-derivation";
drv.inputSrcs = {
store.parseStorePath("/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"),
StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep1"},
};
drv.inputDrvs = {
.map =
{
{
store.parseStorePath("/nix/store/c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv"),
StorePath{"c015dhfh5l0lp6wxyvdn7bmwhbbr6hr9-dep2.drv"},
DerivedPathMap<StringSet>::ChildNode{
.value =
{
@ -293,11 +335,8 @@ Derivation makeDynDepDerivation(const Store & store)
return drv;
}
TEST_JSON(DynDerivationTest, dynDerivationDeps, makeDynDepDerivation(*store))
INSTANTIATE_TEST_SUITE_P(DynDerivationJSONATerm, DynDerivationJsonAtermTest, ::testing::Values(makeDynDepDerivation()));
TEST_ATERM(DynDerivationTest, dynDerivationDeps, makeDynDepDerivation(*store), "dyn-dep-derivation")
#undef TEST_JSON
#undef TEST_ATERM
#undef MAKE_TEST_P
} // namespace nix

View file

@ -3,12 +3,11 @@
#include <rapidcheck/gtest.h>
#include "nix/store/tests/outputs-spec.hh"
#include "nix/util/tests/characterization.hh"
#include "nix/util/tests/json-characterization.hh"
namespace nix {
class OutputsSpecTest : public CharacterizationTest
class OutputsSpecTest : public virtual CharacterizationTest
{
std::filesystem::path unitTestData = getUnitTestData() / "outputs-spec";
@ -20,7 +19,7 @@ public:
}
};
class ExtendedOutputsSpecTest : public CharacterizationTest
class ExtendedOutputsSpecTest : public virtual CharacterizationTest
{
std::filesystem::path unitTestData = getUnitTestData() / "outputs-spec" / "extended";
@ -214,40 +213,49 @@ TEST_F(ExtendedOutputsSpecTest, many_carrot)
ASSERT_EQ(std::string{prefix} + expected.to_string(), "foo^bar^bin,out");
}
#define TEST_JSON(FIXTURE, TYPE, NAME, VAL) \
static const TYPE FIXTURE##_##NAME = VAL; \
\
TEST_F(FIXTURE, NAME##_from_json) \
{ \
using namespace nlohmann; \
\
readTest(#NAME ".json", [&](const auto & encoded_) { \
auto encoded = json::parse(encoded_); \
TYPE got = adl_serializer<TYPE>::from_json(encoded); \
ASSERT_EQ(got, FIXTURE##_##NAME); \
}); \
} \
\
TEST_F(FIXTURE, NAME##_to_json) \
{ \
using namespace nlohmann; \
\
writeTest( \
#NAME ".json", \
[&]() -> json { return static_cast<json>(FIXTURE##_##NAME); }, \
[](const auto & file) { return json::parse(readFile(file)); }, \
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); }); \
#define MAKE_TEST_P(FIXTURE, TYPE) \
TEST_P(FIXTURE, from_json) \
{ \
const auto & [name, value] = GetParam(); \
readJsonTest(name, value); \
} \
\
TEST_P(FIXTURE, to_json) \
{ \
const auto & [name, value] = GetParam(); \
writeJsonTest(name, value); \
}
TEST_JSON(OutputsSpecTest, OutputsSpec, all, OutputsSpec::All{})
TEST_JSON(OutputsSpecTest, OutputsSpec, name, OutputsSpec::Names{"a"})
TEST_JSON(OutputsSpecTest, OutputsSpec, names, (OutputsSpec::Names{"a", "b"}))
struct OutputsSpecJsonTest : OutputsSpecTest,
JsonCharacterizationTest<OutputsSpec>,
::testing::WithParamInterface<std::pair<std::string_view, OutputsSpec>>
{};
TEST_JSON(ExtendedOutputsSpecTest, ExtendedOutputsSpec, def, ExtendedOutputsSpec::Default{})
TEST_JSON(ExtendedOutputsSpecTest, ExtendedOutputsSpec, all, ExtendedOutputsSpec::Explicit{OutputsSpec::All{}})
TEST_JSON(ExtendedOutputsSpecTest, ExtendedOutputsSpec, name, ExtendedOutputsSpec::Explicit{OutputsSpec::Names{"a"}})
TEST_JSON(
ExtendedOutputsSpecTest, ExtendedOutputsSpec, names, (ExtendedOutputsSpec::Explicit{OutputsSpec::Names{"a", "b"}}))
MAKE_TEST_P(OutputsSpecJsonTest, OutputsSpec);
INSTANTIATE_TEST_SUITE_P(
OutputsSpecJSON,
OutputsSpecJsonTest,
::testing::Values(
std::pair{"all", OutputsSpec{OutputsSpec::All{}}},
std::pair{"name", OutputsSpec{OutputsSpec::Names{"a"}}},
std::pair{"names", OutputsSpec{OutputsSpec::Names{"a", "b"}}}));
struct ExtendedOutputsSpecJsonTest : ExtendedOutputsSpecTest,
JsonCharacterizationTest<ExtendedOutputsSpec>,
::testing::WithParamInterface<std::pair<std::string_view, ExtendedOutputsSpec>>
{};
MAKE_TEST_P(ExtendedOutputsSpecJsonTest, ExtendedOutputsSpec);
INSTANTIATE_TEST_SUITE_P(
ExtendedOutputsSpecJSON,
ExtendedOutputsSpecJsonTest,
::testing::Values(
std::pair{"def", ExtendedOutputsSpec{ExtendedOutputsSpec::Default{}}},
std::pair{"all", ExtendedOutputsSpec{ExtendedOutputsSpec::Explicit{OutputsSpec::All{}}}},
std::pair{"name", ExtendedOutputsSpec{ExtendedOutputsSpec::Explicit{OutputsSpec::Names{"a"}}}},
std::pair{"names", ExtendedOutputsSpec{ExtendedOutputsSpec::Explicit{OutputsSpec::Names{"a", "b"}}}}));
#undef TEST_JSON

View file

@ -7,7 +7,7 @@
#include "nix/store/path-regex.hh"
#include "nix/store/store-api.hh"
#include "nix/util/tests/characterization.hh"
#include "nix/util/tests/json-characterization.hh"
#include "nix/store/tests/libstore.hh"
#include "nix/store/tests/path.hh"
@ -16,7 +16,7 @@ namespace nix {
#define STORE_DIR "/nix/store/"
#define HASH_PART "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q"
class StorePathTest : public CharacterizationTest, public LibStoreTest
class StorePathTest : public virtual CharacterizationTest, public LibStoreTest
{
std::filesystem::path unitTestData = getUnitTestData() / "store-path";
@ -149,27 +149,30 @@ RC_GTEST_FIXTURE_PROP(StorePathTest, prop_check_regex_eq_parse, ())
using nlohmann::json;
#define TEST_JSON(FIXTURE, NAME, VAL) \
static const StorePath NAME = VAL; \
\
TEST_F(FIXTURE, NAME##_from_json) \
{ \
readTest(#NAME ".json", [&](const auto & encoded_) { \
auto encoded = json::parse(encoded_); \
StorePath got = static_cast<StorePath>(encoded); \
ASSERT_EQ(got, NAME); \
}); \
} \
\
TEST_F(FIXTURE, 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"); }); \
}
struct StorePathJsonTest : StorePathTest,
JsonCharacterizationTest<StorePath>,
::testing::WithParamInterface<std::pair<std::string_view, StorePath>>
{};
TEST_JSON(StorePathTest, simple, StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"});
TEST_P(StorePathJsonTest, from_json)
{
auto & [name, expected] = GetParam();
readJsonTest(name, expected);
}
TEST_P(StorePathJsonTest, to_json)
{
auto & [name, value] = GetParam();
writeJsonTest(name, value);
}
INSTANTIATE_TEST_SUITE_P(
StorePathJSON,
StorePathJsonTest,
::testing::Values(
std::pair{
"simple",
StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"},
}));
} // namespace nix

View file

@ -6,12 +6,12 @@
#include "nix/store/store-api.hh"
#include "nix/util/tests/characterization.hh"
#include "nix/util/tests/json-characterization.hh"
#include "nix/store/tests/libstore.hh"
namespace nix {
class RealisationTest : public CharacterizationTest, public LibStoreTest
class RealisationTest : public JsonCharacterizationTest<Realisation>, public LibStoreTest
{
std::filesystem::path unitTestData = getUnitTestData() / "realisation";
@ -34,22 +34,14 @@ struct RealisationJsonTest : RealisationTest, ::testing::WithParamInterface<std:
TEST_P(RealisationJsonTest, from_json)
{
auto [name, expected] = GetParam();
readTest(name + ".json", [&](const auto & encoded_) {
auto encoded = json::parse(encoded_);
Realisation got = static_cast<Realisation>(encoded);
ASSERT_EQ(got, expected);
});
const auto & [name, expected] = GetParam();
readJsonTest(name, expected);
}
TEST_P(RealisationJsonTest, to_json)
{
auto [name, value] = GetParam();
writeTest(
name + ".json",
[&]() -> json { return static_cast<json>(value); },
[](const auto & file) { return json::parse(readFile(file)); },
[](const auto & file, const auto & got) { return writeFile(file, got.dump(2) + "\n"); });
const auto & [name, value] = GetParam();
writeJsonTest(name, value);
}
INSTANTIATE_TEST_SUITE_P(