Files
gstack/lib/pregate/init-draft.ts
Garry Tanandcapy-ai[bot] 90ccf30e2a v1.93.15.0 fix(pregate): pytest, Playwright and mixed-language runners; literals stops flagging stray digits (#3598, #3492) (#3741)
* fix(pregate): runner detection for pytest, Playwright and mixed-language roots; literals binds one-character values

A missing pytest is PREGATE_ENVIRONMENT, not the diff's failure (#3598).
Playwright specs run under playwright test, with missing browsers reported
as the machine's gap (#3629). Root tests in two languages each get their
runner, and a repo's bun/vitest/jest claims only JS/TS (#3600). literals
counts a one-character old value only in a statement naming the constant,
and a call-site name also needs the module (#3492, gstack-evals#97).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: pre-landing review fixes

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(pregate): name the import-type shapes the Playwright detection skips

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: escape Playwright file filters; skip type-only Playwright imports; pytest probe and summary read hardening

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): linear Playwright import scan; comments and no-space imports detected

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(pregate): Playwright missing-browsers exception and literals continuation rule

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-11-02 19:23:49 +00:00

101 lines
5.5 KiB
TypeScript

/**
* pregate/init-draft — `gstack-pregate init` writes a policy drafted from the
* repo (#3268) instead of a fixed example: the languages it holds, what the
* lanes check would run, and a `dependencies` entry for each file the import
* graph maps to no test.
*
* A drafted entry only ever adds tests: it maps an unmapped file to the tests
* of the nearest directory above it that has any. An empty list (`[]`) would
* clear the file without a test, so the draft never writes one; a file with no
* nearby test, or past the schema's entry limit, is printed as a TODO for the
* owner to decide. Documentation,
* assets and tool config already clear without a policy (docs-lane.ts), and
* Dart packages run per package root, so `runner` stays null unless no
* runner can be detected.
*/
import * as path from 'node:path';
import { detectRunner } from './checks/lanes';
import { DEFAULT_CONFIG, MAX_LIST, PREGATE_CONFIG_PATH, PREGATE_CONFIG_SCHEMA } from './config';
import { isDocsEmptyLane, isInertNoTests } from './docs-lane';
import { buildGraph, listRepoFiles, testsFor } from './import-graph';
import { GENERATED_PATH, isRegistryUnavailable, loadRegistryFromHead } from '../regen';
import { matchesAny } from '../ship-policy';
import { discoverLanes } from './workflows';
const LANGUAGES: Array<[string, RegExp]> = [
['typescript', /\.[cm]?tsx?$/], ['javascript', /\.[cm]?jsx?$/], ['python', /\.py$/], ['dart', /\.dart$/],
['go', /\.go$/], ['ruby', /\.rb$/], ['rust', /\.rs$/], ['shell', /\.(?:sh|bash|bats)$/],
];
/** Above this many tests a directory's tests are no longer a useful suggestion. */
export const MAX_SUGGESTED = 25;
const MAX_TODO_LINES = 50;
export interface InitDraft {
json: string;
languages: Array<{ language: string; files: number }>;
runnerNote: string;
drafted: number;
todo: string[];
selectionTodo: string[];
}
/** The tests under the nearest ancestor directory of `file` (not the repo root) that has 1..MAX_SUGGESTED tests. */
export function nearestTests(tests: readonly string[], file: string): string[] {
for (let dir = path.posix.dirname(file); dir !== '.' && dir !== ''; dir = path.posix.dirname(dir)) {
const under = tests.filter(t => t.startsWith(`${dir}/`));
if (under.length > MAX_SUGGESTED) return [];
if (under.length) return under;
}
return [];
}
export function draftConfig(repoRoot: string): InitDraft {
const files = listRepoFiles(repoRoot);
const graph = buildGraph(repoRoot);
const tests = [...graph.tests].sort();
const languages = LANGUAGES.map(([language, re]) => ({ language, files: files.filter(f => re.test(f)).length })).filter(l => l.files > 0).sort((a, b) => b.files - a.files);
const dependencies: Record<string, string[]> = {};
const todo: string[] = [];
let drafted = 0;
const registry = loadRegistryFromHead(repoRoot);
const generated = isRegistryUnavailable(registry) ? [] : registry.registry.entries.flatMap(e => [...e.inputs, ...e.outputs]);
for (const file of files) {
if (matchesAny(generated, file)) continue;
if (graph.tests.has(file) || file === PREGATE_CONFIG_PATH || file === GENERATED_PATH) continue;
if (isDocsEmptyLane(file) || isInertNoTests(file)) continue;
if (testsFor(graph, file).tests.length) continue;
const suggested = nearestTests(tests, file);
if (!suggested.length || drafted >= MAX_LIST) { todo.push(file); continue; }
dependencies[file] = suggested;
drafted++;
}
const groups = new Map<string, string[]>();
for (const t of tests) { const lang = t.endsWith('.dart') ? 'dart' : t.endsWith('.rs') ? 'rust' : t.endsWith('.py') ? 'python' : /\.[cm]?[jt]sx?$/.test(t) ? 'javascript' : 'other'; groups.set(lang, [...(groups.get(lang) ?? []), t]); }
const notes = [...groups].map(([lang, list]) => {
if (lang === 'dart') return `${list.length} Dart test file(s) run per package with flutter test or dart test`;
if (lang === 'rust') return `${list.length} Rust test file(s) run per crate with cargo test -p`;
const runner = detectRunner({ repoRoot, config: DEFAULT_CONFIG }, list);
return runner ? `${lang} tests detected at run time: ${runner}` : `no runner detected for ${list.length} ${lang} test file(s); set runner (with {files})`;
});
const runnerNote = notes.length ? notes.join('; ') : 'no test files found';
const selectionTodo = discoverLanes(repoRoot).filter(l => l.runsTests && l.matrixExpression && l.testFiles.length === 0).map(l => l.id);
const config = { $schema: PREGATE_CONFIG_SCHEMA, runner: null, dependencies, empty_lanes: [], selection: {}, warn: [], strays: { allow: [] } };
return { json: JSON.stringify(config, null, 2) + '\n', languages, runnerNote, drafted, todo, selectionTodo };
}
/** The lines `init` prints after writing the draft. */
export function describeDraft(d: InitDraft): string[] {
const lines = [
`PREGATE_INIT: languages ${d.languages.map(l => `${l.language} ${l.files}`).join(', ') || '(none recognised)'}`,
`PREGATE_INIT: runner null (${d.runnerNote})`,
`PREGATE_INIT: ${d.drafted} file(s) with no test lane mapped to the tests nearest them under dependencies; review each entry`,
];
for (const id of d.selectionTodo) lines.push(`TODO selection: ${id} computes its matrix; declare the command that lists its test files under selection`);
for (const f of d.todo.slice(0, MAX_TODO_LINES)) lines.push(`TODO ${f}: no test lane and no nearby test; add a test, map it under dependencies, or declare [] if no test can cover it`);
if (d.todo.length > MAX_TODO_LINES) lines.push(`TODO: +${d.todo.length - MAX_TODO_LINES} more file(s) with no test lane`);
return lines;
}