-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_config.cpp
More file actions
141 lines (110 loc) · 4.63 KB
/
test_config.cpp
File metadata and controls
141 lines (110 loc) · 4.63 KB
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
140
141
#include <gtest/gtest.h>
import std;
import mcpp.config;
import mcpp.pm.index_spec;
namespace {
std::filesystem::path make_tempdir(std::string_view name) {
auto base = std::filesystem::temp_directory_path()
/ std::format("{}-{}", name, std::chrono::steady_clock::now().time_since_epoch().count());
std::filesystem::create_directories(base);
return base;
}
} // namespace
TEST(Config, ProjectXlingsDataRootsIncludeLegacyAndNestedLayouts) {
auto project = std::filesystem::path{"/tmp/mcpp-project"};
auto roots = mcpp::config::project_xlings_data_roots(project);
ASSERT_EQ(roots.size(), 2u);
EXPECT_EQ(roots[0], project / ".mcpp" / "data");
EXPECT_EQ(roots[1], project / ".mcpp" / ".xlings" / "data");
}
TEST(Config, ProjectIndexDataInitializedChecksNestedXlingsData) {
auto project = make_tempdir("mcpp-config-index-state");
auto nestedIndexDir = project / ".mcpp" / ".xlings" / "data" / "xim-index-repos";
std::filesystem::create_directories(nestedIndexDir);
{
std::ofstream os(nestedIndexDir / "xim-indexrepos.json");
os << "{}";
}
EXPECT_TRUE(mcpp::config::project_index_data_initialized(project));
std::filesystem::remove_all(project);
}
TEST(Config, ResolveProjectIndexPathUsesProjectRootForRelativeLocalIndex) {
auto project = std::filesystem::path{"/tmp/mcpp-project"};
mcpp::pm::IndexSpec spec;
spec.name = "xlings";
spec.path = "mcpp";
EXPECT_EQ(mcpp::config::resolve_project_index_path(project, spec),
(project / "mcpp").lexically_normal());
}
TEST(Config, ProjectIndexJsonEscapesLocalIndexPath) {
auto project = make_tempdir("mcpp-config-json-escape");
auto index = project / "local" / "index";
std::filesystem::create_directories(index / "pkgs");
mcpp::pm::IndexSpec local;
local.name = "local\"dev";
local.path = index;
mcpp::pm::IndexSpec remote;
remote.name = "remote";
remote.url = R"(https://example.com/a\b"c)";
std::map<std::string, mcpp::pm::IndexSpec> indices;
indices.emplace(local.name, local);
indices.emplace(remote.name, remote);
mcpp::config::GlobalConfig cfg;
ASSERT_TRUE(mcpp::config::ensure_project_index_dir(cfg, project, indices));
std::ifstream is(project / ".mcpp" / ".xlings.json");
ASSERT_TRUE(is);
std::stringstream ss;
ss << is.rdbuf();
auto content = ss.str();
EXPECT_NE(content.find(R"("name": "local\"dev")"), std::string::npos)
<< content;
EXPECT_NE(content.find(index.generic_string()), std::string::npos)
<< content;
EXPECT_NE(content.find(R"(https://example.com/a\\b\"c)"), std::string::npos)
<< content;
is.close();
std::filesystem::remove_all(project);
}
TEST(Config, ProjectIndexDirExposesOfficialXimIndex) {
auto project = make_tempdir("mcpp-config-project-xim-index");
auto registry = make_tempdir("mcpp-config-registry");
auto official = registry / "data" / "xim-pkgindex";
std::filesystem::create_directories(official / "pkgs" / "p");
{
std::ofstream os(official / "pkgs" / "p" / "python.lua");
os << "package = { name = \"python\" }\n";
}
auto localIndex = project / "compat";
std::filesystem::create_directories(localIndex / "pkgs" / "c");
mcpp::pm::IndexSpec local;
local.name = "compat";
local.path = localIndex;
std::map<std::string, mcpp::pm::IndexSpec> indices;
indices.emplace(local.name, local);
mcpp::config::GlobalConfig cfg;
cfg.registryDir = registry;
ASSERT_TRUE(mcpp::config::ensure_project_index_dir(cfg, project, indices));
auto projectOfficial =
project / ".mcpp" / ".xlings" / "data" / "xim-pkgindex";
EXPECT_TRUE(std::filesystem::exists(projectOfficial / "pkgs" / "p" / "python.lua"));
std::filesystem::remove_all(project);
std::filesystem::remove_all(registry);
}
TEST(Config, ProjectLocalIndexStaleCacheIsRemoved) {
auto project = make_tempdir("mcpp-config-local-index-cache");
auto localIndex = project / "compat";
std::filesystem::create_directories(localIndex / "pkgs" / "c");
{
std::ofstream os(localIndex / ".xlings-index-cache.json");
os << R"({"entries":{"compat.lz4":{"path":"/tmp/deleted/pkgs/c/compat.lz4.lua"}}})";
}
mcpp::pm::IndexSpec local;
local.name = "compat";
local.path = localIndex;
std::map<std::string, mcpp::pm::IndexSpec> indices;
indices.emplace(local.name, local);
mcpp::config::GlobalConfig cfg;
ASSERT_TRUE(mcpp::config::ensure_project_index_dir(cfg, project, indices));
EXPECT_FALSE(std::filesystem::exists(localIndex / ".xlings-index-cache.json"));
std::filesystem::remove_all(project);
}