perf(e2e): fixture build를 브라우저 project마다가 아니라 run에 한 번 만든다
문제: fixture spec 여섯(old-tab-chunk-recovery, generated-feature, preload-use, boot-module-failure, service-worker-update·recovery)이 beforeAll에서 저장소를 복사해 vite build를 동기로 돈다. chromium· firefox·webkit project가 그 hook을 따로 돌아, e2e 한 번에 같은 build를 세 번(약 9 build × 3) 만든다. 그 build는 옆 worker의 브라우저와 CPU를 다툰다. FE-GATE-008이 가끔 떨어진 firefox 시험(runner run 127, desktop p5·fea-v8: `old-tab-chunk-recovery`·`app-shell`·`accessibility`의 첫 goto 뒤 요소 없음)은 이 build가 도는 구간과 겹쳤다. 다만 아래 6회 실행에서 고치기 전·후 모두 재현되지 않아, 이것이 원인이라고는 확정하지 못했다. 고른 방법: e2e 설정의 global setup이 run마다 공유 폴더를 만들고 (`SHARED_FIXTURE_BUILD_ROOT`), `prepareFixtureBuilds`와 `prepareServiceWorkerBuilds`가 key마다 처음 닿은 worker만 build한다. 나머지 worker는 그 결과를 기다려 쓴다(`mkdir` 잠금, 결과 JSON을 rename으로 공개, 실패는 `.failed`로 전한다). 같은 key를 다른 정의로 부르면 거절한다. build는 엔진과 무관하고 시험이 build 폴더에 쓰는 곳은 없다(edit는 build 전에만 돈다). global teardown이 폴더를 지운다. - old-tab spec은 edit 안에서 정하던 기능 목록을 build 뒤에 root에서도 읽는다. 다른 project의 build를 쓴 worker는 edit를 돌지 않는다. - 공유 폴더가 없으면(Vitest 시험, 다른 Playwright 설정) 예전처럼 호출마다 자기 사본을 만든다. - 버린 대안: expect timeout 늘리기·worker 줄이기(규칙 밖), fixture spec을 한 브라우저만 돌리기(브라우저별 계약이 줄어든다). 위험·한계: build를 기다리는 worker는 자기 hook timeout(180~300초) 안에서 기다린다. build한 worker가 죽으면 기다리는 쪽은 그 timeout으로 실패한다. 2026-11-01 리뷰의 R-1(두 helper 중복 정리)은 하지 않았다. 검증(desktop, `taskset -c 0-5` + worker 6으로 runner 모양, 같은 세션에서 고치기 전·후 번갈아 3회): e2e 전체 벽시계 172/150/141 → 123/141/113초 (중앙값 150 → 123초), 6회 모두 183개 통과. 공유 시험 6개 통과, check:types·eslint rc=0. 로그 /tmp/claude-gates/w2fe2-e2eab/, w2fe2-shared1/.
This commit is contained in:
@@ -21,6 +21,9 @@ const origin = `http://127.0.0.1:${port}`;
|
|||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
testDir: "./tests/e2e",
|
testDir: "./tests/e2e",
|
||||||
|
// Fixture specs build a repository copy once for the run, not once per
|
||||||
|
// browser project (tests/e2e/support/shared-fixture-builds.ts).
|
||||||
|
globalSetup: "./tests/e2e/support/e2e-global-setup.ts",
|
||||||
forbidOnly: true,
|
forbidOnly: true,
|
||||||
outputDir: testRunPath("artifacts/tests/e2e/results"),
|
outputDir: testRunPath("artifacts/tests/e2e/results"),
|
||||||
reporter: [
|
reporter: [
|
||||||
|
|||||||
@@ -57,6 +57,14 @@ const PLATFORM: ChunkRoute = Object.freeze({
|
|||||||
});
|
});
|
||||||
/** The generated feature's list, read from build A's copy. */
|
/** The generated feature's list, read from build A's copy. */
|
||||||
let featureList: ChunkRoute | undefined;
|
let featureList: ChunkRoute | undefined;
|
||||||
|
function readFeatureList(root: string): ChunkRoute {
|
||||||
|
const { list, messages } = generatedFeature(root);
|
||||||
|
return Object.freeze({
|
||||||
|
...list,
|
||||||
|
navigationLabel: messages["ko-KR"][`route.${list.routeId}.navigation`]!,
|
||||||
|
title: messages["ko-KR"][`route.${list.routeId}.title`]!,
|
||||||
|
});
|
||||||
|
}
|
||||||
function FEATURE(): ChunkRoute {
|
function FEATURE(): ChunkRoute {
|
||||||
if (!featureList) throw new Error("Fixture builds were not prepared.");
|
if (!featureList) throw new Error("Fixture builds were not prepared.");
|
||||||
return featureList;
|
return featureList;
|
||||||
@@ -110,12 +118,7 @@ test.beforeAll(async () => {
|
|||||||
name: "a",
|
name: "a",
|
||||||
edit: async (root) => {
|
edit: async (root) => {
|
||||||
await addGeneratedFeature(root);
|
await addGeneratedFeature(root);
|
||||||
const { list, messages } = generatedFeature(root);
|
featureList = readFeatureList(root);
|
||||||
featureList = Object.freeze({
|
|
||||||
...list,
|
|
||||||
navigationLabel: messages["ko-KR"][`route.${list.routeId}.navigation`]!,
|
|
||||||
title: messages["ko-KR"][`route.${list.routeId}.title`]!,
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -131,6 +134,8 @@ test.beforeAll(async () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
// A worker that reused another browser project's builds ran no edit.
|
||||||
|
featureList ??= readFeatureList(builds.root);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.afterAll(async () => {
|
test.afterAll(async () => {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ let builds: ServiceWorkerBuilds | undefined;
|
|||||||
test.beforeAll(async () => {
|
test.beforeAll(async () => {
|
||||||
test.setTimeout(180_000);
|
test.setTimeout(180_000);
|
||||||
builds = await prepareServiceWorkerBuilds({
|
builds = await prepareServiceWorkerBuilds({
|
||||||
|
key: "recovery",
|
||||||
async beforeBuild(name, root) {
|
async beforeBuild(name, root) {
|
||||||
if (name === "a") {
|
if (name === "a") {
|
||||||
await replaceOnce(
|
await replaceOnce(
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ const CHANGED_ROUTE_SOURCE =
|
|||||||
test.beforeAll(async () => {
|
test.beforeAll(async () => {
|
||||||
test.setTimeout(180_000);
|
test.setTimeout(180_000);
|
||||||
builds = await prepareServiceWorkerBuilds({
|
builds = await prepareServiceWorkerBuilds({
|
||||||
|
key: "update",
|
||||||
beforeBuild: async (name, root) => {
|
beforeBuild: async (name, root) => {
|
||||||
if (name !== "b") return;
|
if (name !== "b") return;
|
||||||
await appendFile(
|
await appendFile(
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import { startSharedFixtureBuilds } from "./shared-fixture-builds.ts";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The e2e run's global setup: one shared fixture build root for every browser
|
||||||
|
* project (shared-fixture-builds.ts). The returned function is the global
|
||||||
|
* teardown that removes it.
|
||||||
|
*/
|
||||||
|
export default async function globalSetup(): Promise<() => Promise<void>> {
|
||||||
|
return startSharedFixtureBuilds();
|
||||||
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
import { spawnSync } from "node:child_process";
|
import { spawnSync } from "node:child_process";
|
||||||
import { cp, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
import { cp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
||||||
import { tmpdir } from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts";
|
import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts";
|
||||||
import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts";
|
import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts";
|
||||||
import { selectRepositoryPaths } from "../../../scripts/lib/repository-layout.ts";
|
import { selectRepositoryPaths } from "../../../scripts/lib/repository-layout.ts";
|
||||||
|
import { sharedFixtureBuild } from "./shared-fixture-builds.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Real production builds of an edited temp copy of the repository, in the
|
* Real production builds of an edited temp copy of the repository, in the
|
||||||
@@ -29,6 +29,8 @@ export type FixtureBuild<Name extends string> = Readonly<{
|
|||||||
export type FixtureBuilds<Name extends string> = Readonly<{
|
export type FixtureBuilds<Name extends string> = Readonly<{
|
||||||
root: string;
|
root: string;
|
||||||
dist: Readonly<Record<Name, string>>;
|
dist: Readonly<Record<Name, string>>;
|
||||||
|
/** One copy for the whole e2e run (shared-fixture-builds.ts); the run removes it. */
|
||||||
|
shared: boolean;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
/** Replaces the one occurrence of `search` in `file` of the copy. */
|
/** Replaces the one occurrence of `search` in `file` of the copy. */
|
||||||
@@ -67,32 +69,37 @@ export async function prepareFixtureBuilds<Name extends string>(
|
|||||||
prefix: string,
|
prefix: string,
|
||||||
builds: readonly FixtureBuild<Name>[],
|
builds: readonly FixtureBuild<Name>[],
|
||||||
): Promise<FixtureBuilds<Name>> {
|
): Promise<FixtureBuilds<Name>> {
|
||||||
const root = await mkdtemp(path.join(tmpdir(), `${prefix}-`));
|
// An edit's source text names the build (two specs never share a prefix).
|
||||||
try {
|
const fingerprint = JSON.stringify(
|
||||||
await cp(repoRoot, root, {
|
builds.map((build) => [build.name, build.env ?? {}, build.edit?.toString() ?? ""]),
|
||||||
recursive: true,
|
);
|
||||||
filter: (source) =>
|
const { root, value, shared } = await sharedFixtureBuild(
|
||||||
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
|
`fixture-${prefix}`,
|
||||||
});
|
fingerprint,
|
||||||
await linkFixtureNodeModules(root, repoRoot);
|
async (root) => {
|
||||||
const dist = {} as Record<Name, string>;
|
await cp(repoRoot, root, {
|
||||||
for (const build of builds) {
|
recursive: true,
|
||||||
await build.edit?.(root);
|
filter: (source) =>
|
||||||
const buildId = `${prefix}-${build.name}`;
|
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
|
||||||
run(root, "scripts/generate-product-feature-index.ts", buildId, build.env);
|
});
|
||||||
run(root, "scripts/build-frontend.ts", buildId, build.env);
|
await linkFixtureNodeModules(root, repoRoot);
|
||||||
dist[build.name] = path.join(root, `dist-${build.name}`);
|
const dist = {} as Record<Name, string>;
|
||||||
await rename(path.join(root, "dist"), dist[build.name]);
|
for (const build of builds) {
|
||||||
}
|
await build.edit?.(root);
|
||||||
return { root, dist };
|
const buildId = `${prefix}-${build.name}`;
|
||||||
} catch (error) {
|
run(root, "scripts/generate-product-feature-index.ts", buildId, build.env);
|
||||||
await rm(root, { recursive: true, force: true });
|
run(root, "scripts/build-frontend.ts", buildId, build.env);
|
||||||
throw error;
|
dist[build.name] = path.join(root, `dist-${build.name}`);
|
||||||
}
|
await rename(path.join(root, "dist"), dist[build.name]);
|
||||||
|
}
|
||||||
|
return dist;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return { root, dist: value, shared };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeFixtureBuilds(
|
export async function removeFixtureBuilds(
|
||||||
builds: Readonly<{ root: string }> | undefined,
|
builds: Readonly<{ root: string; shared: boolean }> | undefined,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (builds) await rm(builds.root, { recursive: true, force: true });
|
if (builds && !builds.shared) await rm(builds.root, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import { spawnSync } from "node:child_process";
|
|||||||
import { existsSync } from "node:fs";
|
import { existsSync } from "node:fs";
|
||||||
import {
|
import {
|
||||||
cp,
|
cp,
|
||||||
mkdtemp,
|
|
||||||
readFile,
|
readFile,
|
||||||
rename,
|
rename,
|
||||||
rm,
|
rm,
|
||||||
@@ -10,7 +9,6 @@ import {
|
|||||||
} from "node:fs/promises";
|
} from "node:fs/promises";
|
||||||
import { createServer, type Server } from "node:http";
|
import { createServer, type Server } from "node:http";
|
||||||
import type { AddressInfo } from "node:net";
|
import type { AddressInfo } from "node:net";
|
||||||
import { tmpdir } from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { pathToFileURL } from "node:url";
|
import { pathToFileURL } from "node:url";
|
||||||
import { gzipSync } from "node:zlib";
|
import { gzipSync } from "node:zlib";
|
||||||
@@ -20,6 +18,7 @@ import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts
|
|||||||
import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts";
|
import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts";
|
||||||
import { selectRepositoryPaths } from "../../../scripts/lib/repository-layout.ts";
|
import { selectRepositoryPaths } from "../../../scripts/lib/repository-layout.ts";
|
||||||
import type { StaticAssetManifestV1 } from "../../../src/contracts/service-worker.ts";
|
import type { StaticAssetManifestV1 } from "../../../src/contracts/service-worker.ts";
|
||||||
|
import { sharedFixtureBuild } from "./shared-fixture-builds.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* R3-6 test helper: two real production builds with the Service Worker on,
|
* R3-6 test helper: two real production builds with the Service Worker on,
|
||||||
@@ -66,6 +65,8 @@ export type ServiceWorkerBuilds = Readonly<{
|
|||||||
dist: Readonly<Record<BuildName, string>>;
|
dist: Readonly<Record<BuildName, string>>;
|
||||||
/** Sorted URLs of each build's generated precache list (premise 3). */
|
/** Sorted URLs of each build's generated precache list (premise 3). */
|
||||||
precached: Readonly<Record<BuildName, readonly string[]>>;
|
precached: Readonly<Record<BuildName, readonly string[]>>;
|
||||||
|
/** One copy for the whole e2e run (shared-fixture-builds.ts); the run removes it. */
|
||||||
|
shared: boolean;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
const SELECTION_FILE = "src/features/product-capabilities.ts";
|
const SELECTION_FILE = "src/features/product-capabilities.ts";
|
||||||
@@ -183,52 +184,53 @@ function run(cwd: string, script: string, buildId: string): void {
|
|||||||
/**
|
/**
|
||||||
* `beforeBuild` edits the temp copy right before build `name` (06-03: a
|
* `beforeBuild` edits the temp copy right before build `name` (06-03: a
|
||||||
* broken worker A, then a `REMOVE_REGISTRATION` release B). Edits persist
|
* broken worker A, then a `REMOVE_REGISTRATION` release B). Edits persist
|
||||||
* into the later build.
|
* into the later build. `key` names this pair of builds in the e2e run
|
||||||
|
* (shared-fixture-builds.ts); each spec passes its own.
|
||||||
*/
|
*/
|
||||||
export async function prepareServiceWorkerBuilds(
|
export async function prepareServiceWorkerBuilds(
|
||||||
options: Readonly<{
|
options: Readonly<{
|
||||||
|
key: string;
|
||||||
beforeBuild?: (name: BuildName, root: string) => Promise<void>;
|
beforeBuild?: (name: BuildName, root: string) => Promise<void>;
|
||||||
}> = {},
|
}>,
|
||||||
): Promise<ServiceWorkerBuilds> {
|
): Promise<ServiceWorkerBuilds> {
|
||||||
const root = await mkdtemp(path.join(tmpdir(), "ca-sw-builds-"));
|
const { root, value, shared } = await sharedFixtureBuild(
|
||||||
try {
|
`sw-${options.key}`,
|
||||||
await cp(repoRoot, root, {
|
options.beforeBuild?.toString() ?? "",
|
||||||
recursive: true,
|
async (root) => {
|
||||||
filter: (source) =>
|
await cp(repoRoot, root, {
|
||||||
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
|
recursive: true,
|
||||||
});
|
filter: (source) =>
|
||||||
await linkFixtureNodeModules(root, repoRoot);
|
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
|
||||||
await turnOnServiceWorker(root);
|
});
|
||||||
const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts");
|
await linkFixtureNodeModules(root, repoRoot);
|
||||||
await replaceOnce(
|
await turnOnServiceWorker(root);
|
||||||
hostFile,
|
const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts");
|
||||||
"export function createServiceWorkerRuntimeHost(",
|
await replaceOnce(
|
||||||
"function createServiceWorkerRuntimeHostUnobserved(",
|
hostFile,
|
||||||
);
|
"export function createServiceWorkerRuntimeHost(",
|
||||||
await writeFile(hostFile, (await readFile(hostFile, "utf8")) + HOST_PROBE);
|
"function createServiceWorkerRuntimeHostUnobserved(",
|
||||||
|
);
|
||||||
|
await writeFile(hostFile, (await readFile(hostFile, "utf8")) + HOST_PROBE);
|
||||||
|
|
||||||
const dist = { a: path.join(root, "dist-a"), b: path.join(root, "dist-b") };
|
const dist = { a: path.join(root, "dist-a"), b: path.join(root, "dist-b") };
|
||||||
const precached: Record<BuildName, readonly string[]> = { a: [], b: [] };
|
const precached: Record<BuildName, readonly string[]> = { a: [], b: [] };
|
||||||
for (const name of ["a", "b"] as const) {
|
for (const name of ["a", "b"] as const) {
|
||||||
await options.beforeBuild?.(name, root);
|
await options.beforeBuild?.(name, root);
|
||||||
run(root, "scripts/generate-product-feature-index.ts", `sw-e2e-${name}`);
|
run(root, "scripts/generate-product-feature-index.ts", `sw-e2e-${name}`);
|
||||||
run(root, "scripts/build-frontend.ts", `sw-e2e-${name}`);
|
run(root, "scripts/build-frontend.ts", `sw-e2e-${name}`);
|
||||||
await rename(path.join(root, "dist"), dist[name]);
|
await rename(path.join(root, "dist"), dist[name]);
|
||||||
precached[name] = await precachedUrls(root, name);
|
precached[name] = await precachedUrls(root, name);
|
||||||
}
|
}
|
||||||
return { root, dist, precached };
|
return { dist, precached };
|
||||||
} catch (error) {
|
},
|
||||||
// The spec never receives `root` from a failed setup, so afterAll cannot
|
);
|
||||||
// remove it.
|
return { root, ...value, shared };
|
||||||
await rm(root, { recursive: true, force: true });
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function removeServiceWorkerBuilds(
|
export async function removeServiceWorkerBuilds(
|
||||||
builds: ServiceWorkerBuilds | undefined,
|
builds: ServiceWorkerBuilds | undefined,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
if (builds) await rm(builds.root, { recursive: true, force: true });
|
if (builds && !builds.shared) await rm(builds.root, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AssetHeaders = Readonly<{
|
export type AssetHeaders = Readonly<{
|
||||||
|
|||||||
@@ -0,0 +1,135 @@
|
|||||||
|
import { mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
||||||
|
import { tmpdir } from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One fixture build per Playwright run instead of one per browser project.
|
||||||
|
*
|
||||||
|
* The fixture specs build a copy of the repository in `beforeAll`, and every
|
||||||
|
* browser project runs that hook again: one e2e run made the same builds
|
||||||
|
* three times, synchronously, while the other workers drove browsers. Those
|
||||||
|
* builds took the CPU from the browsers next to them, and Firefox's first
|
||||||
|
* render in a test then missed its 5 s expectation (FE-GATE-008, desktop and
|
||||||
|
* runner). Builds are the same for every engine and no test writes into them.
|
||||||
|
*
|
||||||
|
* With `SHARED_FIXTURE_BUILD_ROOT` set (the e2e config's global setup sets it
|
||||||
|
* for the run), the first worker that needs `key` builds it under that root and
|
||||||
|
* the others wait for it and reuse it; the global teardown removes the root.
|
||||||
|
* Unset (a Vitest test, another config), every call builds its own copy in a
|
||||||
|
* new temp folder as before.
|
||||||
|
*/
|
||||||
|
export const SHARED_FIXTURE_BUILD_ROOT_VARIABLE = "SHARED_FIXTURE_BUILD_ROOT";
|
||||||
|
|
||||||
|
const POLL_MS = 200;
|
||||||
|
const KEY = /^[a-z0-9][a-z0-9-]{0,63}$/u;
|
||||||
|
|
||||||
|
export type SharedFixtureBuild<Value> = Readonly<{
|
||||||
|
/** The folder `create` built into. */
|
||||||
|
root: string;
|
||||||
|
value: Value;
|
||||||
|
/** Owned by the run (not removable by the spec) when true. */
|
||||||
|
shared: boolean;
|
||||||
|
}>;
|
||||||
|
|
||||||
|
type BuildRecord = Readonly<{ fingerprint: string; value: unknown }>;
|
||||||
|
|
||||||
|
function hasCode(error: unknown, code: string): boolean {
|
||||||
|
return typeof error === "object" && error !== null && "code" in error && error.code === code;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readRecord(file: string): Promise<BuildRecord | undefined> {
|
||||||
|
try {
|
||||||
|
return JSON.parse(await readFile(file, "utf8")) as BuildRecord;
|
||||||
|
} catch (error) {
|
||||||
|
if (hasCode(error, "ENOENT")) return undefined;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readFailure(file: string): Promise<string | undefined> {
|
||||||
|
try {
|
||||||
|
return await readFile(file, "utf8");
|
||||||
|
} catch (error) {
|
||||||
|
if (hasCode(error, "ENOENT")) return undefined;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `create(root)` fills the empty folder `root` and returns a JSON value that
|
||||||
|
* describes it (paths under `root`). `fingerprint` names what `create` builds;
|
||||||
|
* a second caller of the same `key` with another fingerprint is refused.
|
||||||
|
*/
|
||||||
|
export async function sharedFixtureBuild<Value>(
|
||||||
|
key: string,
|
||||||
|
fingerprint: string,
|
||||||
|
create: (root: string) => Promise<Value>,
|
||||||
|
environment: NodeJS.ProcessEnv = process.env,
|
||||||
|
): Promise<SharedFixtureBuild<Value>> {
|
||||||
|
if (!KEY.test(key)) throw new Error(`Invalid fixture build key: ${key}`);
|
||||||
|
const sharedRoot = environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE];
|
||||||
|
if (!sharedRoot) {
|
||||||
|
const root = await mkdtemp(path.join(tmpdir(), `${key}-`));
|
||||||
|
try {
|
||||||
|
return { root, value: await create(root), shared: false };
|
||||||
|
} catch (error) {
|
||||||
|
await rm(root, { recursive: true, force: true });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const root = path.join(sharedRoot, key);
|
||||||
|
const recordFile = `${root}.json`;
|
||||||
|
const failureFile = `${root}.failed`;
|
||||||
|
let owner = true;
|
||||||
|
try {
|
||||||
|
await mkdir(root);
|
||||||
|
} catch (error) {
|
||||||
|
if (!hasCode(error, "EEXIST")) throw error;
|
||||||
|
owner = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (owner) {
|
||||||
|
let value: Value;
|
||||||
|
try {
|
||||||
|
value = await create(root);
|
||||||
|
} catch (error) {
|
||||||
|
await writeFile(failureFile, String(error instanceof Error ? error.stack : error));
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
const temporary = `${recordFile}.${process.pid}.tmp`;
|
||||||
|
await writeFile(temporary, JSON.stringify({ fingerprint, value }));
|
||||||
|
await rename(temporary, recordFile);
|
||||||
|
return { root, value, shared: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Another worker builds `key`; the spec's own hook timeout bounds this wait.
|
||||||
|
for (;;) {
|
||||||
|
const record = await readRecord(recordFile);
|
||||||
|
if (record) {
|
||||||
|
if (record.fingerprint !== fingerprint) {
|
||||||
|
throw new Error(
|
||||||
|
`Fixture build ${key} was built from another definition in this run.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return { root, value: record.value as Value, shared: true };
|
||||||
|
}
|
||||||
|
const failure = await readFailure(failureFile);
|
||||||
|
if (failure !== undefined) {
|
||||||
|
throw new Error(`Fixture build ${key} failed in another worker:\n${failure}`);
|
||||||
|
}
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, POLL_MS));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The e2e config's global setup: a shared root for this run, and its teardown. */
|
||||||
|
export async function startSharedFixtureBuilds(
|
||||||
|
environment: NodeJS.ProcessEnv = process.env,
|
||||||
|
): Promise<() => Promise<void>> {
|
||||||
|
const root = await mkdtemp(path.join(tmpdir(), "e2e-fixture-builds-"));
|
||||||
|
environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE] = root;
|
||||||
|
return async () => {
|
||||||
|
delete environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE];
|
||||||
|
await rm(root, { recursive: true, force: true });
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
import { access, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||||
|
import { tmpdir } from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
import { afterEach, describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import {
|
||||||
|
SHARED_FIXTURE_BUILD_ROOT_VARIABLE,
|
||||||
|
sharedFixtureBuild,
|
||||||
|
startSharedFixtureBuilds,
|
||||||
|
} from "../e2e/support/shared-fixture-builds.ts";
|
||||||
|
|
||||||
|
const roots: string[] = [];
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await Promise.all(
|
||||||
|
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
async function sharedEnvironment(): Promise<NodeJS.ProcessEnv> {
|
||||||
|
const root = await mkdtemp(path.join(tmpdir(), "shared-builds-test-"));
|
||||||
|
roots.push(root);
|
||||||
|
return { [SHARED_FIXTURE_BUILD_ROOT_VARIABLE]: root };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("one fixture build per e2e run", () => {
|
||||||
|
it("builds a key once and hands every caller the same copy", async () => {
|
||||||
|
const environment = await sharedEnvironment();
|
||||||
|
let builds = 0;
|
||||||
|
const create = async (root: string) => {
|
||||||
|
builds += 1;
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||||
|
await writeFile(path.join(root, "dist.txt"), "built");
|
||||||
|
return { dist: path.join(root, "dist.txt") };
|
||||||
|
};
|
||||||
|
|
||||||
|
const results = await Promise.all(
|
||||||
|
[1, 2, 3].map(() => sharedFixtureBuild("fixture-a", "same", create, environment)),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(builds).toBe(1);
|
||||||
|
expect(new Set(results.map((result) => result.root)).size).toBe(1);
|
||||||
|
for (const result of results) {
|
||||||
|
expect(result.shared).toBe(true);
|
||||||
|
expect(result.value).toEqual({ dist: path.join(result.root, "dist.txt") });
|
||||||
|
}
|
||||||
|
await access(results[0]!.value.dist);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses a second definition under the same key", async () => {
|
||||||
|
const environment = await sharedEnvironment();
|
||||||
|
await sharedFixtureBuild("fixture-b", "first", async () => ({}), environment);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
sharedFixtureBuild("fixture-b", "second", async () => ({}), environment),
|
||||||
|
).rejects.toThrow(/fixture-b was built from another definition/u);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("fails the waiting callers with the builder's failure", async () => {
|
||||||
|
const environment = await sharedEnvironment();
|
||||||
|
const owner = sharedFixtureBuild(
|
||||||
|
"fixture-c",
|
||||||
|
"same",
|
||||||
|
async () => {
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||||
|
throw new Error("vite build exited 1");
|
||||||
|
},
|
||||||
|
environment,
|
||||||
|
);
|
||||||
|
const waiter = sharedFixtureBuild("fixture-c", "same", async () => ({}), environment);
|
||||||
|
|
||||||
|
await expect(owner).rejects.toThrow(/vite build exited 1/u);
|
||||||
|
await expect(waiter).rejects.toThrow(
|
||||||
|
/fixture-c failed in another worker:[\s\S]*vite build exited 1/u,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("builds a private copy and removes it on failure without a shared root", async () => {
|
||||||
|
const result = await sharedFixtureBuild("fixture-d", "x", async (root) => root, {});
|
||||||
|
roots.push(result.root);
|
||||||
|
expect(result).toMatchObject({ shared: false, value: result.root });
|
||||||
|
expect(path.dirname(result.root)).toBe(tmpdir());
|
||||||
|
|
||||||
|
let failedRoot = "";
|
||||||
|
await expect(
|
||||||
|
sharedFixtureBuild("fixture-d", "x", async (root) => {
|
||||||
|
failedRoot = root;
|
||||||
|
throw new Error("broken copy");
|
||||||
|
}, {}),
|
||||||
|
).rejects.toThrow(/broken copy/u);
|
||||||
|
await expect(access(failedRoot)).rejects.toMatchObject({ code: "ENOENT" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("refuses a key that is not one lower-case path segment", async () => {
|
||||||
|
for (const key of ["", "../x", "a/b", "Fixture", `a${"b".repeat(64)}`]) {
|
||||||
|
await expect(sharedFixtureBuild(key, "x", async () => ({}), {})).rejects.toThrow(
|
||||||
|
/Invalid fixture build key/u,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("gives the run a root and removes it with the teardown", async () => {
|
||||||
|
const environment: NodeJS.ProcessEnv = {};
|
||||||
|
const teardown = await startSharedFixtureBuilds(environment);
|
||||||
|
const root = environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE];
|
||||||
|
expect(root).toBeTruthy();
|
||||||
|
await sharedFixtureBuild("fixture-e", "x", async () => ({}), environment);
|
||||||
|
|
||||||
|
await teardown();
|
||||||
|
|
||||||
|
expect(environment[SHARED_FIXTURE_BUILD_ROOT_VARIABLE]).toBeUndefined();
|
||||||
|
await expect(access(root!)).rejects.toMatchObject({ code: "ENOENT" });
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user