Skip to content

Commit 91c9cfd

Browse files
committed
Add tests
1 parent 05c25af commit 91c9cfd

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

test/unit/util.test.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import os from "node:os";
12
import { describe, it, expect } from "vitest";
23

3-
import { countSubstring, parseRemoteAuthority, toSafeHost } from "@/util";
4+
import {
5+
countSubstring,
6+
escapeCommandArg,
7+
expandPath,
8+
parseRemoteAuthority,
9+
toSafeHost,
10+
} from "@/util";
411

512
it("ignore unrelated authorities", () => {
613
const tests = [
@@ -124,3 +131,62 @@ describe("countSubstring", () => {
124131
expect(countSubstring("aa", "aaaaaa")).toBe(3);
125132
});
126133
});
134+
135+
describe("escapeCommandArg", () => {
136+
it("wraps simple string in quotes", () => {
137+
expect(escapeCommandArg("hello")).toBe('"hello"');
138+
});
139+
140+
it("handles empty string", () => {
141+
expect(escapeCommandArg("")).toBe('""');
142+
});
143+
144+
it("escapes double quotes", () => {
145+
expect(escapeCommandArg('say "hello"')).toBe(String.raw`"say \"hello\""`);
146+
});
147+
148+
it("preserves backslashes", () => {
149+
expect(escapeCommandArg(String.raw`path\to\file`)).toBe(
150+
String.raw`"path\to\file"`,
151+
);
152+
});
153+
154+
it("handles string with spaces", () => {
155+
expect(escapeCommandArg("hello world")).toBe('"hello world"');
156+
});
157+
});
158+
159+
describe("expandPath", () => {
160+
const home = os.homedir();
161+
162+
it("expands tilde at start of path", () => {
163+
expect(expandPath("~/foo/bar")).toBe(`${home}/foo/bar`);
164+
});
165+
166+
it("expands standalone tilde", () => {
167+
expect(expandPath("~")).toBe(home);
168+
});
169+
170+
it("does not expand tilde in middle of path", () => {
171+
expect(expandPath("/foo/~/bar")).toBe("/foo/~/bar");
172+
});
173+
174+
it("expands ${userHome} variable", () => {
175+
expect(expandPath("${userHome}/foo")).toBe(`${home}/foo`);
176+
});
177+
178+
it("expands multiple ${userHome} variables", () => {
179+
expect(expandPath("${userHome}/foo/${userHome}/bar")).toBe(
180+
`${home}/foo/${home}/bar`,
181+
);
182+
});
183+
184+
it("leaves paths without tilde or variable unchanged", () => {
185+
expect(expandPath("/absolute/path")).toBe("/absolute/path");
186+
expect(expandPath("relative/path")).toBe("relative/path");
187+
});
188+
189+
it("expands both tilde and ${userHome}", () => {
190+
expect(expandPath("~/${userHome}/foo")).toBe(`${home}/${home}/foo`);
191+
});
192+
});

0 commit comments

Comments
 (0)