|
1 | 1 | import { describe, it, expect, vi, beforeEach } from "vitest" |
2 | 2 | import * as vscode from "vscode" |
| 3 | +import * as path from "path" |
3 | 4 | import { |
4 | 5 | getWorkspaceRootForFile, |
5 | 6 | isFileInWorkspace, |
@@ -27,38 +28,46 @@ describe("workspace-utils", () => { |
27 | 28 | }) |
28 | 29 |
|
29 | 30 | it("should return the correct workspace root for a file", () => { |
| 31 | + const project1Path = path.normalize("/workspace/project1") |
| 32 | + const project2Path = path.normalize("/workspace/project2") |
30 | 33 | ;(vscode.workspace as any).workspaceFolders = [ |
31 | | - { uri: { fsPath: "/workspace/project1" } }, |
32 | | - { uri: { fsPath: "/workspace/project2" } }, |
| 34 | + { uri: { fsPath: project1Path } }, |
| 35 | + { uri: { fsPath: project2Path } }, |
33 | 36 | ] |
34 | 37 |
|
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) |
37 | 41 | }) |
38 | 42 |
|
39 | 43 | it("should handle nested workspace folders correctly", () => { |
| 44 | + const parentPath = path.normalize("/workspace/parent") |
| 45 | + const childPath = path.join(parentPath, "child") |
40 | 46 | ;(vscode.workspace as any).workspaceFolders = [ |
41 | | - { uri: { fsPath: "/workspace/parent" } }, |
42 | | - { uri: { fsPath: "/workspace/parent/child" } }, |
| 47 | + { uri: { fsPath: parentPath } }, |
| 48 | + { uri: { fsPath: childPath } }, |
43 | 49 | ] |
44 | 50 |
|
45 | 51 | // 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) |
48 | 55 | }) |
49 | 56 |
|
50 | 57 | 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 } }] |
52 | 60 |
|
53 | 61 | const result = getWorkspaceRootForFile("/other/location/file.ts") |
54 | 62 | expect(result).toBeUndefined() |
55 | 63 | }) |
56 | 64 |
|
57 | 65 | 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 } }] |
59 | 68 |
|
60 | | - const result = getWorkspaceRootForFile("/workspace/project") |
61 | | - expect(result).toBe("/workspace/project") |
| 69 | + const result = getWorkspaceRootForFile(workspacePath) |
| 70 | + expect(result).toBe(workspacePath) |
62 | 71 | }) |
63 | 72 | }) |
64 | 73 |
|
@@ -92,26 +101,33 @@ describe("workspace-utils", () => { |
92 | 101 | }) |
93 | 102 |
|
94 | 103 | 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") |
95 | 107 | ;(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 } }, |
99 | 111 | ] |
100 | 112 |
|
101 | 113 | const result = getAllWorkspaceRoots() |
102 | | - expect(result).toEqual(["/workspace/project1", "/workspace/project2", "/workspace/project3"]) |
| 114 | + expect(result).toEqual([project1Path, project2Path, project3Path]) |
103 | 115 | }) |
104 | 116 |
|
105 | 117 | it("should normalize paths", () => { |
| 118 | + const project1Path = "/workspace/project1/" |
| 119 | + const project2Path = "/workspace//project2" |
106 | 120 | ;(vscode.workspace as any).workspaceFolders = [ |
107 | | - { uri: { fsPath: "/workspace/project1/" } }, |
108 | | - { uri: { fsPath: "/workspace//project2" } }, |
| 121 | + { uri: { fsPath: project1Path } }, |
| 122 | + { uri: { fsPath: project2Path } }, |
109 | 123 | ] |
110 | 124 |
|
111 | 125 | 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) |
115 | 131 | }) |
116 | 132 | }) |
117 | 133 |
|
|
0 commit comments