|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include "nix/store/build-result.hh" |
| 4 | +#include "nix/util/tests/json-characterization.hh" |
| 5 | + |
| 6 | +namespace nix { |
| 7 | + |
| 8 | +class BuildResultTest : public virtual CharacterizationTest |
| 9 | +{ |
| 10 | + std::filesystem::path unitTestData = getUnitTestData() / "build-result"; |
| 11 | + |
| 12 | +public: |
| 13 | + std::filesystem::path goldenMaster(std::string_view testStem) const override |
| 14 | + { |
| 15 | + return unitTestData / testStem; |
| 16 | + } |
| 17 | +}; |
| 18 | + |
| 19 | +using nlohmann::json; |
| 20 | + |
| 21 | +struct BuildResultJsonTest : BuildResultTest, |
| 22 | + JsonCharacterizationTest<BuildResult>, |
| 23 | + ::testing::WithParamInterface<std::pair<std::string_view, BuildResult>> |
| 24 | +{}; |
| 25 | + |
| 26 | +TEST_P(BuildResultJsonTest, from_json) |
| 27 | +{ |
| 28 | + auto & [name, expected] = GetParam(); |
| 29 | + readJsonTest(name, expected); |
| 30 | +} |
| 31 | + |
| 32 | +TEST_P(BuildResultJsonTest, to_json) |
| 33 | +{ |
| 34 | + auto & [name, value] = GetParam(); |
| 35 | + writeJsonTest(name, value); |
| 36 | +} |
| 37 | + |
| 38 | +using namespace std::literals::chrono_literals; |
| 39 | + |
| 40 | +INSTANTIATE_TEST_SUITE_P( |
| 41 | + BuildResultJSON, |
| 42 | + BuildResultJsonTest, |
| 43 | + ::testing::Values( |
| 44 | + std::pair{ |
| 45 | + "not-deterministic", |
| 46 | + BuildResult{ |
| 47 | + .inner{BuildResult::Failure{ |
| 48 | + .status = BuildResult::Failure::NotDeterministic, |
| 49 | + .errorMsg = "no idea why", |
| 50 | + .isNonDeterministic = false, // Note: This field is separate from the status |
| 51 | + }}, |
| 52 | + .timesBuilt = 1, |
| 53 | + }, |
| 54 | + }, |
| 55 | + std::pair{ |
| 56 | + "output-rejected", |
| 57 | + BuildResult{ |
| 58 | + .inner{BuildResult::Failure{ |
| 59 | + .status = BuildResult::Failure::OutputRejected, |
| 60 | + .errorMsg = "no idea why", |
| 61 | + .isNonDeterministic = false, |
| 62 | + }}, |
| 63 | + .timesBuilt = 3, |
| 64 | + .startTime = 30, |
| 65 | + .stopTime = 50, |
| 66 | + }, |
| 67 | + }, |
| 68 | + std::pair{ |
| 69 | + "success", |
| 70 | + BuildResult{ |
| 71 | + .inner{BuildResult::Success{ |
| 72 | + .status = BuildResult::Success::Built, |
| 73 | + .builtOutputs{ |
| 74 | + { |
| 75 | + "foo", |
| 76 | + { |
| 77 | + { |
| 78 | + .outPath = StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-foo"}, |
| 79 | + }, |
| 80 | + DrvOutput{ |
| 81 | + .drvHash = Hash::parseSRI("sha256-b4afnqKCO9oWXgYHb9DeQ2berSwOjS27rSd9TxXDc/U="), |
| 82 | + .outputName = "foo", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + { |
| 87 | + "bar", |
| 88 | + { |
| 89 | + { |
| 90 | + .outPath = StorePath{"g1w7hy3qg1w7hy3qg1w7hy3qg1w7hy3q-bar"}, |
| 91 | + }, |
| 92 | + DrvOutput{ |
| 93 | + .drvHash = Hash::parseSRI("sha256-b4afnqKCO9oWXgYHb9DeQ2berSwOjS27rSd9TxXDc/U="), |
| 94 | + .outputName = "bar", |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }}, |
| 100 | + .timesBuilt = 3, |
| 101 | + .startTime = 30, |
| 102 | + .stopTime = 50, |
| 103 | + .cpuUser = std::chrono::microseconds(500s), |
| 104 | + .cpuSystem = std::chrono::microseconds(604s), |
| 105 | + }, |
| 106 | + })); |
| 107 | + |
| 108 | +} // namespace nix |
0 commit comments