diff --git a/test/js/bun/shell/bunshell-default.test.ts b/test/js/bun/shell/bunshell-default.test.ts index be46c48976525b..6266b603e86c3f 100644 --- a/test/js/bun/shell/bunshell-default.test.ts +++ b/test/js/bun/shell/bunshell-default.test.ts @@ -1,25 +1,54 @@ import { $ } from "bun"; +import { bunExe, createTestBuilder } from "./test_builder"; +import { bunEnv } from "harness"; +const TestBuilder = createTestBuilder(import.meta.path); test("default throw on command failure", async () => { - try { - await $`echo hi; ls oogabooga`.quiet(); - expect.unreachable(); - } catch (e: any) { - expect(e).toBeInstanceOf(Error); - expect(e.exitCode).toBe(1); - expect(e.message).toBe("Failed with exit code 1"); - expect(e.stdout.toString("utf-8")).toBe("hi\n"); - expect(e.stderr.toString("utf-8")).toBe("ls: oogabooga: No such file or directory\n"); - } + // Run in a subproc because other tests may change the value of $.throws + const code = /* ts */ ` + import { $ } from "bun"; + import { afterAll, beforeAll, describe, expect, test } from "bun:test"; + test('test', async () => { + try { + await $\`echo hi; ls oogabooga\`.quiet(); + expect.unreachable(); + } catch (e: any) { + expect(e).toBeInstanceOf(Error); + expect(e.exitCode).toBe(1); + expect(e.message).toBe("Failed with exit code 1"); + expect(e.stdout.toString("utf-8")).toBe("hi\\n"); + expect(e.stderr.toString("utf-8")).toBe("ls: oogabooga: No such file or directory\\n"); + } + }) + `; + + await TestBuilder.command`echo ${code} > index.test.ts; ${bunExe()} test index.test.ts` + .ensureTempDir() + .stderr(s => s.includes("1 pass")) + .env(bunEnv) + .run(); }); test("ShellError has .text()", async () => { - try { - await $`ls oogabooga`.quiet(); - expect.unreachable(); - } catch (e: any) { - expect(e).toBeInstanceOf(Error); - expect(e.exitCode).toBe(1); - expect(e.stderr.toString("utf-8")).toBe("ls: oogabooga: No such file or directory\n"); - } + // Run in a subproc because other tests may change the value of $.throws + const code = /* ts */ ` + import { $ } from "bun"; + import { afterAll, beforeAll, describe, expect, test } from "bun:test"; + test('test', async () => { + try { + await $\`ls oogabooga\`.quiet(); + expect.unreachable(); + } catch (e: any) { + expect(e).toBeInstanceOf(Error); + expect(e.exitCode).toBe(1); + expect(e.stderr.toString("utf-8")).toBe("ls: oogabooga: No such file or directory\\n"); + } + }) + `; + + await TestBuilder.command`echo ${code} > index.test.ts; ${bunExe()} test index.test.ts` + .ensureTempDir() + .stderr(s => s.includes("1 pass")) + .env(bunEnv) + .run(); }); diff --git a/test/js/bun/shell/commands/cp.test.ts b/test/js/bun/shell/commands/cp.test.ts index b16dc8536ad153..3f57f9a494945b 100644 --- a/test/js/bun/shell/commands/cp.test.ts +++ b/test/js/bun/shell/commands/cp.test.ts @@ -1,5 +1,5 @@ import { $ } from "bun"; -import { TestBuilder } from "../test_builder"; +import { createTestBuilder } from "../test_builder"; import { beforeAll, describe, test, expect, beforeEach } from "bun:test"; import { sortedShellOutput } from "../util"; import { tempDirWithFiles } from "harness"; @@ -7,6 +7,8 @@ import fs from "fs"; import { shellInternals } from "bun:internal-for-testing"; const { builtinDisabled } = shellInternals; +const TestBuilder = createTestBuilder(import.meta.path); + const p = process.platform === "win32" ? (s: string) => s.replaceAll("/", "\\") : (s: string) => s; $.nothrow(); diff --git a/test/js/bun/shell/test_builder.ts b/test/js/bun/shell/test_builder.ts index 4cdc53756ad1f8..24377aaaffe90a 100644 --- a/test/js/bun/shell/test_builder.ts +++ b/test/js/bun/shell/test_builder.ts @@ -363,6 +363,8 @@ export function createTestBuilder(path: string) { } } } + + return TestBuilder; } function generateRandomString(length: number): string {