Skip to content

The design language that makes your AI harness better at design.

JavaScript

test:rust-changed: keep non-ASCII paths, let the log flush, and pin the selection with tests

Paul Bakaus7bafe008174a
diff --git a/scripts/lib/rust-test-selection.mjs b/scripts/lib/rust-test-selection.mjs
new file mode 100644
index 000000000..580e7a1be
--- /dev/null
+++ b/scripts/lib/rust-test-selection.mjs
@@ -0,0 +1,75 @@
+// Which workspace crates a change reaches, for scripts/test-rust-changed.mjs.
+// Pure: the caller supplies the changed paths, the workspace packages and a
+// search over crate sources, so tests/rust-test-selection.test.mjs can pin
+// the selection without git or cargo.
+
+export const WORKSPACE_FILES = [/^Cargo\.(toml|lock)$/, /^rust-toolchain(\.toml)?$/, /^\.cargo\//];
+
+// Crates name what they read either relative to their manifest
+// (`include_str!("../../../skill/scripts/live-browser.js")`,
+// `CARGO_MANIFEST_DIR/../../tests/fixtures/concept-catalog`) or from the repo
+// root (`repo_root().join("tests/fixtures/antipatterns/x.html")`). The terms
+// are the file itself and each directory above it as a whole path in either
+// spelling: `../<file>`, `../<dir>"`, `../<dir>/"` (a `concat!` completes it),
+// and the same after an opening `"` for paths of two segments or more. A
+// crate that includes one file of scripts/, mentions a path in a comment, or
+// reads a user project's `package.json` is not selected by every edit there.
+export function searchTerms(file) {
+ const parts = file.split('/');
+ const terms = new Set([`../${file}`]);
+ if (parts.length >= 2) terms.add(`"${file}`);
+ for (let i = parts.length - 1; i >= 1; i--) {
+ const dir = parts.slice(0, i).join('/');
+ terms.add(`../${dir}"`);
+ terms.add(`../${dir}/"`);
+ if (i >= 2) {
+ terms.add(`"${dir}"`);
+ terms.add(`"${dir}/"`);
+ }
+ }
+ return [...terms];
+}
+
+// changed: repo-relative paths. packages: [{ name, dir, deps }] with `dir`
+// repo-relative (`crates/live`) and `deps` the workspace path dependencies,
+// dev-dependencies included. searchCrates(terms): repo-relative paths of the
+// files under crates/ that contain any of the terms.
+// Returns { whole, selected: Map<name, reason> }; `whole` lists the changed
+// files that select the whole workspace.
+export function selectCrates({ changed, packages, searchCrates }) {
+ const byDir = new Map(packages.map((p) => [p.dir, p.name]));
+ const whole = changed.filter((f) => WORKSPACE_FILES.some((re) => re.test(f)));
+ const selected = new Map();
+ const pick = (name, why) => {
+ if (!selected.has(name)) selected.set(name, why);
+ };
+ const crateOf = (file) => byDir.get(file.split('/').slice(0, 2).join('/'));
+
+ for (const file of changed) {
+ if (whole.includes(file)) continue;
+ if (file.startsWith('crates/')) {
+ const name = crateOf(file);
+ if (name) pick(name, file);
+ continue;
+ }
+ for (const hit of searchCrates(searchTerms(file))) {
+ const name = crateOf(hit);
+ if (name) pick(name, file);
+ }
+ }
+
+ // Reverse dependencies: anything in the workspace that depends on a
+ // selected crate is selected too.
+ for (let grew = true; grew;) {
+ grew = false;
+ for (const p of packages) {
+ if (selected.has(p.name)) continue;
+ const dep = p.deps.find((d) => selected.has(d));
+ if (dep) {
+ pick(p.name, `depends on ${dep}`);
+ grew = true;
+ }
+ }
+ }
+ return { whole, selected };
+}
diff --git a/scripts/test-rust-changed.mjs b/scripts/test-rust-changed.mjs
index 597b008d1..957db9393 100644
--- a/scripts/test-rust-changed.mjs
+++ b/scripts/test-rust-changed.mjs
@@ -21,9 +21,9 @@ import fs from 'node:fs';
import path from 'node:path';
import { execFileSync, spawn } from 'node:child_process';
import { fileURLToPath } from 'node:url';
+import { selectCrates } from './lib/rust-test-selection.mjs';
const ROOT = path.resolve(fileURLToPath(new URL('..', import.meta.url)));
-const WORKSPACE_FILES = [/^Cargo\.(toml|lock)$/, /^rust-toolchain(\.toml)?$/, /^\.cargo\//];
const argv = process.argv.slice(2);
const dashDash = argv.indexOf('--');
@@ -39,7 +39,9 @@ if (!base) fail('--base needs a ref');
const printOnly = own.includes('--print');
const git = (args) => execFileSync('git', args, { cwd: ROOT, encoding: 'utf-8', maxBuffer: 64 * 1024 * 1024 });
-const lines = (text) => text.split(/\r?\n/).filter(Boolean);
+// NUL-separated output keeps paths verbatim: without -z git quotes and
+// escapes a name such as `café.html`, which then matches no crate.
+const paths = (text) => text.split('\0').filter(Boolean);
let mergeBase;
try {
@@ -50,8 +52,8 @@ try {
const changed = [...new Set([
// --no-renames lists both sides of a move, so a file moved out of a crate
// still selects the crate it left.
- ...lines(git(['diff', '--name-only', '--no-renames', mergeBase])),
- ...lines(git(['ls-files', '--others', '--exclude-standard'])),
+ ...paths(git(['diff', '--name-only', '--no-renames', '-z', mergeBase])),
+ ...paths(git(['ls-files', '--others', '--exclude-standard', '-z'])),
])];
const metadata = JSON.parse(execFileSync('cargo', ['metadata', '--format-version', '1', '--no-deps'], {
@@ -62,69 +64,16 @@ const packages = metadata.packages.map((p) => ({
dir: path.relative(ROOT, path.dirname(p.manifest_path)).split(path.sep).join('/'),
deps: p.dependencies.filter((d) => d.path).map((d) => d.name),
}));
-const byDir = new Map(packages.map((p) => [p.dir, p.name]));
-const whole = changed.filter((f) => WORKSPACE_FILES.some((re) => re.test(f)));
-const selected = new Set();
-const reasons = new Map();
-const pick = (name, why) => {
- if (!selected.has(name)) reasons.set(name, why);
- selected.add(name);
-};
-
-const external = [];
-for (const file of changed) {
- const m = /^(crates\/[^/]+)\//.exec(file);
- if (m) {
- const name = byDir.get(m[1]);
- if (name) pick(name, file);
- } else if (!whole.includes(file)) {
- external.push(file);
- }
-}
-for (const file of external) {
- // Crates name what they read either relative to their manifest
- // (`include_str!("../../../skill/scripts/live-browser.js")`, a
- // `CARGO_MANIFEST_DIR/../../browser-bundle` dir) or from the repo root
- // (`repo_root().join("tests/fixtures/antipatterns/x.html")`). Search for the
- // file itself, a manifest-relative read of a directory above it
- // (`../<dir>"`), and a repo-relative path under a directory at least two
- // levels deep (`tests/fixtures/antipatterns/`). A crate that includes one
- // file of scripts/ or merely mentions `package.json` is not selected by
- // every edit there.
- const parts = file.split('/');
- const terms = new Set([`../${file}`]);
- if (parts.length > 2) terms.add(file);
- for (let i = parts.length - 1; i >= 1; i--) {
- const dir = parts.slice(0, i).join('/');
- terms.add(`../${dir}"`);
- if (i >= 2) terms.add(`${dir}/`);
- }
- let hits = '';
+const searchCrates = (terms) => {
try {
- hits = git(['grep', '-l', '-F', ...[...terms].flatMap((t) => ['-e', t]), '--', 'crates']);
- } catch {
- continue; // git grep exits 1 when nothing matches
- }
- for (const hit of lines(hits)) {
- const name = byDir.get(hit.split('/').slice(0, 2).join('/'));
- if (name) pick(name, file);
+ return paths(git(['grep', '-l', '-z', '-F', ...terms.flatMap((t) => ['-e', t]), '--', 'crates']));
+ } catch (error) {
+ if (error.status === 1) return []; // git grep exits 1 when nothing matches
+ throw error;
}
-}
-
-// Reverse dependencies: anything in the workspace that depends on a selected
-// crate is selected too.
-for (let grew = true; grew;) {
- grew = false;
- for (const p of packages) {
- if (selected.has(p.name)) continue;
- const dep = p.deps.find((d) => selected.has(d));
- if (dep) {
- pick(p.name, `depends on ${dep}`);
- grew = true;
- }
- }
-}
+};
+const { whole, selected } = selectCrates({ changed, packages, searchCrates });
if (whole.length) {
console.log(`Whole workspace: ${whole.join(', ')} changed against ${base}.`);
@@ -133,10 +82,10 @@ if (whole.length) {
process.exit(0);
} else {
console.log(`${selected.size} of ${packages.length} crates against ${base}:`);
- for (const name of [...selected].sort()) console.log(` ${name} (${reasons.get(name)})`);
+ for (const name of [...selected.keys()].sort()) console.log(` ${name} (${selected.get(name)})`);
}
-const cargoArgs = ['test', ...(whole.length ? ['--workspace'] : [...selected].sort().flatMap((n) => ['-p', n])), '--no-fail-fast', ...passthrough];
+const cargoArgs = ['test', ...(whole.length ? ['--workspace'] : [...selected.keys()].sort().flatMap((n) => ['-p', n])), '--no-fail-fast', ...passthrough];
console.log(`cargo ${cargoArgs.join(' ')}`);
if (printOnly) process.exit(0);
@@ -167,7 +116,8 @@ child.on('close', (code, signal) => {
console.log(`${status}: ${passed} passed, ${failed} failed in ${secs}s`);
for (const name of failures) console.log(` FAILED ${name}`);
if (code !== 0 && !failures.length) console.log(` see the log for the error: ${logPath}`);
- process.exit(code ?? 1);
+ // exitCode rather than exit(): the log stream may still be flushing.
+ process.exitCode = code ?? 1;
});
function fail(message) {
diff --git a/scripts/test-suites.mjs b/scripts/test-suites.mjs
index d6ee7c012..8df8a7574 100644
--- a/scripts/test-suites.mjs
+++ b/scripts/test-suites.mjs
@@ -74,6 +74,7 @@ export const SUITES = {
timeoutMs: 180000,
files: [
'tests/ci-test-plan.test.mjs',
+ 'tests/rust-test-selection.test.mjs',
'tests/cli-shim.test.mjs',
'tests/launcher-download.test.mjs',
'tests/publish-platform-packages.test.mjs',
diff --git a/tests/rust-test-selection.test.mjs b/tests/rust-test-selection.test.mjs
new file mode 100644
index 000000000..ec60f41e3
--- /dev/null
+++ b/tests/rust-test-selection.test.mjs
@@ -0,0 +1,75 @@
+import { describe, it } from 'node:test';
+import assert from 'node:assert/strict';
+import { searchTerms, selectCrates } from '../scripts/lib/rust-test-selection.mjs';
+
+// A small workspace shaped like the real one: core at the bottom, html on
+// core, cli on html, and a dev-dependency edge from tool to core.
+const PACKAGES = [
+ { name: 'core', dir: 'crates/core', deps: [] },
+ { name: 'html', dir: 'crates/html', deps: ['core'] },
+ { name: 'cli', dir: 'crates/cli', deps: ['html'] },
+ { name: 'tool', dir: 'crates/tool', deps: ['core'] },
+ { name: 'comp', dir: 'crates/comp', deps: [] },
+];
+
+// Crate sources keyed by path; searchCrates returns the files that contain a term.
+function select(changed, sources = {}) {
+ const searchCrates = (terms) =>
+ Object.entries(sources).filter(([, text]) => terms.some((t) => text.includes(t))).map(([file]) => file);
+ const { whole, selected } = selectCrates({ changed, packages: PACKAGES, searchCrates });
+ return { whole, names: [...selected.keys()].sort() };
+}
+
+describe('rust-test-selection', () => {
+ it('selects a touched crate and every crate that depends on it', () => {
+ assert.deepEqual(select(['crates/html/src/lib.rs']).names, ['cli', 'html']);
+ assert.deepEqual(select(['crates/core/src/lib.rs']).names, ['cli', 'core', 'html', 'tool']);
+ assert.deepEqual(select(['crates/comp/tests/fixtures/café.png']).names, ['comp']);
+ });
+
+ it('selects nothing for files no crate reads', () => {
+ assert.deepEqual(select(['README.md', 'docs/ENGINE.md'], { 'crates/core/src/lib.rs': '// see README.md' }), {
+ whole: [],
+ names: [],
+ });
+ });
+
+ it('selects the whole workspace for Cargo and toolchain files', () => {
+ for (const file of ['Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml', '.cargo/config.toml']) {
+ assert.deepEqual(select([file, 'crates/comp/src/lib.rs']).whole, [file]);
+ }
+ assert.deepEqual(select(['crates/core/Cargo.toml']).whole, []);
+ });
+
+ it('selects crates that read an outside file or its directory', () => {
+ const sources = {
+ 'crates/html/src/a.rs': 'include_str!("../../../skill/scripts/live-browser.js")',
+ 'crates/comp/tests/b.rs': 'env!("CARGO_MANIFEST_DIR")).join("../../tests/fixtures/concept-catalog")',
+ 'crates/tool/src/c.rs': 'repo_root().join("tests/fixtures/antipatterns/x.html"); repo.join("tests/oracle/golden")',
+ 'crates/core/src/d.rs': 'include_str!(concat!("../../../browser-bundle/", $name))',
+ };
+ assert.deepEqual(select(['skill/scripts/live-browser.js'], sources).names, ['cli', 'html']);
+ assert.deepEqual(select(['tests/fixtures/concept-catalog/cards/a.json'], sources).names, ['comp']);
+ assert.deepEqual(select(['tests/fixtures/antipatterns/x.html'], sources).names, ['tool']);
+ assert.deepEqual(select(['tests/fixtures/antipatterns/other.html'], sources).names, []);
+ assert.deepEqual(select(['tests/oracle/golden/detect-a.json'], sources).names, ['tool']);
+ assert.deepEqual(select(['browser-bundle/10-probe.js'], sources).names, ['cli', 'core', 'html', 'tool']);
+ });
+
+ it('does not select a crate for a file it only mentions or a sibling it includes', () => {
+ const sources = {
+ 'crates/html/src/a.rs': 'include_str!("../../../scripts/bundle-signing-keys.json")',
+ 'crates/comp/src/b.rs': '// rebuilt by scripts/lib/build.js; reads the project\'s "package.json"',
+ 'crates/tool/src/c.rs': '//! fixtures live in `tests/fixtures/antipatterns/`',
+ 'crates/core/src/d.rs': 'let rel = "skill/scripts/impeccable is the launcher";',
+ };
+ assert.deepEqual(select(['scripts/test-rust-changed.mjs', 'scripts/lib/build.js', 'package.json'], sources).names, []);
+ assert.deepEqual(select(['tests/fixtures/antipatterns/x.html', 'skill/scripts/live-browser.js'], sources).names, []);
+ assert.deepEqual(select(['scripts/bundle-signing-keys.json'], sources).names, ['cli', 'html']);
+ });
+
+ it('builds terms for the file and its directories', () => {
+ assert.deepEqual(searchTerms('package.json'), ['../package.json']);
+ assert.deepEqual(searchTerms('a/b/c.txt'), ['../a/b/c.txt', '"a/b/c.txt', '../a/b"', '../a/b/"', '"a/b"', '"a/b/"', '../a"', '../a/"']);
+ });
+});