|
| 1 | +import { afterEach, beforeEach, expect, test } from "bun:test"; |
| 2 | +import { mkdtemp, rm } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +type CapturedRunResult = { |
| 7 | + readonly exitCode: number; |
| 8 | + readonly stdout: string; |
| 9 | + readonly stderr: string; |
| 10 | +}; |
| 11 | + |
| 12 | +let tempDir: string | null = null; |
| 13 | +let originalSetupSyncMode: string | undefined; |
| 14 | +let originalLogger: string | undefined; |
| 15 | + |
| 16 | +beforeEach(async () => { |
| 17 | + tempDir = await mkdtemp(join(tmpdir(), "hack-up-missing-project-")); |
| 18 | + originalSetupSyncMode = process.env.HACK_SETUP_SYNC_MODE; |
| 19 | + originalLogger = process.env.HACK_LOGGER; |
| 20 | + process.env.HACK_SETUP_SYNC_MODE = "off"; |
| 21 | + process.env.HACK_LOGGER = "console"; |
| 22 | +}); |
| 23 | + |
| 24 | +afterEach(async () => { |
| 25 | + if (tempDir) { |
| 26 | + await rm(tempDir, { recursive: true, force: true }); |
| 27 | + tempDir = null; |
| 28 | + } |
| 29 | + if (originalSetupSyncMode !== undefined) { |
| 30 | + process.env.HACK_SETUP_SYNC_MODE = originalSetupSyncMode; |
| 31 | + } else { |
| 32 | + process.env.HACK_SETUP_SYNC_MODE = undefined; |
| 33 | + } |
| 34 | + if (originalLogger !== undefined) { |
| 35 | + process.env.HACK_LOGGER = originalLogger; |
| 36 | + } else { |
| 37 | + process.env.HACK_LOGGER = undefined; |
| 38 | + } |
| 39 | +}); |
| 40 | + |
| 41 | +test("up without .hack prints a user message without stack trace", async () => { |
| 42 | + if (!tempDir) { |
| 43 | + throw new Error("Missing temp directory"); |
| 44 | + } |
| 45 | + |
| 46 | + const result = await runCliWithCapturedOutput(["up", "--path", tempDir]); |
| 47 | + |
| 48 | + expect(result.exitCode).toBe(1); |
| 49 | + |
| 50 | + const combinedOutput = `${result.stdout}\n${result.stderr}`; |
| 51 | + expect(combinedOutput).toContain( |
| 52 | + "No .hack/ (or legacy .dev/) found. Run: hack init" |
| 53 | + ); |
| 54 | + expect(combinedOutput).not.toContain("at requireProjectContext"); |
| 55 | + expect(combinedOutput).not.toContain("at async handleUp"); |
| 56 | + expect(combinedOutput).not.toContain("ERROR Error:"); |
| 57 | +}); |
| 58 | + |
| 59 | +test("up still reports unrelated usage errors", async () => { |
| 60 | + const result = await runCliWithCapturedOutput([ |
| 61 | + "up", |
| 62 | + "--definitely-not-a-real-flag", |
| 63 | + ]); |
| 64 | + |
| 65 | + expect(result.exitCode).toBe(1); |
| 66 | + const combinedOutput = `${result.stdout}\n${result.stderr}`; |
| 67 | + expect(combinedOutput).toContain("Unknown option"); |
| 68 | + expect(combinedOutput).toContain("--definitely-not-a-real-flag"); |
| 69 | + expect(combinedOutput).toContain("Usage:"); |
| 70 | +}); |
| 71 | + |
| 72 | +async function runCliWithCapturedOutput( |
| 73 | + args: readonly string[] |
| 74 | +): Promise<CapturedRunResult> { |
| 75 | + let stdout = ""; |
| 76 | + let stderr = ""; |
| 77 | + const originalStdoutWrite = process.stdout.write; |
| 78 | + const originalStderrWrite = process.stderr.write; |
| 79 | + |
| 80 | + process.stdout.write = ((chunk: string | Uint8Array) => { |
| 81 | + stdout += |
| 82 | + typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8"); |
| 83 | + return true; |
| 84 | + }) as typeof process.stdout.write; |
| 85 | + |
| 86 | + process.stderr.write = ((chunk: string | Uint8Array) => { |
| 87 | + stderr += |
| 88 | + typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8"); |
| 89 | + return true; |
| 90 | + }) as typeof process.stderr.write; |
| 91 | + |
| 92 | + try { |
| 93 | + const { runCli } = await import("../src/cli/run.ts"); |
| 94 | + const exitCode = await runCli(args); |
| 95 | + return { exitCode, stdout, stderr }; |
| 96 | + } finally { |
| 97 | + process.stdout.write = originalStdoutWrite; |
| 98 | + process.stderr.write = originalStderrWrite; |
| 99 | + } |
| 100 | +} |
0 commit comments