Skip to content

Commit 7d6353b

Browse files
committed
Validate SVG file names.
1 parent 0946216 commit 7d6353b

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

script/test-svg-dimensions.spec.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

script/test-svg.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { expect, test } from "bun:test";
2+
import { basename, dirname } from "node:path";
3+
import { file, Glob } from "bun";
4+
import { JSDOM } from "jsdom";
5+
6+
// Collect into an array so we can iterate over it again multiple times.
7+
const svgFiles = await Array.fromAsync(new Glob("./src/svg/*/*.svg").scan());
8+
9+
test("SVG files are all 500×500", async () => {
10+
let numSVGs = 0;
11+
for await (const svgFile of svgFiles) {
12+
numSVGs++;
13+
const svgElem = new JSDOM(
14+
await file(svgFile).text(),
15+
).window.document.querySelector("svg");
16+
expect(svgElem).not.toBeNull();
17+
expect(svgElem?.getAttribute("width")).toEqual("500");
18+
expect(svgElem?.getAttribute("height")).toEqual("500");
19+
}
20+
21+
/**
22+
* - We could hardcode the number in the test here, but that would require every new SVG contribution to increment the expected value.
23+
* - We could compare against the built set of icons, but that introduces test dependencies.
24+
*
25+
* So instead we just hardcode a reasonable lower bound check.
26+
*/
27+
expect(numSVGs).toBeGreaterThan(50);
28+
});
29+
30+
test("SVG files follow naming conventions", async () => {
31+
let numSVGs = 0;
32+
for await (const svgFile of svgFiles) {
33+
numSVGs++;
34+
const parentFolder = basename(dirname(svgFile));
35+
if (parentFolder === "penalty") {
36+
expect(basename(svgFile)).toMatch(
37+
/^([A-Z]+\d+([a-z]+\d*)?|\d+[a-z]+(\d+[a-z]*)?)\.svg$/,
38+
);
39+
} else {
40+
expect(basename(svgFile)).toMatch(/^[a-z0-9_]+\.svg$/);
41+
}
42+
}
43+
44+
/**
45+
* - We could hardcode the number in the test here, but that would require every new SVG contribution to increment the expected value.
46+
* - We could compare against the built set of icons, but that introduces test dependencies.
47+
*
48+
* So instead we just hardcode a reasonable lower bound check.
49+
*/
50+
expect(numSVGs).toBeGreaterThan(50);
51+
});

0 commit comments

Comments
 (0)