Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

feat(test): isolate runner from host config [C7,S13]

Corey Olsonc213a8717ea5
diff --git a/bin/test.js b/bin/test.js
new file mode 100644
index 00000000..fabe51e7
--- /dev/null
+++ b/bin/test.js
@@ -0,0 +1,90 @@
+#!/usr/bin/env node
+/**
+ * Run the test suite with a private home, no inherited Git identity or config, and a refusing CLI shim.
+ */
+import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { delimiter, join, resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { spawnSync } from 'node:child_process';
+import { tmpdir } from 'node:os';
+
+const runner = fileURLToPath(import.meta.url);
+
+/**
+ * Build a private environment that makes local tests fail for the same missing Git and CLI setup as CI.
+ *
+ * @param {string} sandbox
+ * @returns {NodeJS.ProcessEnv}
+ */
+function testEnvironment(sandbox) {
+ const home = join(sandbox, 'home');
+ const shims = join(sandbox, 'bin');
+ mkdirSync(home, { recursive: true });
+ mkdirSync(shims, { recursive: true });
+ const refusal = join(shims, 'pullboard');
+ writeFileSync(refusal, '#!/bin/sh\nprintf "pullboard is not installed; current test: %s\\n" "${PULLBOARD_TEST_FILE:-unknown}" >&2\nexit 1\n');
+ chmodSync(refusal, 0o755);
+
+ const env = { ...process.env };
+ for (const key of Object.keys(env)) {
+ if (key.startsWith('GIT_')) delete env[key];
+ }
+ env.HOME = home;
+ env.USERPROFILE = home;
+ env.PULLBOARD_HOME = home;
+ env.TMPDIR = sandbox;
+ env.TMP = sandbox;
+ env.TEMP = sandbox;
+ env.GIT_CONFIG_GLOBAL = '/dev/null';
+ env.GIT_CONFIG_NOSYSTEM = '1';
+ env.GIT_CONFIG_COUNT = '1';
+ env.GIT_CONFIG_KEY_0 = 'user.useConfigOnly';
+ env.GIT_CONFIG_VALUE_0 = 'true';
+ env.PATH = [shims, process.env.PATH].filter(Boolean).join(delimiter);
+ delete env.NODE_TEST_CONTEXT;
+ delete env.PULLBOARD_TEST_FILE;
+ return env;
+}
+
+/**
+ * Start the Node test runner and return its result to the caller for faithful exit handling.
+ *
+ * @param {string[]} args
+ * @param {NodeJS.ProcessEnv} env
+ * @returns {import('node:child_process').SpawnSyncReturns<string>}
+ */
+function runTests(args, env) {
+ return spawnSync(process.execPath, [
+ '--disable-warning=ExperimentalWarning',
+ '--test',
+ '--import',
+ runner,
+ ...args,
+ ], { env, stdio: 'inherit' });
+}
+
+/**
+ * Launch Node's test runner in a disposable environment and remove it after the run ends.
+ *
+ * @param {string[]} args
+ * @returns {void}
+ */
+function main(args) {
+ const sandbox = mkdtempSync(join(tmpdir(), 'pullboard-test-run-'));
+ let signal = null;
+ try {
+ const result = runTests(args, testEnvironment(sandbox));
+ if (result.error) throw result.error;
+ signal = result.signal;
+ process.exitCode = result.status ?? 1;
+ } finally {
+ rmSync(sandbox, { recursive: true, force: true });
+ }
+ if (signal) process.kill(process.pid, signal);
+}
+
+if (process.argv[1] && resolve(process.argv[1]) === runner) {
+ main(process.argv.slice(2));
+} else if (process.argv[1]) {
+ process.env.PULLBOARD_TEST_FILE = process.argv[1];
+}
diff --git a/test/test-runner.test.js b/test/test-runner.test.js
new file mode 100644
index 00000000..4aadc509
--- /dev/null
+++ b/test/test-runner.test.js
@@ -0,0 +1,70 @@
+/** Test the CI-like test runner against real Git and PATH behavior [C7, S13]. */
+import assert from 'node:assert/strict';
+import { execFileSync, spawnSync } from 'node:child_process';
+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { delimiter, join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { test } from 'node:test';
+
+test('tests run without inherited Git identity or config and refuse ambient pullboard [C7, S13]', (t) => {
+ assert.equal(process.env.GIT_CONFIG_GLOBAL, '/dev/null');
+ assert.equal(process.env.GIT_CONFIG_NOSYSTEM, '1');
+ assert.equal(process.env.GIT_CONFIG_COUNT, '1');
+ assert.equal(process.env.GIT_CONFIG_KEY_0, 'user.useConfigOnly');
+ assert.equal(process.env.GIT_CONFIG_VALUE_0, 'true');
+ assert.equal(process.env.GIT_AUTHOR_NAME, undefined);
+ assert.equal(process.env.GIT_AUTHOR_EMAIL, undefined);
+ assert.equal(process.env.GIT_COMMITTER_NAME, undefined);
+ assert.equal(process.env.GIT_COMMITTER_EMAIL, undefined);
+ assert.equal(process.env.HOME, process.env.PULLBOARD_HOME);
+ assert.ok(process.env.PULLBOARD_TEST_FILE.endsWith('test/test-runner.test.js'));
+
+ const root = mkdtempSync(join(tmpdir(), 'pullboard-runner-git-'));
+ t.after(() => rmSync(root, { recursive: true, force: true }));
+ execFileSync('git', ['init', '-q', root]);
+ const identity = spawnSync('git', ['var', 'GIT_AUTHOR_IDENT'], { cwd: root, encoding: 'utf8' });
+ assert.notEqual(identity.status, 0, 'Git refuses to infer an author identity from the machine');
+ assert.match(identity.stderr, /author identity unknown/i);
+
+ const refused = spawnSync('pullboard', ['init'], { cwd: root, encoding: 'utf8' });
+ assert.equal(refused.status, 1);
+ assert.match(refused.stderr, /pullboard is not installed/);
+ assert.match(refused.stderr, /test-runner\.test\.js/);
+
+ const fixtureShims = join(root, 'fixture-bin');
+ mkdirSync(fixtureShims);
+ const fixture = join(fixtureShims, 'pullboard');
+ writeFileSync(fixture, '#!/bin/sh\nprintf "fixture shim first\\n"\n');
+ chmodSync(fixture, 0o755);
+ const preferred = spawnSync('pullboard', ['init'], {
+ cwd: root,
+ env: { ...process.env, PATH: `${fixtureShims}${delimiter}${process.env.PATH}` },
+ encoding: 'utf8',
+ });
+ assert.equal(preferred.status, 0);
+ assert.match(preferred.stdout, /fixture shim first/);
+});
+
+test('runner removes its private home after test workers exit [C7, S13]', (t) => {
+ const scratch = mkdtempSync(join(tmpdir(), 'pullboard-runner-cleanup-'));
+ t.after(() => rmSync(scratch, { recursive: true, force: true }));
+ const probe = join(scratch, 'runner-probe.test.js');
+ const report = join(scratch, 'sandbox-path');
+ writeFileSync(probe, `import { test } from 'node:test';
+import { writeFileSync } from 'node:fs';
+test('reports its sandbox', () => writeFileSync(${JSON.stringify(report)}, process.env.TMPDIR));
+`);
+ const runner = fileURLToPath(new URL('../bin/test.js', import.meta.url));
+ const result = spawnSync(process.execPath, [runner, probe], { encoding: 'utf8' });
+ assert.equal(result.status, 0, `${result.stdout}${result.stderr}`);
+ assert.ok(existsSync(report), `${result.stdout}${result.stderr}`);
+ const sandbox = readFileSync(report, 'utf8');
+ assert.equal(existsSync(sandbox), false, 'the private sandbox is removed after Node exits');
+
+ const failingProbe = join(scratch, 'runner-failure.test.js');
+ writeFileSync(failingProbe, "import { test } from 'node:test';\ntest('fails', () => { throw new Error('forward this failure'); });\n");
+ const failed = spawnSync(process.execPath, [runner, failingProbe], { encoding: 'utf8' });
+ assert.equal(failed.status, 1, `${failed.stdout}${failed.stderr}`);
+ assert.match(failed.stdout, /forward this failure/);
+});