diff --git a/scripts/ci/change-plan.mjs b/scripts/ci/change-plan.mjs index 13390bab84b..3be309d95d2 100644 --- a/scripts/ci/change-plan.mjs +++ b/scripts/ci/change-plan.mjs @@ -1,6 +1,7 @@ import { execFileSync } from "node:child_process"; import { matchesGlob } from "node:path"; import { isLivingProsePath } from "../lib/docs-facts.mjs"; +import { mayGateTestToDarwin } from "./darwin-gate-candidates.mjs"; import { CI_FLAGS as flags, MANUAL_VERIFIERS, @@ -116,6 +117,25 @@ const selectTypedLintFiles = (files, listCheckedOutFiles) => { : null; }; +/** + * The first changed test at `head` that may gate a test to macOS. A deleted + * test has nothing to run. + */ +const changedTestMayGateDarwin = (repository, head, files) => { + const changedTests = files.filter((file) => + /^(src|tests)\/.*\.test\.ts$/u.test(file), + ); + if (changedTests.length === 0) return undefined; + const present = new Set(repository.listFiles(head, [])); + const sources = repository.readFiles( + head, + changedTests.filter((file) => present.has(file)), + ); + for (const [file, source] of sources) + if (mayGateTestToDarwin(source)) return file; + return undefined; +}; + /** * Plan the CI lanes for one event. `repository` answers the Git questions the * plan asks, so the classification runs without a process per decision. @@ -444,6 +464,15 @@ export function planChanges(request, repository) { // Disable rename detection so both the removed and added paths participate. files = repository.changedFiles(base, head); for (const file of files) classifyFile(file, base, head); + // Linux lanes skip a test gated to macOS, so a change to one runs the + // macOS lane that does. + const gated = selected.apple + ? undefined + : changedTestMayGateDarwin(repository, head, files); + if (gated !== undefined) { + selected.apple = true; + reasons.push(`Changed test may be gated to macOS: ${gated}`); + } } // Scheduled fast sweeps always execute the complete portable suite and // refresh test-timings-fast, even when only prose changed since the ancestor. diff --git a/scripts/ci/darwin-gate-candidates.mjs b/scripts/ci/darwin-gate-candidates.mjs new file mode 100644 index 00000000000..d8e160bb720 --- /dev/null +++ b/scripts/ci/darwin-gate-candidates.mjs @@ -0,0 +1,16 @@ +// The CI planning job runs before dependencies are installed, so it cannot +// load the parser that darwin-gated-tests.mjs uses to decide a gate exactly. +// This dependency-free test admits a superset of that decision, and the +// exact discovery parses only the modules it admits, so the two cannot +// disagree about a gated module. A gate can tell macOS from Linux only by +// comparing process.platform with a "darwin" or "linux" literal. It is a +// skipIf/runIf call, or a case list chosen by a conditional on that literal. +// The test may admit modules that are not gated. + +const PLATFORM_LITERAL = /["'`](?:darwin|linux)["'`]/u; +const GATE_SHAPE = + /\.(?:skipIf|runIf)\s*\(|["'`](?:darwin|linux)["'`]\s*\)?\s*\?\s*\(?\s*\[/u; + +/** Whether a test module's text could gate a test to macOS. */ +export const mayGateTestToDarwin = (source) => + PLATFORM_LITERAL.test(source) && GATE_SHAPE.test(source); diff --git a/scripts/ci/darwin-gated-tests.mjs b/scripts/ci/darwin-gated-tests.mjs index e5f0ac1b411..8fbb7e4fc56 100644 --- a/scripts/ci/darwin-gated-tests.mjs +++ b/scripts/ci/darwin-gated-tests.mjs @@ -7,6 +7,7 @@ import { parse } from "@babel/parser"; import { traverseFast } from "@babel/types"; import { projectForTest } from "../lib/test-projects.mjs"; +import { mayGateTestToDarwin } from "./darwin-gate-candidates.mjs"; // Tests gated to macOS never run on the Linux lanes, so the macOS lane runs // every one of them. A gate is a Vitest `skipIf`/`runIf` condition, or a @@ -139,6 +140,7 @@ const calleeMethod = (node) => * `source` is TypeScript module text; a syntax error is thrown, not ignored. */ export const isDarwinGatedTestSource = (source) => { + if (!mayGateTestToDarwin(source)) return false; const tree = parse(source, { sourceType: "module", plugins: ["typescript"] }); const constants = moduleConstants(tree.program); let gated = false; @@ -154,7 +156,8 @@ export const isDarwinGatedTestSource = (source) => { } if ((method === "for" || method === "each") && node.arguments[0]) traverseFast(node.arguments[0], (child) => { - // `...(process.platform === "darwin" ? [cases] : [])` + // `...(process.platform === "darwin" ? [cases] : [])`. The candidate + // filter admits only a conditional that names the platform literal. if (gated || child.type !== "ConditionalExpression") return; const linux = evaluate(child.test, "linux", constants); const darwin = evaluate(child.test, "darwin", constants); diff --git a/scripts/ci/scopes.mjs b/scripts/ci/scopes.mjs index 1cf1e357b0c..334e6313446 100644 --- a/scripts/ci/scopes.mjs +++ b/scripts/ci/scopes.mjs @@ -162,6 +162,7 @@ const PATH_SCOPES = { "bridge/process/**", "scripts/verify/apple/**", "scripts/ci/darwin-gated-tests.mjs", + "scripts/ci/darwin-gate-candidates.mjs", "tests/boundary/filesystem/ciDarwinGatedTests.test.ts", "tests/acceptance/artifactDmgAttachmentRecoveryCli.test.ts", "tests/boundary/providers/native/nativeDmgArtifactReader.test.ts", diff --git a/tests/boundary/filesystem/ciChangeScope.test.ts b/tests/boundary/filesystem/ciChangeScope.test.ts index f3c617b8c4b..334aa0487cd 100644 --- a/tests/boundary/filesystem/ciChangeScope.test.ts +++ b/tests/boundary/filesystem/ciChangeScope.test.ts @@ -7,6 +7,7 @@ import { readFile, readdir, rename, + rm, writeFile, } from "node:fs/promises"; import { dirname, join, matchesGlob } from "node:path"; @@ -436,6 +437,36 @@ it("keeps unknown CI scripts on the fast Linux baseline", async () => { for (const flag of CI_FLAGS) expect(full[flag]).toBe(true); }); +// Linux lanes skip a test gated to macOS; changing one runs the macOS lane. +it.for([ + [`it.skipIf(process.platform !== "darwin")("runs", () => {});\n`, true], + [`it.skipIf(process.platform === "linux")("runs", () => {});\n`, true], + [`it("runs everywhere", () => {});\n`, false], +] as const)( + "selects the macOS lane for a changed test that may be gated to it: %j", + async ([source, apple]) => { + const { directory, base } = await fixture(); + const test = "tests/boundary/filesystem/gated.test.ts"; + const head = await change(directory, [test], source); + expect(await classify(directory, base, head)).toMatchObject({ apple }); + }, +); + +it("does not select the macOS lane for a deleted macOS-gated test", async () => { + const { directory } = await fixture(); + const test = "tests/boundary/filesystem/gated.test.ts"; + const base = await change( + directory, + [test], + `it.skipIf(process.platform !== "darwin")("runs", () => {});\n`, + ); + await rm(join(directory, test)); + const head = await commit(directory); + expect(await classify(directory, base, head)).toMatchObject({ + apple: false, + }); +}); + it("does not credit a fixture test as coverage for an unrelated shared script", async () => { const { directory } = await fixture(); const module = "scripts/lib/uncovered.mjs"; diff --git a/tests/boundary/filesystem/ciDarwinGatedTests.test.ts b/tests/boundary/filesystem/ciDarwinGatedTests.test.ts index e391a6cac20..ba79b4bd685 100644 --- a/tests/boundary/filesystem/ciDarwinGatedTests.test.ts +++ b/tests/boundary/filesystem/ciDarwinGatedTests.test.ts @@ -26,6 +26,7 @@ const FIXTURES = { "darwinConstant.test.ts": `const onDarwin = process.platform === "darwin";\nit.skipIf(!onDarwin)("runs", () => {});`, "darwinArm64.test.ts": `it.skipIf(process.platform !== "darwin" || process.arch !== "arm64")("runs", () => {});`, "darwinCases.test.ts": `it.for(["portable", ...(process.platform === "darwin" ? ["plist"] : [])])("%s", () => {});`, + "notLinux.test.ts": `it.skipIf(process.platform === "linux")("runs", () => {});`, "ungated.test.ts": `it("runs everywhere", () => {});`, "linuxOnly.test.ts": `it.skipIf(process.platform !== "linux")("runs", () => {});`, "posixHosts.test.ts": `it.skipIf(!["darwin", "linux"].includes(process.platform))("runs", () => {});`, @@ -41,6 +42,7 @@ const GATED = [ "darwinConstant.test.ts", "darwinRun.test.ts", "darwinSkip.test.ts", + "notLinux.test.ts", ].map((name) => `${DIRECTORY}/${name}`); const fixtureRepository = async (files: Readonly>) => {