From 8e5461fdd6d77d1f5f7e3a3f998d1cab06f31d38 Mon Sep 17 00:00:00 2001 From: garrytan Date: Mon, 19 Oct 2026 19:03:32 +0000 Subject: [PATCH] fix(pregate): outside-voice review fixes for the non-JS lanes - Tool config is an allowlist: unknown dotfiles (.rspec, .husky hooks) and stylesheets still need a lane; snapshot, golden and fixture trees and .gstack/ are never inert. - Django edges need Django evidence (manage.py or code importing django) and settings assignments in code, not strings or comments. - A cd after ||, &, control flow or braces leaves a lane unknown. - Dart directives allow metadata and quoted semicolons. - init drafts at most 256 dependencies (the schema limit); the rest are TODO. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> --- CHANGELOG.md | 8 +++---- docs/pregate.md | 20 +++++++++------- docs/troubleshooting.md | 4 ++-- lib/pregate/config.ts | 4 ++-- lib/pregate/django.ts | 16 +++++++++---- lib/pregate/docs-lane.ts | 35 ++++++++++++++------------- lib/pregate/import-graph.ts | 17 ++++++++------ lib/pregate/init-draft.ts | 7 +++--- lib/pregate/workflows.ts | 7 +++--- test/pregate-non-js.test.ts | 47 ++++++++++++++++++++++++++++++++----- 10 files changed, 109 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e7dbe060..8eacfce75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,14 +5,14 @@ **`gstack-pregate` works on Django, Dart and Flutter repos, and on branches that only touch assets or tool config.** Before, a Django migration, an `admin.py`, a template, a font, a vendored licence or `.pre-commit-config.yaml` stopped `/ship` with `PREGATE_NO_LANE` in a repo that had never opted in, and every touched `_test.dart` file did the same. `gstack-pregate init` now drafts a policy from the repo instead of writing a fixed example. Contributed by @arlophoenix (#3285) and @szsunyuan (#3290); reported by @dgrant (#3266, #3267, #3268, #3269). ### Fixed -- **Django files loaded by name select tests (#3266).** In a Django project (a Python file assigns `INSTALLED_APPS` or `ROOT_URLCONF`, or a config file names `DJANGO_SETTINGS_MODULE`), every Python test depends on the settings module; a dotted string in settings and an `include("app.urls")` argument count as imports; an app's migrations, `admin.py`, `templates/`, `static/`, `templatetags/`, `management/`, `fixtures/` and `locale/` files select that app's tests and the tests that reach its `models.py`; a project-level template selects the suite. -- **Files no test runner loads take an empty lane (#3267).** Images, fonts, media, PDFs, stylesheets and dotfile tool config (`.pre-commit-config.yaml`, `.vscode/`, `.impeccable/`) print `no-tests (assets and tool config)`, and licence text under any vendored name (`roboto-LICENSE.txt`, `LICENSE-MIT`) is documentation, when no test names the file. CI workflows, `.gstack/`, `.env*`, version pins and runner config (babel, swc, mocha, coverage) still need a lane, and so does unmapped code. +- **Django files loaded by name select tests (#3266).** In a Django project (a manage.py or code importing django, plus Python code that assigns `INSTALLED_APPS` or `ROOT_URLCONF`, or a config file naming `DJANGO_SETTINGS_MODULE`), every Python test depends on the settings module; a dotted string in settings and an `include("app.urls")` argument count as imports; an app's migrations, `admin.py`, `templates/`, `static/`, `templatetags/`, `management/`, `fixtures/` and `locale/` files select that app's tests and the tests that reach its `models.py`; a project-level template selects the suite. +- **Files no test runner loads take an empty lane (#3267).** Images, fonts, media and PDFs (outside snapshot, golden and fixture trees) and allowlisted tool config (`.pre-commit-config.yaml`, `.editorconfig`, lint and format config, `.vscode/`, `.idea/`, `.impeccable/`, GitHub issue templates and CODEOWNERS) print `no-tests (assets and tool config)`, and licence text under any vendored name (`roboto-LICENSE.txt`, `LICENSE-MIT`) is documentation, when no test names the file. Stylesheets, unknown dotfiles (`.rspec`, `.husky/` hooks), CI workflows, `.gstack/`, `.env*` and version pins still need a lane, and so does unmapped code. - **Dart and Flutter lanes (#3285, @arlophoenix).** The import graph reads `import`/`export`/`part` directives and resolves `package:` URIs through every tracked `pubspec.yaml`. `lanes` runs each package's tests from its own root with `flutter test` or `dart test`, through fvm when the package pins it. flutter, dart, melos and very_good CI jobs are lanes, and a run step's test paths resolve against its `working-directory` and literal `cd`. A missing Dart SDK or a `.dart` file outside any package is `incomplete`, never a pass; device tests are not unit lanes. - **`--base ` gets a branch suggestion, not an impossible fetch (#3269, #3290, @szsunyuan).** ### Changed -- **`gstack-pregate init` drafts from the repo (#3268).** It prints the languages it found and the runner each language's tests get, maps each file the import graph cannot place to the tests of the nearest directory above it, and lists the rest as `TODO`. It never writes `[]`, which would clear a file without a test. -- **Review fixes on the Dart port.** A `cd` inside a pipeline, a command substitution or a redirected subshell now leaves a CI lane's test set unknown (it may run any test) instead of resolving later paths against the wrong directory, which could drop a platform obligation. Dart directives are read to their `;` whatever the line indentation, and nested block comments are skipped, so an unindented conditional import or a long `show` list no longer loses its edges. +- **`gstack-pregate init` drafts from the repo (#3268).** It prints the languages it found and the runner each language's tests get, maps each file the import graph cannot place to the tests of the nearest directory above it, and lists the rest as `TODO` (as it does past the 256-entry schema limit). It never writes `[]`, which would clear a file without a test. +- **Review fixes on the Dart port.** A `cd` inside a pipeline, an alternative, a background job, shell control flow, a command substitution or a redirected subshell now leaves a CI lane's test set unknown (it may run any test) instead of resolving later paths against the wrong directory, which could drop a platform obligation. Dart directives are read to their `;` whatever the line indentation, metadata before a directive and semicolons inside a URI are handled, and nested block comments are skipped, so an unindented conditional import or a long `show` list no longer loses its edges. - **Gate integrity:** new free regressions in `test/pregate-non-js.test.ts` (Django, assets and licence, init, and the two review fixes, each with a negative control) and `test/pregate-dart.test.ts`. Each new positive case fails on the tree before this change. No existing test, threshold, trial count or case kind changed; `test/helpers/pregate-fixture.ts` only reports more about a failed git spawn. ## [1.91.148.0] - 2026-10-19 diff --git a/docs/pregate.md b/docs/pregate.md index 058e72599..8e7660cd2 100644 --- a/docs/pregate.md +++ b/docs/pregate.md @@ -78,7 +78,8 @@ takes effect. `gstack-pregate validate` / `init` cover the working-tree file. `gstack-pregate init` writes a draft built from the repo, not a fixed example. It prints the languages it found and the runner each language's tests would get, then maps every file the import graph cannot place to the tests of the -nearest directory above it that holds at most 25 of them. The draft never +nearest directory above it that holds at most 25 of them, up to the +256-entry schema limit. The draft never writes `[]`, which would clear a file without a test. A file with no nearby test, and a lane whose matrix a planner computes, is printed as a `TODO`. Review each entry before committing the file on the base branch. @@ -144,14 +145,15 @@ Two stages with separate clocks, both printed: still selects that test. `empty_lanes` adds globs for other documentation; it cannot clear a source file or a file under a test directory. Files no test runner loads take a second empty lane by the same rule - (`no-tests (assets and tool config): …`): images, fonts, media, PDFs and - stylesheets, plus dotfile tool configuration (a root dotfile or anything - under a top-level dot-directory, such as `.pre-commit-config.yaml` or - `.vscode/settings.json`). CI workflows and actions, `.gstack/`, and the - dotfiles that change how a runtime or runner behaves (`.env*`, `.nvmrc` and - other version pins, `.npmrc`, babel, swc, mocha and coverage config) are not - in it and still need a lane. - In a Django project (a Python file assigns `INSTALLED_APPS` or + (`no-tests (assets and tool config): …`): images, fonts, media and PDFs + outside snapshot, golden and fixture directories, plus an allowlist of tool + configuration (`.editorconfig`, `.gitignore`, `.pre-commit-config.yaml`, + lint and format config, `.vscode/`, `.idea/`, `.impeccable/`, and GitHub + issue templates, CODEOWNERS and dependabot config). Stylesheets (they import + each other), any other dotfile (`.rspec`, `.husky/` hooks, `.env*`, version + pins), CI workflows and actions, and `.gstack/` still need a lane. + In a Django project (a manage.py or code importing django, plus Python + code, not a string or comment, that assigns `INSTALLED_APPS` or `ROOT_URLCONF`, or a config file names `DJANGO_SETTINGS_MODULE`), the files Django loads by name get `framework` edges: every Python test depends on each settings module; a dotted string in a settings module and an diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 1207b6203..a6f3b6004 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -2855,8 +2855,8 @@ test depends on is a docs/no-tests empty lane and is not this error, whether or not the name says plan, evidence or redproof. A test that reads one of those files (a quoted path or a `join()` of path segments) still selects that test. An unmapped source or code file still fails. A file under a test -directory is not this empty lane. Images, fonts, media, stylesheets and -dotfile tool config no test names are the assets-and-tool-config empty lane, +directory is not this empty lane. Images, fonts and media, and +allowlisted tool config no test names, are the assets-and-tool-config empty lane, not this error. **Fix.** Add a test that names the file, or declare its lane (an explicit `[]` diff --git a/lib/pregate/config.ts b/lib/pregate/config.ts index 16e5c44cf..cb9d82a9e 100644 --- a/lib/pregate/config.ts +++ b/lib/pregate/config.ts @@ -62,7 +62,7 @@ export interface Guard { id: string; when: string[]; command: string; fix?: stri export const DEFAULT_CONFIG: PregateConfig = { runner: null, dependencies: {}, empty_lanes: [], selection: {}, warn: [], strays: { allow: [] }, guards: [], serial_patterns: DEFAULT_SERIAL_PATTERNS, price_file: null, stall_timeout_s: null, remote_gate: null }; export interface ConfigError { code: ResultCodeName; path: string; message: string } -const MAX_LIST = 256; +export const MAX_LIST = 256; const MAX_COMMAND = 512; const KEYS = ['runner', 'dependencies', 'empty_lanes', 'selection', 'warn', 'strays', 'guards', 'serial_patterns', 'price_file', 'sealed', 'stall_timeout_s', 'remote_gate']; const MAX_GUARDS = 32; @@ -219,7 +219,7 @@ export function explainConfig(): string { return [ 'runner: the test command with a {files} placeholder; null detects bun test / vitest / jest / pytest from the repo', 'dependencies: glob → test files the import graph cannot see (subprocess targets, runtime reads, generated inputs); [] declares no lane needed', - 'empty_lanes: extra globs treated as docs/no-tests when nothing else maps them. Built-in, with or without this file: any Markdown file the import graph maps to no test (a quoted path, a join() of path segments, an import, or a dependencies entry), plus README/CHANGELOG/CONTRIBUTING/LICENSE/AGENTS/CLAUDE and licence text under any name; assets (images, fonts, media, stylesheets) and dotfile tool config take their own empty lane. Source code and files under a test directory never qualify. A test that reads the file still selects that test', + 'empty_lanes: extra globs treated as docs/no-tests when nothing else maps them. Built-in, with or without this file: any Markdown file the import graph maps to no test (a quoted path, a join() of path segments, an import, or a dependencies entry), plus README/CHANGELOG/CONTRIBUTING/LICENSE/AGENTS/CLAUDE and licence text under any name; assets (images, fonts, media) and allowlisted tool config take their own empty lane. Source code and files under a test directory never qualify. A test that reads the file still selects that test', 'selection: / → the runner-owned command that lists that lane’s test files when its matrix is planner-computed', `warn: check ids downgraded to warn; never ${NON_DOWNGRADABLE.join(', ')}; lanes keeps its required-lane and requires-remote verdicts`, 'strays.allow: root files or new top-level directories that are intentional', diff --git a/lib/pregate/django.ts b/lib/pregate/django.ts index 7971eb95d..b1e6d3ac2 100644 --- a/lib/pregate/django.ts +++ b/lib/pregate/django.ts @@ -4,8 +4,10 @@ * a project from settings strings and directory conventions, so without these * edges a migration, an admin module or a template has no test lane. * - * A repo is a Django project when a Python file assigns INSTALLED_APPS or - * ROOT_URLCONF, or a config file names DJANGO_SETTINGS_MODULE. Then: + * A repo is a Django project when it carries Django evidence (a manage.py, or + * Python code that imports django) and a settings module: Python code (strings + * and comments excluded) that assigns INSTALLED_APPS or ROOT_URLCONF, or the + * module a config file names in DJANGO_SETTINGS_MODULE. Then: * - every Python test depends on each settings module (the test runner * loads it), so a settings change selects the suite; * - a dotted string in a settings module (`"stores.backends.StoreOwnerBackend"`, @@ -26,6 +28,8 @@ export interface DjangoInput { files: readonly string[]; tests: ReadonlySet; read: (rel: string) => string | null; + /** Python source with strings blanked and comments dropped. */ + code: (source: string) => string; /** A dotted Python name → the repo files of its longest module prefix ([] outside the repo). */ resolveDotted: (dotted: string) => string[]; } @@ -34,6 +38,7 @@ export interface DjangoInput { export interface DjangoEdge { dependency: string; dependent: string } const SETTINGS_ASSIGN = /^(?:INSTALLED_APPS|ROOT_URLCONF)[ \t]*(?::[^=\n]*)?[+]?=/m; +const IMPORTS_DJANGO = /^[ \t]*(?:from[ \t]+django[\w.]*[ \t]+import|import[ \t]+django\b)/m; const SETTINGS_ENV = /DJANGO_SETTINGS_MODULE['"]?\s*[,=:]\s*['"]?([A-Za-z_][\w.]*)/g; const SETTINGS_ENV_FILES = /(?:^|\/)(?:manage\.py|pytest\.ini|pyproject\.toml|setup\.cfg|tox\.ini|conftest\.py|\.env\.example)$/; const DOTTED_STRING = /['"]([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)+)['"]/g; @@ -44,16 +49,19 @@ const APP_LOADED = /^(?:migrations\/.+\.py|admin\.py|admin\/.+|templates\/.+|jin function settingsModules(input: DjangoInput): string[] { const out = new Set(); + let evidence = input.files.some(f => /(?:^|\/)manage\.py$/.test(f)); for (const f of input.files) { if (f.endsWith('.py')) { const text = input.read(f); - if (text && SETTINGS_ASSIGN.test(text)) out.add(f); + const code = text ? input.code(text) : ''; + if (SETTINGS_ASSIGN.test(code)) out.add(f); + if (!evidence && IMPORTS_DJANGO.test(code)) evidence = true; } if (!SETTINGS_ENV_FILES.test(f)) continue; const text = input.read(f) ?? ''; for (const m of text.matchAll(SETTINGS_ENV)) for (const t of input.resolveDotted(m[1]!)) if (t.endsWith('.py') && !t.endsWith('/__init__.py')) out.add(t); } - return [...out].sort(); + return evidence ? [...out].sort() : []; } /** App directories: a directory holding apps.py, models.py, models/__init__.py or migrations/__init__.py. */ diff --git a/lib/pregate/docs-lane.ts b/lib/pregate/docs-lane.ts index 2af680f77..0644dcef6 100644 --- a/lib/pregate/docs-lane.ts +++ b/lib/pregate/docs-lane.ts @@ -12,11 +12,13 @@ * Unmapped code stays `PREGATE_NO_LANE`. * * `isInertNoTests` is the second empty-lane class (#3267): files no test - * runner loads — binary and media assets, stylesheets, and editor, lint and - * hook configuration in dotfiles. It applies under the same rule (only when - * nothing maps the file to a test) and never to code, the test tree, CI - * workflows, the pin files, or the dotfiles that change how a runtime or a - * test runner behaves (version pins, env files, babel/swc/mocha/coverage). + * runner loads — binary and media assets, and a named allowlist of editor, + * lint, format and repository-metadata configuration. It applies under the + * same rule (only when nothing maps the file to a test) and never to code, + * the test tree, snapshot or golden baselines, CI workflows, the pin files, + * stylesheets (they import each other), or any dotfile not on the list: an + * unknown dotfile may be read by a runner (`.rspec`, `.taprc`) or executed + * (`.husky/pre-commit`), so it still needs a lane. */ import { SOURCE_EXT } from './import-graph'; import { matchesAny } from '../ship-policy'; @@ -27,11 +29,13 @@ const MARKDOWN = /\.(?:md|markdown|rst|adoc|asciidoc)$/i; const PROSE_NAME = /^(?:README|CHANGELOG|CONTRIBUTING|AGENTS|CLAUDE|ETHOS|LICENSE|LICENCE|COPYING|NOTICE)(?:\.md)?$/i; /** Licence text under any name a vendored asset ships it with: `roboto-LICENSE.txt`, `LICENSE-MIT`, `COPYING.LESSER`, `licenses/apache.txt`. */ const LICENCE_TEXT = /(?:^|[-_.])(?:LICEN[CS]E|COPYING|COPYRIGHT|NOTICE)(?:[-_.][\w-]*)?(?:\.(?:txt|md))?$|^(?:licen[cs]es?|LICENSES)\/(?:.*\/)?[^/]+\.(?:txt|md)$/i; -const ASSET = /\.(?:png|jpe?g|gif|webp|avif|bmp|ico|icns|tiff?|svg|heic|woff2?|ttf|otf|eot|mp3|mp4|m4a|wav|ogg|oga|webm|mov|flac|pdf|psd|css|scss|sass|less)$/i; -/** Dot-directories whose contents CI, the runtime or pregate itself reads. */ -const RUNTIME_DOT_DIR = /^\.(?:github\/(?:workflows|actions)|gstack|cargo|fvm|dart_tool|mvn|gradle|yarn|config|circleci|buildkite|storybook)(?:\/|$)/i; -/** Dotfiles that change how a runtime, package manager or test runner behaves. */ -const RUNTIME_DOTFILE = /^\.(?:env(?:\..*)?|nvmrc|node-version|python-version|ruby-version|tool-versions|bun-version|fvmrc|npmrc|yarnrc(?:\.yml)?|babelrc(?:\..*)?|swcrc|mocharc(?:\..*)?|c8rc(?:\..*)?|nycrc(?:\..*)?|coveragerc|gitmodules|pnp\..*|gitlab-ci\.yml|travis\.yml)$/i; +const ASSET = /\.(?:png|jpe?g|gif|webp|avif|bmp|ico|icns|tiff?|svg|heic|woff2?|ttf|otf|eot|mp3|mp4|m4a|wav|ogg|oga|webm|mov|flac|pdf|psd)$/i; +/** Directories whose files a test compares against or that pregate and CI read: never inert, assets included. */ +const PROTECTED_TREE = /(?:^|\/)(?:__snapshots__|__image_snapshots__|[^/]*-snapshots|snapshots?|goldens?|baselines?|fixtures?)\/|^\.(?:github\/(?:workflows|actions)|gstack)\//i; +/** Editor, lint, format, hook-manager and repository-metadata configuration no test runner reads. */ +const INERT_DOTFILE = /^\.(?:editorconfig|gitignore|gitattributes|gitmessage|mailmap|dockerignore|npmignore|prettierignore|eslintignore|pre-commit-config\.ya?ml|pre-commit-hooks\.ya?ml|prettierrc(?:\.(?:json|ya?ml|toml))?|eslintrc(?:\.(?:json|ya?ml))?|stylelintrc(?:\.(?:json|ya?ml))?|markdownlint(?:\.(?:json|jsonc|ya?ml))?|markdownlintignore|yamllint(?:\.ya?ml)?|flake8|pylintrc|isort\.cfg|rubocop\.yml|shellcheckrc|hadolint\.ya?ml|codespellrc|clang-format)$/i; +/** Dot-directories of editors and design tools, and GitHub's repository metadata (not workflows or actions). */ +const INERT_DOT_DIR = /^\.(?:vscode|idea|fleet|zed|cursor|impeccable)\/|^\.github\/(?:ISSUE_TEMPLATE\/|DISCUSSION_TEMPLATE\/|PULL_REQUEST_TEMPLATE|pull_request_template|CODEOWNERS$|FUNDING\.ya?ml$|dependabot\.ya?ml$|labeler\.ya?ml$|release\.ya?ml$)/; function isMarkdown(rel: string): boolean { return MARKDOWN.test(rel); @@ -72,21 +76,20 @@ export function isDocsEmptyLane(rel: string, extraGlobs: readonly string[] = []) return isBuiltinDocsPath(norm); } -/** Dotfile tool configuration: a root dotfile, or anything under a top-level dot-directory, that no runtime or runner reads. */ +/** Tool configuration on the allowlist: a named dotfile at any depth, or a file under an editor or design-tool dot-directory. */ function isToolConfig(rel: string): boolean { - const base = rel.split('/').pop() ?? rel; - if (!rel.startsWith('.') || RUNTIME_DOT_DIR.test(rel) || RUNTIME_DOTFILE.test(base)) return false; - return rel.includes('/') || base.startsWith('.'); + return INERT_DOTFILE.test(rel.split('/').pop() ?? rel) || INERT_DOT_DIR.test(rel); } /** * True when `rel` is a file no test runner loads (#3267): a binary or media - * asset, a stylesheet, or dotfile tool configuration. Callers apply this only + * asset outside any snapshot, golden or fixture tree, or allowlisted tool + * configuration. Callers apply this only * after the import graph found no test, so an asset a test imports or names * still selects that test. */ export function isInertNoTests(rel: string): boolean { const norm = rel.replace(/\\/g, '/').replace(/^\.\//, ''); - if (!norm || isTestTree(norm) || isSourceCode(norm)) return false; + if (!norm || isTestTree(norm) || isSourceCode(norm) || PROTECTED_TREE.test(norm)) return false; return ASSET.test(norm) || isToolConfig(norm); } diff --git a/lib/pregate/import-graph.ts b/lib/pregate/import-graph.ts index bac42851c..a50595e7f 100644 --- a/lib/pregate/import-graph.ts +++ b/lib/pregate/import-graph.ts @@ -109,19 +109,22 @@ function resolveDartUri(files: Set, packages: Map, fro /** * The URIs of every `import`/`export`/`part` directive (never `part of`), comments blanked. A directive runs from its keyword at - * a line start or after a `;` to the next `;`, across any number of lines whatever their indentation (a conditional import's + * a line start or after a `;`, metadata (`@deprecated`) allowed before it, to the next unquoted `;`, across any number of lines whatever their indentation (a conditional import's * `if (…) 'uri'` clause may start a line). The scan resumes after each directive, so it is one linear pass; a directive missing its * `;` reads to the end of the file, which can only add edges. */ export function dartDirectiveUris(source: string): string[] { const code = blankDartComments(source.replace(/^\uFEFF/, '')); - const start = /(?:^|;)[ \t]*(?:import|export|part)\b(?!\s+of\b)/gm; + const start = /(?:^|;)[ \t]*(?:@[\w.$]+(?:\([^;]*?\))?\s+)*(?:import|export|part)\b(?!\s+of\b)/gm; const uris: string[] = []; for (let m = start.exec(code); m; m = start.exec(code)) { const from = m.index + m[0].length; - const semi = code.indexOf(';', from); - const end = semi < 0 ? code.length : semi; - for (const u of code.slice(from, end).matchAll(/['"]([^'"\n]+)['"]/g)) uris.push(u[1]!); + let end = from; + for (let quote = ''; end < code.length && (quote || code[end] !== ';'); end++) { + const c = code[end]!; + if (quote) { if (c === '\\') end++; else if (c === quote || c === '\n') quote = ''; } else if (c === "'" || c === '"') quote = c; + } + for (const u of code.slice(from, end).matchAll(/'([^'\n]+)'|"([^"\n]+)"/g)) uris.push((u[1] ?? u[2])!); start.lastIndex = Math.max(end, from); } return uris; @@ -325,7 +328,7 @@ function pyFromBase(rel: string, spec: string): string | null { * stay inert. Backslash escapes are honoured; an unterminated string runs to * its line end, or to the end of the file when triple-quoted. */ -function pythonCode(source: string): string { +export function pythonCode(source: string): string { const parts: string[] = []; let from = 0; for (let i = 0; i < source.length;) { @@ -462,7 +465,7 @@ export function buildGraph(repoRoot: string, opts: BuildOptions = {}): Graph { for (const { target, via } of referencesIn(files, dirs, rel, source, config, graph.tests.has(rel) ? unresolved : undefined, packages)) addEdge(graph, target, rel, via); if (unresolved.length) graph.unresolved!.set(rel, unresolved); } - const django = djangoEdges({ files: [...files], tests: graph.tests, read: rel => pySources.get(rel) ?? readRepoFile(repoRoot, rel, maxBytes), resolveDotted: dotted => pythonPatchTarget(files, dotted) }); + const django = djangoEdges({ files: [...files], tests: graph.tests, read: rel => pySources.get(rel) ?? readRepoFile(repoRoot, rel, maxBytes), code: pythonCode, resolveDotted: dotted => pythonPatchTarget(files, dotted) }); for (const e of django) addEdge(graph, e.dependency, e.dependent, 'framework'); for (const test of graph.tests) { const m = /^(?:[\w.-]+\/)*?test\/([\w-]+)\.(?:test|spec)\./.exec(test); diff --git a/lib/pregate/init-draft.ts b/lib/pregate/init-draft.ts index e63f06cce..974d48169 100644 --- a/lib/pregate/init-draft.ts +++ b/lib/pregate/init-draft.ts @@ -7,14 +7,15 @@ * 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 is printed as a TODO for the owner to decide. Documentation, + * 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, PREGATE_CONFIG_PATH, PREGATE_CONFIG_SCHEMA } from './config'; +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'; @@ -65,7 +66,7 @@ export function draftConfig(repoRoot: string): InitDraft { if (isDocsEmptyLane(file) || isInertNoTests(file)) continue; if (testsFor(graph, file).tests.length) continue; const suggested = nearestTests(tests, file); - if (!suggested.length) { todo.push(file); continue; } + if (!suggested.length || drafted >= MAX_LIST) { todo.push(file); continue; } dependencies[file] = suggested; drafted++; } diff --git a/lib/pregate/workflows.ts b/lib/pregate/workflows.ts index e336bea2d..8b43588c3 100644 --- a/lib/pregate/workflows.ts +++ b/lib/pregate/workflows.ts @@ -104,9 +104,10 @@ function commandTestFiles(command: string, start: string | null, scripts: Record const out: string[] = []; const saved: Array = []; const segments = command.split(/&&|\|\||[;|\n]/); - // A pipeline element, command substitution, or a `)` with text after it (a redirect) runs a `cd` in a scope this walk cannot - // track, so in such a command any directory change makes the directory unknown (the lane may run any test). - const untracked = /(? /\)/.test(seg.replace(/\)+\s*$/, ''))); + // A pipeline element, a `||` alternative, a background job, a command substitution, shell control flow, or a `)` with text after + // it (a redirect) runs a `cd` in a scope or order this walk cannot track, so in such a command any directory change makes the + // directory unknown (the lane may run any test). + const untracked = /\||(? /\)/.test(seg.replace(/\)+\s*$/, ''))); for (const raw of segments) { // A `( … )` subshell's cds end at its `)`. const opens = /^\s*(\(+)/.exec(raw)?.[1]!.length ?? 0; diff --git a/test/pregate-non-js.test.ts b/test/pregate-non-js.test.ts index 1e60d1e26..bf162617d 100644 --- a/test/pregate-non-js.test.ts +++ b/test/pregate-non-js.test.ts @@ -68,7 +68,7 @@ describe('Django files loaded by name have a lane (#3266)', () => { expect(r.out).toMatch(/tests\/test_smoke\.py ← config\/settings\.py ← config\/urls\.py ← shop\/urls\.py ← shop\/views\.py \(framework\)/); }); - test('negative controls: without a settings module there are no Django edges, and unmapped Python in a Django repo still fails closed', () => { + test('negative controls: no Django edges without a settings module, from settings text inside a string or comment, or without Django evidence; unmapped Python in a Django repo still fails closed', () => { const plain = fixture(DJANGO_APP); commit(plain, { 'orders/admin.py': 'from django.contrib import admin\nadmin.site.site_header = "Orders"\n' }); const r = lanes(plain); @@ -76,6 +76,18 @@ describe('Django files loaded by name have a lane (#3266)', () => { expect(r.out).toContain('FAIL lanes — orders/admin.py has no test lane'); expect(r.out).not.toContain('(framework)'); + const quoted = fixture({ ...DJANGO_APP, 'docs_example.py': 'EXAMPLE = """\nINSTALLED_APPS = []\n"""\n# ROOT_URLCONF = "config.urls"\n' }); + commit(quoted, { 'orders/admin.py': 'from django.contrib import admin\nadmin.site.site_header = "Orders"\n' }); + const q = lanes(quoted); + expect(q.out).toContain('FAIL lanes — orders/admin.py has no test lane'); + expect(q.out).not.toContain('(framework)'); + + const noDjango = fixture({ 'app/settings.py': 'INSTALLED_APPS = ["app.plugins.Core"]\n', 'app/__init__.py': '', 'app/plugins.py': 'class Core: pass\n', 'app/admin.py': 'X = 1\n', 'app/models.py': 'Y = 2\n', 'tests/test_app.py': 'def test_x():\n assert True\n' }); + commit(noDjango, { 'app/admin.py': 'X = 2\n' }); + const n = lanes(noDjango); + expect(n.out).toContain('FAIL lanes — app/admin.py has no test lane'); + expect(n.out).not.toContain('(framework)'); + const django = fixture({ ...DJANGO_APP, 'config/settings.py': SETTINGS }); commit(django, { 'scripts/oneoff.py': 'print("not loaded by Django or any test")\n' }); const bad = lanes(django); @@ -121,7 +133,7 @@ describe('assets, licence text and tool config take an empty lane (#3267)', () = expect(r.out).toContain('no-tests (assets and tool config): .pre-commit-config.yaml'); }); - test('negative controls: runtime dotfiles, CI workflows, plain text and code stay unmapped; an asset a test names selects that test', () => { + test('negative controls: runtime and unknown dotfiles, hooks, CI workflows, stylesheets, snapshot baselines, plain text and code stay unmapped; an asset a test names selects that test', () => { const repo = fixture(); commit(repo, { '.nvmrc': '24\n', @@ -129,10 +141,15 @@ describe('assets, licence text and tool config take an empty lane (#3267)', () = '.github/workflows/lint.yml': 'name: Lint\non: [push]\njobs:\n lint:\n runs-on: ubuntu-24.04\n steps:\n - run: echo lint\n', 'notes/sketch.txt': 'not licence text\n', 'lib/orphan.js': 'exports.lonely = () => 3;\n', + '.rspec': '--tag ~slow\n', + '.husky/pre-commit': 'npx lint-staged\n', + 'styles/colors.css': ':root { --brand: red; }\n', + 'src/__image_snapshots__/home.png': '\u0089PNG', + '.gstack/icon.svg': '\n', }); const r = lanes(repo); expect(r.status).toBe(EXIT.fail); - for (const f of ['.nvmrc', '.env.test', '.github/workflows/lint.yml', 'notes/sketch.txt', 'lib/orphan.js']) expect(r.out).toContain(`FAIL lanes — ${f} has no test lane`); + for (const f of ['.nvmrc', '.env.test', '.github/workflows/lint.yml', 'notes/sketch.txt', 'lib/orphan.js', '.rspec', '.husky/pre-commit', 'styles/colors.css', 'src/__image_snapshots__/home.png', '.gstack/icon.svg']) expect(r.out).toContain(`FAIL lanes — ${f} has no test lane`); expect(r.out).not.toContain('no-tests (assets and tool config)'); const named = fixture({ 'assets/logo.svg': '\n', 'test/logo.test.js': "const test = require('node:test');\nconst fs = require('node:fs');\ntest('logo', () => { fs.readFileSync('assets/logo.svg', 'utf8'); });\n" }); @@ -188,24 +205,42 @@ describe('gstack-pregate init drafts a policy from the repo (#3268)', () => { }); }); +describe('gstack-pregate init respects the schema limit (#3268)', () => { + test('past 256 drafted entries the rest are TODO, and the draft still validates', () => { + const many: Record = { 'pkg/tests/test_pkg.py': 'def test_pkg():\n assert True\n' }; + for (let n = 0; n < 260; n++) many[`pkg/mod_${n}.py`] = `VALUE = ${n}\n`; + const repo = fixture(many); + const draft = draftConfig(repo.work); + expect(Object.keys(JSON.parse(draft.json).dependencies)).toHaveLength(256); + expect(draft.todo.filter(f => f.startsWith('pkg/mod_'))).toHaveLength(4); + expect(runBin(repo, 'gstack-pregate', ['init']).status).toBe(EXIT.ok); + expect(runBin(repo, 'gstack-pregate', ['validate']).status).toBe(EXIT.ok); + }); +}); + describe('review fixes on the #3285 port: selection never shrinks on syntax it cannot follow', () => { const files = (run: string) => parseWorkflow('ci.yml', `jobs:\n unit:\n runs-on: ubuntu-24.04\n steps:\n - run: ${run}\n`)[0]!.testFiles; - test('a cd inside a pipeline or a redirected subshell leaves the lane unknown instead of moving later paths', () => { + test('a cd inside a pipeline, an alternative, a background job, control flow or a redirected subshell leaves the lane unknown instead of moving later paths', () => { expect(files('cd pkg | cat; bun test test/a.test.ts')).toEqual([]); expect(files('(cd pkg && echo ok) > logfile; bun test test/a.test.ts')).toEqual([]); expect(files('cd pkg && bun test test/a.test.ts')).toEqual(['pkg/test/a.test.ts']); expect(files('(cd pkg && bun test test/b.test.ts); bun test test/a.test.ts')).toEqual(['pkg/test/b.test.ts', 'test/a.test.ts']); expect(files('bun test test/a.test.ts | tee log')).toEqual(['test/a.test.ts']); + expect(files('cd . || cd elsewhere; bun test test/a.test.ts')).toEqual([]); + expect(files('cd pkg & wait; bun test test/a.test.ts')).toEqual([]); + expect(files('if [ -d pkg ]; then cd pkg; fi; bun test test/a.test.ts')).toEqual([]); }); - test('Dart directives: an unindented conditional clause, nested comments, a long show list, two on a line; part of is not a directive', () => { - const files = new Set(['lib/core.dart', 'lib/core_io.dart', 'lib/a.dart', 'lib/b.dart', 'lib/c.dart', 'lib/big.dart', 'lib/nope.dart', 'lib/commented.dart', 'lib/lib.dart']); + test('Dart directives: an unindented conditional clause, nested comments, a long show list, two on a line, metadata, a quoted semicolon; part of is not a directive', () => { + const files = new Set(['lib/part;name.dart', 'lib/core.dart', 'lib/core_io.dart', 'lib/a.dart', 'lib/b.dart', 'lib/c.dart', 'lib/big.dart', 'lib/nope.dart', 'lib/commented.dart', 'lib/lib.dart']); const targets = (source: string) => referencesIn(files, new Set(['lib']), 'lib/main.dart', source).map(r => r.target); expect(targets("import 'core.dart'\nif (dart.library.io) 'core_io.dart';\n")).toEqual(['lib/core.dart', 'lib/core_io.dart']); expect(targets("/* outer /* inner */ still a comment 'nope.dart' */\nimport 'a.dart';\n")).toEqual(['lib/a.dart']); expect(targets(`import 'big.dart' show ${Array.from({ length: 600 }, (_, n) => `name${n}`).join(', ')};\n`)).toEqual(['lib/big.dart']); expect(targets("import 'a.dart'; export 'b.dart';\npart 'c.dart';\npart of 'lib.dart';\n")).toEqual(['lib/a.dart', 'lib/b.dart', 'lib/c.dart']); expect(targets("// import 'commented.dart';\nvoid main() {}\n")).toEqual([]); + expect(targets("@deprecated import 'a.dart';\n@Deprecated('use b') export 'b.dart';\n")).toEqual(['lib/a.dart', 'lib/b.dart']); + expect(targets("import 'part;name.dart';\n")).toEqual(['lib/part;name.dart']); }); });