Skip to content

Commit b1d3dff

Browse files
committed
refactor(test): remove repeated suite boilerplate
1 parent be27814 commit b1d3dff

62 files changed

Lines changed: 2676 additions & 3384 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎scripts/check-file-size.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const LINE_BUDGETS = new Map([
77
["src/permissions.ts", 566],
88
["src/root-impl.ts", 1750],
99
["src/root-path.ts", 862],
10-
["test/api-coverage.test.ts", 983],
10+
["test/api-coverage.test.ts", 915],
1111
["test/new-primitives.test.ts", 1500],
1212
]);
1313

‎test/absolute-directory.test.ts‎

Lines changed: 88 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import fs from "node:fs/promises";
2-
import os from "node:os";
32
import path from "node:path";
43
import { afterEach, describe, expect, it, vi } from "vitest";
4+
import { itPosix, useTempDirs } from "./helpers/vitest.js";
55
import { ensureAbsoluteDirectory } from "../src/absolute-path.js";
66

7-
const tempDirs: string[] = [];
7+
const { tempRoot } = useTempDirs();
88

9-
async function tempRoot(prefix: string): Promise<string> {
10-
const dir = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
11-
tempDirs.push(dir);
12-
return dir;
13-
}
149

1510
afterEach(async () => {
1611
vi.restoreAllMocks();
17-
await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { force: true, recursive: true })));
1812
});
1913

2014
describe("ensureAbsoluteDirectory", () => {
@@ -46,43 +40,37 @@ describe("ensureAbsoluteDirectory", () => {
4640
).resolves.toMatchObject({ ok: false, code: "not-file" });
4741
});
4842

49-
it.runIf(process.platform !== "win32")(
50-
"rejects absolute directory creation through symlinked existing segments",
51-
async () => {
52-
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-link-"));
53-
const outside = await fs.realpath(await tempRoot("fs-safe-absolute-dir-outside-"));
54-
const linkDir = path.join(root, "link");
55-
await fs.symlink(outside, linkDir);
43+
itPosix("rejects absolute directory creation through symlinked existing segments", async () => {
44+
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-link-"));
45+
const outside = await fs.realpath(await tempRoot("fs-safe-absolute-dir-outside-"));
46+
const linkDir = path.join(root, "link");
47+
await fs.symlink(outside, linkDir);
5648

57-
await expect(
58-
ensureAbsoluteDirectory(path.join(linkDir, "nested"), {
59-
scopeLabel: "output directory",
60-
}),
61-
).resolves.toMatchObject({ ok: false, code: "symlink" });
62-
await expect(fs.readdir(outside)).resolves.toEqual([]);
63-
},
64-
);
65-
66-
it.runIf(process.platform !== "win32")(
67-
"rejects symlinked parents even when the requested suffix already exists",
68-
async () => {
69-
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-link-existing-"));
70-
const outside = await fs.realpath(
71-
await tempRoot("fs-safe-absolute-dir-link-existing-outside-"),
72-
);
73-
const existing = path.join(outside, "existing");
74-
const linkDir = path.join(root, "link");
75-
await fs.mkdir(existing);
76-
await fs.symlink(outside, linkDir);
49+
await expect(
50+
ensureAbsoluteDirectory(path.join(linkDir, "nested"), {
51+
scopeLabel: "output directory",
52+
}),
53+
).resolves.toMatchObject({ ok: false, code: "symlink" });
54+
await expect(fs.readdir(outside)).resolves.toEqual([]);
55+
});
7756

78-
await expect(
79-
ensureAbsoluteDirectory(path.join(linkDir, "existing", "new"), {
80-
scopeLabel: "output directory",
81-
}),
82-
).resolves.toMatchObject({ ok: false, code: "symlink" });
83-
await expect(fs.stat(path.join(existing, "new"))).rejects.toMatchObject({ code: "ENOENT" });
84-
},
85-
);
57+
itPosix("rejects symlinked parents even when the requested suffix already exists", async () => {
58+
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-link-existing-"));
59+
const outside = await fs.realpath(
60+
await tempRoot("fs-safe-absolute-dir-link-existing-outside-"),
61+
);
62+
const existing = path.join(outside, "existing");
63+
const linkDir = path.join(root, "link");
64+
await fs.mkdir(existing);
65+
await fs.symlink(outside, linkDir);
66+
67+
await expect(
68+
ensureAbsoluteDirectory(path.join(linkDir, "existing", "new"), {
69+
scopeLabel: "output directory",
70+
}),
71+
).resolves.toMatchObject({ ok: false, code: "symlink" });
72+
await expect(fs.stat(path.join(existing, "new"))).rejects.toMatchObject({ code: "ENOENT" });
73+
});
8674

8775
it("returns a policy failure when an intermediate component is a file", async () => {
8876
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-file-component-"));
@@ -96,72 +84,66 @@ describe("ensureAbsoluteDirectory", () => {
9684
).resolves.toMatchObject({ ok: false, code: "not-file" });
9785
});
9886

99-
it.runIf(process.platform !== "win32")(
100-
"rejects absolute directory creation when an existing parent is swapped before mkdir",
101-
async () => {
102-
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-race-"));
103-
const outside = await fs.realpath(await tempRoot("fs-safe-absolute-dir-race-outside-"));
104-
const parentDir = path.join(root, "parent");
105-
const targetDir = path.join(parentDir, "child");
106-
await fs.mkdir(parentDir);
107-
108-
const realLstat = fs.lstat.bind(fs);
109-
let swapped = false;
110-
const lstatSpy = vi.spyOn(fs, "lstat").mockImplementation(async (...args) => {
111-
const candidate = String(args[0]);
112-
if (!swapped && candidate === targetDir) {
113-
swapped = true;
114-
await fs.rename(parentDir, `${parentDir}-real`);
115-
await fs.symlink(outside, parentDir, "dir");
116-
}
117-
return await realLstat(...args);
118-
});
119-
120-
try {
121-
await expect(
122-
ensureAbsoluteDirectory(targetDir, { scopeLabel: "output directory" }),
123-
).resolves.toMatchObject({ ok: false, code: "symlink" });
124-
} finally {
125-
lstatSpy.mockRestore();
87+
itPosix("rejects absolute directory creation when an existing parent is swapped before mkdir", async () => {
88+
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-race-"));
89+
const outside = await fs.realpath(await tempRoot("fs-safe-absolute-dir-race-outside-"));
90+
const parentDir = path.join(root, "parent");
91+
const targetDir = path.join(parentDir, "child");
92+
await fs.mkdir(parentDir);
93+
94+
const realLstat = fs.lstat.bind(fs);
95+
let swapped = false;
96+
const lstatSpy = vi.spyOn(fs, "lstat").mockImplementation(async (...args) => {
97+
const candidate = String(args[0]);
98+
if (!swapped && candidate === targetDir) {
99+
swapped = true;
100+
await fs.rename(parentDir, `${parentDir}-real`);
101+
await fs.symlink(outside, parentDir, "dir");
126102
}
103+
return await realLstat(...args);
104+
});
105+
106+
try {
107+
await expect(
108+
ensureAbsoluteDirectory(targetDir, { scopeLabel: "output directory" }),
109+
).resolves.toMatchObject({ ok: false, code: "symlink" });
110+
} finally {
111+
lstatSpy.mockRestore();
112+
}
113+
114+
await expect(fs.stat(path.join(outside, "child"))).rejects.toMatchObject({ code: "ENOENT" });
115+
});
127116

128-
await expect(fs.stat(path.join(outside, "child"))).rejects.toMatchObject({ code: "ENOENT" });
129-
},
130-
);
131-
132-
it.runIf(process.platform !== "win32")(
133-
"rejects absolute directory creation when the existing target changes before return",
134-
async () => {
135-
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-target-race-"));
136-
const outside = await fs.realpath(
137-
await tempRoot("fs-safe-absolute-dir-target-race-outside-"),
138-
);
139-
const targetDir = path.join(root, "target");
140-
await fs.mkdir(targetDir);
141-
142-
const realRealpath = fs.realpath.bind(fs);
143-
let swapped = false;
144-
const realpathSpy = vi.spyOn(fs, "realpath").mockImplementation(async (...args) => {
145-
const candidate = String(args[0]);
146-
if (!swapped && candidate === targetDir) {
147-
swapped = true;
148-
const resolved = await realRealpath(...args);
149-
await fs.rename(targetDir, `${targetDir}-real`);
150-
await fs.symlink(outside, targetDir, "dir");
151-
return resolved;
152-
}
153-
return await realRealpath(...args);
154-
});
155-
156-
try {
157-
await expect(
158-
ensureAbsoluteDirectory(targetDir, { scopeLabel: "output directory" }),
159-
).resolves.toMatchObject({ ok: false, code: "symlink" });
160-
} finally {
161-
realpathSpy.mockRestore();
117+
itPosix("rejects absolute directory creation when the existing target changes before return", async () => {
118+
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-target-race-"));
119+
const outside = await fs.realpath(
120+
await tempRoot("fs-safe-absolute-dir-target-race-outside-"),
121+
);
122+
const targetDir = path.join(root, "target");
123+
await fs.mkdir(targetDir);
124+
125+
const realRealpath = fs.realpath.bind(fs);
126+
let swapped = false;
127+
const realpathSpy = vi.spyOn(fs, "realpath").mockImplementation(async (...args) => {
128+
const candidate = String(args[0]);
129+
if (!swapped && candidate === targetDir) {
130+
swapped = true;
131+
const resolved = await realRealpath(...args);
132+
await fs.rename(targetDir, `${targetDir}-real`);
133+
await fs.symlink(outside, targetDir, "dir");
134+
return resolved;
162135
}
163-
},
164-
);
136+
return await realRealpath(...args);
137+
});
138+
139+
try {
140+
await expect(
141+
ensureAbsoluteDirectory(targetDir, { scopeLabel: "output directory" }),
142+
).resolves.toMatchObject({ ok: false, code: "symlink" });
143+
} finally {
144+
realpathSpy.mockRestore();
145+
}
146+
});
165147

166148
it("rethrows operational absolute directory creation failures", async () => {
167149
const root = await fs.realpath(await tempRoot("fs-safe-absolute-dir-io-"));

0 commit comments

Comments
 (0)