diff --git a/playwright.capabilities.config.ts b/playwright.capabilities.config.ts index 98f33f3d..7756a806 100644 --- a/playwright.capabilities.config.ts +++ b/playwright.capabilities.config.ts @@ -1,5 +1,7 @@ import { defineConfig, devices } from "@playwright/test"; +import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; + export default defineConfig({ testDir: "./tests/browser-capabilities", forbidOnly: true, @@ -30,7 +32,7 @@ export default defineConfig({ // BUILD_ID is local-build. CI sets VITE_BUILD_ID for the whole workflow; // built into the dev bundle it makes every page a rolling-release mismatch // that reloads once and then fails closed (CONFIG_BUILD_MISMATCH). - env: { VITE_BUILD_ID: "local-build", VITE_COMMIT_SHA: "local" }, + env: { ...LOCAL_BUILD_IDENTITY }, command: "corepack pnpm dev --host 127.0.0.1 --port 4174", url: "http://127.0.0.1:4174", diff --git a/playwright.dev.config.ts b/playwright.dev.config.ts index bf1ff377..ad112e92 100644 --- a/playwright.dev.config.ts +++ b/playwright.dev.config.ts @@ -1,6 +1,7 @@ import { defineConfig } from "@playwright/test"; import releaseConfig from "./playwright.config.ts"; +import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; export default defineConfig({ ...releaseConfig, @@ -13,7 +14,7 @@ export default defineConfig({ // BUILD_ID is local-build. CI sets VITE_BUILD_ID for the whole workflow; // built into the dev bundle it makes every page a rolling-release mismatch // that reloads once and then fails closed (CONFIG_BUILD_MISMATCH). - env: { VITE_BUILD_ID: "local-build", VITE_COMMIT_SHA: "local" }, + env: { ...LOCAL_BUILD_IDENTITY }, command: "corepack pnpm dev --host 127.0.0.1", url: "http://127.0.0.1:5173", reuseExistingServer: true, diff --git a/playwright.visual.config.ts b/playwright.visual.config.ts index 3a5f1981..89352b93 100644 --- a/playwright.visual.config.ts +++ b/playwright.visual.config.ts @@ -2,6 +2,8 @@ import { fileURLToPath } from "node:url"; import { defineConfig, devices } from "@playwright/test"; +import { LOCAL_BUILD_IDENTITY } from "./scripts/lib/build-environment.ts"; + export default defineConfig({ testDir: "./tests/visual", forbidOnly: true, @@ -51,11 +53,8 @@ export default defineConfig({ // whole workflow, so its build drew other text than the goldens and every // CI run failed here. The goldens are the template's shape, captured with // the local identity. - env: { - VITE_BUILD_ID: "local-build", - VITE_COMMIT_SHA: "local", - RELEASE_ID: "local-release", - }, + // 1103/11-08: CI too, or the build is checked as a release build. + env: { ...LOCAL_BUILD_IDENTITY }, command: "corepack pnpm build && corepack pnpm preview --host 127.0.0.1 --port 4174", url: "http://127.0.0.1:4174", diff --git a/scripts/lib/build-environment.ts b/scripts/lib/build-environment.ts index 9db5193d..a7242109 100644 --- a/scripts/lib/build-environment.ts +++ b/scripts/lib/build-environment.ts @@ -40,6 +40,32 @@ export function ciBuildEnvironmentFailures( return failures; } +/** + * 1103/11-08. The workflow sets CI=true and the CI build identity for every + * job. A build a test makes of a copy (a fixture build, the visual goldens' + * build, a dev server) is a local build, never the job's release build, so it + * starts without them. The job's own release build keeps the check above. + * + * `localBuildEnvironment` is for a process this code spawns: the caller's + * environment without CI and the CI identity. `LOCAL_BUILD_IDENTITY` is for a + * Playwright web server, whose env Playwright merges over process.env, so CI + * is overridden there instead of removed. + */ +export const LOCAL_BUILD_IDENTITY = Object.freeze({ + CI: "", + VITE_BUILD_ID: "local-build", + VITE_COMMIT_SHA: "local", + RELEASE_ID: "local-release", +}); + +export function localBuildEnvironment( + environment: Readonly> = process.env, +): Record { + const local: Record = { ...environment }; + for (const name of ["CI", ...CI_BUILD_ENVIRONMENT_VARIABLES]) delete local[name]; + return local; +} + export function assertCiBuildEnvironment( environment: Readonly>, ) { diff --git a/tests/e2e/support/fixture-builds.ts b/tests/e2e/support/fixture-builds.ts index 7e1350ea..405b44ff 100644 --- a/tests/e2e/support/fixture-builds.ts +++ b/tests/e2e/support/fixture-builds.ts @@ -3,6 +3,7 @@ import { cp, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; +import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts"; import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts"; /** @@ -60,7 +61,8 @@ function run( const result = spawnSync("node", [script], { cwd, encoding: "utf8", - env: { ...process.env, ...env, VITE_BUILD_ID: buildId, RELEASE_ID: buildId }, + // 1103/11-08. A local build, not the CI job's release build. + env: { ...localBuildEnvironment(), ...env, VITE_BUILD_ID: buildId, RELEASE_ID: buildId }, }); if (result.status !== 0) { throw new Error( diff --git a/tests/e2e/support/service-worker-builds.ts b/tests/e2e/support/service-worker-builds.ts index 4c132e6b..d375b66a 100644 --- a/tests/e2e/support/service-worker-builds.ts +++ b/tests/e2e/support/service-worker-builds.ts @@ -16,6 +16,7 @@ import { pathToFileURL } from "node:url"; import { gzipSync } from "node:zlib"; import { SERVICE_WORKER_ASSETS_OUTPUT } from "../../../scripts/generate-service-worker-assets.ts"; +import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts"; import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts"; import type { StaticAssetManifestV1 } from "../../../src/contracts/service-worker.ts"; @@ -174,7 +175,8 @@ function run(cwd: string, script: string, buildId: string): void { const result = spawnSync("node", [script], { cwd, encoding: "utf8", - env: { ...process.env, VITE_BUILD_ID: buildId, RELEASE_ID: buildId }, + // 1103/11-08. A local build, not the CI job's release build. + env: { ...localBuildEnvironment(), VITE_BUILD_ID: buildId, RELEASE_ID: buildId }, }); if (result.status !== 0) { throw new Error( diff --git a/tests/unit/local-build-environment.test.ts b/tests/unit/local-build-environment.test.ts new file mode 100644 index 00000000..18c60e42 --- /dev/null +++ b/tests/unit/local-build-environment.test.ts @@ -0,0 +1,51 @@ +import { describe, expect, it } from "vitest"; + +import { + ciBuildEnvironmentFailures, + LOCAL_BUILD_IDENTITY, + localBuildEnvironment, +} from "../../scripts/lib/build-environment.ts"; +import visualConfig from "../../playwright.visual.config.ts"; + +/** + * 1103/11-08. The workflow sets CI=true and the CI build identity for every + * job. A build a test makes of a copy is a local build: it used to inherit + * CI=true and be checked as a release build. Inside vitest, whose config + * drops VITE_COMMIT_SHA (11-07), that failed with "missing required CI build + * environment: VITE_COMMIT_SHA"; the visual goldens' build failed on its + * local commit id "local". + */ +const CI_JOB = Object.freeze({ + CI: "true", + VITE_BUILD_ID: "gitea-41-1", + RELEASE_ID: "refs/heads/fix/example-41-1", + CI_RUNNER_IMAGE: `local/runner@sha256:${"a".repeat(64)}`, + SOURCE_DATE_EPOCH: "1793700000", + PATH: "/usr/bin", +}); + +describe("local build environment (1103/11-08)", () => { + it("is not checked as a CI build, whatever CI identity the job has", () => { + const local = localBuildEnvironment(CI_JOB); + + expect(ciBuildEnvironmentFailures(local)).toEqual([]); + expect(local).not.toHaveProperty("CI"); + expect(local).not.toHaveProperty("VITE_BUILD_ID"); + expect(local["PATH"]).toBe("/usr/bin"); + }); + + it("keeps a CI job's own release build checked", () => { + expect(ciBuildEnvironmentFailures(CI_JOB)).toContain( + "missing required CI build environment: VITE_COMMIT_SHA", + ); + }); + + it("starts the visual goldens' build as a local build inside a CI job", () => { + const webServer = visualConfig.webServer; + const env = Array.isArray(webServer) ? undefined : webServer?.env; + + // Playwright merges a web server's env over the runner's process.env. + expect(ciBuildEnvironmentFailures({ ...CI_JOB, ...env })).toEqual([]); + expect(env).toMatchObject(LOCAL_BUILD_IDENTITY); + }); +});