Nix 2.36.0
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 "nix/util/types.hh"
9
10namespace nix {
11
17static inline bool testAccept()
18{
19 return getEnv("_NIX_TEST_ACCEPT") == "1";
20}
21
25struct CharacterizationTest : virtual ::testing::Test
26{
31 virtual std::filesystem::path goldenMaster(std::string_view testStem) const = 0;
32
39 void readTest(std::string_view testStem, auto && test)
40 {
41 auto file = goldenMaster(testStem);
42
43 if (testAccept()) {
44 GTEST_SKIP() << "Cannot read golden master " << file << "because another test is also updating it";
45 } else {
46 test(readFile(file));
47 }
48 }
49
56 void writeTest(std::string_view testStem, auto && test, auto && readFile2, auto && writeFile2)
57 {
58 auto file = goldenMaster(testStem);
59
60 auto got = test();
61
62 if (testAccept()) {
63 createDirs(file.parent_path());
64 writeFile2(file, got);
65 GTEST_SKIP() << "Updating golden master " << file;
66 } else {
67 decltype(got) expected = readFile2(file);
68 ASSERT_EQ(got, expected);
69 }
70 }
71
75 void writeTest(std::string_view testStem, auto && test)
76 {
78 testStem,
79 test,
80 [](const std::filesystem::path & f) -> std::string { return readFile(f); },
81 [](const std::filesystem::path & f, const std::string & c) { return writeFile(f, c); });
82 }
83};
84
85} // namespace nix
Utilities for working with the file system and file paths.
Definition characterization.hh:26
void readTest(std::string_view testStem, auto &&test)
Definition characterization.hh:39
void writeTest(std::string_view testStem, auto &&test)
Definition characterization.hh:75
void writeTest(std::string_view testStem, auto &&test, auto &&readFile2, auto &&writeFile2)
Definition characterization.hh:56
virtual std::filesystem::path goldenMaster(std::string_view testStem) const =0