Repository navigation
Expand file tree
/
Copy pathResolverTest.cpp
More file actions
552 lines (524 loc) · 23.3 KB
/
Copy pathResolverTest.cpp
File metadata and controls
552 lines (524 loc) · 23.3 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#include <atomic>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <new>
#include <optional>
#include <string>
#include <vector>
#include "FakePlatform.h"
#include "ns/AppFiles.h"
#include "ns/LoaderConfig.h"
#include "ns/Platform.h"
#include "ns/loader/ImportMap.h"
#include "ns/loader/ModuleIdentity.h"
#include "ns/loader/Resolver.h"
namespace {
std::atomic<long> allocations{0};
} // namespace
void* operator new(size_t size) {
allocations++;
void* p = std::malloc(size == 0 ? 1 : size);
if (p == nullptr) {
throw std::bad_alloc();
}
return p;
}
void operator delete(void* p) noexcept { std::free(p); }
void operator delete(void* p, size_t) noexcept { std::free(p); }
using namespace nativescript::common;
using namespace nativescript::common::loader;
using Kind = ModuleResolution::Kind;
namespace {
int failures = 0;
void Expect(const std::string& actual, const std::string& expected,
const std::string& what) {
if (actual != expected) {
failures++;
std::fprintf(stderr, "FAIL %s\n expected: %s\n actual: %s\n",
what.c_str(), expected.c_str(), actual.c_str());
}
}
const char* KindName(Kind kind) {
switch (kind) {
case Kind::kUnresolved:
return "unresolved";
case Kind::kBuiltin:
return "builtin";
case Kind::kHttp:
return "http";
case Kind::kFile:
return "file";
case Kind::kBlob:
return "blob";
}
return "?";
}
constexpr const char* kRoot = "/data/app";
LoaderConfig Config() {
LoaderConfig config;
config.appRoot = kRoot;
config.virtualFileRoots = {"/app"};
config.appRootAliases = {{"/data/alias", "/data"}};
return config;
}
void SetFiles() {
fake_platform::ClearFiles();
for (const char* path : {
"/data/app/main.mjs",
"/data/app/lib/util.mjs",
"/data/app/lib/plain.js",
"/data/app/pkg/index.mjs",
"/data/app/legacy/index.js",
"/data/app/app/under.mjs",
"/data/app/dual",
"/data/app/dual.mjs",
"/data/app/h#.mjs",
"/etc/outside.mjs",
"/data/appx/y.mjs",
"/data/appx.mjs",
"/y.mjs",
"etc/outside.mjs",
"/etc/escape.mjs",
"/data/app/etc/escape.mjs",
"/data/app/data/appx/y.mjs",
"/.mjs",
}) {
fake_platform::SetFile(path, "export {}", "t");
}
// Q3: no path API can open a name with a NUL in it.
fake_platform::SetFile(std::string("/data/app/lib/util.mjs\0x", 24),
"export {}", "t");
}
LoaderVocabulary Vocabulary() {
LoaderVocabulary vocabulary;
std::string error;
auto add = [&](ImportMapSection& section, const char* key,
const char* target) {
if (!AddImportMapEntry(section, "imports", key, target, &error)) {
failures++;
std::fprintf(stderr, "FAIL add %s: %s\n", key, error.c_str());
}
};
ParsedImportMap& map = vocabulary.importMap;
add(map.imports, "alias", "./lib/util.mjs");
add(map.imports, "fs-alias", "node:fs");
add(map.imports, "pre/", "./lib/");
add(map.imports, "remote", "https://h.example/r.mjs");
add(map.imports, "rooted", "/lib/plain");
add(map.imports, "blobbed", "blob:nativescript/1#x");
ImportMapSection scoped;
add(scoped, "alias", "./scoped.mjs");
map.scopes.emplace_back(ImportMapScopePrefix("https://h.example/app/"),
std::move(scoped));
ImportMapSection pkgScope;
add(pkgScope, "alias", "../lib/plain.js");
map.scopes.emplace_back(ImportMapScopePrefix("/data/app/pkg/"),
std::move(pkgScope));
FinishImportMap(map);
return vocabulary;
}
struct Case {
std::string specifier;
ResolveReferrer referrer;
Kind kind;
// kFile: the key; kHttp, kBlob: the url; kBuiltin: the specifier;
// kUnresolved: UnresolvedModuleMessage.
std::string expected;
ImportMapLookup lookup = ImportMapLookup::kApply;
};
std::string Answer(const ModuleResolution& r) {
switch (r.kind) {
case Kind::kFile:
return r.key;
case Kind::kHttp:
case Kind::kBlob:
return r.url;
case Kind::kBuiltin:
return r.specifier;
case Kind::kUnresolved:
return UnresolvedModuleMessage(r);
}
return std::string();
}
std::string Missing(const std::string& specifier, const std::string& tried) {
return "Cannot find module '" + specifier + "' (tried " + tried + ")";
}
std::string Blocked(const std::string& specifier, const std::string& importer,
const std::string& reason) {
return "Cannot resolve module specifier \"" + specifier +
"\" imported from " + importer + ": " + reason;
}
bool IsFileOrAppSpelling(const std::string& specifier) {
std::string scheme = specifier.substr(0, 5);
for (char& c : scheme) {
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return scheme == "file:" || scheme.substr(0, 4) == "app:";
}
// A resolved key and CanonicalizeModuleKey (invalidateModules,
// RegistryKeyFor) must agree: a key canonicalizes to itself, and an absolute
// spelling that names a file exactly (a file: or app: URL, or a path already
// under appRoot, Q7) canonicalizes to the key it resolves to. Relative, "~/"
// and bare specifiers, root-absolute paths prefixed with appRoot, and probed
// spellings name their file only through the resolver.
void ExpectKeyAgrees(const std::string& specifier, const ModuleResolution& r,
const LoaderConfig& config) {
if (r.kind != Kind::kFile) {
return;
}
Expect(CanonicalizeModuleKey(r.key, config, nullptr), r.key,
"the key of " + specifier + " is canonical");
std::optional<std::string> named;
if (IsFileOrAppSpelling(specifier)) {
named = FileUrlToPath(specifier, config);
} else if (!specifier.empty() && specifier[0] == '/') {
named = CanonicalFilePath(
specifier.substr(0, specifier.find_first_of("?#")), config);
}
if (named && *named == r.path) {
Expect(CanonicalizeModuleKey(specifier, config, nullptr), r.key,
"CanonicalizeModuleKey(" + specifier + ")");
}
}
// Spellings of one file whose scheme, case, query or fragment differ.
void TestKeySpellings() {
SetFiles();
const LoaderConfig config = Config();
const LoaderVocabulary empty;
const ResolveReferrer main{"/data/app/main.mjs", "/data/app/main.mjs"};
const std::vector<std::pair<std::string, std::string>> spellings = {
{"file:/data/app/lib/util.mjs", "/data/app/lib/util.mjs"},
{"FILE:///data/app/lib/util.mjs", "/data/app/lib/util.mjs"},
{"File://localhost/data/app/lib/util.mjs", "/data/app/lib/util.mjs"},
{"app:/lib/util.mjs", "/data/app/lib/util.mjs"},
{"APP:///lib/util.mjs", "/data/app/lib/util.mjs"},
{"file:///data/app/lib/util.mjs?a b", "/data/app/lib/util.mjs?a%20b"},
{"file:///data/app/lib/util.mjs?n=caf\xC3\xA9",
"/data/app/lib/util.mjs?n=caf%C3%A9"},
{"file:///data/app/lib/util.mjs?v=1#f", "/data/app/lib/util.mjs?v=1"},
{"/data/app/lib/util.mjs?a b", "/data/app/lib/util.mjs?a%20b"},
{"/data/app/lib/util.mjs?n=caf\xC3\xA9",
"/data/app/lib/util.mjs?n=caf%C3%A9"},
{"/data/app/lib/util.mjs?v=1#f", "/data/app/lib/util.mjs?v=1"},
{"/data/app/./lib/util.mjs?a/../b", "/data/app/lib/util.mjs?a/../b"},
{"file:///data/app/h%23.mjs?v=1", "/data/app/h#.mjs?v=1"},
};
for (const auto& [specifier, key] : spellings) {
const ModuleResolution r = ResolveSpecifier(specifier, main, empty, config);
Expect(KindName(r.kind), "file", specifier + ": kind");
Expect(r.key, key, specifier + ": key");
ExpectKeyAgrees(specifier, r, config);
}
}
void TestResolve() {
SetFiles();
const LoaderConfig config = Config();
const LoaderVocabulary vocabulary = Vocabulary();
const ResolveReferrer main{"/data/app/main.mjs", "/data/app/main.mjs"};
const ResolveReferrer pkg{"/data/app/pkg/index.mjs",
"/data/app/pkg/index.mjs"};
const ResolveReferrer queried{"/data/app/lib/util.mjs?v=1",
"/data/app/lib/util.mjs"};
const ResolveReferrer none{"", ""};
const ResolveReferrer blob{"blob:nativescript/1", ""};
const ResolveReferrer builtin{"ns:module", ""};
const ResolveReferrer queriedNoRecord{"/data/app/main.mjs?next=/lib/x", ""};
const std::string httpKey = "https://h.example/app/main.mjs";
const ResolveReferrer http{httpKey, ""};
const std::string bareReason =
"it is not in the import map (bare specifiers from an HTTP module must "
"be mapped).";
const std::vector<Case> cases = {
// Builtins, before the import map.
{"ns:fs", main, Kind::kBuiltin, "ns:fs"},
{"node:path", http, Kind::kBuiltin, "node:path"},
{"ns:", main, Kind::kBuiltin, "ns:"},
// Relative, joined lexically against the importer's file.
{"./lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"./lib/../lib/./util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"../app/lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"./plain", queried, Kind::kFile, "/data/app/lib/plain.js"},
{"./lib/util.mjs", none, Kind::kFile, "/data/app/lib/util.mjs"},
// An importer with no absolute path has no directory to join with.
{"../etc/outside.mjs", blob, Kind::kUnresolved,
Missing("../etc/outside.mjs", "")},
{"./etc/outside.mjs", builtin, Kind::kUnresolved,
Missing("./etc/outside.mjs", "")},
// A key's query is not its directory.
{"./lib/util.mjs", queriedNoRecord, Kind::kFile,
"/data/app/lib/util.mjs"},
// Probe order: as written, .mjs, .js, index.mjs, index.js.
{"./lib/util", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"./lib/plain", main, Kind::kFile, "/data/app/lib/plain.js"},
{"./dual", main, Kind::kFile, "/data/app/dual"},
{"./pkg", main, Kind::kFile, "/data/app/pkg/index.mjs"},
{"./pkg/", main, Kind::kFile, "/data/app/pkg/index.mjs"},
{"./legacy", main, Kind::kFile, "/data/app/legacy/index.js"},
{"./nope", main, Kind::kUnresolved, Missing("./nope", "/data/app/nope")},
// The query is part of the key; the fragment is not (Q8: nothing
// before them resolves to nothing).
{"./lib/util.mjs?v=1#f", main, Kind::kFile, "/data/app/lib/util.mjs?v=1"},
{"./lib/util?v=1", main, Kind::kFile, "/data/app/lib/util.mjs?v=1"},
{"?v=1", main, Kind::kUnresolved, Missing("?v=1", "")},
{"#f", main, Kind::kUnresolved, Missing("#f", "")},
// A NUL, written or after the import map, names no file.
{std::string("./lib/util.mjs\0x", 16), main, Kind::kUnresolved,
Missing(std::string("./lib/util.mjs\0x", 16), "")},
{std::string("pre/util.mjs\0x", 14), main, Kind::kUnresolved,
Missing(std::string("./lib/util.mjs\0x", 16), "")},
// "~/": appRoot, then appRoot/app.
{"~/lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"~/under.mjs", main, Kind::kFile, "/data/app/app/under.mjs"},
{"~/nope", main, Kind::kUnresolved,
Missing("~/nope", "/data/app/app/nope")},
// Root-absolute: under appRoot, then without a leading /app; a path
// already under appRoot as is (Q7).
{"/lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"/app/lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"/app/under.mjs", main, Kind::kFile, "/data/app/app/under.mjs"},
{"/data/app/lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"/data/alias/app/lib/util.mjs", main, Kind::kFile,
"/data/app/lib/util.mjs"},
{"/etc/outside.mjs", main, Kind::kUnresolved,
Missing("/etc/outside.mjs", "/data/app/etc/outside.mjs")},
{"/data/appx/y.mjs", main, Kind::kFile, "/data/app/data/appx/y.mjs"},
// "~/", root-absolute and bare forms name paths below appRoot: a ".."
// at appRoot is dropped, as at a URL's root.
{"~/../../etc/escape.mjs", main, Kind::kFile, "/data/app/etc/escape.mjs"},
{"~/../app/under.mjs", main, Kind::kFile, "/data/app/app/under.mjs"},
{"/../../etc/escape.mjs", main, Kind::kFile, "/data/app/etc/escape.mjs"},
{"/app/../../etc/escape.mjs", main, Kind::kFile,
"/data/app/etc/escape.mjs"},
{"/data/app/../../etc/escape.mjs", main, Kind::kFile,
"/data/app/etc/escape.mjs"},
{"lib/../../../etc/escape.mjs", main, Kind::kFile,
"/data/app/etc/escape.mjs"},
{"~/../../nope", main, Kind::kUnresolved,
Missing("~/../../nope", "/data/app/app/nope")},
// A relative specifier is joined with the importer's directory, and
// may leave appRoot (A3).
{"../../etc/escape.mjs", main, Kind::kFile, "/etc/escape.mjs"},
// Bare, unmapped: under appRoot.
{"lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"lodash", main, Kind::kUnresolved,
Missing("lodash", "/data/app/lodash")},
// file: and app: URLs (A3, A10, Q3, Q10).
{"file:///data/app/lib/util.mjs", main, Kind::kFile,
"/data/app/lib/util.mjs"},
{"file:///data/app/lib/util", main, Kind::kFile,
"/data/app/lib/util.mjs"},
{"file:///data/app/lib/util.mjs?v=2#f", main, Kind::kFile,
"/data/app/lib/util.mjs?v=2"},
{"file:///etc/outside.mjs", main, Kind::kFile, "/etc/outside.mjs"},
{"file:///app/lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"file://localhost/etc/outside.mjs", main, Kind::kFile,
"/etc/outside.mjs"},
{"file:///data/app/h%23.mjs", main, Kind::kFile, "/data/app/h#.mjs"},
{"file:///", main, Kind::kFile, "/.mjs"},
{"app:///lib/util.mjs", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"app://localhost/lib/util.mjs", main, Kind::kFile,
"/data/app/lib/util.mjs"},
{"app:x.mjs", main, Kind::kUnresolved, Missing("app:x.mjs", "app:x.mjs")},
{"app:x/../../y.mjs", main, Kind::kUnresolved,
Missing("app:x/../../y.mjs", "app:x/../../y.mjs")},
{"app:///%2e%2e/%2e%2e/etc/outside.mjs", main, Kind::kUnresolved,
Missing("app:///%2e%2e/%2e%2e/etc/outside.mjs",
"/data/app/etc/outside.mjs")},
{"file://host/etc/outside.mjs", main, Kind::kUnresolved,
Missing("file://host/etc/outside.mjs", "file://host/etc/outside.mjs")},
{"file:///data/app/a%2Fb.mjs", main, Kind::kUnresolved,
Missing("file:///data/app/a%2Fb.mjs", "file:///data/app/a%2Fb.mjs")},
// The import map (A9): a target may name a builtin, an http(s) URL, a
// blob: URL; it is resolved from the importer, never mapped again.
{"alias", main, Kind::kFile, "/data/app/lib/util.mjs"},
{"alias", pkg, Kind::kFile, "/data/app/lib/plain.js"},
{"fs-alias", main, Kind::kBuiltin, "node:fs"},
{"pre/plain", main, Kind::kFile, "/data/app/lib/plain.js"},
{"pre/util?v=3", main, Kind::kFile, "/data/app/lib/util.mjs?v=3"},
{"remote", main, Kind::kHttp, "https://h.example/r.mjs"},
{"rooted", main, Kind::kFile, "/data/app/lib/plain.js"},
{"blobbed", main, Kind::kBlob, "blob:nativescript/1"},
{"alias", main, Kind::kUnresolved, Missing("alias", "/data/app/alias"),
ImportMapLookup::kSkip},
// blob: and http(s) URLs.
{"blob:nativescript/2#frag", main, Kind::kBlob, "blob:nativescript/2"},
{"https://h.example/x.mjs", main, Kind::kHttp, "https://h.example/x.mjs"},
{"HTTPS://h.example/x.mjs", main, Kind::kHttp, "https://h.example/x.mjs"},
{"Http://h.example/x.mjs", main, Kind::kHttp, "http://h.example/x.mjs"},
{"BLOB:nativescript/3", main, Kind::kBlob, "blob:nativescript/3"},
// An http(s) importer: a browser's rules.
{"./x.mjs", http, Kind::kHttp, "https://h.example/app/x.mjs"},
{"../x.mjs", http, Kind::kHttp, "https://h.example/x.mjs"},
{"/x.mjs", http, Kind::kHttp, "https://h.example/x.mjs"},
{"//other.example/x.mjs", http, Kind::kHttp,
"https://other.example/x.mjs"},
{"https://other.example/y.mjs", http, Kind::kHttp,
"https://other.example/y.mjs"},
{"lodash", http, Kind::kUnresolved,
Blocked("lodash", httpKey, bareReason)},
{".x", http, Kind::kUnresolved, Blocked(".x", httpKey, bareReason)},
{"~/x.mjs", http, Kind::kUnresolved,
Blocked("~/x.mjs", httpKey,
"app-relative \"~/\" paths are not available to modules loaded "
"over HTTP (use a file:// URL, an import-map entry, or "
"createRequire).")},
{"foo://x/y", http, Kind::kUnresolved, Missing("foo://x/y", "foo://x/y")},
{"file:///data/app/lib/util.mjs", http, Kind::kFile,
"/data/app/lib/util.mjs"},
{"app:///lib/util.mjs", http, Kind::kFile, "/data/app/lib/util.mjs"},
{"alias", http, Kind::kHttp, "https://h.example/app/scoped.mjs"},
{"pre/plain.js", http, Kind::kHttp, "https://h.example/app/lib/plain.js"},
{"remote", http, Kind::kHttp, "https://h.example/r.mjs"},
};
for (const Case& c : cases) {
const ModuleResolution r =
ResolveSpecifier(c.specifier, c.referrer, vocabulary, config, c.lookup);
ExpectKeyAgrees(c.specifier, r, config);
const std::string what =
c.specifier + " from '" + std::string(c.referrer.key) + "'" +
(c.lookup == ImportMapLookup::kSkip ? " (skip)" : "");
Expect(KindName(r.kind), KindName(c.kind), what + ": kind");
Expect(Answer(r), c.expected, what);
if (r.kind == Kind::kFile) {
Expect(r.path + r.query, r.key, what + ": key is path + query");
}
}
#if defined(NS_TEST_URL_PARSER_ADA)
// The parser reads a scheme after leading C0 controls and spaces, with
// tabs and newlines anywhere dropped; the blob: test must too.
Expect(
ResolveSpecifier(" \tbL\nob:nativescript/4#f", main, vocabulary, config)
.url,
"blob:nativescript/4", "a blob: URL as the parser reads it");
#endif
const ModuleResolution mapped =
ResolveSpecifier("alias", main, vocabulary, config);
Expect(mapped.specifier, "./lib/util.mjs", "specifier after the map");
Expect(ResolveSpecifier("", main, vocabulary, config).attempted, "",
"an empty specifier");
}
// What one resolve allocates, beyond the fake platform::Stat (which copies
// its argument into a std::string). Each Stat above builds its argument
// (the "- 1").
//
// The counts are pinned under libc++, both runtimes' standard library: its
// short-string buffer holds 22 bytes, and a string that fits allocates
// nothing. Rows that parse a URL are pinned only against ada: the fake
// parser allocates differently. Every lane, ada's included, uses the fake
// platform's NormalizeHttpModuleUrl.
void CheckAllocations() {
// An app root as long as a real one, so no path fits a short-string buffer.
const std::string root =
"/private/var/containers/Bundle/Application/"
"0F5C7E2A-1B3D-4E5F-8A9B-0C1D2E3F4A5B/App.app/app";
fake_platform::ClearFiles();
for (const char* file :
{"/main.mjs", "/lib/util.mjs", "/lib/plain.js", "/legacy/index.js",
"/app/under.mjs", "/helpers/with-a-long-name/h.mjs",
"/buffer/with-a-long-name/b.mjs"}) {
fake_platform::SetFile(root + file, "export {}", "t");
}
LoaderConfig config = Config();
config.appRoot = root;
const LoaderVocabulary empty;
const LoaderVocabulary vocabulary = Vocabulary();
fake_platform::SetTraceEnabled(false);
const std::string mainPath = root + "/main.mjs";
const ResolveReferrer main{mainPath, mainPath};
const long beforeMiss = allocations;
(void)platform::Stat(root + "/not-there.mjs");
const long statMiss = allocations - beforeMiss - 1;
const long beforeHit = allocations;
(void)platform::Stat(root + "/lib/util.mjs");
const long statHit = allocations - beforeHit - 1;
const ResolveReferrer http{"https://h.example/app/main.mjs", ""};
struct Row {
const char* label;
std::string specifier;
const LoaderVocabulary* vocabulary;
const ResolveReferrer* referrer;
long expected;
bool parsesUrl = false;
};
const std::vector<Row> rows = {
// The candidate, which becomes `path`, and `key`.
{"relative hit, no map", "./lib/util.mjs", &empty, &main, 2},
{"relative hit, map present", "./lib/util.mjs", &vocabulary, &main, 2},
{"bare hit", "lib/util.mjs", &empty, &main, 2},
{"~/ hit on the first candidate", "~/lib/util.mjs", &empty, &main, 2},
{"root-absolute hit", "/lib/util.mjs", &empty, &main, 2},
// One buffer shared by every extension and index probe.
{"relative, .mjs probe", "./lib/util", &empty, &main, 3},
{"relative, index.js probe", "./legacy", &empty, &main, 3},
// The second "~/" candidate, built after the first misses.
{"~/ hit on the second candidate", "~/under.mjs", &empty, &main, 4},
// A miss keeps the last candidate as `attempted`.
{"relative miss", "./nope.mjs", &empty, &main, 2},
{"bare miss", "lodash", &empty, &main, 2},
// The import-map target fits the short-string buffer.
{"mapped exact", "alias", &vocabulary, &main, 2},
{"mapped prefix", "pre/plain", &vocabulary, &main, 3},
// A specifier past the short-string buffer is copied into
// `specifier`; so is a query into `query`.
{"relative, short query", "./lib/util.mjs?v=1", &empty, &main, 2},
{"relative, long specifier", "./lib/util.mjs?t=1700000000000&v=1", &empty,
&main, 3},
{"relative, long query", "./lib/util.mjs?t=1700000000000&hot=12", &empty,
&main, 4},
{"root-absolute under appRoot", root + "/lib/util.mjs", &empty, &main, 3},
// A long name starting with 'h' or 'b' is not copied for the http(s)
// and blob: tests.
{"bare, long, starting with h", "helpers/with-a-long-name/h.mjs", &empty,
&main, 3},
{"bare, long, starting with b", "buffer/with-a-long-name/b.mjs", &empty,
&main, 3},
{"builtin", "ns:fs", &vocabulary, &main, 0},
{"file: URL", "file://" + root + "/lib/util.mjs", &empty, &main, 7, true},
{"file:///app URL", "file:///app/lib/util.mjs", &empty, &main, 5, true},
{"app: URL", "app:///lib/util.mjs", &empty, &main, 3, true},
{"http URL", "https://h.example/x.mjs", &empty, &main, 3, true},
{"relative from an http importer", "./x.mjs", &empty, &http, 3, true},
};
#if defined(_LIBCPP_VERSION)
const bool pinned = true;
#else
const bool pinned = false;
#endif
#if defined(NS_TEST_URL_PARSER_ADA)
const bool parserPinned = pinned;
#else
const bool parserPinned = false;
#endif
std::printf("allocations per resolve, tracing off (fake Stat excluded):\n");
for (const Row& row : rows) {
// Warm once: the first call may initialise statics.
(void)ResolveSpecifier(row.specifier, *row.referrer, *row.vocabulary,
config);
const int statsBefore = fake_platform::statCalls;
const long before = allocations;
const ModuleResolution r =
ResolveSpecifier(row.specifier, *row.referrer, *row.vocabulary, config);
const long total = allocations - before;
const int stats = fake_platform::statCalls - statsBefore;
const int hits = r.kind == Kind::kFile ? 1 : 0;
const long own = total - (stats - hits) * statMiss - hits * statHit;
std::printf(" %-34s %ld (%d stat)\n", row.label, own, stats);
if (row.parsesUrl ? parserPinned : pinned) {
Expect(std::to_string(own), std::to_string(row.expected),
std::string("allocations: ") + row.label);
}
}
}
} // namespace
int main() {
TestResolve();
TestKeySpellings();
CheckAllocations();
if (failures != 0) {
std::fprintf(stderr, "%d failure(s)\n", failures);
return 1;
}
std::printf("ResolverTest: ok\n");
return 0;
}