|
| 1 | +import { test, expect, describe, afterEach, beforeAll, afterAll } from "bun:test"; |
| 2 | +import { $ } from "bun"; |
| 3 | +import { mkdtemp, rm } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { openEditor, EditorError } from "./index.ts"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Integration tests. Real editors are interactive; we point `$EDITOR` at a |
| 10 | + * tiny shell script that mutates the tempfile and exits. |
| 11 | + */ |
| 12 | + |
| 13 | +let scriptDir: string; |
| 14 | +let appendScript: string; |
| 15 | +let visualScript: string; |
| 16 | +let failScript: string; |
| 17 | + |
| 18 | +beforeAll(async () => { |
| 19 | + scriptDir = await mkdtemp(join(tmpdir(), "bbcli-editor-test-")); |
| 20 | + |
| 21 | + appendScript = join(scriptDir, "append.sh"); |
| 22 | + await Bun.write(appendScript, '#!/bin/sh\necho "hello from editor" >> "$1"\n'); |
| 23 | + await $`chmod +x ${appendScript}`.quiet(); |
| 24 | + |
| 25 | + visualScript = join(scriptDir, "visual.sh"); |
| 26 | + await Bun.write(visualScript, '#!/bin/sh\necho "VISUAL_ran" >> "$1"\n'); |
| 27 | + await $`chmod +x ${visualScript}`.quiet(); |
| 28 | + |
| 29 | + failScript = join(scriptDir, "fail.sh"); |
| 30 | + await Bun.write(failScript, "#!/bin/sh\nexit 2\n"); |
| 31 | + await $`chmod +x ${failScript}`.quiet(); |
| 32 | +}); |
| 33 | + |
| 34 | +afterAll(async () => { |
| 35 | + if (scriptDir) await rm(scriptDir, { recursive: true, force: true }); |
| 36 | +}); |
| 37 | + |
| 38 | +const originalEditor = process.env["EDITOR"]; |
| 39 | +const originalVisual = process.env["VISUAL"]; |
| 40 | + |
| 41 | +afterEach(() => { |
| 42 | + if (originalEditor === undefined) delete process.env["EDITOR"]; |
| 43 | + else process.env["EDITOR"] = originalEditor; |
| 44 | + if (originalVisual === undefined) delete process.env["VISUAL"]; |
| 45 | + else process.env["VISUAL"] = originalVisual; |
| 46 | +}); |
| 47 | + |
| 48 | +describe("openEditor", () => { |
| 49 | + test("invokes $EDITOR and returns the file contents after exit", async () => { |
| 50 | + process.env["EDITOR"] = appendScript; |
| 51 | + delete process.env["VISUAL"]; |
| 52 | + |
| 53 | + const result = await openEditor("seed line\n"); |
| 54 | + expect(result).toBe("seed line\nhello from editor\n"); |
| 55 | + }); |
| 56 | + |
| 57 | + test("prefers $VISUAL over $EDITOR", async () => { |
| 58 | + process.env["EDITOR"] = appendScript; |
| 59 | + process.env["VISUAL"] = visualScript; |
| 60 | + |
| 61 | + const result = await openEditor(); |
| 62 | + expect(result).toContain("VISUAL_ran"); |
| 63 | + expect(result).not.toContain("hello from editor"); |
| 64 | + }); |
| 65 | + |
| 66 | + test("throws EditorError when neither env var is set", async () => { |
| 67 | + delete process.env["EDITOR"]; |
| 68 | + delete process.env["VISUAL"]; |
| 69 | + |
| 70 | + const err = await openEditor().catch((e) => e); |
| 71 | + expect(err).toBeInstanceOf(EditorError); |
| 72 | + }); |
| 73 | + |
| 74 | + test("throws EditorError when the editor exits non-zero", async () => { |
| 75 | + process.env["EDITOR"] = failScript; |
| 76 | + delete process.env["VISUAL"]; |
| 77 | + |
| 78 | + const err = await openEditor().catch((e) => e); |
| 79 | + expect(err).toBeInstanceOf(EditorError); |
| 80 | + expect((err as Error).message).toContain("code 2"); |
| 81 | + }); |
| 82 | +}); |
0 commit comments