Skip to content

Commit 2d94d7a

Browse files
committed
fix: make path tests cross-platform compatible
- Use path.join() instead of hardcoded Unix paths - Use path.sep for platform-specific separators - Ensures tests work correctly on both Unix and Windows
1 parent 0c7ff93 commit 2d94d7a

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/services/code-index/shared/__tests__/get-relative-path.spec.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,56 @@ describe("get-relative-path", () => {
66
describe("generateNormalizedAbsolutePath", () => {
77
it("should use provided workspace root", () => {
88
const filePath = "src/file.ts"
9-
const workspaceRoot = "/custom/workspace"
9+
const workspaceRoot = path.join(path.sep, "custom", "workspace")
1010
const result = generateNormalizedAbsolutePath(filePath, workspaceRoot)
11-
expect(result).toBe(path.normalize("/custom/workspace/src/file.ts"))
11+
expect(result).toBe(path.join(workspaceRoot, filePath))
1212
})
1313

1414
it("should handle absolute paths", () => {
15-
const filePath = "/absolute/path/file.ts"
16-
const workspaceRoot = "/custom/workspace"
15+
const filePath = path.join(path.sep, "absolute", "path", "file.ts")
16+
const workspaceRoot = path.join(path.sep, "custom", "workspace")
1717
const result = generateNormalizedAbsolutePath(filePath, workspaceRoot)
18-
expect(result).toBe(path.normalize("/absolute/path/file.ts"))
18+
expect(result).toBe(filePath)
1919
})
2020

2121
it("should normalize paths with . and .. segments", () => {
2222
const filePath = "./src/../src/file.ts"
23-
const workspaceRoot = "/custom/workspace"
23+
const workspaceRoot = path.join(path.sep, "custom", "workspace")
2424
const result = generateNormalizedAbsolutePath(filePath, workspaceRoot)
25-
expect(result).toBe(path.normalize("/custom/workspace/src/file.ts"))
25+
expect(result).toBe(path.join(workspaceRoot, "src", "file.ts"))
2626
})
2727
})
2828

2929
describe("generateRelativeFilePath", () => {
3030
it("should use provided workspace root", () => {
31-
const absolutePath = "/custom/workspace/src/file.ts"
32-
const workspaceRoot = "/custom/workspace"
31+
const workspaceRoot = path.join(path.sep, "custom", "workspace")
32+
const absolutePath = path.join(workspaceRoot, "src", "file.ts")
3333
const result = generateRelativeFilePath(absolutePath, workspaceRoot)
34-
expect(result).toBe(path.normalize("src/file.ts"))
34+
expect(result).toBe(path.join("src", "file.ts"))
3535
})
3636

3737
it("should handle paths outside workspace", () => {
38-
const absolutePath = "/outside/workspace/file.ts"
39-
const workspaceRoot = "/custom/workspace"
38+
const absolutePath = path.join(path.sep, "outside", "workspace", "file.ts")
39+
const workspaceRoot = path.join(path.sep, "custom", "workspace")
4040
const result = generateRelativeFilePath(absolutePath, workspaceRoot)
4141
// The result will have .. segments to navigate outside
4242
expect(result).toContain("..")
4343
})
4444

4545
it("should handle same path as workspace", () => {
46-
const absolutePath = "/custom/workspace"
47-
const workspaceRoot = "/custom/workspace"
46+
const workspaceRoot = path.join(path.sep, "custom", "workspace")
47+
const absolutePath = workspaceRoot
4848
const result = generateRelativeFilePath(absolutePath, workspaceRoot)
4949
expect(result).toBe(".")
5050
})
5151

5252
it("should handle multi-workspace scenarios", () => {
5353
// Simulate the error scenario from the issue
54-
const absolutePath = "/Users/test/admin/.prettierrc.json"
55-
const workspaceRoot = "/Users/test/project"
54+
const workspaceRoot = path.join(path.sep, "Users", "test", "project")
55+
const absolutePath = path.join(path.sep, "Users", "test", "admin", ".prettierrc.json")
5656
const result = generateRelativeFilePath(absolutePath, workspaceRoot)
5757
// Should generate a valid relative path, not throw an error
58-
expect(result).toBe(path.normalize("../admin/.prettierrc.json"))
58+
expect(result).toBe(path.join("..", "admin", ".prettierrc.json"))
5959
})
6060
})
6161
})

0 commit comments

Comments
 (0)