From 6ef8aaba651f37b5a7098fcd0a96f6aaccde5ce0 Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Sat, 29 Aug 2026 05:31:02 +0000 Subject: [PATCH] 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 --- test/benchmark-cli.test.ts | 13 +++++++------ test/evidence.test.ts | 8 +++----- test/explain-level-config.test.ts | 19 +++++-------------- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/test/benchmark-cli.test.ts b/test/benchmark-cli.test.ts index 834f5d88e..cc94d38fc 100644 --- a/test/benchmark-cli.test.ts +++ b/test/benchmark-cli.test.ts @@ -13,6 +13,8 @@ import { describe, test, expect } from 'bun:test'; import { spawnSync } from 'child_process'; + +import { runBin } from './helpers/run-bin'; import * as fs from 'fs'; import * as path from 'path'; 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'); function run(args: string[], opts: { env?: Record } = {}): { status: number | null; stdout: string; stderr: string } { - const result = spawnSync('bun', ['run', BIN, ...args], { + const result = runBin('bun', ['run', BIN, ...args], { cwd: ROOT, - env: { ...process.env, ...opts.env }, - encoding: 'utf-8', - timeout: 15000, + env: opts.env, + timeoutMs: 15000, }); return { status: result.status, - stdout: result.stdout?.toString() ?? '', - stderr: result.stderr?.toString() ?? '', + stdout: result.stdout, + stderr: result.stderr, }; } diff --git a/test/evidence.test.ts b/test/evidence.test.ts index 978866dce..c7637c4d6 100644 --- a/test/evidence.test.ts +++ b/test/evidence.test.ts @@ -11,20 +11,18 @@ let gstackHome: string; let repoDir: string; import { gitIn, findFilesBySuffix } from './helpers/scratch-repo'; +import { runBin } from './helpers/run-bin'; function git(args: string) { gitIn(repoDir, args); } 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, - env: { ...process.env, GSTACK_HOME: gstackHome }, - encoding: 'utf-8', - timeout: 60000, + env: { GSTACK_HOME: gstackHome }, 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 { diff --git a/test/explain-level-config.test.ts b/test/explain-level-config.test.ts index cdb61296c..ef30b86f1 100644 --- a/test/explain-level-config.test.ts +++ b/test/explain-level-config.test.ts @@ -12,7 +12,8 @@ import { describe, test, expect, beforeEach, afterEach } from 'bun:test'; import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; -import { spawnSync } from 'child_process'; + +import { runBin } from './helpers/run-bin'; const ROOT = path.resolve(import.meta.dir, '..'); const BIN_CONFIG = path.join(ROOT, 'bin', 'gstack-config'); @@ -28,19 +29,9 @@ afterEach(() => { }); function run(...args: string[]): { stdout: string; stderr: string; status: number } { - // gstack-config precedence is `${GSTACK_HOME:-${GSTACK_STATE_DIR:-$HOME/.gstack}}`, - // so GSTACK_HOME from the developer's parent env wins over the test's - // GSTACK_STATE_DIR. Override both to isolate from the real ~/.gstack. - 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, - }; + // runBin's gstackHome sets GSTACK_HOME + GSTACK_STATE_DIR together — the + // config-precedence isolation this file used to document by hand. + return runBin(BIN_CONFIG, args, { gstackHome: tmpHome, cwd: ROOT, trim: true }); } describe('gstack-config explain_level', () => {