mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 08:16:15 +02:00
Merge pull request #9268 from obsidiansystems/systematize-characterization-tests
Systematize characterization tests a bit more
This commit is contained in:
commit
727ada1a41
6 changed files with 179 additions and 183 deletions
|
@ -1,28 +0,0 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* The path to the `unit-test-data` directory. See the contributing
|
||||
* guide in the manual for further details.
|
||||
*/
|
||||
static Path getUnitTestData() {
|
||||
return getEnv("_NIX_TEST_UNIT_DATA").value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we should update "golden masters" instead of running tests
|
||||
* against them. See the contributing guide in the manual for further
|
||||
* details.
|
||||
*/
|
||||
static bool testAccept() {
|
||||
return getEnv("_NIX_TEST_ACCEPT") == "1";
|
||||
}
|
||||
|
||||
constexpr std::string_view cannotReadGoldenMaster =
|
||||
"Cannot read golden master because another test is also updating it";
|
||||
|
||||
constexpr std::string_view updatingGoldenMaster =
|
||||
"Updating golden master";
|
||||
}
|
|
@ -20,16 +20,9 @@ public:
|
|||
* Golden test for `T` reading
|
||||
*/
|
||||
template<typename T>
|
||||
void readTest(PathView testStem, T value)
|
||||
void readProtoTest(PathView testStem, const T & expected)
|
||||
{
|
||||
if (testAccept())
|
||||
{
|
||||
GTEST_SKIP() << cannotReadGoldenMaster;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto encoded = readFile(goldenMaster(testStem));
|
||||
|
||||
CharacterizationTest::readTest(testStem, [&](const auto & encoded) {
|
||||
T got = ({
|
||||
StringSource from { encoded };
|
||||
CommonProto::Serialise<T>::read(
|
||||
|
@ -37,44 +30,33 @@ public:
|
|||
CommonProto::ReadConn { .from = from });
|
||||
});
|
||||
|
||||
ASSERT_EQ(got, value);
|
||||
}
|
||||
ASSERT_EQ(got, expected);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Golden test for `T` write
|
||||
*/
|
||||
template<typename T>
|
||||
void writeTest(PathView testStem, const T & value)
|
||||
void writeProtoTest(PathView testStem, const T & decoded)
|
||||
{
|
||||
auto file = goldenMaster(testStem);
|
||||
|
||||
CharacterizationTest::writeTest(testStem, [&]() -> std::string {
|
||||
StringSink to;
|
||||
CommonProto::write(
|
||||
CommonProto::Serialise<T>::write(
|
||||
*store,
|
||||
CommonProto::WriteConn { .to = to },
|
||||
value);
|
||||
|
||||
if (testAccept())
|
||||
{
|
||||
createDirs(dirOf(file));
|
||||
writeFile(file, to.s);
|
||||
GTEST_SKIP() << updatingGoldenMaster;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto expected = readFile(file);
|
||||
ASSERT_EQ(to.s, expected);
|
||||
}
|
||||
decoded);
|
||||
return to.s;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
#define CHARACTERIZATION_TEST(NAME, STEM, VALUE) \
|
||||
TEST_F(CommonProtoTest, NAME ## _read) { \
|
||||
readTest(STEM, VALUE); \
|
||||
readProtoTest(STEM, VALUE); \
|
||||
} \
|
||||
TEST_F(CommonProtoTest, NAME ## _write) { \
|
||||
writeTest(STEM, VALUE); \
|
||||
writeProtoTest(STEM, VALUE); \
|
||||
}
|
||||
|
||||
CHARACTERIZATION_TEST(
|
||||
|
|
|
@ -11,20 +11,20 @@ namespace nix {
|
|||
|
||||
using nlohmann::json;
|
||||
|
||||
class DerivationTest : public LibStoreTest
|
||||
class DerivationTest : public CharacterizationTest, public LibStoreTest
|
||||
{
|
||||
Path unitTestData = getUnitTestData() + "/libstore/derivation";
|
||||
|
||||
public:
|
||||
Path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData + "/" + testStem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
Path unitTestData = getUnitTestData() + "/libstore/derivation";
|
||||
|
||||
Path goldenMaster(std::string_view testStem) {
|
||||
return unitTestData + "/" + testStem;
|
||||
}
|
||||
};
|
||||
|
||||
class CaDerivationTest : public DerivationTest
|
||||
|
@ -73,14 +73,8 @@ TEST_F(DynDerivationTest, BadATerm_oldVersionDynDeps) {
|
|||
|
||||
#define TEST_JSON(FIXTURE, NAME, VAL, DRV_NAME, OUTPUT_NAME) \
|
||||
TEST_F(FIXTURE, DerivationOutput_ ## NAME ## _from_json) { \
|
||||
if (testAccept()) \
|
||||
{ \
|
||||
GTEST_SKIP() << cannotReadGoldenMaster; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
auto encoded = json::parse( \
|
||||
readFile(goldenMaster("output-" #NAME ".json"))); \
|
||||
readTest("output-" #NAME ".json", [&](const auto & encoded_) { \
|
||||
auto encoded = json::parse(encoded_); \
|
||||
DerivationOutput got = DerivationOutput::fromJSON( \
|
||||
*store, \
|
||||
DRV_NAME, \
|
||||
|
@ -89,28 +83,20 @@ TEST_F(DynDerivationTest, BadATerm_oldVersionDynDeps) {
|
|||
mockXpSettings); \
|
||||
DerivationOutput expected { VAL }; \
|
||||
ASSERT_EQ(got, expected); \
|
||||
} \
|
||||
}); \
|
||||
} \
|
||||
\
|
||||
TEST_F(FIXTURE, DerivationOutput_ ## NAME ## _to_json) { \
|
||||
auto file = goldenMaster("output-" #NAME ".json"); \
|
||||
\
|
||||
json got = DerivationOutput { VAL }.toJSON( \
|
||||
writeTest("output-" #NAME ".json", [&]() -> json { \
|
||||
return DerivationOutput { (VAL) }.toJSON( \
|
||||
*store, \
|
||||
DRV_NAME, \
|
||||
OUTPUT_NAME); \
|
||||
\
|
||||
if (testAccept()) \
|
||||
{ \
|
||||
createDirs(dirOf(file)); \
|
||||
writeFile(file, got.dump(2) + "\n"); \
|
||||
GTEST_SKIP() << updatingGoldenMaster; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
auto expected = json::parse(readFile(file)); \
|
||||
ASSERT_EQ(got, expected); \
|
||||
} \
|
||||
(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"); \
|
||||
}); \
|
||||
}
|
||||
|
||||
TEST_JSON(DerivationTest, inputAddressed,
|
||||
|
@ -167,50 +153,30 @@ TEST_JSON(ImpureDerivationTest, impure,
|
|||
|
||||
#define TEST_JSON(FIXTURE, NAME, VAL) \
|
||||
TEST_F(FIXTURE, Derivation_ ## NAME ## _from_json) { \
|
||||
if (testAccept()) \
|
||||
{ \
|
||||
GTEST_SKIP() << cannotReadGoldenMaster; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
auto encoded = json::parse( \
|
||||
readFile(goldenMaster( #NAME ".json"))); \
|
||||
readTest(#NAME ".json", [&](const auto & encoded_) { \
|
||||
auto encoded = json::parse(encoded_); \
|
||||
Derivation expected { VAL }; \
|
||||
Derivation got = Derivation::fromJSON( \
|
||||
*store, \
|
||||
encoded, \
|
||||
mockXpSettings); \
|
||||
ASSERT_EQ(got, expected); \
|
||||
} \
|
||||
}); \
|
||||
} \
|
||||
\
|
||||
TEST_F(FIXTURE, Derivation_ ## NAME ## _to_json) { \
|
||||
auto file = goldenMaster( #NAME ".json"); \
|
||||
\
|
||||
json got = Derivation { VAL }.toJSON(*store); \
|
||||
\
|
||||
if (testAccept()) \
|
||||
{ \
|
||||
createDirs(dirOf(file)); \
|
||||
writeFile(file, got.dump(2) + "\n"); \
|
||||
GTEST_SKIP() << updatingGoldenMaster; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
auto expected = json::parse(readFile(file)); \
|
||||
ASSERT_EQ(got, expected); \
|
||||
} \
|
||||
writeTest(#NAME ".json", [&]() -> json { \
|
||||
return Derivation { VAL }.toJSON(*store); \
|
||||
}, [](const auto & file) { \
|
||||
return json::parse(readFile(file)); \
|
||||
}, [](const auto & file, const auto & got) { \
|
||||
return writeFile(file, got.dump(2) + "\n"); \
|
||||
}); \
|
||||
}
|
||||
|
||||
#define TEST_ATERM(FIXTURE, NAME, VAL, DRV_NAME) \
|
||||
TEST_F(FIXTURE, Derivation_ ## NAME ## _from_aterm) { \
|
||||
if (testAccept()) \
|
||||
{ \
|
||||
GTEST_SKIP() << cannotReadGoldenMaster; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
auto encoded = readFile(goldenMaster( #NAME ".drv")); \
|
||||
readTest(#NAME ".drv", [&](auto encoded) { \
|
||||
Derivation expected { VAL }; \
|
||||
auto got = parseDerivation( \
|
||||
*store, \
|
||||
|
@ -219,25 +185,13 @@ TEST_JSON(ImpureDerivationTest, impure,
|
|||
mockXpSettings); \
|
||||
ASSERT_EQ(got.toJSON(*store), expected.toJSON(*store)) ; \
|
||||
ASSERT_EQ(got, expected); \
|
||||
} \
|
||||
}); \
|
||||
} \
|
||||
\
|
||||
TEST_F(FIXTURE, Derivation_ ## NAME ## _to_aterm) { \
|
||||
auto file = goldenMaster( #NAME ".drv"); \
|
||||
\
|
||||
auto got = (VAL).unparse(*store, false); \
|
||||
\
|
||||
if (testAccept()) \
|
||||
{ \
|
||||
createDirs(dirOf(file)); \
|
||||
writeFile(file, got); \
|
||||
GTEST_SKIP() << updatingGoldenMaster; \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
auto expected = readFile(file); \
|
||||
ASSERT_EQ(got, expected); \
|
||||
} \
|
||||
writeTest(#NAME ".drv", [&]() -> std::string { \
|
||||
return (VAL).unparse(*store, false); \
|
||||
}); \
|
||||
}
|
||||
|
||||
Derivation makeSimpleDrv(const Store & store) {
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
class LibStoreTest : public ::testing::Test {
|
||||
class LibStoreTest : public virtual ::testing::Test {
|
||||
public:
|
||||
static void SetUpTestSuite() {
|
||||
initLibStore();
|
||||
|
|
|
@ -7,12 +7,11 @@
|
|||
namespace nix {
|
||||
|
||||
template<class Proto, const char * protocolDir>
|
||||
class ProtoTest : public LibStoreTest
|
||||
class ProtoTest : public CharacterizationTest, public LibStoreTest
|
||||
{
|
||||
protected:
|
||||
Path unitTestData = getUnitTestData() + "/libstore/" + protocolDir;
|
||||
|
||||
Path goldenMaster(std::string_view testStem) {
|
||||
Path goldenMaster(std::string_view testStem) const override {
|
||||
return unitTestData + "/" + testStem + ".bin";
|
||||
}
|
||||
};
|
||||
|
@ -25,18 +24,11 @@ public:
|
|||
* Golden test for `T` reading
|
||||
*/
|
||||
template<typename T>
|
||||
void readTest(PathView testStem, typename Proto::Version version, T value)
|
||||
void readProtoTest(PathView testStem, typename Proto::Version version, T expected)
|
||||
{
|
||||
if (testAccept())
|
||||
{
|
||||
GTEST_SKIP() << cannotReadGoldenMaster;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto expected = readFile(ProtoTest<Proto, protocolDir>::goldenMaster(testStem));
|
||||
|
||||
CharacterizationTest::readTest(testStem, [&](const auto & encoded) {
|
||||
T got = ({
|
||||
StringSource from { expected };
|
||||
StringSource from { encoded };
|
||||
Proto::template Serialise<T>::read(
|
||||
*LibStoreTest::store,
|
||||
typename Proto::ReadConn {
|
||||
|
@ -45,47 +37,36 @@ public:
|
|||
});
|
||||
});
|
||||
|
||||
ASSERT_EQ(got, value);
|
||||
}
|
||||
ASSERT_EQ(got, expected);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Golden test for `T` write
|
||||
*/
|
||||
template<typename T>
|
||||
void writeTest(PathView testStem, typename Proto::Version version, const T & value)
|
||||
void writeProtoTest(PathView testStem, typename Proto::Version version, const T & decoded)
|
||||
{
|
||||
auto file = ProtoTest<Proto, protocolDir>::goldenMaster(testStem);
|
||||
|
||||
CharacterizationTest::writeTest(testStem, [&]() {
|
||||
StringSink to;
|
||||
Proto::write(
|
||||
Proto::template Serialise<T>::write(
|
||||
*LibStoreTest::store,
|
||||
typename Proto::WriteConn {
|
||||
.to = to,
|
||||
.version = version,
|
||||
},
|
||||
value);
|
||||
|
||||
if (testAccept())
|
||||
{
|
||||
createDirs(dirOf(file));
|
||||
writeFile(file, to.s);
|
||||
GTEST_SKIP() << updatingGoldenMaster;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto expected = readFile(file);
|
||||
ASSERT_EQ(to.s, expected);
|
||||
}
|
||||
decoded);
|
||||
return std::move(to.s);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
|
||||
TEST_F(FIXTURE, NAME ## _read) { \
|
||||
readTest(STEM, VERSION, VALUE); \
|
||||
readProtoTest(STEM, VERSION, VALUE); \
|
||||
} \
|
||||
TEST_F(FIXTURE, NAME ## _write) { \
|
||||
writeTest(STEM, VERSION, VALUE); \
|
||||
writeProtoTest(STEM, VERSION, VALUE); \
|
||||
}
|
||||
|
||||
}
|
||||
|
|
107
src/libutil/tests/characterization.hh
Normal file
107
src/libutil/tests/characterization.hh
Normal file
|
@ -0,0 +1,107 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "types.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
/**
|
||||
* The path to the `unit-test-data` directory. See the contributing
|
||||
* guide in the manual for further details.
|
||||
*/
|
||||
static Path getUnitTestData() {
|
||||
return getEnv("_NIX_TEST_UNIT_DATA").value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether we should update "golden masters" instead of running tests
|
||||
* against them. See the contributing guide in the manual for further
|
||||
* details.
|
||||
*/
|
||||
static bool testAccept() {
|
||||
return getEnv("_NIX_TEST_ACCEPT") == "1";
|
||||
}
|
||||
|
||||
/**
|
||||
* Mixin class for writing characterization tests
|
||||
*/
|
||||
class CharacterizationTest : public virtual ::testing::Test
|
||||
{
|
||||
protected:
|
||||
/**
|
||||
* While the "golden master" for this characterization test is
|
||||
* located. It should not be shared with any other test.
|
||||
*/
|
||||
virtual Path goldenMaster(PathView testStem) const = 0;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Golden test for reading
|
||||
*
|
||||
* @param test hook that takes the contents of the file and does the
|
||||
* actual work
|
||||
*/
|
||||
void readTest(PathView testStem, auto && test)
|
||||
{
|
||||
auto file = goldenMaster(testStem);
|
||||
|
||||
if (testAccept())
|
||||
{
|
||||
GTEST_SKIP()
|
||||
<< "Cannot read golden master "
|
||||
<< file
|
||||
<< "because another test is also updating it";
|
||||
}
|
||||
else
|
||||
{
|
||||
test(readFile(file));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Golden test for writing
|
||||
*
|
||||
* @param test hook that produces contents of the file and does the
|
||||
* actual work
|
||||
*/
|
||||
void writeTest(
|
||||
PathView testStem, auto && test, auto && readFile2, auto && writeFile2)
|
||||
{
|
||||
auto file = goldenMaster(testStem);
|
||||
|
||||
auto got = test();
|
||||
|
||||
if (testAccept())
|
||||
{
|
||||
createDirs(dirOf(file));
|
||||
writeFile2(file, got);
|
||||
GTEST_SKIP()
|
||||
<< "Updating golden master "
|
||||
<< file;
|
||||
}
|
||||
else
|
||||
{
|
||||
decltype(got) expected = readFile2(file);
|
||||
ASSERT_EQ(got, expected);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Specialize to `std::string`
|
||||
*/
|
||||
void writeTest(PathView testStem, auto && test)
|
||||
{
|
||||
writeTest(
|
||||
testStem, test,
|
||||
[](const Path & f) -> std::string {
|
||||
return readFile(f);
|
||||
},
|
||||
[](const Path & f, const std::string & c) {
|
||||
return writeFile(f, c);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in a new issue