1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include "catch2/catch.hpp"
#include "src/buildtool/build_engine/target_map/result_map.hpp"
#include "src/buildtool/file_system/file_system_manager.hpp"
namespace {
[[nodiscard]] auto GetTestDir() -> std::filesystem::path {
auto* tmp_dir = std::getenv("TEST_TMPDIR");
if (tmp_dir != nullptr) {
return tmp_dir;
}
return FileSystemManager::GetCurrentDirectory() /
"test/buildtool/build_engine/target_map";
}
[[nodiscard]] auto CreateAnalysedTarget(
TargetResult const& result,
std::vector<ActionDescription> const& descs,
std::vector<std::string> const& blobs) -> AnalysedTargetPtr {
return std::make_shared<AnalysedTarget>(result,
descs,
blobs,
std::vector<Tree>(),
std::unordered_set<std::string>{},
std::set<std::string>{});
}
} // namespace
TEST_CASE("empty map", "[result_map]") {
using BuildMaps::Target::ResultTargetMap;
ResultTargetMap map{0};
CHECK(map.ToResult().actions.empty());
CHECK(map.ToResult().blobs.empty());
CHECK(map.ToJson() == R"({"actions": {}, "blobs": [], "trees": {}})"_json);
auto filename = (GetTestDir() / "test_empty.graph").string();
map.ToFile(filename);
std::ifstream file(filename);
nlohmann::json from_file{};
file >> from_file;
CHECK(from_file == R"({"actions": {}, "blobs": [], "trees": {}})"_json);
}
TEST_CASE("origins creation", "[result_map]") {
using BuildMaps::Base::EntityName;
using BuildMaps::Target::ResultTargetMap;
auto foo =
ActionDescription{{}, {}, Action{"run_foo", {"touch", "foo"}, {}}, {}};
auto bar =
ActionDescription{{}, {}, Action{"run_bar", {"touch", "bar"}, {}}, {}};
auto baz =
ActionDescription{{}, {}, Action{"run_baz", {"touch", "baz"}, {}}, {}};
ResultTargetMap map{0};
CHECK(map.Add(EntityName{"", ".", "foobar"},
{},
CreateAnalysedTarget(
{}, std::vector<ActionDescription>{foo, bar}, {})));
CHECK(map.Add(
EntityName{"", ".", "baz"},
{},
CreateAnalysedTarget({}, std::vector<ActionDescription>{baz}, {})));
auto result = map.ToResult();
REQUIRE(result.actions.size() == 3);
CHECK(result.blobs.empty());
auto expect_foo = foo.ToJson();
auto expect_bar = bar.ToJson();
auto expect_baz = baz.ToJson();
CHECK(map.ToJson() == nlohmann::json{{"actions",
{{foo.Id(), expect_foo},
{bar.Id(), expect_bar},
{baz.Id(), expect_baz}}},
{"blobs", nlohmann::json::array()},
{"trees", nlohmann::json::object()}});
expect_foo["origins"] =
R"([{"target": ["@", "", "", "foobar"], "config": {}, "subtask":
0}])"_json;
expect_bar["origins"] =
R"([{"target": ["@", "", "", "foobar"], "config": {}, "subtask":
1}])"_json;
expect_baz["origins"] =
R"([{"target": ["@", "", "", "baz"], "config": {}, "subtask":
0}])"_json;
auto filename = (GetTestDir() / "test_with_origins.graph").string();
map.ToFile(filename);
std::ifstream file(filename);
nlohmann::json from_file{};
file >> from_file;
CHECK(from_file == nlohmann::json{{"actions",
{{foo.Id(), expect_foo},
{bar.Id(), expect_bar},
{baz.Id(), expect_baz}}},
{"blobs", nlohmann::json::array()},
{"trees", nlohmann::json::object()}});
}
TEST_CASE("blobs uniqueness", "[result_map]") {
using BuildMaps::Base::EntityName;
using BuildMaps::Target::ResultTargetMap;
ResultTargetMap map{0};
CHECK(map.Add(EntityName{"", ".", "foobar"},
{},
CreateAnalysedTarget({}, {}, {"foo", "bar"})));
CHECK(map.Add(EntityName{"", ".", "barbaz"},
{},
CreateAnalysedTarget({}, {}, {"bar", "baz"})));
auto result = map.ToResult();
CHECK(result.actions.empty());
CHECK(result.blobs.size() == 3);
CHECK(map.ToJson() == nlohmann::json{{"actions", nlohmann::json::object()},
{"blobs", {"bar", "baz", "foo"}},
{"trees", nlohmann::json::object()}});
auto filename = (GetTestDir() / "test_unique_blobs.graph").string();
map.ToFile</*kIncludeOrigins=*/false>(filename);
std::ifstream file(filename);
nlohmann::json from_file{};
file >> from_file;
CHECK(from_file == nlohmann::json{{"actions", nlohmann::json::object()},
{"blobs", {"bar", "baz", "foo"}},
{"trees", nlohmann::json::object()}});
}
|