forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathutils.unit.test.ts
More file actions
248 lines (177 loc) · 8.2 KB
/
utils.unit.test.ts
File metadata and controls
248 lines (177 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Uri, WorkspaceFolder } from 'vscode';
import { resolveFilePath } from '../../client/chat/utils';
import * as workspaceApis from '../../client/common/vscodeApis/workspaceApis';
suite('Chat Utils - resolveFilePath()', () => {
let getWorkspaceFoldersStub: sinon.SinonStub;
setup(() => {
getWorkspaceFoldersStub = sinon.stub(workspaceApis, 'getWorkspaceFolders');
getWorkspaceFoldersStub.returns([]);
});
teardown(() => {
sinon.restore();
});
suite('When filepath is undefined or empty', () => {
test('Should return first workspace folder URI when workspace folders exist', () => {
const expectedUri = Uri.file('/test/workspace');
const mockFolder: WorkspaceFolder = {
uri: expectedUri,
name: 'test',
index: 0,
};
getWorkspaceFoldersStub.returns([mockFolder]);
const result = resolveFilePath(undefined);
expect(result?.toString()).to.equal(expectedUri.toString());
});
test('Should return first folder when multiple workspace folders exist', () => {
const firstUri = Uri.file('/first/workspace');
const secondUri = Uri.file('/second/workspace');
const mockFolders: WorkspaceFolder[] = [
{ uri: firstUri, name: 'first', index: 0 },
{ uri: secondUri, name: 'second', index: 1 },
];
getWorkspaceFoldersStub.returns(mockFolders);
const result = resolveFilePath(undefined);
expect(result?.toString()).to.equal(firstUri.toString());
});
test('Should return undefined when no workspace folders exist', () => {
getWorkspaceFoldersStub.returns(undefined);
const result = resolveFilePath(undefined);
expect(result).to.be.undefined;
});
test('Should return undefined when workspace folders is empty array', () => {
getWorkspaceFoldersStub.returns([]);
const result = resolveFilePath(undefined);
expect(result).to.be.undefined;
});
test('Should return undefined for empty string when no workspace folders', () => {
getWorkspaceFoldersStub.returns(undefined);
const result = resolveFilePath('');
expect(result).to.be.undefined;
});
});
suite('Windows file paths', () => {
test('Should handle Windows path with lowercase drive letter', () => {
const filepath = 'c:\\GIT\\tests\\simple-python-app';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
// Uri.file normalizes drive letters to lowercase
expect(result?.fsPath.toLowerCase()).to.include('git');
});
test('Should handle Windows path with uppercase drive letter', () => {
const filepath = 'C:\\Users\\test\\project';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
expect(result?.fsPath.toLowerCase()).to.include('users');
});
test('Should handle Windows path with forward slashes', () => {
const filepath = 'C:/Users/test/project';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
});
suite('Unix file paths', () => {
test('Should handle Unix absolute path', () => {
const filepath = '/home/user/projects/myapp';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
expect(result?.path).to.include('/home/user/projects/myapp');
});
test('Should handle Unix root path', () => {
const filepath = '/';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
});
suite('Relative paths', () => {
test('Should handle relative path with dot prefix', () => {
const filepath = './src/main.py';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
test('Should handle relative path without prefix', () => {
const filepath = 'src/main.py';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
test('Should handle parent directory reference', () => {
const filepath = '../other-project/file.py';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
});
suite('URI schemes', () => {
test('Should handle file:// URI scheme', () => {
const filepath = 'file:///home/user/test.py';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
expect(result?.path).to.include('/home/user/test.py');
});
test('Should handle vscode-notebook:// URI scheme', () => {
const filepath = 'vscode-notebook://jupyter/notebook.ipynb';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('vscode-notebook');
});
test('Should handle untitled: URI scheme without double slash as file path', () => {
const filepath = 'untitled:Untitled-1';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
// untitled: doesn't have ://, so it will be treated as a file path
expect(result?.scheme).to.equal('file');
});
test('Should handle https:// URI scheme', () => {
const filepath = 'https://example.com/path';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('https');
});
test('Should handle vscode-vfs:// URI scheme', () => {
const filepath = 'vscode-vfs://github/microsoft/vscode/file.ts';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('vscode-vfs');
});
});
suite('Edge cases', () => {
test('Should handle path with spaces', () => {
const filepath = '/home/user/my project/file.py';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
test('Should handle path with special characters', () => {
const filepath = '/home/user/project-name_v2/file.py';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
test('Should not treat Windows drive letter colon as URI scheme', () => {
// Windows path should not be confused with a URI scheme
const filepath = 'd:\\projects\\test';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
test('Should not treat single colon as URI scheme', () => {
// A path with a colon but not :// should be treated as a file
const filepath = 'c:somepath';
const result = resolveFilePath(filepath);
expect(result).to.not.be.undefined;
expect(result?.scheme).to.equal('file');
});
});
});