Repository navigation
Expand file tree
/
Copy pathsync-icons.test.ts
More file actions
353 lines (299 loc) · 9.58 KB
/
Copy pathsync-icons.test.ts
File metadata and controls
353 lines (299 loc) · 9.58 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
import { describe, expect, test } from "bun:test";
import fs from "node:fs";
import path from "node:path";
import type { IconManifest } from "./icons";
import { syncIcons } from "./sync-icons";
import { useTmpDirs, VALID_SVG } from "./test-helpers";
const tmp = useTmpDirs();
/** Builds a fake devicon package containing the given `icons/<name>/<name>-<variant>.svg` files. */
const fakeDevicon = (icons: Record<string, string>) => {
const dir = tmp();
fs.writeFileSync(path.join(dir, "LICENSE"), "MIT License");
for (const [file, content] of Object.entries(icons)) {
const name = file.split("-")[0] ?? file;
fs.mkdirSync(path.join(dir, "icons", name), { recursive: true });
fs.writeFileSync(path.join(dir, "icons", name, `${file}.svg`), content);
}
return dir;
};
describe("syncIcons", () => {
test("copies the original variant and the devicon licence", () => {
const deviconDir = fakeDevicon({ "foo-original": VALID_SVG });
const outDir = tmp();
const manifest: IconManifest = { foo: { source: "devicon", name: "foo" } };
syncIcons({ manifest, deviconDir, outDir });
expect(fs.readFileSync(path.join(outDir, "foo.svg"), "utf-8")).toBe(
VALID_SVG,
);
expect(fs.readFileSync(path.join(outDir, "LICENSE.devicon"), "utf-8")).toBe(
"MIT License",
);
});
test("uses an explicit variant", () => {
const deviconDir = fakeDevicon({
"foo-original": VALID_SVG,
"foo-line": VALID_SVG.replace("M0 0h1v1z", "M1 1h1v1z"),
});
const outDir = tmp();
syncIcons({
manifest: { foo: { source: "devicon", name: "foo", variant: "line" } },
deviconDir,
outDir,
});
expect(fs.readFileSync(path.join(outDir, "foo.svg"), "utf-8")).toContain(
"M1 1h1v1z",
);
});
test("writes a dark variant with the requested root fill", () => {
const deviconDir = fakeDevicon({ "foo-original": VALID_SVG });
const outDir = tmp();
syncIcons({
manifest: {
foo: { source: "devicon", name: "foo", dark: { fill: "#ffffff" } },
},
deviconDir,
outDir,
});
const dark = fs.readFileSync(path.join(outDir, "foo.dark.svg"), "utf-8");
expect(dark.match(/<svg[^>]*>/)?.[0]).toContain('fill="#ffffff"');
});
test("is idempotent", () => {
const deviconDir = fakeDevicon({ "foo-original": VALID_SVG });
const outDir = tmp();
const opts = {
manifest: {
foo: { source: "devicon", name: "foo", dark: { fill: "#ffffff" } },
} satisfies IconManifest,
deviconDir,
outDir,
};
const read = () =>
fs
.readdirSync(outDir)
.toSorted()
.map((f) => [f, fs.readFileSync(path.join(outDir, f), "utf-8")]);
syncIcons(opts);
const first = read();
syncIcons(opts);
expect(read()).toEqual(first);
});
test("writes nothing when any manifest entry names a missing icon", () => {
const deviconDir = fakeDevicon({ "good-original": VALID_SVG });
const outDir = tmp();
const manifest: IconManifest = {
good: { source: "devicon", name: "good" },
bad: { source: "devicon", name: "nonexistent" },
};
expect(() => syncIcons({ manifest, deviconDir, outDir })).toThrow(/bad/);
expect(fs.readdirSync(outDir)).toEqual([]);
});
test("skips custom entries", () => {
const outDir = tmp();
syncIcons({
manifest: { general: { source: "custom", license: "CC0" } },
deviconDir: fakeDevicon({}),
outDir,
});
expect(fs.readdirSync(outDir).filter((f) => f.endsWith(".svg"))).toEqual(
[],
);
});
describe("stale files", () => {
const devicon = (dark = false): IconManifest => ({
foo: {
source: "devicon",
name: "foo",
...(dark ? { dark: { fill: "#ffffff" } } : {}),
},
});
const ls = (dir: string) => fs.readdirSync(dir).toSorted();
test("removes a dark variant the manifest no longer asks for", () => {
const deviconDir = fakeDevicon({ "foo-original": VALID_SVG });
const outDir = tmp();
syncIcons({ manifest: devicon(true), deviconDir, outDir });
const removed = syncIcons({ manifest: devicon(), deviconDir, outDir });
expect(ls(outDir)).toEqual(["LICENSE.devicon", "foo.svg"]);
expect(removed).toEqual(["foo.dark.svg"]);
});
test("removes icons for entries deleted from the manifest", () => {
const deviconDir = fakeDevicon({
"foo-original": VALID_SVG,
"bar-original": VALID_SVG,
});
const outDir = tmp();
const both: IconManifest = {
...devicon(),
bar: { source: "devicon", name: "bar" },
};
syncIcons({ manifest: both, deviconDir, outDir });
syncIcons({ manifest: devicon(), deviconDir, outDir });
expect(ls(outDir)).toEqual(["LICENSE.devicon", "foo.svg"]);
});
test("removes the licence and notice once nothing needs them", () => {
const deviconDir = fakeDevicon({ "foo-original": VALID_SVG });
const outDir = tmp();
fs.writeFileSync(path.join(outDir, "NOTICE.md"), "old");
syncIcons({ manifest: devicon(), deviconDir, outDir });
expect(ls(outDir)).toEqual(["LICENSE.devicon", "foo.svg"]);
syncIcons({
manifest: { foo: { source: "custom", license: "MIT" } },
deviconDir,
outDir,
});
expect(ls(outDir)).toEqual(["foo.svg"]);
});
test("keeps hand-made icons of custom entries, and files it doesn't manage", () => {
const outDir = tmp();
for (const f of ["foo.svg", "foo.dark.svg", "manifest.yaml"]) {
fs.writeFileSync(path.join(outDir, f), "mine");
}
const removed = syncIcons({
manifest: { foo: { source: "custom", license: "MIT" } },
deviconDir: fakeDevicon({}),
outDir,
});
expect(ls(outDir)).toEqual(["foo.dark.svg", "foo.svg", "manifest.yaml"]);
expect(removed).toEqual([]);
});
test("removes an svg that belongs to no manifest entry", () => {
const outDir = tmp();
fs.writeFileSync(path.join(outDir, "ghost.svg"), VALID_SVG);
const removed = syncIcons({
manifest: {},
deviconDir: fakeDevicon({}),
outDir,
});
expect(ls(outDir)).toEqual([]);
expect(removed).toEqual(["ghost.svg"]);
});
test("removes nothing when the sync fails", () => {
const outDir = tmp();
fs.writeFileSync(path.join(outDir, "ghost.svg"), VALID_SVG);
expect(() =>
syncIcons({
manifest: devicon(),
deviconDir: fakeDevicon({}),
outDir,
}),
).toThrow();
expect(ls(outDir)).toEqual(["ghost.svg"]);
});
});
describe("iconify", () => {
/** A fake loader serving the given sets, as `@iconify-json/<set>` would. */
const fakeIconify =
(sets: Record<string, Record<string, string>>) => (set: string) => {
const icons = sets[set];
if (!icons) {
throw new Error(`Cannot find module '@iconify-json/${set}'`);
}
return {
icons: {
prefix: set,
width: 36,
height: 36,
icons: Object.fromEntries(
Object.entries(icons).map(([name, body]) => [name, { body }]),
),
},
info: {
name: `${set} set`,
author: { name: "Some Author", url: "https://author.test" },
license: {
title: "CC BY 4.0",
spdx: "CC-BY-4.0",
url: "https://license.test",
},
},
};
};
const manifest: IconManifest = {
general: { source: "iconify", set: "fake", name: "books" },
};
test("wraps the icon body in an svg with the set's viewBox", () => {
const outDir = tmp();
syncIcons({
manifest,
deviconDir: fakeDevicon({}),
loadIconSet: fakeIconify({ fake: { books: '<path d="M0 0h1v1z"/>' } }),
outDir,
});
expect(fs.readFileSync(path.join(outDir, "general.svg"), "utf-8")).toBe(
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 36"><path d="M0 0h1v1z"/></svg>',
);
});
test("writes a notice crediting each set used, from its metadata", () => {
const outDir = tmp();
syncIcons({
manifest: {
...manifest,
other: { source: "iconify", set: "second", name: "x" },
},
deviconDir: fakeDevicon({}),
loadIconSet: fakeIconify({
fake: { books: "<path/>" },
second: { x: "<path/>" },
}),
outDir,
});
const notice = fs.readFileSync(path.join(outDir, "NOTICE.md"), "utf-8");
expect(notice).toContain("fake set");
expect(notice).toContain("second set");
expect(notice).toContain("Some Author");
expect(notice).toContain("https://author.test");
expect(notice).toContain("CC BY 4.0");
expect(notice).toContain("https://license.test");
});
test("writes no notice when no iconify icons are used", () => {
const outDir = tmp();
syncIcons({
manifest: { foo: { source: "devicon", name: "foo" } },
deviconDir: fakeDevicon({ "foo-original": VALID_SVG }),
outDir,
});
expect(fs.existsSync(path.join(outDir, "NOTICE.md"))).toBe(false);
});
test("writes nothing when a named icon is missing from its set", () => {
const outDir = tmp();
expect(() =>
syncIcons({
manifest: {
...manifest,
good: { source: "iconify", set: "fake", name: "brain" },
},
deviconDir: fakeDevicon({}),
loadIconSet: fakeIconify({ fake: { brain: "<path/>" } }),
outDir,
}),
).toThrow(/general.*books/);
expect(fs.readdirSync(outDir)).toEqual([]);
});
test("throws naming the set when its package is not installed", () => {
expect(() =>
syncIcons({
manifest,
deviconDir: fakeDevicon({}),
loadIconSet: fakeIconify({}),
outDir: tmp(),
}),
).toThrow(/@iconify-json\/fake/);
});
test("loads each set once however many icons use it", () => {
let loads = 0;
const load = fakeIconify({ fake: { a: "<path/>", b: "<path/>" } });
syncIcons({
manifest: {
one: { source: "iconify", set: "fake", name: "a" },
two: { source: "iconify", set: "fake", name: "b" },
},
deviconDir: fakeDevicon({}),
loadIconSet: (set) => {
loads++;
return load(set);
},
outDir: tmp(),
});
expect(loads).toBe(1);
});
});
});