Repository navigation
Expand file tree
/
Copy pathutils.test.js
More file actions
107 lines (97 loc) · 3.49 KB
/
Copy pathutils.test.js
File metadata and controls
107 lines (97 loc) · 3.49 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
// Tests for scripts/utils.js; run with `node --test scripts/utils.test.js`
const assert = require("assert");
const fs = require("fs");
const os = require("os");
const path = require("path");
const { test } = require("node:test");
const { escapeTemplateLiteral, getBuildEntries } = require("./utils.js");
// What a template literal with the escaped text in it evaluates to
const evaluate = (escaped) =>
// eslint-disable-next-line no-new-func
new Function(`return \`${escaped}\`;`)();
test("escaped text survives a template literal unchanged", () => {
[
"",
".a { color: red; }",
'.a::before { content: "\\201C"; }',
".a { content: '`'; }",
".a { content: '\\`'; }",
".a { content: '${b}'; }",
".a { content: '\\${b}'; }",
".a { content: '$'; } .b { content: '{'; }",
"\\\\",
"`${`\\",
].forEach((text) =>
assert.strictEqual(evaluate(escapeTemplateLiteral(text)), text)
);
});
test("backslashes are escaped before backticks", () => {
assert.strictEqual(escapeTemplateLiteral("`"), "\\`");
assert.strictEqual(escapeTemplateLiteral("\\"), "\\\\");
assert.strictEqual(escapeTemplateLiteral("\\`"), "\\\\\\`");
});
// A source directory with the given (empty) files, paths relative to it,
// removed after the test
function makeSourceDir(t, files) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "build-entries-"));
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
files.forEach((file) => {
fs.mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
fs.writeFileSync(path.join(dir, file), "");
});
return dir;
}
// The build entries of a source directory, relative to it and sorted
function relativeEntries(dir) {
return getBuildEntries(dir)
.map((entry) => path.relative(dir, entry).split(path.sep).join("/"))
.sort();
}
test("build entries: root, components, providers, react and utils", (t) => {
const dir = makeSourceDir(t, [
"index.ts",
"components/input/index.ts",
"components/input/input.ts",
"components/input/input.test.ts",
"components/input/tests/helper.ts",
"providers/api.ts",
"providers/tests/mock.ts",
"react/input.ts",
"utils/index.ts",
"utils/problem-details.ts",
"utils/problem-details.test.ts",
]);
assert.deepStrictEqual(relativeEntries(dir), [
"components/input/index.ts",
"components/input/input.ts",
"index.ts",
"providers/api.ts",
"react/input.ts",
"utils/index.ts",
]);
});
test("build entries: no utils entry without utils/index.ts", (t) => {
const dir = makeSourceDir(t, ["index.ts", "components/a/index.ts"]);
assert.deepStrictEqual(relativeEntries(dir), [
"components/a/index.ts",
"index.ts",
]);
});
// Apps import the utilities from `@elixir-cloud/design/utils` without
// registering the components; CI checks the built subpath after the build
// (`scripts/check-design-utils.mjs`)
test("design package builds and exports ./utils", () => {
const packageDir = path.join(__dirname, "..", "packages", "ecc-utils-design");
assert.ok(
relativeEntries(path.join(packageDir, "src")).includes("utils/index.ts")
);
const { exports, typesVersions } = JSON.parse(
fs.readFileSync(path.join(packageDir, "package.json"), "utf-8")
);
assert.deepStrictEqual(exports["./utils"], {
types: "./dist/utils/index.d.ts",
import: "./dist/utils/index.js",
});
// For TypeScript's `node` module resolution, which ignores `exports`
assert.deepStrictEqual(typesVersions["*"].utils, ["./dist/utils/index.d.ts"]);
});