33import os from "os"
44import * as path from "path"
55
6- import { arePathsEqual , getReadablePath , getWorkspacePath } from "../path"
6+ import {
7+ arePathsEqual ,
8+ getReadablePath ,
9+ getWorkspacePath ,
10+ getAllWorkspacePaths ,
11+ getWorkspaceFolderForPath ,
12+ } from "../path"
713
814// Mock modules
15+ const mockWorkspaceFolders = vi . fn ( )
16+ const mockGetWorkspaceFolder = vi . fn ( )
17+ const mockActiveTextEditor = vi . fn ( )
918
1019vi . mock ( "vscode" , ( ) => ( {
1120 window : {
12- activeTextEditor : {
13- document : {
14- uri : { fsPath : "/test/workspaceFolder/file.ts" } ,
15- } ,
21+ get activeTextEditor ( ) {
22+ return mockActiveTextEditor ( )
1623 } ,
1724 } ,
1825 workspace : {
19- workspaceFolders : [
26+ get workspaceFolders ( ) {
27+ return mockWorkspaceFolders ( )
28+ } ,
29+ getWorkspaceFolder : mockGetWorkspaceFolder ,
30+ } ,
31+ } ) )
32+ describe ( "Path Utilities" , ( ) => {
33+ const originalPlatform = process . platform
34+ // Helper to mock VS Code configuration
35+
36+ beforeEach ( ( ) => {
37+ // Reset mocks before each test
38+ vi . clearAllMocks ( )
39+
40+ // Set default mock values
41+ mockWorkspaceFolders . mockReturnValue ( [
2042 {
2143 uri : { fsPath : "/test/workspace" } ,
2244 name : "test" ,
2345 index : 0 ,
2446 } ,
25- ] ,
26- getWorkspaceFolder : vi . fn ( ) . mockReturnValue ( {
47+ ] )
48+
49+ mockActiveTextEditor . mockReturnValue ( {
50+ document : {
51+ uri : { fsPath : "/test/workspaceFolder/file.ts" } ,
52+ } ,
53+ } )
54+
55+ mockGetWorkspaceFolder . mockReturnValue ( {
2756 uri : {
2857 fsPath : "/test/workspaceFolder" ,
2958 } ,
30- } ) ,
31- } ,
32- } ) )
33- describe ( "Path Utilities" , ( ) => {
34- const originalPlatform = process . platform
35- // Helper to mock VS Code configuration
59+ } )
60+ } )
3661
3762 afterEach ( ( ) => {
3863 Object . defineProperty ( process , "platform" , {
@@ -56,14 +81,129 @@ describe("Path Utilities", () => {
5681 expect ( extendedPath . toPosix ( ) ) . toBe ( "\\\\?\\C:\\Very\\Long\\Path" )
5782 } )
5883 } )
84+
5985 describe ( "getWorkspacePath" , ( ) => {
60- it ( "should return the current workspace path" , ( ) => {
61- const workspacePath = "/Users/test/project"
62- expect ( getWorkspacePath ( workspacePath ) ) . toBe ( "/Users/test/project" )
86+ it ( "should return the workspace folder of the active editor" , ( ) => {
87+ mockActiveTextEditor . mockReturnValue ( {
88+ document : {
89+ uri : { fsPath : "/test/workspaceFolder/file.ts" } ,
90+ } ,
91+ } )
92+ mockGetWorkspaceFolder . mockReturnValue ( {
93+ uri : { fsPath : "/test/workspaceFolder" } ,
94+ } )
95+ mockWorkspaceFolders . mockReturnValue ( [ { uri : { fsPath : "/test/workspace" } , name : "workspace" , index : 0 } ] )
96+
97+ expect ( getWorkspacePath ( ) ) . toBe ( "/test/workspaceFolder" )
98+ } )
99+
100+ it ( "should return the first workspace folder when no active editor" , ( ) => {
101+ mockActiveTextEditor . mockReturnValue ( undefined )
102+ mockGetWorkspaceFolder . mockReturnValue ( undefined )
103+ mockWorkspaceFolders . mockReturnValue ( [
104+ { uri : { fsPath : "/test/workspace1" } , name : "workspace1" , index : 0 } ,
105+ { uri : { fsPath : "/test/workspace2" } , name : "workspace2" , index : 1 } ,
106+ ] )
107+
108+ expect ( getWorkspacePath ( ) ) . toBe ( "/test/workspace1" )
109+ } )
110+
111+ it ( "should return default path when no workspace folders" , ( ) => {
112+ mockActiveTextEditor . mockReturnValue ( undefined )
113+ mockGetWorkspaceFolder . mockReturnValue ( undefined )
114+ mockWorkspaceFolders . mockReturnValue ( undefined )
115+
116+ expect ( getWorkspacePath ( "/default/path" ) ) . toBe ( "/default/path" )
117+ } )
118+
119+ it ( "should handle multi-root workspaces correctly" , ( ) => {
120+ mockWorkspaceFolders . mockReturnValue ( [
121+ { uri : { fsPath : "/test/frontend" } , name : "frontend" , index : 0 } ,
122+ { uri : { fsPath : "/test/backend" } , name : "backend" , index : 1 } ,
123+ ] )
124+
125+ // When active editor is in backend folder
126+ mockActiveTextEditor . mockReturnValue ( {
127+ document : { uri : { fsPath : "/test/backend/src/app.ts" } } ,
128+ } )
129+ mockGetWorkspaceFolder . mockReturnValue ( {
130+ uri : { fsPath : "/test/backend" } ,
131+ } )
132+
133+ expect ( getWorkspacePath ( ) ) . toBe ( "/test/backend" )
134+ } )
135+ } )
136+
137+ describe ( "getAllWorkspacePaths" , ( ) => {
138+ it ( "should return all workspace folder paths" , ( ) => {
139+ mockWorkspaceFolders . mockReturnValue ( [
140+ { uri : { fsPath : "/test/frontend" } , name : "frontend" , index : 0 } ,
141+ { uri : { fsPath : "/test/backend" } , name : "backend" , index : 1 } ,
142+ ] )
143+
144+ const paths = getAllWorkspacePaths ( )
145+ expect ( paths ) . toEqual ( [ "/test/frontend" , "/test/backend" ] )
146+ } )
147+
148+ it ( "should return empty array when no workspace folders" , ( ) => {
149+ mockWorkspaceFolders . mockReturnValue ( undefined )
150+
151+ const paths = getAllWorkspacePaths ( )
152+ expect ( paths ) . toEqual ( [ ] )
153+ } )
154+
155+ it ( "should handle single workspace folder" , ( ) => {
156+ mockWorkspaceFolders . mockReturnValue ( [ { uri : { fsPath : "/test/workspace" } , name : "workspace" , index : 0 } ] )
157+
158+ const paths = getAllWorkspacePaths ( )
159+ expect ( paths ) . toEqual ( [ "/test/workspace" ] )
160+ } )
161+ } )
162+
163+ describe ( "getWorkspaceFolderForPath" , ( ) => {
164+ beforeEach ( ( ) => {
165+ mockWorkspaceFolders . mockReturnValue ( [
166+ { uri : { fsPath : "/test/frontend" } , name : "frontend" , index : 0 } ,
167+ { uri : { fsPath : "/test/backend" } , name : "backend" , index : 1 } ,
168+ ] )
169+ } )
170+
171+ it ( "should return the workspace folder containing the path" , ( ) => {
172+ expect ( getWorkspaceFolderForPath ( "/test/frontend/src/app.ts" ) ) . toBe ( "/test/frontend" )
173+ expect ( getWorkspaceFolderForPath ( "/test/backend/src/server.ts" ) ) . toBe ( "/test/backend" )
174+ } )
175+
176+ it ( "should return the workspace folder for exact match" , ( ) => {
177+ expect ( getWorkspaceFolderForPath ( "/test/frontend" ) ) . toBe ( "/test/frontend" )
178+ expect ( getWorkspaceFolderForPath ( "/test/backend" ) ) . toBe ( "/test/backend" )
179+ } )
180+
181+ it ( "should return null for paths outside any workspace" , ( ) => {
182+ expect ( getWorkspaceFolderForPath ( "/other/path/file.ts" ) ) . toBeNull ( )
183+ expect ( getWorkspaceFolderForPath ( "/test/other/file.ts" ) ) . toBeNull ( )
184+ } )
185+
186+ it ( "should return null when no workspace folders" , ( ) => {
187+ mockWorkspaceFolders . mockReturnValue ( undefined )
188+ expect ( getWorkspaceFolderForPath ( "/test/frontend/src/app.ts" ) ) . toBeNull ( )
63189 } )
64190
65- it ( "should return undefined when outside a workspace" , ( ) => { } )
191+ it ( "should handle relative paths by resolving them" , ( ) => {
192+ // This depends on the current working directory, so we'll use path.resolve
193+ const resolvedPath = path . resolve ( "./src/app.ts" )
194+ const result = getWorkspaceFolderForPath ( "./src/app.ts" )
195+
196+ // The result should be based on the resolved path
197+ if ( resolvedPath . startsWith ( "/test/frontend" ) ) {
198+ expect ( result ) . toBe ( "/test/frontend" )
199+ } else if ( resolvedPath . startsWith ( "/test/backend" ) ) {
200+ expect ( result ) . toBe ( "/test/backend" )
201+ } else {
202+ expect ( result ) . toBeNull ( )
203+ }
204+ } )
66205 } )
206+
67207 describe ( "arePathsEqual" , ( ) => {
68208 describe ( "on Windows" , ( ) => {
69209 beforeEach ( ( ) => {
0 commit comments