64 lines
1.8 KiB
TypeScript
64 lines
1.8 KiB
TypeScript
import { afterEach, describe, expect, it } from "bun:test";
|
|
import { getEditorCommand } from "../src/utils/external-editor";
|
|
|
|
interface MutableProcess {
|
|
platform: NodeJS.Platform;
|
|
}
|
|
|
|
function setPlatform(value: NodeJS.Platform): void {
|
|
(process as unknown as MutableProcess).platform = value;
|
|
}
|
|
|
|
describe("getEditorCommand", () => {
|
|
const originalPlatform = process.platform;
|
|
const originalVisual = Bun.env.VISUAL;
|
|
const originalEditor = Bun.env.EDITOR;
|
|
|
|
afterEach(() => {
|
|
setPlatform(originalPlatform);
|
|
if (originalVisual === undefined) delete Bun.env.VISUAL;
|
|
else Bun.env.VISUAL = originalVisual;
|
|
if (originalEditor === undefined) delete Bun.env.EDITOR;
|
|
else Bun.env.EDITOR = originalEditor;
|
|
});
|
|
|
|
it("prefers $VISUAL over $EDITOR and the platform default", () => {
|
|
Bun.env.VISUAL = "nvim";
|
|
Bun.env.EDITOR = "nano";
|
|
setPlatform("win32");
|
|
expect(getEditorCommand()).toBe("nvim");
|
|
});
|
|
|
|
it("falls back to $EDITOR when $VISUAL is unset", () => {
|
|
delete Bun.env.VISUAL;
|
|
Bun.env.EDITOR = "nano";
|
|
expect(getEditorCommand()).toBe("nano");
|
|
});
|
|
|
|
it("trims whitespace so an accidentally padded value still works", () => {
|
|
Bun.env.VISUAL = " code --wait ";
|
|
delete Bun.env.EDITOR;
|
|
expect(getEditorCommand()).toBe("code --wait");
|
|
});
|
|
|
|
it("treats a whitespace-only $VISUAL as unset and consults $EDITOR", () => {
|
|
Bun.env.VISUAL = " ";
|
|
Bun.env.EDITOR = "vim";
|
|
expect(getEditorCommand()).toBe("vim");
|
|
});
|
|
|
|
it("defaults to notepad on Windows when neither variable is set", () => {
|
|
delete Bun.env.VISUAL;
|
|
delete Bun.env.EDITOR;
|
|
setPlatform("win32");
|
|
expect(getEditorCommand()).toBe("notepad");
|
|
});
|
|
|
|
it("returns undefined on POSIX when neither variable is set", () => {
|
|
delete Bun.env.VISUAL;
|
|
delete Bun.env.EDITOR;
|
|
setPlatform("linux");
|
|
expect(getEditorCommand()).toBeUndefined();
|
|
});
|
|
});
|