Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/core/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { afterEach, describe, expect, test } from "bun:test";
import { formatCliError, HunkUserError } from "./errors";

const originalDebug = process.env.HUNK_DEBUG;

afterEach(() => {
if (originalDebug === undefined) {
delete process.env.HUNK_DEBUG;
} else {
process.env.HUNK_DEBUG = originalDebug;
}
});

describe("formatCliError", () => {
test("formats expected user errors with optional details and no stack", () => {
expect(formatCliError(new HunkUserError("Not in a repo"))).toBe("hunk: Not in a repo\n");
expect(formatCliError(new HunkUserError("Invalid ref", ["Try `HEAD~1`."]))).toBe(
"hunk: Invalid ref\n\nTry `HEAD~1`.\n",
);
});

test("hides unexpected stacks unless debug output is explicitly enabled", () => {
const error = new Error("Boom");
error.stack = "Error: Boom\n at internal";

delete process.env.HUNK_DEBUG;
expect(formatCliError(error)).toBe("hunk: Boom\n");

process.env.HUNK_DEBUG = "1";
expect(formatCliError(error)).toBe("Error: Boom\n at internal\n");
});

test("stringifies non-error thrown values", () => {
expect(formatCliError("plain failure")).toBe("hunk: plain failure\n");
});
});
Loading
Loading