Files
gstack/lib/pregate/playwright-spec.ts
T
Garry Tanandcapy-ai[bot] 942a12c618 Let a repo keep server-backed Playwright tests out of the local pre-merge run (GST-3553) (#4885)
A repo can list folders as `"local_skip": ["apps/web/e2e/**"]` in
`.gstack/pregate.json`, and /gstack-ship leaves the Playwright tests
under them out of the run on this machine, prints how many it left out,
and still expects them from CI.

Why: Playwright suites outside `test/` that need dev servers failed
green branches (#4709); only files Playwright plainly owns are ever left
out, so a too-wide folder never skips a unit test.

Nothing to do unless your repo has such a suite.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: s11/outside-voice#1 reader=outside-voice
Review-Finding: s11/outside-voice#2 reader=outside-voice
Review-Finding: s11/adversarial-native#1 reader=adversarial-native
Review-Finding: s11/adversarial-native#2 reader=adversarial-native
2026-11-28 21:06:10 +00:00

70 lines
4.7 KiB
TypeScript

/**
* pregate/playwright-spec — whether a JS/TS test file is a Playwright spec, the one predicate that routes a test to
* `playwright test` in the lanes check and lets a policy's `local_skip` leave it out of the local lane (#4709).
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import { readRepoFile } from './import-graph';
/**
* Whether a JS/TS test imports a value from Playwright's runner in a statement that starts a line: `import … from '@playwright/test'`
* (one line or several), a bare import, or `require`. A type-only import (`import type …`, or every specifier `type X`) and a string or
* fixture body that mentions the import mid-line do not count.
*/
function importsPlaywright(text: string): boolean {
const code = text.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, '');
if (/^[\t ]*(?:import[\t ]*|(?:const|let|var)\b[^=\n]*=[\t ]*require\([\t ]*)['"]@playwright\/test['"]/m.test(code)) return true;
for (const m of code.matchAll(/^[\t ]*import(?=[\s{*])([^'"`;]*?)\bfrom[\t ]*['"]@playwright\/test['"]/gm)) {
const spec = m[1]!.trim();
if (!/^type(?=[\s{*])/.test(spec) && spec.replace(/[{}]/g, '').split(',').some(s => s.trim() && !/^type\s/.test(s.trim()))) return true;
}
return false;
}
/** Where a file takes the value `test` from: a relative module or `@playwright/test`, by `import { test } from …` (aliased or not), `export { test } from …` or `export * from …`. */
function testSources(text: string): string[] {
const code = text.replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, '');
return [...code.matchAll(/^[\t ]*(?:(?:import|export)[\t ]*\{([^}]*)\}|export[\t ]*\*)[\t ]*from[\t ]*['"](\.{1,2}\/[^'"]+|@playwright\/test)['"]/gm)].filter(m => m[1] === undefined || m[1].split(',').some(s => /^test(?:\s+as\s+[\w$]+)?$/.test(s.trim()))).map(m => m[2]!);
}
/**
* Whether a JS/TS test is a Playwright spec: it imports a value from `@playwright/test`, or takes `test` from a repo module that does and doesn't take
* `test` from a unit runner, directly or through further such modules, up to 8 deep (Playwright's fixtures pattern: `base.extend(…)`, `mergeTests(…)`).
* Only the modules `test` comes from are followed, so a unit test whose helper or vitest fixture uses other Playwright APIs keeps its runner (#4347).
*/
export function playwrightSpec(repoRoot: string, file: string, owned = false): boolean {
const exts = ['.ts', '.tsx', '.mts', '.cts', '.js', '.jsx', '.mjs', '.cjs'];
const seen = new Set<string>();
let level = [file];
for (let depth = 0; level.length && depth <= 8; depth++) {
const next: string[] = [];
for (const mod of level.filter(m => !seen.has(m))) {
seen.add(mod);
const raw = readRepoFile(repoRoot, mod) ?? '', text = owned ? raw.replace(TEMPLATE_LITERAL, '') : raw, sources = testSources(text);
if (depth === 0 ? (owned ? sources.includes('@playwright/test') : importsPlaywright(text)) : sources.includes('@playwright/test') || (importsPlaywright(text) && !/^[\t ]*import\b[^;]*\btest\b[^;]*\bfrom[\t ]*['"](?:vitest|@jest\/globals|bun:test|node:test)['"]/m.test(text))) return true;
for (const base of sources.filter(s => s.startsWith('.')).map(spec => path.posix.normalize(path.posix.join(path.posix.dirname(mod), spec))).filter(b => b !== '..' && !b.startsWith('../'))) {
const stem = base.replace(/\.[cm]?jsx?$/, '');
const hit = [base, ...exts.map(e => stem + e), ...exts.map(e => `${base}/index${e}`)].find(c => readRepoFile(repoRoot, c) !== null);
if (hit) next.push(hit);
}
}
level = next;
}
return false;
}
const TEMPLATE_LITERAL = /`(?:\\[\s\S]|[^\\`])*`/g;
const UNIT_RUNNER = /^[\t ]*(?:import\b[^;'"`]*?\bfrom[\t ]*|import[\t ]*|(?:const|let|var)\b[^=\n]*=[\t ]*require\([\t ]*)['"](?:vitest|@jest\/globals|bun:test|node:test)['"]/m;
/**
* Whether a policy's `local_skip` may leave a test out of the local lane (#4709): positive evidence that Playwright owns it, stricter than
* playwrightSpec so an ambiguous file keeps running. A JS/TS file under a package.json whose own `test` comes from `@playwright/test`,
* directly or through repo fixture modules, and that loads no unit runner; import-shaped text in a template literal or comment does not count.
*/
export function playwrightOwned(repoRoot: string, file: string): boolean {
if (!/\.[cm]?[jt]sx?$/.test(file)) return false;
const code = (readRepoFile(repoRoot, file) ?? '').replace(TEMPLATE_LITERAL, '').replace(/\/\*[\s\S]*?\*\/|\/\/[^\n]*/g, '');
if (UNIT_RUNNER.test(code)) return false;
for (let d = path.posix.dirname(file); ; d = path.posix.dirname(d)) {
if (fs.existsSync(path.join(repoRoot, d === '.' ? '' : d, 'package.json'))) return playwrightSpec(repoRoot, file, true);
if (d === '.' || d === '/' || d === '') return false;
}
}