|
1 | 1 | import { describe, expect, test } from "bun:test"; |
| 2 | +import { resolve } from "path"; |
2 | 3 | import { |
3 | 4 | containsPathTraversal, |
4 | 5 | expandPath, |
5 | 6 | normalizePathForConfigKey, |
| 7 | + toRelativePath, |
| 8 | + getDirectoryForPath, |
6 | 9 | } from "../path"; |
7 | 10 |
|
8 | 11 | // ─── containsPathTraversal ────────────────────────────────────────────── |
@@ -129,3 +132,68 @@ describe("normalizePathForConfigKey", () => { |
129 | 132 | expect(result).toBe("foo/bar"); |
130 | 133 | }); |
131 | 134 | }); |
| 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