Skip to content

Commit fdb2442

Browse files
JiayuuWangclaude
andauthored
test: add coverage for toRelativePath and getDirectoryForPath (#95)
These two exported functions in src/utils/path.ts lacked unit tests. toRelativePath wraps Node's path.relative() and keeps the absolute path when the result would start with '..' (i.e. the target is outside the CWD). Four tests cover: child-of-cwd, outside-cwd, cwd-itself, and the type-safety invariant. getDirectoryForPath uses statSync to distinguish directories from files, falling back to dirname() for non-existent paths and bypassing the filesystem for UNC paths (NTLM credential-leak prevention). Three tests cover: existing directory, known file, and non-existent path. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 00b044e commit fdb2442

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

src/utils/__tests__/path.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { describe, expect, test } from "bun:test";
2+
import { resolve } from "path";
23
import {
34
containsPathTraversal,
45
expandPath,
56
normalizePathForConfigKey,
7+
toRelativePath,
8+
getDirectoryForPath,
69
} from "../path";
710

811
// ─── containsPathTraversal ──────────────────────────────────────────────
@@ -129,3 +132,68 @@ describe("normalizePathForConfigKey", () => {
129132
expect(result).toBe("foo/bar");
130133
});
131134
});
135+
136+
// ─── toRelativePath ─────────────────────────────────────────────────────
137+
138+
describe("toRelativePath", () => {
139+
test("returns relative path for a child of cwd", () => {
140+
// Build a path that is inside the current working directory.
141+
// resolve() returns an absolute path, and toRelativePath should give
142+
// back just the final segment (or relative form without ..).
143+
const abs = resolve(process.cwd(), "package.json");
144+
const result = toRelativePath(abs);
145+
expect(result).toBe("package.json");
146+
expect(result).not.toContain("..");
147+
});
148+
149+
test("returns absolute path when target is outside cwd", () => {
150+
// A well-known absolute path that is always outside any typical cwd
151+
// (any absolute path that doesn't start with process.cwd() will work)
152+
const cwd = process.cwd();
153+
// Build a path guaranteed to be outside cwd by going to the root's parent
154+
// of cwd, then a sibling directory with an unlikely name
155+
const outsidePath = resolve(cwd, "../../__unlikely_dir_xyz__");
156+
const result = toRelativePath(outsidePath);
157+
// relative(cwd, outsidePath) will start with '../..' so function returns absolute
158+
expect(result).toBe(outsidePath);
159+
});
160+
161+
test("returns empty string for cwd itself", () => {
162+
const cwd = process.cwd();
163+
const result = toRelativePath(cwd);
164+
// relative(cwd, cwd) === '' which does not start with '..'
165+
expect(result).toBe("");
166+
});
167+
168+
test("returns a string for any absolute path", () => {
169+
const abs = resolve(process.cwd(), "src");
170+
const result = toRelativePath(abs);
171+
expect(typeof result).toBe("string");
172+
});
173+
});
174+
175+
// ─── getDirectoryForPath ─────────────────────────────────────────────────
176+
177+
describe("getDirectoryForPath", () => {
178+
test("returns the path itself when given an existing directory", () => {
179+
// The src directory is guaranteed to exist in this repo
180+
const dir = resolve(process.cwd(), "src");
181+
const result = getDirectoryForPath(dir);
182+
expect(result).toBe(dir);
183+
});
184+
185+
test("returns parent directory for a known file", () => {
186+
// package.json is at the repo root
187+
const file = resolve(process.cwd(), "package.json");
188+
const expectedParent = process.cwd();
189+
const result = getDirectoryForPath(file);
190+
expect(result).toBe(expectedParent);
191+
});
192+
193+
test("returns parent directory for a non-existent path", () => {
194+
const nonExistent = resolve(process.cwd(), "does-not-exist-xyz123.ts");
195+
const expectedParent = process.cwd();
196+
const result = getDirectoryForPath(nonExistent);
197+
expect(result).toBe(expectedParent);
198+
});
199+
});

0 commit comments

Comments
 (0)