Systematize characterization tests a bit more

Deduplicating code moreover enforcing the pattern means:

 - It is easier to write new characterization tests because less boilerplate

 - It is harder to mess up new tests because there are fewer places to
   make mistakes.

Co-authored-by: Jacek Galowicz <jacek@galowicz.de>
This commit is contained in:
John Ericson 2023-11-01 11:15:21 -04:00
parent 1e61c007be
commit b107431816
6 changed files with 183 additions and 183 deletions

View file

@ -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";
}

View file

@ -20,16 +20,9 @@ public:
* Golden test for `T` reading * Golden test for `T` reading
*/ */
template<typename T> template<typename T>
void readTest(PathView testStem, T value) void readProtoTest(PathView testStem, const T & expected)
{ {
if (testAccept()) CharacterizationTest::readTest(testStem, [&](const auto & encoded) {
{
GTEST_SKIP() << cannotReadGoldenMaster;
}
else
{
auto encoded = readFile(goldenMaster(testStem));
T got = ({ T got = ({
StringSource from { encoded }; StringSource from { encoded };
CommonProto::Serialise<T>::read( CommonProto::Serialise<T>::read(
@ -37,44 +30,33 @@ public:
CommonProto::ReadConn { .from = from }); CommonProto::ReadConn { .from = from });
}); });
ASSERT_EQ(got, value); ASSERT_EQ(got, expected);
} });
} }
/** /**
* Golden test for `T` write * Golden test for `T` write
*/ */
template<typename T> 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;
StringSink to; CommonProto::Serialise<T>::write(
CommonProto::write( *store,
*store, CommonProto::WriteConn { .to = to },
CommonProto::WriteConn { .to = to }, decoded);
value); return to.s;
});
if (testAccept())
{
createDirs(dirOf(file));
writeFile(file, to.s);
GTEST_SKIP() << updatingGoldenMaster;
}
else
{
auto expected = readFile(file);
ASSERT_EQ(to.s, expected);
}
} }
}; };
#define CHARACTERIZATION_TEST(NAME, STEM, VALUE) \ #define CHARACTERIZATION_TEST(NAME, STEM, VALUE) \
TEST_F(CommonProtoTest, NAME ## _read) { \ TEST_F(CommonProtoTest, NAME ## _read) { \
readTest(STEM, VALUE); \ readProtoTest(STEM, VALUE); \
} \ } \
TEST_F(CommonProtoTest, NAME ## _write) { \ TEST_F(CommonProtoTest, NAME ## _write) { \
writeTest(STEM, VALUE); \ writeProtoTest(STEM, VALUE); \
} }
CHARACTERIZATION_TEST( CHARACTERIZATION_TEST(

View file

@ -11,20 +11,20 @@ namespace nix {
using nlohmann::json; using nlohmann::json;
class DerivationTest : public LibStoreTest class DerivationTest : public CharacterizationTest, public LibStoreTest
{ {
Path unitTestData = getUnitTestData() + "/libstore/derivation";
public: 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 * 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. * to worry about race conditions if the tests run concurrently.
*/ */
ExperimentalFeatureSettings mockXpSettings; ExperimentalFeatureSettings mockXpSettings;
Path unitTestData = getUnitTestData() + "/libstore/derivation";
Path goldenMaster(std::string_view testStem) {
return unitTestData + "/" + testStem;
}
}; };
class CaDerivationTest : public DerivationTest class CaDerivationTest : public DerivationTest
@ -73,14 +73,8 @@ TEST_F(DynDerivationTest, BadATerm_oldVersionDynDeps) {
#define TEST_JSON(FIXTURE, NAME, VAL, DRV_NAME, OUTPUT_NAME) \ #define TEST_JSON(FIXTURE, NAME, VAL, DRV_NAME, OUTPUT_NAME) \
TEST_F(FIXTURE, DerivationOutput_ ## NAME ## _from_json) { \ TEST_F(FIXTURE, DerivationOutput_ ## NAME ## _from_json) { \
if (testAccept()) \ readTest("output-" #NAME ".json", [&](const auto & encoded_) { \
{ \ auto encoded = json::parse(encoded_); \
GTEST_SKIP() << cannotReadGoldenMaster; \
} \
else \
{ \
auto encoded = json::parse( \
readFile(goldenMaster("output-" #NAME ".json"))); \
DerivationOutput got = DerivationOutput::fromJSON( \ DerivationOutput got = DerivationOutput::fromJSON( \
*store, \ *store, \
DRV_NAME, \ DRV_NAME, \
@ -89,28 +83,20 @@ TEST_F(DynDerivationTest, BadATerm_oldVersionDynDeps) {
mockXpSettings); \ mockXpSettings); \
DerivationOutput expected { VAL }; \ DerivationOutput expected { VAL }; \
ASSERT_EQ(got, expected); \ ASSERT_EQ(got, expected); \
} \ }); \
} \ } \
\ \
TEST_F(FIXTURE, DerivationOutput_ ## NAME ## _to_json) { \ TEST_F(FIXTURE, DerivationOutput_ ## NAME ## _to_json) { \
auto file = goldenMaster("output-" #NAME ".json"); \ writeTest<json>("output-" #NAME ".json", [&]() -> json { \
\ return DerivationOutput { (VAL) }.toJSON( \
json got = DerivationOutput { VAL }.toJSON( \ *store, \
*store, \ (DRV_NAME), \
DRV_NAME, \ (OUTPUT_NAME)); \
OUTPUT_NAME); \ }, [](const auto & file) { \
\ return json::parse(readFile(file)); \
if (testAccept()) \ }, [](const auto & file, const auto & got) { \
{ \ return writeFile(file, got.dump(2) + "\n"); \
createDirs(dirOf(file)); \ }); \
writeFile(file, got.dump(2) + "\n"); \
GTEST_SKIP() << updatingGoldenMaster; \
} \
else \
{ \
auto expected = json::parse(readFile(file)); \
ASSERT_EQ(got, expected); \
} \
} }
TEST_JSON(DerivationTest, inputAddressed, TEST_JSON(DerivationTest, inputAddressed,
@ -167,50 +153,30 @@ TEST_JSON(ImpureDerivationTest, impure,
#define TEST_JSON(FIXTURE, NAME, VAL) \ #define TEST_JSON(FIXTURE, NAME, VAL) \
TEST_F(FIXTURE, Derivation_ ## NAME ## _from_json) { \ TEST_F(FIXTURE, Derivation_ ## NAME ## _from_json) { \
if (testAccept()) \ readTest(#NAME ".json", [&](const auto & encoded_) { \
{ \ auto encoded = json::parse(encoded_); \
GTEST_SKIP() << cannotReadGoldenMaster; \
} \
else \
{ \
auto encoded = json::parse( \
readFile(goldenMaster( #NAME ".json"))); \
Derivation expected { VAL }; \ Derivation expected { VAL }; \
Derivation got = Derivation::fromJSON( \ Derivation got = Derivation::fromJSON( \
*store, \ *store, \
encoded, \ encoded, \
mockXpSettings); \ mockXpSettings); \
ASSERT_EQ(got, expected); \ ASSERT_EQ(got, expected); \
} \ }); \
} \ } \
\ \
TEST_F(FIXTURE, Derivation_ ## NAME ## _to_json) { \ TEST_F(FIXTURE, Derivation_ ## NAME ## _to_json) { \
auto file = goldenMaster( #NAME ".json"); \ writeTest<json>(#NAME ".json", [&]() -> json { \
\ return Derivation { VAL }.toJSON(*store); \
json got = Derivation { VAL }.toJSON(*store); \ }, [](const auto & file) { \
\ return json::parse(readFile(file)); \
if (testAccept()) \ }, [](const auto & file, const auto & got) { \
{ \ return writeFile(file, got.dump(2) + "\n"); \
createDirs(dirOf(file)); \ }); \
writeFile(file, got.dump(2) + "\n"); \
GTEST_SKIP() << updatingGoldenMaster; \
} \
else \
{ \
auto expected = json::parse(readFile(file)); \
ASSERT_EQ(got, expected); \
} \
} }
#define TEST_ATERM(FIXTURE, NAME, VAL, DRV_NAME) \ #define TEST_ATERM(FIXTURE, NAME, VAL, DRV_NAME) \
TEST_F(FIXTURE, Derivation_ ## NAME ## _from_aterm) { \ TEST_F(FIXTURE, Derivation_ ## NAME ## _from_aterm) { \
if (testAccept()) \ readTest(#NAME ".drv", [&](auto encoded) { \
{ \
GTEST_SKIP() << cannotReadGoldenMaster; \
} \
else \
{ \
auto encoded = readFile(goldenMaster( #NAME ".drv")); \
Derivation expected { VAL }; \ Derivation expected { VAL }; \
auto got = parseDerivation( \ auto got = parseDerivation( \
*store, \ *store, \
@ -219,25 +185,13 @@ TEST_JSON(ImpureDerivationTest, impure,
mockXpSettings); \ mockXpSettings); \
ASSERT_EQ(got.toJSON(*store), expected.toJSON(*store)) ; \ ASSERT_EQ(got.toJSON(*store), expected.toJSON(*store)) ; \
ASSERT_EQ(got, expected); \ ASSERT_EQ(got, expected); \
} \ }); \
} \ } \
\ \
TEST_F(FIXTURE, Derivation_ ## NAME ## _to_aterm) { \ TEST_F(FIXTURE, Derivation_ ## NAME ## _to_aterm) { \
auto file = goldenMaster( #NAME ".drv"); \ writeTest(#NAME ".drv", [&]() -> std::string { \
\ return (VAL).unparse(*store, false); \
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); \
} \
} }
Derivation makeSimpleDrv(const Store & store) { Derivation makeSimpleDrv(const Store & store) {

View file

@ -8,7 +8,7 @@
namespace nix { namespace nix {
class LibStoreTest : public ::testing::Test { class LibStoreTest : public virtual ::testing::Test {
public: public:
static void SetUpTestSuite() { static void SetUpTestSuite() {
initLibStore(); initLibStore();

View file

@ -7,12 +7,11 @@
namespace nix { namespace nix {
template<class Proto, const char * protocolDir> template<class Proto, const char * protocolDir>
class ProtoTest : public LibStoreTest class ProtoTest : public CharacterizationTest, public LibStoreTest
{ {
protected:
Path unitTestData = getUnitTestData() + "/libstore/" + protocolDir; Path unitTestData = getUnitTestData() + "/libstore/" + protocolDir;
Path goldenMaster(std::string_view testStem) { Path goldenMaster(std::string_view testStem) const override {
return unitTestData + "/" + testStem + ".bin"; return unitTestData + "/" + testStem + ".bin";
} }
}; };
@ -25,18 +24,11 @@ public:
* Golden test for `T` reading * Golden test for `T` reading
*/ */
template<typename T> 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()) CharacterizationTest::readTest(testStem, [&](const auto & encoded) {
{
GTEST_SKIP() << cannotReadGoldenMaster;
}
else
{
auto expected = readFile(ProtoTest<Proto, protocolDir>::goldenMaster(testStem));
T got = ({ T got = ({
StringSource from { expected }; StringSource from { encoded };
Proto::template Serialise<T>::read( Proto::template Serialise<T>::read(
*LibStoreTest::store, *LibStoreTest::store,
typename Proto::ReadConn { typename Proto::ReadConn {
@ -45,47 +37,36 @@ public:
}); });
}); });
ASSERT_EQ(got, value); ASSERT_EQ(got, expected);
} });
} }
/** /**
* Golden test for `T` write * Golden test for `T` write
*/ */
template<typename T> 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;
StringSink to; Proto::template Serialise<T>::write(
Proto::write( *LibStoreTest::store,
*LibStoreTest::store, typename Proto::WriteConn {
typename Proto::WriteConn { .to = to,
.to = to, .version = version,
.version = version, },
}, decoded);
value); return std::move(to.s);
});
if (testAccept())
{
createDirs(dirOf(file));
writeFile(file, to.s);
GTEST_SKIP() << updatingGoldenMaster;
}
else
{
auto expected = readFile(file);
ASSERT_EQ(to.s, expected);
}
} }
}; };
#define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \ #define VERSIONED_CHARACTERIZATION_TEST(FIXTURE, NAME, STEM, VERSION, VALUE) \
TEST_F(FIXTURE, NAME ## _read) { \ TEST_F(FIXTURE, NAME ## _read) { \
readTest(STEM, VERSION, VALUE); \ readProtoTest(STEM, VERSION, VALUE); \
} \ } \
TEST_F(FIXTURE, NAME ## _write) { \ TEST_F(FIXTURE, NAME ## _write) { \
writeTest(STEM, VERSION, VALUE); \ writeProtoTest(STEM, VERSION, VALUE); \
} }
} }

View file

@ -0,0 +1,111 @@
#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
*/
template<typename T>
void writeTest(
PathView testStem,
std::invocable<> auto && test,
std::invocable<const Path &> auto && readFile2,
std::invocable<const Path &, const T &> auto && writeFile2)
{
auto file = goldenMaster(testStem);
T got = test();
if (testAccept())
{
createDirs(dirOf(file));
writeFile2(file, got);
GTEST_SKIP()
<< "Updating golden master "
<< file;
}
else
{
T expected = readFile2(file);
ASSERT_EQ(got, expected);
}
}
/**
* Specialize to `std::string`
*/
void writeTest(PathView testStem, auto && test)
{
writeTest<std::string>(
testStem, test,
[](const Path & f) -> std::string {
return readFile(f);
},
[](const Path & f, const std::string & c) {
return writeFile(f, c);
});
}
};
}