mirror of
https://github.com/NixOS/nix.git
synced 2025-11-14 14:32:42 +01:00
nlohmann::json instance and JSON Schema for ContentAddress
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
This commit is contained in:
parent
60f9489b83
commit
91b69e9e70
18 changed files with 242 additions and 37 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#include <gtest/gtest.h>
|
||||
|
||||
#include "nix/store/content-address.hh"
|
||||
#include "nix/util/tests/json-characterization.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
|
@ -8,33 +9,93 @@ namespace nix {
|
|||
* ContentAddressMethod::parse, ContentAddressMethod::render
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
||||
TEST(ContentAddressMethod, testRoundTripPrintParse_1)
|
||||
static auto methods = ::testing::Values(
|
||||
std::pair{ContentAddressMethod::Raw::Text, "text"},
|
||||
std::pair{ContentAddressMethod::Raw::Flat, "flat"},
|
||||
std::pair{ContentAddressMethod::Raw::NixArchive, "nar"},
|
||||
std::pair{ContentAddressMethod::Raw::Git, "git"});
|
||||
|
||||
struct ContentAddressMethodTest : ::testing::Test,
|
||||
::testing::WithParamInterface<std::pair<ContentAddressMethod, std::string_view>>
|
||||
{};
|
||||
|
||||
TEST_P(ContentAddressMethodTest, testRoundTripPrintParse_1)
|
||||
{
|
||||
for (ContentAddressMethod cam : {
|
||||
ContentAddressMethod::Raw::Text,
|
||||
ContentAddressMethod::Raw::Flat,
|
||||
ContentAddressMethod::Raw::NixArchive,
|
||||
ContentAddressMethod::Raw::Git,
|
||||
}) {
|
||||
EXPECT_EQ(ContentAddressMethod::parse(cam.render()), cam);
|
||||
}
|
||||
auto & [cam, _] = GetParam();
|
||||
EXPECT_EQ(ContentAddressMethod::parse(cam.render()), cam);
|
||||
}
|
||||
|
||||
TEST(ContentAddressMethod, testRoundTripPrintParse_2)
|
||||
TEST_P(ContentAddressMethodTest, testRoundTripPrintParse_2)
|
||||
{
|
||||
for (const std::string_view camS : {
|
||||
"text",
|
||||
"flat",
|
||||
"nar",
|
||||
"git",
|
||||
}) {
|
||||
EXPECT_EQ(ContentAddressMethod::parse(camS).render(), camS);
|
||||
}
|
||||
auto & [cam, camS] = GetParam();
|
||||
EXPECT_EQ(ContentAddressMethod::parse(camS).render(), camS);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(ContentAddressMethod, ContentAddressMethodTest, methods);
|
||||
|
||||
TEST(ContentAddressMethod, testParseContentAddressMethodOptException)
|
||||
{
|
||||
EXPECT_THROW(ContentAddressMethod::parse("narwhal"), UsageError);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* JSON
|
||||
* --------------------------------------------------------------------------*/
|
||||
|
||||
class ContentAddressTest : public virtual CharacterizationTest
|
||||
{
|
||||
std::filesystem::path unitTestData = getUnitTestData() / "content-address";
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* We set these in tests rather than the regular globals so we don't have
|
||||
* to worry about race conditions if the tests run concurrently.
|
||||
*/
|
||||
ExperimentalFeatureSettings mockXpSettings;
|
||||
|
||||
std::filesystem::path goldenMaster(std::string_view testStem) const override
|
||||
{
|
||||
return unitTestData / testStem;
|
||||
}
|
||||
};
|
||||
|
||||
using nlohmann::json;
|
||||
|
||||
struct ContentAddressJsonTest : ContentAddressTest,
|
||||
JsonCharacterizationTest<ContentAddress>,
|
||||
::testing::WithParamInterface<std::pair<std::string_view, ContentAddress>>
|
||||
{};
|
||||
|
||||
TEST_P(ContentAddressJsonTest, from_json)
|
||||
{
|
||||
auto & [name, expected] = GetParam();
|
||||
readJsonTest(name, expected);
|
||||
}
|
||||
|
||||
TEST_P(ContentAddressJsonTest, to_json)
|
||||
{
|
||||
auto & [name, value] = GetParam();
|
||||
writeJsonTest(name, value);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(
|
||||
ContentAddressJSON,
|
||||
ContentAddressJsonTest,
|
||||
::testing::Values(
|
||||
std::pair{
|
||||
"text",
|
||||
ContentAddress{
|
||||
.method = ContentAddressMethod::Raw::Text,
|
||||
.hash = hashString(HashAlgorithm::SHA256, "asdf"),
|
||||
},
|
||||
},
|
||||
std::pair{
|
||||
"nar",
|
||||
ContentAddress{
|
||||
.method = ContentAddressMethod::Raw::NixArchive,
|
||||
.hash = hashString(HashAlgorithm::SHA256, "qwer"),
|
||||
},
|
||||
}));
|
||||
|
||||
} // namespace nix
|
||||
|
|
|
|||
8
src/libstore-tests/data/content-address/nar.json
Normal file
8
src/libstore-tests/data/content-address/nar.json
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"hash": {
|
||||
"algorithm": "sha256",
|
||||
"format": "base64",
|
||||
"hash": "9vLqj0XYoFfJVmoz+ZR02i5camYE1zYSFlDicwxvsKM="
|
||||
},
|
||||
"method": "nar"
|
||||
}
|
||||
8
src/libstore-tests/data/content-address/text.json
Normal file
8
src/libstore-tests/data/content-address/text.json
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"hash": {
|
||||
"algorithm": "sha256",
|
||||
"format": "base64",
|
||||
"hash": "8OTC92xYkW7CWPJGhRvqCR0U1CR6L8PhhpRGGxgW4Ts="
|
||||
},
|
||||
"method": "text"
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue