Files
pplus-core/tests/build-args.test.mjs
alexm 401084bab3
Test / gate (push) Failing after 19m0s
Initial commit
2026-11-12 22:23:54 -05:00

173 lines
11 KiB
JavaScript

// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// Mojo is built one stated way everywhere Programming+ builds it or asks godbolt to: library/mojo/pin.json's
// build.args, then a program's own. These tests fail when a builder writes build flags of its own, and when the
// TypeScript and Python halves stop agreeing on how the two combine.
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { MOJO_BUILD_ARGS } from '../dist/mojo/generated/pin.js';
import { mojoBuildArgs, BUILD_ARGS, BUILD_LEVELS } from '../dist/services/godbolt.js';
const ROOT = new URL('..', import.meta.url).pathname;
const pin = JSON.parse(fs.readFileSync(path.join(ROOT, 'library/mojo/pin.json'), 'utf8'));
const python = spawnSync('python3', ['--version']).status === 0;
test('the generated pin carries the build arguments of library/mojo/pin.json, each with its reason', () => {
assert.equal(MOJO_BUILD_ARGS, pin.build.args);
// every option in build.args says why it is there, under the option as build.args spells it
const options = pin.build.args.match(/-\S+(?:\s+(?!-)\S+)?/g) ?? [];
assert.deepEqual(Object.keys(pin.build.why).sort(), options.sort());
for (const why of Object.values(pin.build.why)) assert.ok(why.length > 40, 'a reason, not a label');
});
const CASES = [
['', true], ['-O0 --debug-level=line-tables', true], ['--debug-level=line-tables', true],
['--fp-mode contract=fast', true], ['--fp-mode=contract=fast -O0', true], ['--target-cpu x86-64-v4', true],
['', false], ['-O0', false], ['--target-cpu apple-m1', false],
];
test('a program\'s own arguments come after the pin\'s, and an option it names replaces the pin\'s', () => {
const base = '--target-cpu x86-64-v3 --fp-mode contract=off';
assert.equal(mojoBuildArgs('', { base }), base);
assert.equal(mojoBuildArgs('-O0 --debug-level=line-tables', { base }), `${base} -O0 --debug-level=line-tables`);
assert.equal(mojoBuildArgs('--fp-mode contract=fast', { base }), '--target-cpu x86-64-v3 --fp-mode contract=fast');
assert.equal(mojoBuildArgs('--fp-mode=contract=fast', { base }), '--target-cpu x86-64-v3 --fp-mode=contract=fast');
assert.equal(mojoBuildArgs('--target-cpu x86-64-v4 -O0', { base }), '--fp-mode contract=off --target-cpu x86-64-v4 -O0');
// a machine that is not x86-64 cannot build for an x86-64 level: that one option is left out
assert.equal(mojoBuildArgs('-O0', { base, x86_64: false }), '--fp-mode contract=off -O0');
assert.equal(mojoBuildArgs(), MOJO_BUILD_ARGS);
});
test('the Compile button sends the pin\'s arguments, then its level\'s', () => {
for (const level of Object.keys(BUILD_LEVELS)) assert.equal(BUILD_ARGS[level], mojoBuildArgs(BUILD_LEVELS[level]));
assert.ok(BUILD_ARGS.O0.startsWith(MOJO_BUILD_ARGS));
});
test('the Python pipeline combines the arguments exactly as the TypeScript does', { skip: !python && !process.env.CI ? 'python3 is not installed' : false }, () => {
const code = `import sys, json; sys.path.insert(0, 'scripts/mojo')\nfrom mojocorpus.common import build_args, load_pin\ncases = json.loads(sys.argv[1])\nprint(json.dumps({"pin": load_pin().build, "out": [build_args(load_pin().build, own, x86_64=x) for own, x in cases]}))`;
const p = spawnSync('python3', ['-c', code, JSON.stringify(CASES)], { encoding: 'utf8', cwd: ROOT });
assert.equal(p.status, 0, p.stderr);
const got = JSON.parse(p.stdout);
assert.equal(got.pin, MOJO_BUILD_ARGS);
assert.deepEqual(got.out, CASES.map(([own, x]) => mojoBuildArgs(own, { x86_64: x })));
});
/* Every builder, read: no flag that decides the code is written anywhere but the pin. */
const BUILDERS = ['scripts', 'src/services']; // their tests (scripts/**/tests) are not builders
// a code-generation option given a value (naming the option, as the code that combines the arguments does, is not)
const CODEGEN = /x86-64-v\d|contract=(?:off|fast|on)\b|--(?:target-cpu|target-features|target-triple|mcpu|march|mtune|fp-mode)(?:=|["']?\s*,?\s*["']?)(?:\S*\d|\S*=|native|generic|haswell|skylake|icelake|alderlake|sapphirerapids|[+-]\w)/;
function files(dir) {
return fs.readdirSync(path.join(ROOT, dir), { withFileTypes: true }).flatMap((e) => e.isDirectory() ? (['__pycache__', 'tests'].includes(e.name) ? [] : files(path.join(dir, e.name)))
: /\.(?:mjs|js|ts|py)$/.test(e.name) ? [path.join(dir, e.name)] : []);
}
const comment = (line) => /^\s*(?:\/\/|\*|\/\*\*?|#)/.test(line);
test('no builder writes a code-generation flag of its own: they come from library/mojo/pin.json', () => {
const found = [];
for (const f of [...BUILDERS.flatMap(files), 'pixi.toml', 'package.json']) {
fs.readFileSync(path.join(ROOT, f), 'utf8').split('\n').forEach((line, i) => {
if (!comment(line) && CODEGEN.test(line)) found.push(`${f}:${i + 1}: ${line.trim().slice(0, 120)}`);
});
}
assert.deepEqual(found, [], 'put the flag in library/mojo/pin.json build.args (or a program\'s own # args:) and take it from mojoBuildArgs or build_args');
});
test('every place that runs the compiler on a program gives it the pin\'s arguments', () => {
const sites = [];
for (const f of BUILDERS.flatMap(files)) {
fs.readFileSync(path.join(ROOT, f), 'utf8').split('\n').forEach((line, i) => {
if (comment(line)) return;
if (/['"]mojo['"],\s*\[\s*['"](?:build|run)['"]/.test(line) || /\[\s*["']mojo["'],\s*["'](?:build|run)["']/.test(line)) sites.push({ f, i, line });
});
}
// the ones there are today; a new one fails here until it is read and added
assert.deepEqual(sites.map((s) => s.f).sort(), ['scripts/library.mjs', 'scripts/mojo/corpus.py', 'scripts/mojo/fuzz.mjs', 'scripts/mojo/local.mjs']);
for (const s of sites) assert.match(s.line, /\.\.\.(?:BUILD_ARGS|extra|args\.split)|build_args\(/, `${s.f}:${s.i + 1} builds without the pin's arguments`);
// and every request to godbolt carries arguments that went through them
const asks = [];
for (const f of BUILDERS.flatMap(files)) {
fs.readFileSync(path.join(ROOT, f), 'utf8').split('\n').forEach((line, i) => {
if (!comment(line) && /\b(?:request_body|mojoRequestBody)\(/.test(line) && !/^\s*(?:def|export function)\b/.test(line)) asks.push(`${f}:${i + 1}: ${line.trim()}`);
});
}
assert.equal(asks.length, 4, asks.join('\n'));
for (const a of asks) assert.match(a, /command_args|args=args|BUILD_ARGS\[/, `${a}: a request with arguments that are not the pin's`);
// the tasks a person runs go through corpus.py, which reads the pin
const tasks = fs.readFileSync(path.join(ROOT, 'pixi.toml'), 'utf8');
assert.doesNotMatch(tasks, /\bmojo (?:build|run)\b/, 'a pixi task runs the compiler directly; use python scripts/mojo/corpus.py mojo-run');
});
/* And run: a stand-in `mojo` on the PATH writes down every argument each builder gives it. */
test('the builders hand the compiler exactly the pin\'s arguments and the program\'s own', { skip: process.platform === 'win32' || (!python && !process.env.CI) ? 'needs a POSIX shell and python3' : false }, () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pplus-fake-mojo-'));
try {
const log = path.join(dir, 'calls');
fs.writeFileSync(path.join(dir, 'mojo'), `#!/bin/sh\nif [ "$1" = "--version" ]; then echo "Mojo ${pin.mojo.version} (stand-in)"; exit 0; fi\nprintf '%s\\n' "$*" >> "${log}"\nexit 1\n`, { mode: 0o755 });
const env = { ...process.env, PATH: `${dir}:${process.env.PATH}` };
const host = { x86_64: process.arch === 'x64' };
const probe = 'library/mojo/probes/godbolt-compile-button.mojo';
const own = /^# args: (.*)$/m.exec(fs.readFileSync(path.join(ROOT, probe), 'utf8'))[1].trim();
const calls = () => {
if (!fs.existsSync(log)) return [];
const lines = fs.readFileSync(log, 'utf8').trim().split('\n');
fs.rmSync(log);
return lines;
};
const strip = (line) => line.split(' ').filter((w, i, a) => !['build', 'run', '-o', '--disable-warnings'].includes(w) && a[i - 1] !== '-o' && !w.endsWith('.mojo')).join(' ');
spawnSync(process.execPath, ['scripts/mojo/local.mjs', '--only', 'own', '--item', 'godbolt-compile-button', '--no-cache', '--jobs', '1'], { cwd: ROOT, env, encoding: 'utf8' });
assert.deepEqual(calls().map(strip), [mojoBuildArgs(own, host)], 'scripts/mojo/local.mjs');
spawnSync('python3', ['scripts/mojo/corpus.py', 'mojo-run', probe], { cwd: ROOT, env, encoding: 'utf8' });
assert.deepEqual(calls().map(strip), [mojoBuildArgs(own, host)], 'corpus.py mojo-run (pixi run mojo-run, mojo-probes-local, play)');
spawnSync(process.execPath, ['scripts/mojo/fuzz.mjs', '--seed', '1', '--programs', '1', '--lines', '2', '--jobs', '1'], { cwd: ROOT, env, encoding: 'utf8' });
const fuzz = calls().map(strip);
assert.ok(fuzz.length >= 1, 'the fuzzer built something');
for (const c of fuzz) assert.equal(c, mojoBuildArgs('', host), 'scripts/mojo/fuzz.mjs');
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('mojo-local holds a program given stdin to godbolt\'s stored answer too', { skip: process.platform === 'win32' ? 'needs a POSIX shell' : false }, (t) => {
// godbolt gives a program its stdin as a pipe, in run mode as in the Compile button's (probes/input-pipe-first-line
// printed the first line it was given), so its answer is comparable with a local run on the same stdin
const status = JSON.parse(fs.readFileSync(path.join(ROOT, 'library/mojo/golden/status.json'), 'utf8'));
const row = status.items.find((r) => r.id === 'probes/input-pipe-first-line');
if (!row?.key) return t.skip('no stored answer for probes/input-pipe-first-line');
const g = JSON.parse(fs.readFileSync(path.join(ROOT, 'library/mojo/golden', `${row.key}.json`), 'utf8'));
assert.ok(g.stdin && g.result.stdout.trim() === '[a b]', 'godbolt printed the first line of the stdin it was given');
// a stand-in `mojo` whose every build prints what godbolt printed
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'pplus-fake-mojo-'));
try {
fs.writeFileSync(path.join(dir, 'mojo'), `#!/bin/sh\nif [ "$1" = "--version" ]; then echo "Mojo ${pin.mojo.version} (stand-in)"; exit 0; fi\nprintf '#!/bin/sh\\necho "[a b]"\\n' > example; chmod +x example\n`, { mode: 0o755 });
const p = spawnSync(process.execPath, ['scripts/mojo/local.mjs', '--only', 'own', '--item', 'input-pipe-first-line', '--no-cache', '--jobs', '1', '--json'],
{ cwd: ROOT, env: { ...process.env, PATH: `${dir}:${process.env.PATH}` }, encoding: 'utf8' });
assert.equal(JSON.parse(p.stdout).counts.godboltCompared, process.arch === 'x64' ? 1 : 0, p.stderr);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
test('no listing godbolt answered holds AVX-512, which x86-64-v3 does not have and the model machine does not know', () => {
const dir = path.join(ROOT, 'library/mojo/golden');
const found = [];
let listings = 0;
for (const f of fs.readdirSync(dir).filter((x) => /^[0-9a-f]{64}\.json$/.test(x))) {
const g = JSON.parse(fs.readFileSync(path.join(dir, f), 'utf8'));
if (!g.result?.asm?.length) continue;
listings++;
const avx512 = g.result.asm.find((a) => /%zmm\d|%k[0-7]\b|\{%k[0-7]\}/.test(a.text ?? ''));
if (avx512) found.push(`${g.firstItem} (${g.args}): ${avx512.text.trim()}`);
}
assert.ok(listings >= 8, `only ${listings} listings in the corpus`);
assert.deepEqual(found, []);
});