Skip to content

Isometric line figures that answer the pointer. For React and for anything with a DOM.

Skill: build and validate still run on Windows when the drive letter's case differs, instead of exiting silently as if the page passed

Lucas Marques790ac0fdb801
diff --git a/packages/hairline/test/skill/build.test.ts b/packages/hairline/test/skill/build.test.ts
index 2dbd43f..51daecb 100644
--- a/packages/hairline/test/skill/build.test.ts
+++ b/packages/hairline/test/skill/build.test.ts
@@ -4,6 +4,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { expect, it } from "vitest";
+import { same } from "../../../../skills/hairline-create/build.mjs";
/** build.mjs puts a figure on the bench: the page is the bench, the kernel and the figure, and nothing else. */
const SKILL = fileURLToPath(new URL("../../../../skills/hairline-create/", import.meta.url));
@@ -53,3 +54,10 @@ it("says how to call it when it is given nothing", () => {
expect(run.status).toBe(2);
expect(run.stderr).toContain("usage: node build.mjs <figure.js> [out.html]");
});
+
+it("knows a path that differs only in the case of its drive letter is the same path on Windows, and only there", () => {
+ expect(same("C:\\x\\build.mjs", "c:\\x\\build.mjs", "win32")).toBe(true);
+ expect(same("C:\\x\\build.mjs", "c:\\x\\build.mjs", "darwin")).toBe(false);
+ expect(same("/x/build.mjs", "/x/build.mjs", "darwin")).toBe(true);
+ expect(same("C:\\x\\build.mjs", "C:\\x\\build.mjs", "win32")).toBe(true);
+});
diff --git a/packages/hairline/test/skill/validate.test.ts b/packages/hairline/test/skill/validate.test.ts
index fc8b62f..3e672f9 100644
--- a/packages/hairline/test/skill/validate.test.ts
+++ b/packages/hairline/test/skill/validate.test.ts
@@ -4,6 +4,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
+import { same } from "../../../../skills/hairline-create/validate.mjs";
/**
* validate.mjs passes the two examples and fails a page broken in one way,
@@ -145,3 +146,10 @@ it("says how to call it when it is given nothing, and when the file is not there
expect(gone.status).toBe(2);
expect(gone.stderr).toContain("cannot read");
});
+
+it("knows a path that differs only in the case of its drive letter is the same path on Windows, and only there", () => {
+ expect(same("C:\\x\\validate.mjs", "c:\\x\\validate.mjs", "win32")).toBe(true);
+ expect(same("C:\\x\\validate.mjs", "c:\\x\\validate.mjs", "darwin")).toBe(false);
+ expect(same("/x/validate.mjs", "/x/validate.mjs", "darwin")).toBe(true);
+ expect(same("C:\\x\\validate.mjs", "C:\\x\\validate.mjs", "win32")).toBe(true);
+});
diff --git a/skills/hairline-create/build.mjs b/skills/hairline-create/build.mjs
index dc4fcfb..e7a407b 100644
--- a/skills/hairline-create/build.mjs
+++ b/skills/hairline-create/build.mjs
@@ -22,8 +22,11 @@ export function assemble(figure) {
/** The figure's name, read from its declaration. */
export const nameOf = (figure) => /\bname:\s*["'`]([a-z][a-z0-9-]*)["'`]/.exec(figure)?.[1] ?? null;
+/** Whether two resolved paths are one file. Windows paths ignore case, and the drive letter's case can differ between the two. */
+export const same = (a, b, platform = process.platform) => platform === "win32" ? a.toLowerCase() === b.toLowerCase() : a === b;
+
/* The skill is often installed as a symlink, so the path Node was given is resolved before it is compared. */
-if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(here("./build.mjs"))) {
+if (process.argv[1] && same(realpathSync(process.argv[1]), realpathSync(here("./build.mjs")))) {
const [src, out] = process.argv.slice(2);
if (!src) {
console.error("usage: node build.mjs <figure.js> [out.html]");
diff --git a/skills/hairline-create/validate.mjs b/skills/hairline-create/validate.mjs
index 23db42b..2238c32 100644
--- a/skills/hairline-create/validate.mjs
+++ b/skills/hairline-create/validate.mjs
@@ -153,8 +153,11 @@ export function validate(input) {
return out;
}
+/** Whether two resolved paths are one file. Windows paths ignore case, and the drive letter's case can differ between the two. */
+export const same = (a, b, platform = process.platform) => platform === "win32" ? a.toLowerCase() === b.toLowerCase() : a === b;
+
/* The skill is often installed as a symlink, so the path Node was given is resolved before it is compared. */
-if (process.argv[1] && realpathSync(process.argv[1]) === realpathSync(here("./validate.mjs"))) {
+if (process.argv[1] && same(realpathSync(process.argv[1]), realpathSync(here("./validate.mjs")))) {
const file = process.argv[2];
if (!file) {
console.error("usage: node validate.mjs <page.html>");