-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest_config.cpp
More file actions
313 lines (267 loc) · 12 KB
/
Copy pathtest_config.cpp
File metadata and controls
313 lines (267 loc) · 12 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <gtest/gtest.h>
#include <fstream>
#include <sstream>
#include <string>
import std;
import mcpp.config;
import mcpp.fallback.config_migration;
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);
}
// The official global `xim` index must NOT be injected/exposed into the project
// scope. `xim` (and its dynamic sub-indexes) are xlings GLOBAL-default indices —
// global IS the default scope; injecting xim as a project repo made every `xim:*`
// tool (cmake/glibc/gcc/make) resolve project-scoped and install into the project
// store instead of the shared registry, breaking ELF loader resolution for
// build-dep tools. `xim:*` resolves at global scope via the registry-local clone.
// See .agents/docs/2026-07-09-project-index-scope-global-infra-fix.md.
TEST(Config, ProjectIndexDirDoesNotInjectOfficialXimIndex) {
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));
// xim is NOT copied/exposed into the project data dir (it stays global).
auto projectOfficial =
project / ".mcpp" / ".xlings" / "data" / "xim-pkgindex";
EXPECT_FALSE(std::filesystem::exists(projectOfficial / "pkgs" / "p" / "python.lua"));
// The user-declared local `compat` index IS exposed project-locally.
auto projectLocal = project / ".mcpp" / ".xlings" / "data" / "compat";
EXPECT_TRUE(std::filesystem::exists(projectLocal / "pkgs"));
// The seeded project .xlings.json index_repos must not contain `xim`.
auto projJson = project / ".mcpp" / ".xlings.json";
if (std::filesystem::exists(projJson)) {
std::ifstream is(projJson);
std::stringstream ss; ss << is.rdbuf();
EXPECT_EQ(ss.str().find("\"xim\""), std::string::npos);
}
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);
}
// ── #267-A/#269: index org migration + artifact adoption ────────────────────
namespace {
std::string read_all(const std::filesystem::path& p) {
std::stringstream ss;
ss << std::ifstream(p).rdbuf();
return ss.str();
}
} // namespace
TEST(ConfigIndexMigration, CanonicalizeRewritesOrgAndName) {
mcpp::config::GlobalConfig cfg;
cfg.defaultIndex = "mcpp-index";
cfg.indexRepos.push_back({ "mcpp-index",
std::string(mcpp::config::kMcpplibsIndexUrlLegacy) });
mcpp::config::canonicalize_legacy_index_names(cfg);
ASSERT_EQ(cfg.indexRepos.size(), 1u);
EXPECT_EQ(cfg.defaultIndex, "mcpplibs");
EXPECT_EQ(cfg.indexRepos[0].name, "mcpplibs");
EXPECT_EQ(cfg.indexRepos[0].url, mcpp::config::kMcpplibsIndexUrl);
}
TEST(ConfigIndexMigration, CanonicalizeFoldsLegacyAndDefaultEntries) {
mcpp::config::GlobalConfig cfg;
cfg.indexRepos.push_back({ "mcpplibs",
std::string(mcpp::config::kMcpplibsIndexUrlLegacy) });
cfg.indexRepos.push_back({ "mcpplibs",
std::string(mcpp::config::kMcpplibsIndexUrl) });
mcpp::config::canonicalize_legacy_index_names(cfg);
EXPECT_EQ(cfg.indexRepos.size(), 1u);
}
TEST(ConfigIndexMigration, CanonicalizeLeavesForeignReposAlone) {
mcpp::config::GlobalConfig cfg;
cfg.indexRepos.push_back({ "other", "https://example.com/other-index.git" });
mcpp::config::canonicalize_legacy_index_names(cfg);
ASSERT_EQ(cfg.indexRepos.size(), 1u);
EXPECT_EQ(cfg.indexRepos[0].name, "other");
EXPECT_EQ(cfg.indexRepos[0].url, "https://example.com/other-index.git");
}
TEST(ConfigIndexMigration, MigrateConfigTomlRewritesOrgUrlIdempotently) {
auto dir = make_tempdir("mcpp-migrate-toml");
auto p = dir / "config.toml";
{
std::ofstream os(p);
os << "[index]\ndefault = \"mcpplibs\"\n\n[index.repos.\"mcpplibs\"]\n"
"url = \"https://github.com/mcpp-community/mcpp-index.git\"\n";
}
EXPECT_TRUE(mcpp::fallback::migrate_config_toml_index_names(p));
auto text = read_all(p);
EXPECT_NE(text.find("github.com/mcpplibs/mcpp-index.git"), std::string::npos);
EXPECT_EQ(text.find("mcpp-community/mcpp-index"), std::string::npos);
EXPECT_FALSE(mcpp::fallback::migrate_config_toml_index_names(p)); // idempotent
EXPECT_EQ(read_all(p), text);
std::filesystem::remove_all(dir);
}
TEST(ConfigIndexMigration, CanonicalizeFillsDefaultArtifactForOfficialRepo) {
mcpp::config::GlobalConfig cfg;
cfg.indexRepos.push_back({ "mcpplibs",
std::string(mcpp::config::kMcpplibsIndexUrlLegacy) });
mcpp::config::canonicalize_legacy_index_names(cfg);
ASSERT_EQ(cfg.indexRepos.size(), 1u);
EXPECT_EQ(cfg.indexRepos[0].artifact, mcpp::config::kMcpplibsIndexArtifact);
}
TEST(ConfigIndexMigration, CanonicalizeRespectsExplicitGitSourceAndCustomArtifact) {
mcpp::config::GlobalConfig cfg;
mcpp::config::IndexRepo git{ "mcpplibs",
std::string(mcpp::config::kMcpplibsIndexUrl) };
git.source = "git";
cfg.indexRepos.push_back(git);
mcpp::config::IndexRepo custom{ "mirror",
std::string(mcpp::config::kMcpplibsIndexUrl) };
custom.artifact = "https://example.com/my-mirror";
cfg.indexRepos.push_back(custom);
cfg.indexRepos.push_back({ "other", "https://example.com/other-index.git" });
mcpp::config::canonicalize_legacy_index_names(cfg);
ASSERT_EQ(cfg.indexRepos.size(), 3u);
EXPECT_TRUE(cfg.indexRepos[0].artifact.empty()); // explicit git opt-out
EXPECT_EQ(cfg.indexRepos[1].artifact, "https://example.com/my-mirror");
EXPECT_TRUE(cfg.indexRepos[2].artifact.empty()); // foreign repo untouched
}
TEST(ConfigIndexMigration, XlingsJsonHealsPrettyLegacyEntry) {
auto dir = make_tempdir("mcpp-migrate-xj");
auto p = dir / ".xlings.json";
{
std::ofstream os(p);
os << "{\n \"index_repos\": [\n"
" { \"name\": \"mcpplibs\", \"url\": "
"\"https://github.com/mcpp-community/mcpp-index.git\" }\n"
" ],\n \"subos\": \"default\",\n \"mirror\": \"auto\"\n}\n";
}
EXPECT_TRUE(mcpp::fallback::migrate_xlings_json_index_names(p));
auto text = read_all(p);
EXPECT_NE(text.find(
"\"url\": \"https://github.com/mcpplibs/mcpp-index.git\", "
"\"artifact\": \"https://github.com/xlings-res/mcpp-index\""),
std::string::npos) << text;
EXPECT_NE(text.find("\"subos\": \"default\""), std::string::npos);
EXPECT_EQ(text.find("mcpp-community/mcpp-index"), std::string::npos);
EXPECT_FALSE(mcpp::fallback::migrate_xlings_json_index_names(p)); // idempotent
EXPECT_EQ(read_all(p), text);
std::filesystem::remove_all(dir);
}
TEST(ConfigIndexMigration, XlingsJsonHealsCompactLegacyNameAndUrl) {
auto dir = make_tempdir("mcpp-migrate-xjc");
auto p = dir / ".xlings.json";
{
std::ofstream os(p);
os << "{\"index_repos\":[{\"name\":\"mcpp-index\","
"\"url\":\"https://github.com/mcpp-community/mcpp-index.git\"}],"
"\"mirror\":\"auto\"}";
}
EXPECT_TRUE(mcpp::fallback::migrate_xlings_json_index_names(p));
auto text = read_all(p);
EXPECT_NE(text.find("\"name\":\"mcpplibs\""), std::string::npos);
EXPECT_NE(text.find(
"\"url\":\"https://github.com/mcpplibs/mcpp-index.git\", "
"\"artifact\": \"https://github.com/xlings-res/mcpp-index\""),
std::string::npos) << text;
EXPECT_FALSE(mcpp::fallback::migrate_xlings_json_index_names(p));
std::filesystem::remove_all(dir);
}
TEST(ConfigIndexMigration, XlingsJsonSkipsInjectionWhenArtifactPresent) {
auto dir = make_tempdir("mcpp-migrate-xja");
auto p = dir / ".xlings.json";
std::string body =
"{\n \"index_repos\": [\n"
" { \"name\": \"mcpplibs\", \"url\": "
"\"https://github.com/mcpplibs/mcpp-index.git\", "
"\"artifact\": \"https://github.com/xlings-res/mcpp-index\" }\n"
" ]\n}\n";
{
std::ofstream os(p);
os << body;
}
EXPECT_FALSE(mcpp::fallback::migrate_xlings_json_index_names(p));
EXPECT_EQ(read_all(p), body);
std::filesystem::remove_all(dir);
}