expand-home-path.test.ts

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