Skip to content

Commit 950cccd

Browse files
test: cli and studio-server tests remove the temp folders they create (#4566)
* test: cli and studio-server tests remove the temp folders they create * test(cli): each temp folder is removed where it is made, even when a test fails * test(cli): the one-liner helper relies on its project's own cleanup
1 parent 8dfdceb commit 950cccd

10 files changed

Lines changed: 151 additions & 141 deletions

File tree

‎packages/cli/src/capture/screenshotCapture.degrade.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { describe, expect, it, vi } from "vitest";
2-
import { mkdtempSync } from "node:fs";
1+
import { describe, expect, it, onTestFinished, vi } from "vitest";
2+
import { mkdtempSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { TimeoutError } from "puppeteer-core";
@@ -9,6 +9,7 @@ import { captureScrollScreenshots } from "./screenshotCapture.js";
99
describe("captureScrollScreenshots degradation", () => {
1010
it("rethrows protocol evaluate timeouts for the caller warning path", async () => {
1111
const dir = mkdtempSync(join(tmpdir(), "hf-scroll-degrade-"));
12+
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
1213
const page = {
1314
evaluate: vi.fn(async () => {
1415
throw new TimeoutError(

‎packages/cli/src/capture/screenshotCapture.test.ts‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { describe, expect, it, vi } from "vitest";
2-
import { existsSync, mkdtempSync, readFileSync } from "node:fs";
1+
import { describe, expect, it, onTestFinished, vi } from "vitest";
2+
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import type { Page } from "puppeteer-core";
@@ -10,6 +10,12 @@ import {
1010
pngHeight,
1111
} from "./screenshotCapture.js";
1212

13+
function tempDir(prefix: string): string {
14+
const dir = mkdtempSync(join(tmpdir(), prefix));
15+
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
16+
return dir;
17+
}
18+
1319
// A real 1920x800 PNG header, so the produced-height guard sees something valid.
1420
function pngBuffer(height: number, width = 1920): Buffer {
1521
const buf = Buffer.alloc(24);
@@ -38,7 +44,7 @@ function fakePage(
3844

3945
describe("captureFullPagePlate — the scroll shot's plate", () => {
4046
it("writes one full-page png and returns its relative path", async () => {
41-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
47+
const dir = tempDir("hf-plate-");
4248
const { page, screenshot } = fakePage({ docHeight: 10962, plateHeight: 10962 });
4349

4450
const out = await captureFullPagePlate(page, dir);
@@ -49,7 +55,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
4955
});
5056

5157
it("stays 1x: it never touches the viewport's deviceScaleFactor", async () => {
52-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
58+
const dir = tempDir("hf-plate-");
5359
const setViewport = vi.fn(async () => undefined);
5460
const { page } = fakePage({}, { setViewport });
5561

@@ -60,7 +66,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
6066
});
6167

6268
it("skips a page taller than Chrome can capture, instead of writing a clipped plate", async () => {
63-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
69+
const dir = tempDir("hf-plate-");
6470
const { page, screenshot } = fakePage({ docHeight: MAX_PLATE_HEIGHT_PX + 1 });
6571

6672
const out = await captureFullPagePlate(page, dir);
@@ -71,7 +77,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
7177
});
7278

7379
it("neutralises sticky/fixed chrome for the shot and restores it afterwards", async () => {
74-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
80+
const dir = tempDir("hf-plate-");
7581
const { page, evaluate, screenshot } = fakePage();
7682

7783
await captureFullPagePlate(page, dir);
@@ -96,7 +102,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
96102
});
97103

98104
it("restores the page even when the screenshot throws", async () => {
99-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
105+
const dir = tempDir("hf-plate-");
100106
const screenshot = vi.fn(async (_opts?: unknown) => {
101107
throw new Error("capture failed");
102108
});
@@ -111,7 +117,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
111117

112118
describe("captureScrollScreenshots — capture budget", () => {
113119
it("does not begin page work when the post-navigation budget is exhausted", async () => {
114-
const dir = mkdtempSync(join(tmpdir(), "hf-scroll-budget-"));
120+
const dir = tempDir("hf-scroll-budget-");
115121
const evaluate = vi.fn(async () => 1080);
116122
const screenshot = vi.fn(async () => pngBuffer(1080));
117123
const page = { evaluate, screenshot } as unknown as Page;
@@ -126,7 +132,7 @@ describe("captureScrollScreenshots — capture budget", () => {
126132
it("re-checks the budget after settling and before each viewport screenshot", async () => {
127133
vi.useFakeTimers();
128134
vi.setSystemTime(0);
129-
const dir = mkdtempSync(join(tmpdir(), "hf-scroll-expiring-budget-"));
135+
const dir = tempDir("hf-scroll-expiring-budget-");
130136
const evaluate = vi.fn(async (expression: unknown) => {
131137
const source = String(expression);
132138
if (source.includes("Math.max(document.body.scrollHeight")) return 1080;
@@ -155,7 +161,7 @@ describe("captureFullPagePlate — capture budget", () => {
155161
it("re-checks the budget immediately before the full-page screenshot", async () => {
156162
vi.useFakeTimers();
157163
vi.setSystemTime(0);
158-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-expiring-budget-"));
164+
const dir = tempDir("hf-plate-expiring-budget-");
159165
const screenshot = vi.fn(async () => pngBuffer(8000));
160166
const evaluate = vi.fn(async (expression: unknown) => {
161167
if (String(expression).includes("scrollHeight")) {
@@ -181,7 +187,7 @@ describe("captureFullPagePlate — capture budget", () => {
181187

182188
describe("captureFullPagePlate — guards against a silently clipped plate", () => {
183189
it("measures the height itself, after lazy content has grown the page", async () => {
184-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
190+
const dir = tempDir("hf-plate-");
185191
// A page that measured 9000 before scrolling but is 20000 once lazy images land: the
186192
// pre-scroll number would have passed the guard and emitted a clipped plate.
187193
const { page, screenshot } = fakePage({ docHeight: 20000 });
@@ -191,7 +197,7 @@ describe("captureFullPagePlate — guards against a silently clipped plate", ()
191197
});
192198

193199
it("discards a plate Chrome clipped, even when the measurement passed", async () => {
194-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
200+
const dir = tempDir("hf-plate-");
195201
// Measurement said 16000, but the capture itself triggered more loading and came back
196202
// over the cap. Emitting it would be undetectable downstream.
197203
const { page } = fakePage({ docHeight: 16000, plateHeight: MAX_PLATE_HEIGHT_PX + 500 });
@@ -201,7 +207,7 @@ describe("captureFullPagePlate — guards against a silently clipped plate", ()
201207
});
202208

203209
it("survives a restore that throws — the real error is what propagates", async () => {
204-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
210+
const dir = tempDir("hf-plate-");
205211
let call = 0;
206212
const evaluate = vi.fn(async (script?: unknown) => {
207213
call++;
@@ -233,7 +239,7 @@ describe("pngHeight", () => {
233239

234240
describe("captureFullPagePlate — the guard sees the post-neutralisation page (Magi's case)", () => {
235241
it("skips when the initial height is under the cap but the final height is over it", async () => {
236-
const dir = mkdtempSync(join(tmpdir(), "hf-plate-"));
242+
const dir = tempDir("hf-plate-");
237243
// Pre-traversal the page measured 9000. Lazy content and un-fixing the sticky header push
238244
// it over the cap by the time the plate would be shot. Probing before either step would
239245
// have passed the guard and emitted a clipped plate.

‎packages/cli/src/commands/cloud/render.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
1+
import { afterEach, beforeEach, describe, expect, it, onTestFinished, vi } from "vitest";
22
import { spawnSync } from "node:child_process";
33
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
44
import { join, resolve } from "node:path";
@@ -26,6 +26,7 @@ afterEach(() => {
2626

2727
function writeComposition(width: number, height: number): string {
2828
const dir = mkdtempSync(join(tmpdir(), "hf-cloud-render-test-"));
29+
onTestFinished(() => rmSync(dir, { recursive: true, force: true }));
2930
writeFileSync(
3031
join(dir, "index.html"),
3132
`<!doctype html><html><body><div data-composition-id="main" data-width="${width}" data-height="${height}"></div></body></html>`,

‎packages/cli/src/commands/keyframes.test.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { existsSync, linkSync, mkdtempSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
1+
import {
2+
existsSync,
3+
linkSync,
4+
mkdtempSync,
5+
mkdirSync,
6+
readFileSync,
7+
rmSync,
8+
writeFileSync,
9+
} from "node:fs";
210
import { tmpdir } from "node:os";
311
import { join } from "node:path";
4-
import { beforeAll, describe, expect, it } from "vitest";
12+
import { beforeAll, describe, expect, it, onTestFinished } from "vitest";
513
import type { ArgsDef } from "citty";
614
import { ensureDOMParser } from "../utils/dom.js";
715
import keyframesCommand from "./keyframes.js";
@@ -28,6 +36,7 @@ const wrap = (script: string) =>
2836
describe("keyframes direct composition scope", () => {
2937
it("keeps the project root and passes the nested HTML entry to --shot", () => {
3038
const projectDir = mkdtempSync(join(tmpdir(), "hf-keyframes-target-"));
39+
onTestFinished(() => rmSync(projectDir, { recursive: true, force: true }));
3140
const compositionsDir = join(projectDir, "compositions");
3241
mkdirSync(compositionsDir);
3342
writeFileSync(join(projectDir, "index.html"), wrap(""));
@@ -44,6 +53,7 @@ describe("keyframes direct composition scope", () => {
4453
describe("keyframes shot output", () => {
4554
it("rejects an output path that would overwrite the composition source", () => {
4655
const projectDir = mkdtempSync(join(tmpdir(), "hf-keyframes-shot-source-"));
56+
onTestFinished(() => rmSync(projectDir, { recursive: true, force: true }));
4757
const sourcePath = join(projectDir, "index.html");
4858
writeFileSync(sourcePath, wrap(""));
4959

@@ -55,6 +65,7 @@ describe("keyframes shot output", () => {
5565

5666
it("rejects an existing output alias that refers to the composition source", () => {
5767
const projectDir = mkdtempSync(join(tmpdir(), "hf-keyframes-shot-alias-"));
68+
onTestFinished(() => rmSync(projectDir, { recursive: true, force: true }));
5869
const sourcePath = join(projectDir, "index.html");
5970
const aliasPath = join(projectDir, "shot.png");
6071
writeFileSync(sourcePath, wrap(""));
@@ -67,6 +78,7 @@ describe("keyframes shot output", () => {
6778

6879
it("creates a missing parent directory before writing --shot", () => {
6980
const projectDir = mkdtempSync(join(tmpdir(), "hf-keyframes-shot-dir-"));
81+
onTestFinished(() => rmSync(projectDir, { recursive: true, force: true }));
7082
const outputDir = join(projectDir, "nested", "proofs");
7183
ensureShotOutputDir(join(outputDir, "shot.png"));
7284
expect(existsSync(outputDir)).toBe(true);

0 commit comments

Comments
 (0)