Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
characterization.hh
Go to the documentation of this file.
1#pragma once
3
4#include <gtest/gtest.h>
5
6#include "types.hh"
8#include "file-system.hh"
9
10namespace nix {
11
16static inline std::filesystem::path getUnitTestData() {
17 return getEnv("_NIX_TEST_UNIT_DATA").value();
18}
19
25static inline bool testAccept() {
26 return getEnv("_NIX_TEST_ACCEPT") == "1";
27}
28
32class CharacterizationTest : public virtual ::testing::Test
33{
34protected:
39 virtual std::filesystem::path goldenMaster(PathView testStem) const = 0;
40
41public:
48 void readTest(PathView testStem, auto && test)
49 {
50 auto file = goldenMaster(testStem);
51
52 if (testAccept())
53 {
54 GTEST_SKIP()
55 << "Cannot read golden master "
56 << file
57 << "because another test is also updating it";
58 }
59 else
60 {
61 test(readFile(file));
62 }
63 }
64
72 PathView testStem, auto && test, auto && readFile2, auto && writeFile2)
73 {
74 auto file = goldenMaster(testStem);
75
76 auto got = test();
77
78 if (testAccept())
79 {
80 std::filesystem::create_directories(file.parent_path());
81 writeFile2(file, got);
82 GTEST_SKIP()
83 << "Updating golden master "
84 << file;
85 }
86 else
87 {
88 decltype(got) expected = readFile2(file);
89 ASSERT_EQ(got, expected);
90 }
91 }
92
96 void writeTest(PathView testStem, auto && test)
97 {
99 testStem, test,
100 [](const std::filesystem::path & f) -> std::string {
101 return readFile(f);
102 },
103 [](const std::filesystem::path & f, const std::string & c) {
104 return writeFile(f, c);
105 });
106 }
107};
108
109}
Definition characterization.hh:33
void writeTest(PathView testStem, auto &&test)
Definition characterization.hh:96
void writeTest(PathView testStem, auto &&test, auto &&readFile2, auto &&writeFile2)
Definition characterization.hh:71
virtual std::filesystem::path goldenMaster(PathView testStem) const =0
void readTest(PathView testStem, auto &&test)
Definition characterization.hh:48
boost::format f(fs)
std::string readFile() const