expand-home-path.test.ts

 1import { describe, test, expect, beforeEach, afterEach } from "bun:test";
 2import { expandHomePath } from "../src/util/path.js";
 3
 4describe("expandHomePath", () => {
 5  let savedHome: string | undefined;
 6
 7  beforeEach(() => {
 8    savedHome = process.env["HOME"];
 9  });
10
11  afterEach(() => {
12    if (savedHome === undefined) {
13      delete process.env["HOME"];
14    } else {
15      process.env["HOME"] = savedHome;
16    }
17  });
18
19  test("returns path unchanged when HOME is unset", () => {
20    delete process.env["HOME"];
21    expect(expandHomePath("~/foo/bar")).toBe("~/foo/bar");
22  });
23
24  test("bare ~ returns HOME", () => {
25    process.env["HOME"] = "/Users/alice";
26    expect(expandHomePath("~")).toBe("/Users/alice");
27  });
28
29  test("~/foo/bar expands to $HOME/foo/bar", () => {
30    process.env["HOME"] = "/Users/alice";
31    expect(expandHomePath("~/foo/bar")).toBe("/Users/alice/foo/bar");
32  });
33
34  test("paths without tilde are returned unchanged", () => {
35    process.env["HOME"] = "/Users/alice";
36    expect(expandHomePath("/absolute/path")).toBe("/absolute/path");
37    expect(expandHomePath("relative/path")).toBe("relative/path");
38  });
39
40  test("~user/foo is returned unchanged (not our expansion)", () => {
41    process.env["HOME"] = "/Users/alice";
42    expect(expandHomePath("~user/foo")).toBe("~user/foo");
43  });
44});