Files
sprites/tools/build/test/executor.test.ts
Christopher Monsanto 45adc3579f Make rule inputs source paths only
Nothing chains any more, so Input collapses to string and the machinery
that existed to order rules against each other goes with it: the
executor's demand graph and per-decl memo, the DryDirty sentinel, the
whole blocked outcome, the driver's producer closure, and inspect's
transitive-consumer fixpoint. What is left is a flat parallel sweep whose
only coupling is byte-identical declarations sharing one execution.

Every deploy's tree comes out byte-identical and every rule key is
unchanged: a full dry run reports 7748 up to date, 0 would run.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MQicnpaee6kQXGG7uVExLS
2026-08-21 01:13:15 -04:00

236 lines
8.4 KiB
TypeScript

import assert from 'node:assert/strict';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as pathlib from 'node:path';
import {beforeEach, test} from 'node:test';
import {type Artifact, type CmdSpec, getDecls, resetDecls, rule} from '../artifact.ts';
import {casPath} from '../cas.ts';
import {type DriveResult, build} from '../driver.ts';
import {type RuleOutcome} from '../executor.ts';
import {Store} from '../store.ts';
beforeEach(resetDecls);
type Env = {
root: string,
dbPath: string,
casDir: string,
tmpDir: string,
execLog: string,
};
function setup(): Env {
let root = fs.mkdtempSync(pathlib.join(os.tmpdir(), 'executor-test-'));
fs.mkdirSync(pathlib.join(root, 'src'));
return {
root,
dbPath: pathlib.join(root, '.build/db.sqlite'),
casDir: pathlib.join(root, '.build/cas'),
tmpDir: pathlib.join(root, '.build/tmp'),
execLog: pathlib.join(root, 'exec.log'),
};
}
function src(env: Env, name: string, content: string): string {
let p = pathlib.join(env.root, 'src', name);
fs.writeFileSync(p, content);
return p;
}
// A copy rule that also counts its executions in env.execLog.
function copyRule(env: Env, input: string, out: string,
extra: Partial<CmdSpec> = {}): Artifact {
return rule(input, {cmds: [`cat %f > %o && echo x >> ${env.execLog}`], ...extra}, out);
}
function execCount(env: Env): number {
try {
return fs.readFileSync(env.execLog, 'utf8').split('\n').filter(l => l !== '').length;
} catch {
return 0;
}
}
async function runBuild(env: Env, opts: {dryRun?: boolean, gc?: boolean} = {},
decls = getDecls()): Promise<DriveResult> {
let store = new Store(env.dbPath);
try {
return await build(decls, {
root: env.root,
store,
casDir: env.casDir,
tmpDir: env.tmpDir,
jobs: 2,
dryRun: opts.dryRun ?? false,
failFast: false,
verbose: false,
gc: opts.gc ?? false,
signal: new AbortController().signal,
log: () => {},
logError: () => {},
});
} finally {
store.close();
}
}
function statuses(result: DriveResult): RuleOutcome['status'][] {
return getDecls().map(d => result.outcomes.get(d)!.status);
}
test('second build is clean and executes nothing', async () => {
let env = setup();
let out = copyRule(env, src(env, 'a.txt', 'hello'), 'out.txt');
let first = await runBuild(env);
assert.ok(first.ok);
assert.deepEqual(statuses(first), ['ran']);
assert.equal(fs.readFileSync(casPath(env.casDir, out.hash, 'txt'), 'utf8'), 'hello');
resetDecls();
copyRule(env, pathlib.join(env.root, 'src/a.txt'), 'out.txt');
let second = await runBuild(env);
assert.deepEqual(statuses(second), ['clean']);
assert.equal(execCount(env), 1);
});
test('input content change reruns; gc removes the old rule and object', async () => {
let env = setup();
let a = src(env, 'a.txt', 'v1');
let out1 = copyRule(env, a, 'out.txt');
await runBuild(env, {gc: true});
let oldObject = casPath(env.casDir, out1.hash, 'txt');
resetDecls();
fs.writeFileSync(a, 'v2');
copyRule(env, a, 'out.txt');
let result = await runBuild(env, {gc: true});
assert.deepEqual(statuses(result), ['ran']);
assert.equal(execCount(env), 2);
assert.ok(!fs.existsSync(oldObject));
let store = new Store(env.dbPath);
assert.deepEqual(store.liveObjects().size, 1);
store.close();
});
test('same-bytes source rename executes nothing; nameSensitive reruns', async () => {
let env = setup();
copyRule(env, src(env, 'a.txt', 'stable'), 'out.txt');
copyRule(env, src(env, 'ns.txt', 'stable'), 'ns-out.txt', {nameSensitive: true});
await runBuild(env);
assert.equal(execCount(env), 2);
resetDecls();
fs.renameSync(pathlib.join(env.root, 'src/a.txt'), pathlib.join(env.root, 'src/moved.txt'));
fs.renameSync(pathlib.join(env.root, 'src/ns.txt'), pathlib.join(env.root, 'src/ns2.txt'));
copyRule(env, pathlib.join(env.root, 'src/moved.txt'), 'out.txt');
copyRule(env, pathlib.join(env.root, 'src/ns2.txt'), 'ns-out.txt', {nameSensitive: true});
let result = await runBuild(env);
assert.deepEqual(statuses(result), ['clean', 'ran']);
assert.equal(execCount(env), 3);
});
test('byte-identical inputs share one execution across two declarations', async () => {
let env = setup();
let one = copyRule(env, src(env, 'one.txt', 'same-bytes'), 'one.txt');
let two = copyRule(env, src(env, 'two.txt', 'same-bytes'), 'two.txt');
let result = await runBuild(env);
assert.ok(result.ok);
assert.equal(execCount(env), 1);
assert.deepEqual(statuses(result).sort(), ['clean', 'ran']);
assert.equal(one.hash, two.hash);
assert.equal(one.filename, 'one.txt');
assert.equal(two.filename, 'two.txt');
});
test('a failed rule records nothing and fails the build', async () => {
let env = setup();
rule(src(env, 'a.txt', 'x'), ['false'], 'mid.txt');
let result = await runBuild(env);
assert.ok(!result.ok);
assert.deepEqual(statuses(result), ['failed']);
let store = new Store(env.dbPath);
assert.equal(store.liveObjects().size, 0);
store.close();
});
test('multi-output rules route %oN and skip all-or-nothing', async () => {
let env = setup();
let declare = () => rule(src(env, 'a.txt', 'multi'),
[`printf one > %o1 && printf two > %o2 && echo x >> ${env.execLog}`],
['x.txt', 'y.css']);
let [x1, y1] = declare();
await runBuild(env);
assert.equal(fs.readFileSync(casPath(env.casDir, x1.hash, 'txt'), 'utf8'), 'one');
assert.equal(fs.readFileSync(casPath(env.casDir, y1.hash, 'css'), 'utf8'), 'two');
resetDecls();
fs.rmSync(casPath(env.casDir, y1.hash, 'css'));
declare();
let rerun = await runBuild(env);
assert.deepEqual(statuses(rerun), ['ran']);
assert.equal(execCount(env), 2);
});
test('a missing declared output fails the rule', async () => {
let env = setup();
rule(src(env, 'a.txt', 'x'), ['printf one > %o1'], ['x.txt', 'missing.css']);
let result = await runBuild(env);
assert.ok(!result.ok);
let outcome = result.outcomes.get(getDecls()[0]!)!;
assert.equal(outcome.status, 'failed');
assert.match((outcome as {message: string}).message, /did not produce: missing.css/);
let store = new Store(env.dbPath);
assert.equal(store.liveObjects().size, 0);
store.close();
});
test('dry run reports without writing state', async () => {
let env = setup();
copyRule(env, src(env, 'a.txt', 'dry'), 'one.txt');
copyRule(env, src(env, 'b.txt', 'dry too'), 'two.txt');
let result = await runBuild(env, {dryRun: true});
assert.ok(!result.ok);
assert.deepEqual(result.outcomes.get(getDecls()[0]!), {status: 'would-run', reason: 'new'});
assert.deepEqual(result.outcomes.get(getDecls()[1]!), {status: 'would-run', reason: 'new'});
assert.equal(execCount(env), 0);
assert.ok(!fs.existsSync(env.casDir));
let store = new Store(env.dbPath);
assert.equal(store.liveObjects().size, 0);
store.close();
});
test('a truncated CAS object reads as dirty and is repaired', async () => {
let env = setup();
let out = copyRule(env, src(env, 'a.txt', 'truncate-me'), 'out.txt');
await runBuild(env);
let obj = casPath(env.casDir, out.hash, 'txt');
fs.chmodSync(obj, 0o644);
fs.truncateSync(obj);
resetDecls();
copyRule(env, pathlib.join(env.root, 'src/a.txt'), 'out.txt');
let rerun = await runBuild(env);
assert.deepEqual(statuses(rerun), ['ran']);
assert.equal(fs.readFileSync(obj, 'utf8'), 'truncate-me');
assert.equal(fs.statSync(obj).mode & 0o777, 0o444);
});
test('a subset build touches only the decls it is given', async () => {
let env = setup();
copyRule(env, src(env, 'a.txt', 'skipped'), 'a.txt');
let picked = copyRule(env, src(env, 'b.txt', 'solo'), 'b.txt');
let result = await runBuild(env, {}, [picked.decl]);
assert.ok(result.ok);
assert.equal(execCount(env), 1);
assert.equal(fs.readFileSync(casPath(env.casDir, picked.hash, 'txt'), 'utf8'), 'solo');
});
test('missing sources fail upfront', async () => {
let env = setup();
copyRule(env, pathlib.join(env.root, 'src/nope.txt'), 'out.txt');
await assert.rejects(() => runBuild(env), /Missing input files/);
});