refactor(test): first runBin migration batch (3 of ~36 run() duplicates)

explain-level-config, benchmark-cli, evidence move onto the shared
helper; each file's remaining special-case spawnSync sites (raw-buffer
probes, env-scrub probes) stay put deliberately. 55/55 green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Garry Tan
2026-08-29 05:31:02 +00:00
co-authored by Claude Fable 5
parent 1055561cae
commit 6ef8aaba65
3 changed files with 15 additions and 25 deletions
+7 -6
View File
@@ -13,6 +13,8 @@
import { describe, test, expect } from 'bun:test'; import { describe, test, expect } from 'bun:test';
import { spawnSync } from 'child_process'; import { spawnSync } from 'child_process';
import { runBin } from './helpers/run-bin';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
@@ -21,16 +23,15 @@ const ROOT = path.resolve(import.meta.dir, '..');
const BIN = path.join(ROOT, 'bin', 'gstack-model-benchmark'); const BIN = path.join(ROOT, 'bin', 'gstack-model-benchmark');
function run(args: string[], opts: { env?: Record<string, string> } = {}): { status: number | null; stdout: string; stderr: string } { function run(args: string[], opts: { env?: Record<string, string> } = {}): { status: number | null; stdout: string; stderr: string } {
const result = spawnSync('bun', ['run', BIN, ...args], { const result = runBin('bun', ['run', BIN, ...args], {
cwd: ROOT, cwd: ROOT,
env: { ...process.env, ...opts.env }, env: opts.env,
encoding: 'utf-8', timeoutMs: 15000,
timeout: 15000,
}); });
return { return {
status: result.status, status: result.status,
stdout: result.stdout?.toString() ?? '', stdout: result.stdout,
stderr: result.stderr?.toString() ?? '', stderr: result.stderr,
}; };
} }
+3 -5
View File
@@ -11,20 +11,18 @@ let gstackHome: string;
let repoDir: string; let repoDir: string;
import { gitIn, findFilesBySuffix } from './helpers/scratch-repo'; import { gitIn, findFilesBySuffix } from './helpers/scratch-repo';
import { runBin } from './helpers/run-bin';
function git(args: string) { function git(args: string) {
gitIn(repoDir, args); gitIn(repoDir, args);
} }
function run(args: string[], opts: { cwd?: string } = {}): { status: number; stdout: string; stderr: string } { function run(args: string[], opts: { cwd?: string } = {}): { status: number; stdout: string; stderr: string } {
const r = spawnSync(EVIDENCE, args, { return runBin(EVIDENCE, args, {
cwd: opts.cwd ?? repoDir, cwd: opts.cwd ?? repoDir,
env: { ...process.env, GSTACK_HOME: gstackHome }, env: { GSTACK_HOME: gstackHome },
encoding: 'utf-8',
timeout: 60000,
maxBuffer: 16 * 1024 * 1024, // the truncation test streams 3MB through the wrapper maxBuffer: 16 * 1024 * 1024, // the truncation test streams 3MB through the wrapper
}); });
return { status: r.status ?? 1, stdout: r.stdout ?? '', stderr: r.stderr ?? '' };
} }
function ledgerFile(): string { function ledgerFile(): string {
+5 -14
View File
@@ -12,7 +12,8 @@ import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as os from 'os'; import * as os from 'os';
import { spawnSync } from 'child_process';
import { runBin } from './helpers/run-bin';
const ROOT = path.resolve(import.meta.dir, '..'); const ROOT = path.resolve(import.meta.dir, '..');
const BIN_CONFIG = path.join(ROOT, 'bin', 'gstack-config'); const BIN_CONFIG = path.join(ROOT, 'bin', 'gstack-config');
@@ -28,19 +29,9 @@ afterEach(() => {
}); });
function run(...args: string[]): { stdout: string; stderr: string; status: number } { function run(...args: string[]): { stdout: string; stderr: string; status: number } {
// gstack-config precedence is `${GSTACK_HOME:-${GSTACK_STATE_DIR:-$HOME/.gstack}}`, // runBin's gstackHome sets GSTACK_HOME + GSTACK_STATE_DIR together — the
// so GSTACK_HOME from the developer's parent env wins over the test's // config-precedence isolation this file used to document by hand.
// GSTACK_STATE_DIR. Override both to isolate from the real ~/.gstack. return runBin(BIN_CONFIG, args, { gstackHome: tmpHome, cwd: ROOT, trim: true });
const res = spawnSync(BIN_CONFIG, args, {
env: { ...process.env, GSTACK_STATE_DIR: tmpHome, GSTACK_HOME: tmpHome },
encoding: 'utf-8',
cwd: ROOT,
});
return {
stdout: (res.stdout ?? '').trim(),
stderr: (res.stderr ?? '').trim(),
status: res.status ?? -1,
};
} }
describe('gstack-config explain_level', () => { describe('gstack-config explain_level', () => {