Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test: Bun.spawn and Bun.spawnSync default env to process.env, so preload edits (#7451 git context drop, gpg override) reach every child

garrytan99dcd4821ff3
diff --git a/test/git-context-preload-7451.test.ts b/test/git-context-preload-7451.test.ts
index 0c8c49e019..f2cb71bd35 100644
--- a/test/git-context-preload-7451.test.ts
+++ b/test/git-context-preload-7451.test.ts
@@ -24,7 +24,7 @@ function git(args: string[], cwd: string): string {
return proc.stdout.toString().trim();
}
-function outerRepo(): { root: string; outer: string; probe: string; inner: string } {
+function outerRepo(spawnEnv = 'env: { ...process.env }, '): { root: string; outer: string; probe: string; inner: string } {
const root = mkdtempSync(join(tmpdir(), 'gbrain-git-context-'));
dirs.push(root);
const outer = join(root, 'outer');
@@ -33,7 +33,7 @@ function outerRepo(): { root: string; outer: string; probe: string; inner: strin
const probe = join(root, 'git-context-probe.test.ts');
writeFileSync(probe, `import { expect, test } from 'bun:test';
test('a throwaway repo gets its own config', () => {
- const run = (args: string[], cwd: string) => Bun.spawnSync(['git', ...args], { cwd, env: { ...process.env }, stdout: 'pipe', stderr: 'pipe' });
+ const run = (args: string[], cwd: string) => Bun.spawnSync(['git', ...args], { cwd, ${spawnEnv}stdout: 'pipe', stderr: 'pipe' });
expect(run(['init', '-q', ${JSON.stringify(inner)}], ${JSON.stringify(root)}).exitCode).toBe(0);
expect(run(['-C', ${JSON.stringify(inner)}, 'config', 'user.name', 't'], ${JSON.stringify(root)}).exitCode).toBe(0);
expect(run(['-C', ${JSON.stringify(inner)}, 'config', 'user.email', 't@t'], ${JSON.stringify(root)}).exitCode).toBe(0);
@@ -61,6 +61,18 @@ describe('git-hermetic-preload: inherited git context (#7451)', () => {
expect(git(['config', '--file', join(inner, '.git', 'config'), '--get', 'user.name'], inner)).toBe('t');
}, 60_000);
+ // Bun.spawn and Bun.spawnSync called without `env` hand the child the environment the process started with, not
+ // process.env as the preloads left it, so the preload makes process.env their default.
+ test('a helper that spawns git without an env option leaves the outer repository config untouched too', () => {
+ const { outer, probe, inner } = outerRepo('');
+ const before = git(['config', '--file', join(outer, '.git', 'config'), '--list'], outer);
+ const result = runProbe(probe, { GIT_DIR: join(outer, '.git'), GIT_WORK_TREE: outer, GIT_INDEX_FILE: join(outer, '.git', 'index') });
+ expect(result.output).toContain('1 pass');
+ expect(result.exitCode).toBe(0);
+ expect(git(['config', '--file', join(outer, '.git', 'config'), '--list'], outer)).toBe(before);
+ expect(git(['config', '--file', join(inner, '.git', 'config'), '--get', 'user.name'], inner)).toBe('t');
+ }, 60_000);
+
test('the preload drops exactly what gitChildEnv drops and keeps the gpg signing override', () => {
const { probe } = outerRepo();
const check = probe.replace(/git-context-probe\.test\.ts$/, 'env-probe.test.ts');
diff --git a/test/helpers/git-hermetic-preload.ts b/test/helpers/git-hermetic-preload.ts
index bbd6d76733..8e0011526c 100644
--- a/test/helpers/git-hermetic-preload.ts
+++ b/test/helpers/git-hermetic-preload.ts
@@ -30,11 +30,22 @@
* repository through them, and a helper's `git -C <temp repo> config ...` then wrote into the caller's repository
* (`core.bare=true`, `user.name t`). Transport and credential variables pass through, as in production.
*
+ * Bun.spawn and Bun.spawnSync called without an `env` option give the child the environment the process started with,
+ * not process.env as edited here, so both are wrapped to default `env` to process.env (child_process already does).
+ * Without that, a helper's `Bun.spawnSync(['git', ...])` still saw the hook's GIT_DIR and none of the keys below.
+ *
* Imported by `bunfig.toml` via
* `preload = [..., "./test/helpers/git-hermetic-preload.ts"]`.
*/
import { gitChildEnv } from '../../src/core/git-env.ts';
+for (const name of ['spawn', 'spawnSync'] as const) {
+ const original = Bun[name] as (...args: unknown[]) => unknown;
+ (Bun as unknown as Record<string, unknown>)[name] = (first: unknown, second?: object) => Array.isArray(first)
+ ? original(first, { env: process.env, ...second })
+ : original({ env: process.env, ...(first as object) });
+}
+
const kept = gitChildEnv({}, process.env);
for (const key of Object.keys(process.env)) if (!(key in kept)) delete process.env[key];