mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 03:56:01 +01:00
Merge pull request #14065 from obsidiansystems/realisation-json-unit-test
Add JSON tests for `Realisation`
This commit is contained in:
commit
8ff5aeb367
5 changed files with 128 additions and 0 deletions
6
src/libstore-tests/data/realisation/simple.json
Normal file
6
src/libstore-tests/data/realisation/simple.json
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"dependentRealisations": {},
|
||||||
|
"id": "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad!foo",
|
||||||
|
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv",
|
||||||
|
"signatures": []
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"dependentRealisations": {
|
||||||
|
"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad!foo": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"
|
||||||
|
},
|
||||||
|
"id": "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad!foo",
|
||||||
|
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv",
|
||||||
|
"signatures": []
|
||||||
|
}
|
||||||
8
src/libstore-tests/data/realisation/with-signature.json
Normal file
8
src/libstore-tests/data/realisation/with-signature.json
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
{
|
||||||
|
"dependentRealisations": {},
|
||||||
|
"id": "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad!foo",
|
||||||
|
"outPath": "g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv",
|
||||||
|
"signatures": [
|
||||||
|
"asdfasdfasdf"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
@ -74,6 +74,7 @@ sources = files(
|
||||||
'outputs-spec.cc',
|
'outputs-spec.cc',
|
||||||
'path-info.cc',
|
'path-info.cc',
|
||||||
'path.cc',
|
'path.cc',
|
||||||
|
'realisation.cc',
|
||||||
'references.cc',
|
'references.cc',
|
||||||
's3-binary-cache-store.cc',
|
's3-binary-cache-store.cc',
|
||||||
's3.cc',
|
's3.cc',
|
||||||
|
|
|
||||||
105
src/libstore-tests/realisation.cc
Normal file
105
src/libstore-tests/realisation.cc
Normal file
|
|
@ -0,0 +1,105 @@
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <rapidcheck/gtest.h>
|
||||||
|
|
||||||
|
#include "nix/store/store-api.hh"
|
||||||
|
|
||||||
|
#include "nix/util/tests/characterization.hh"
|
||||||
|
#include "nix/store/tests/libstore.hh"
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
|
||||||
|
class RealisationTest : public CharacterizationTest, public LibStoreTest
|
||||||
|
{
|
||||||
|
std::filesystem::path unitTestData = getUnitTestData() / "realisation";
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
std::filesystem::path goldenMaster(std::string_view testStem) const override
|
||||||
|
{
|
||||||
|
return unitTestData / testStem;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------------------------
|
||||||
|
* JSON
|
||||||
|
* --------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
struct RealisationJsonTest : RealisationTest, ::testing::WithParamInterface<std::pair<std::string_view, Realisation>>
|
||||||
|
{};
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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"); });
|
||||||
|
}
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_SUITE_P(
|
||||||
|
RealisationJSON,
|
||||||
|
RealisationJsonTest,
|
||||||
|
([] {
|
||||||
|
Realisation simple{
|
||||||
|
|
||||||
|
.id =
|
||||||
|
{
|
||||||
|
.drvHash = Hash::parseExplicitFormatUnprefixed(
|
||||||
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
|
||||||
|
HashAlgorithm::SHA256,
|
||||||
|
HashFormat::Base16),
|
||||||
|
.outputName = "foo",
|
||||||
|
},
|
||||||
|
.outPath = StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"},
|
||||||
|
};
|
||||||
|
return ::testing::Values(
|
||||||
|
std::pair{
|
||||||
|
"simple",
|
||||||
|
simple,
|
||||||
|
},
|
||||||
|
std::pair{
|
||||||
|
"with-signature",
|
||||||
|
[&] {
|
||||||
|
auto r = simple;
|
||||||
|
// FIXME actually sign properly
|
||||||
|
r.signatures = {"asdfasdfasdf"};
|
||||||
|
return r;
|
||||||
|
}()},
|
||||||
|
std::pair{
|
||||||
|
"with-dependent-realisations",
|
||||||
|
[&] {
|
||||||
|
auto r = simple;
|
||||||
|
r.dependentRealisations = {{
|
||||||
|
{
|
||||||
|
.drvHash = Hash::parseExplicitFormatUnprefixed(
|
||||||
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
|
||||||
|
HashAlgorithm::SHA256,
|
||||||
|
HashFormat::Base16),
|
||||||
|
.outputName = "foo",
|
||||||
|
},
|
||||||
|
StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo.drv"},
|
||||||
|
}};
|
||||||
|
return r;
|
||||||
|
}(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
()));
|
||||||
|
|
||||||
|
} // namespace nix
|
||||||
Loading…
Add table
Add a link
Reference in a new issue