Skip to content

Commit f0f84f8

Browse files
committed
fix: make path tests platform-agnostic for Windows compatibility
- Updated workspace-utils tests to use path.normalize() and path.join() - Updated get-relative-path tests to handle platform-specific path separators - Fixed failing Windows CI tests by ensuring cross-platform path handling
1 parent 8a5b3f3 commit f0f84f8

File tree

2 files changed

+64
-41
lines changed

2 files changed

+64
-41
lines changed

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ describe("get-relative-path", () => {
5050
})
5151

5252
it("should return normalized absolute paths unchanged", () => {
53-
const absolutePath = "/some/absolute/path/file.ts"
53+
// Use path.resolve to create a proper absolute path for the current platform
54+
const absolutePath = path.resolve("/some/absolute/path/file.ts")
5455
const result = generateNormalizedAbsolutePath(absolutePath)
55-
expect(result).toBe(path.normalize(absolutePath))
56+
expect(result).toBe(absolutePath)
5657
})
5758

5859
it("should use custom workspace root when provided", () => {
59-
const result = generateNormalizedAbsolutePath("src/file.ts", "/custom/workspace")
60-
const expected = path.join("/custom/workspace", "src/file.ts")
60+
const customWorkspace = path.normalize("/custom/workspace")
61+
const result = generateNormalizedAbsolutePath("src/file.ts", customWorkspace)
62+
const expected = path.join(customWorkspace, "src/file.ts")
6163
expect(result).toBe(expected)
6264
})
6365

@@ -71,23 +73,26 @@ describe("get-relative-path", () => {
7173

7274
describe("generateRelativeFilePath", () => {
7375
it("should generate relative path when workspace root is provided", () => {
74-
const absolutePath = "/custom/workspace/src/file.ts"
75-
const result = generateRelativeFilePath(absolutePath, "/custom/workspace")
76-
expect(result).toBe(path.normalize("src/file.ts"))
76+
const workspaceRoot = path.normalize("/custom/workspace")
77+
const absolutePath = path.join(workspaceRoot, "src", "file.ts")
78+
const result = generateRelativeFilePath(absolutePath, workspaceRoot)
79+
expect(result).toBe(path.join("src", "file.ts"))
7780
})
7881

7982
it("should return null for paths outside the provided workspace root", () => {
80-
const absolutePath = "/other/location/file.ts"
81-
const result = generateRelativeFilePath(absolutePath, "/custom/workspace")
83+
const absolutePath = path.normalize("/other/location/file.ts")
84+
const workspaceRoot = path.normalize("/custom/workspace")
85+
const result = generateRelativeFilePath(absolutePath, workspaceRoot)
8286
expect(result).toBeNull()
8387
})
8488

8589
it("should auto-detect workspace root in multi-root workspace", () => {
86-
const absolutePath = "/workspace/project1/src/file.ts"
87-
vi.mocked(workspaceUtils.getWorkspaceRootForFile).mockReturnValue("/workspace/project1")
90+
const workspaceRoot = path.normalize("/workspace/project1")
91+
const absolutePath = path.join(workspaceRoot, "src", "file.ts")
92+
vi.mocked(workspaceUtils.getWorkspaceRootForFile).mockReturnValue(workspaceRoot)
8893

8994
const result = generateRelativeFilePath(absolutePath)
90-
expect(result).toBe(path.normalize("src/file.ts"))
95+
expect(result).toBe(path.join("src", "file.ts"))
9196
expect(workspaceUtils.getWorkspaceRootForFile).toHaveBeenCalledWith(absolutePath)
9297
})
9398

@@ -100,23 +105,25 @@ describe("get-relative-path", () => {
100105
})
101106

102107
it("should handle workspace root as the file path", () => {
103-
const workspaceRoot = "/workspace/project"
108+
const workspaceRoot = path.normalize("/workspace/project")
104109
const result = generateRelativeFilePath(workspaceRoot, workspaceRoot)
105-
expect(result).toBe(path.normalize("."))
110+
expect(result).toBe(".")
106111
})
107112

108113
it("should handle paths with .. when workspace root is provided", () => {
109-
const absolutePath = "/workspace/../outside/file.ts"
110-
const result = generateRelativeFilePath(absolutePath, "/workspace")
114+
const workspaceRoot = path.normalize("/workspace")
115+
const absolutePath = path.normalize("/workspace/../outside/file.ts")
116+
const result = generateRelativeFilePath(absolutePath, workspaceRoot)
111117
expect(result).toBeNull()
112118
})
113119

114120
it("should prioritize provided workspace root over auto-detection", () => {
115-
const absolutePath = "/workspace/project1/src/file.ts"
116-
vi.mocked(workspaceUtils.getWorkspaceRootForFile).mockReturnValue("/workspace/project2")
121+
const workspaceRoot = path.normalize("/workspace/project1")
122+
const absolutePath = path.join(workspaceRoot, "src", "file.ts")
123+
vi.mocked(workspaceUtils.getWorkspaceRootForFile).mockReturnValue(path.normalize("/workspace/project2"))
117124

118-
const result = generateRelativeFilePath(absolutePath, "/workspace/project1")
119-
expect(result).toBe(path.normalize("src/file.ts"))
125+
const result = generateRelativeFilePath(absolutePath, workspaceRoot)
126+
expect(result).toBe(path.join("src", "file.ts"))
120127
expect(workspaceUtils.getWorkspaceRootForFile).not.toHaveBeenCalled()
121128
})
122129
})

src/services/code-index/shared/__tests__/workspace-utils.spec.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest"
22
import * as vscode from "vscode"
3+
import * as path from "path"
34
import {
45
getWorkspaceRootForFile,
56
isFileInWorkspace,
@@ -27,38 +28,46 @@ describe("workspace-utils", () => {
2728
})
2829

2930
it("should return the correct workspace root for a file", () => {
31+
const project1Path = path.normalize("/workspace/project1")
32+
const project2Path = path.normalize("/workspace/project2")
3033
;(vscode.workspace as any).workspaceFolders = [
31-
{ uri: { fsPath: "/workspace/project1" } },
32-
{ uri: { fsPath: "/workspace/project2" } },
34+
{ uri: { fsPath: project1Path } },
35+
{ uri: { fsPath: project2Path } },
3336
]
3437

35-
const result = getWorkspaceRootForFile("/workspace/project1/src/file.ts")
36-
expect(result).toBe("/workspace/project1")
38+
const filePath = path.join(project1Path, "src", "file.ts")
39+
const result = getWorkspaceRootForFile(filePath)
40+
expect(result).toBe(project1Path)
3741
})
3842

3943
it("should handle nested workspace folders correctly", () => {
44+
const parentPath = path.normalize("/workspace/parent")
45+
const childPath = path.join(parentPath, "child")
4046
;(vscode.workspace as any).workspaceFolders = [
41-
{ uri: { fsPath: "/workspace/parent" } },
42-
{ uri: { fsPath: "/workspace/parent/child" } },
47+
{ uri: { fsPath: parentPath } },
48+
{ uri: { fsPath: childPath } },
4349
]
4450

4551
// File in child workspace should return child workspace root
46-
const result = getWorkspaceRootForFile("/workspace/parent/child/src/file.ts")
47-
expect(result).toBe("/workspace/parent/child")
52+
const filePath = path.join(childPath, "src", "file.ts")
53+
const result = getWorkspaceRootForFile(filePath)
54+
expect(result).toBe(childPath)
4855
})
4956

5057
it("should return undefined for files outside all workspace roots", () => {
51-
;(vscode.workspace as any).workspaceFolders = [{ uri: { fsPath: "/workspace/project1" } }]
58+
const workspacePath = path.normalize("/workspace/project1")
59+
;(vscode.workspace as any).workspaceFolders = [{ uri: { fsPath: workspacePath } }]
5260

5361
const result = getWorkspaceRootForFile("/other/location/file.ts")
5462
expect(result).toBeUndefined()
5563
})
5664

5765
it("should handle exact workspace root path", () => {
58-
;(vscode.workspace as any).workspaceFolders = [{ uri: { fsPath: "/workspace/project" } }]
66+
const workspacePath = path.normalize("/workspace/project")
67+
;(vscode.workspace as any).workspaceFolders = [{ uri: { fsPath: workspacePath } }]
5968

60-
const result = getWorkspaceRootForFile("/workspace/project")
61-
expect(result).toBe("/workspace/project")
69+
const result = getWorkspaceRootForFile(workspacePath)
70+
expect(result).toBe(workspacePath)
6271
})
6372
})
6473

@@ -92,26 +101,33 @@ describe("workspace-utils", () => {
92101
})
93102

94103
it("should return all workspace root paths", () => {
104+
const project1Path = path.normalize("/workspace/project1")
105+
const project2Path = path.normalize("/workspace/project2")
106+
const project3Path = path.normalize("/workspace/project3")
95107
;(vscode.workspace as any).workspaceFolders = [
96-
{ uri: { fsPath: "/workspace/project1" } },
97-
{ uri: { fsPath: "/workspace/project2" } },
98-
{ uri: { fsPath: "/workspace/project3" } },
108+
{ uri: { fsPath: project1Path } },
109+
{ uri: { fsPath: project2Path } },
110+
{ uri: { fsPath: project3Path } },
99111
]
100112

101113
const result = getAllWorkspaceRoots()
102-
expect(result).toEqual(["/workspace/project1", "/workspace/project2", "/workspace/project3"])
114+
expect(result).toEqual([project1Path, project2Path, project3Path])
103115
})
104116

105117
it("should normalize paths", () => {
118+
const project1Path = "/workspace/project1/"
119+
const project2Path = "/workspace//project2"
106120
;(vscode.workspace as any).workspaceFolders = [
107-
{ uri: { fsPath: "/workspace/project1/" } },
108-
{ uri: { fsPath: "/workspace//project2" } },
121+
{ uri: { fsPath: project1Path } },
122+
{ uri: { fsPath: project2Path } },
109123
]
110124

111125
const result = getAllWorkspaceRoots()
112-
// path.normalize may keep trailing slashes on some platforms
113-
expect(result[0]).toMatch(/^\/workspace\/project1\/?$/)
114-
expect(result[1]).toMatch(/^\/workspace\/project2\/?$/)
126+
// Normalize the expected paths for comparison
127+
const normalized1 = path.normalize(project1Path)
128+
const normalized2 = path.normalize(project2Path)
129+
expect(result[0]).toBe(normalized1)
130+
expect(result[1]).toBe(normalized2)
115131
})
116132
})
117133

0 commit comments

Comments
 (0)