forked from recca0120/vscode-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathReplacer.test.ts
More file actions
343 lines (253 loc) · 11.9 KB
/
Copy pathPathReplacer.test.ts
File metadata and controls
343 lines (253 loc) · 11.9 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import os from 'node:os';
import { describe, expect, it } from 'vitest';
import { phpUnitProject, phpUnitProjectWin } from '../../tests/utils';
import {
VAR_PATH_SEPARATOR,
VAR_PATH_SEPARATOR_SHORT,
VAR_PWD,
VAR_USER_HOME,
VAR_WORKSPACE_FOLDER,
VAR_WORKSPACE_FOLDER_BASENAME,
} from '../constants';
import { PathReplacer } from './PathReplacer';
describe('PathReplacer', () => {
const givenPathReplacer = (
paths?: Record<string, string>,
cwd?: string,
workspaceFolder?: string,
) => {
return new PathReplacer(
{ cwd: cwd ?? phpUnitProject('') },
paths ?? {
[VAR_WORKSPACE_FOLDER]: '/app',
},
workspaceFolder,
);
};
const toWindows = (path: string) => path.replace(/\//g, '\\').replace(/\\$/g, '');
const givenPathReplacerForWindows = (
paths?: Record<string, string>,
cwd?: string,
workspaceFolder?: string,
) => {
return new PathReplacer(
{ cwd: cwd ?? phpUnitProjectWin('') },
paths ?? {
[VAR_WORKSPACE_FOLDER]: '/app',
},
workspaceFolder,
);
};
it('to remote same path', () => {
const pathReplacer = givenPathReplacer();
const path = phpUnitProject('tests/AssertionsTest.php');
expect(pathReplacer.toRemote(path)).toEqual('/app/tests/AssertionsTest.php');
});
it('to remote replace . to /app/', () => {
const pathReplacer = givenPathReplacer();
const path = './tests/AssertionsTest.php';
expect(pathReplacer.toRemote(path)).toEqual('/app/tests/AssertionsTest.php');
});
it(`to remote replace ${VAR_WORKSPACE_FOLDER} to /app/`, () => {
const pathReplacer = givenPathReplacer();
const path = `${VAR_WORKSPACE_FOLDER}/tests/AssertionsTest.php`;
expect(pathReplacer.toRemote(path)).toEqual('/app/tests/AssertionsTest.php');
});
it(`to remote replace ${VAR_PWD} to /app/`, () => {
const pathReplacer = givenPathReplacer();
const path = `${VAR_PWD}/tests/AssertionsTest.php`;
expect(pathReplacer.toRemote(path)).toEqual('/app/tests/AssertionsTest.php');
});
it('to local same path', () => {
const pathReplacer = givenPathReplacer();
const path = phpUnitProject('tests/AssertionsTest.php');
expect(pathReplacer.toLocal(path)).toEqual(path);
});
it('to local replace /app/ to project root', () => {
const pathReplacer = givenPathReplacer();
const path = '/app/tests/AssertionsTest.php';
expect(pathReplacer.toLocal(path)).toEqual(phpUnitProject('tests/AssertionsTest.php'));
});
it('to local replace . to project root', () => {
const pathReplacer = givenPathReplacer();
const path = './tests/AssertionsTest.php';
expect(pathReplacer.toLocal(path)).toEqual(phpUnitProject('tests/AssertionsTest.php'));
});
it('to local replace /app/ to project root with php_qn', () => {
const pathReplacer = givenPathReplacer();
const path =
'php_qn:///app/tests/AssertionsTest.php::\\Recca0120\\VSCode\\Tests\\AssertionsTest';
expect(pathReplacer.toLocal(path)).toEqual(
`php_qn://${phpUnitProject('tests/AssertionsTest.php')}::\\Recca0120\\VSCode\\Tests\\AssertionsTest`,
);
});
it('to local replace . to project root with php_qn', () => {
const pathReplacer = givenPathReplacer();
const path =
'php_qn://./tests/AssertionsTest.php::\\Recca0120\\VSCode\\Tests\\AssertionsTest';
expect(pathReplacer.toLocal(path)).toEqual(
`php_qn://${phpUnitProject('tests/AssertionsTest.php')}::\\Recca0120\\VSCode\\Tests\\AssertionsTest`,
);
});
it('to local replace /app/ to project root with phpvfscomposer', () => {
const pathReplacer = givenPathReplacer();
const path = 'phpvfscomposer:///app/vendor/phpunit/phpunit/phpunit';
expect(pathReplacer.toLocal(path)).toEqual(
phpUnitProject('vendor/phpunit/phpunit/phpunit'),
);
});
it('to remote same path for windows', () => {
const pathReplacer = givenPathReplacerForWindows();
const path = phpUnitProjectWin('tests/AssertionsTest.php');
expect(pathReplacer.toRemote(path)).toEqual('/app/tests/AssertionsTest.php');
});
it(`to remote replace ${VAR_WORKSPACE_FOLDER} to /app/ for windows`, () => {
const pathReplacer = givenPathReplacerForWindows();
const path = toWindows(`${VAR_WORKSPACE_FOLDER}/tests/AssertionsTest.php`);
expect(pathReplacer.toRemote(path)).toEqual('/app/tests/AssertionsTest.php');
});
it(`to remote replace ${VAR_PWD} to /app/ for windows`, () => {
const pathReplacer = givenPathReplacerForWindows();
const path = toWindows(`${VAR_PWD}/tests/AssertionsTest.php`);
expect(pathReplacer.toRemote(path)).toEqual('/app/tests/AssertionsTest.php');
});
it('remote same path for windows', () => {
const pathReplacer = givenPathReplacerForWindows();
const path = phpUnitProjectWin('tests/AssertionsTest.php');
expect(pathReplacer.toLocal(path)).toEqual(path);
});
it('to local replace . to project root', () => {
const pathReplacer = givenPathReplacerForWindows();
const path = './tests/AssertionsTest.php';
expect(pathReplacer.toLocal(path)).toEqual(phpUnitProjectWin('tests/AssertionsTest.php'));
});
it('to local replace /app/ to project root for windows', () => {
const pathReplacer = givenPathReplacerForWindows();
const path = '/app/tests/AssertionsTest.php';
expect(pathReplacer.toLocal(path)).toEqual(phpUnitProjectWin('tests/AssertionsTest.php'));
});
it('to local replace /app/ to project root with php_qn for windows', () => {
const pathReplacer = givenPathReplacerForWindows();
const path =
'php_qn:///app/tests/AssertionsTest.php::\\Recca0120\\VSCode\\Tests\\AssertionsTest';
expect(pathReplacer.toLocal(path)).toEqual(
`php_qn://${phpUnitProjectWin('tests/AssertionsTest.php')}::\\Recca0120\\VSCode\\Tests\\AssertionsTest`,
);
});
it('to local replace /app/ to project root with phpvfscomposer for windows', () => {
const pathReplacer = givenPathReplacerForWindows();
const path = 'phpvfscomposer:///app/vendor/phpunit/phpunit/phpunit';
expect(pathReplacer.toLocal(path)).toEqual(
phpUnitProjectWin('vendor/phpunit/phpunit/phpunit'),
);
});
it(`to remote replace ${VAR_WORKSPACE_FOLDER} is . for windows`, () => {
const pathReplacer = givenPathReplacerForWindows({
[VAR_WORKSPACE_FOLDER]: '.',
});
const path = `${VAR_PWD}/phpunit.xml`;
expect(pathReplacer.toRemote(path)).toEqual('./phpunit.xml');
});
it('to local should not corrupt paths containing ../ when remote is .', () => {
const pathReplacer = givenPathReplacer({
[VAR_WORKSPACE_FOLDER]: '.',
});
const path = '/abc/../def/Foo.php';
expect(pathReplacer.toLocal(path)).toEqual('/abc/../def/Foo.php');
});
it(`to local replace ${VAR_WORKSPACE_FOLDER} is . for windows`, () => {
const pathReplacer = givenPathReplacerForWindows({
[VAR_WORKSPACE_FOLDER]: '.',
});
const path = './tests/AssertionsTest.php';
expect(pathReplacer.toLocal(path)).toEqual(phpUnitProjectWin('tests/AssertionsTest.php'));
});
it(`replaces ${VAR_WORKSPACE_FOLDER_BASENAME} in paths value`, () => {
const pathReplacer = new PathReplacer(
{ cwd: phpUnitProject('') },
{ [VAR_WORKSPACE_FOLDER]: `/${VAR_WORKSPACE_FOLDER_BASENAME}` },
);
expect(pathReplacer.toRemote(phpUnitProject('tests/AssertionsTest.php'))).toEqual(
'/phpunit-stub/tests/AssertionsTest.php',
);
});
it(`replaces ${VAR_USER_HOME} in paths value`, () => {
const pathReplacer = new PathReplacer(
{ cwd: phpUnitProject('') },
{ [VAR_WORKSPACE_FOLDER]: `${VAR_USER_HOME}/app` },
);
const expected =
process.platform === 'win32'
? `${os.homedir()}\\app\\tests\\AssertionsTest.php`
: `${os.homedir()}/app/tests/AssertionsTest.php`;
expect(pathReplacer.toRemote(phpUnitProject('tests/AssertionsTest.php'))).toEqual(expected);
});
it(`replaces ${VAR_PATH_SEPARATOR} in paths value`, () => {
const pathReplacer = new PathReplacer(
{ cwd: phpUnitProject('') },
{ [VAR_WORKSPACE_FOLDER]: `/app${VAR_PATH_SEPARATOR}sub` },
);
// posixPath normalises backslashes for non-drive-letter paths on all platforms
expect(pathReplacer.toRemote(phpUnitProject('tests/AssertionsTest.php'))).toEqual(
`/app/sub/tests/AssertionsTest.php`,
);
});
it(`replaces ${VAR_PATH_SEPARATOR_SHORT} in paths value`, () => {
const pathReplacer = new PathReplacer(
{ cwd: phpUnitProject('') },
{ [VAR_WORKSPACE_FOLDER]: `/app${VAR_PATH_SEPARATOR_SHORT}sub` },
);
// posixPath normalises backslashes for non-drive-letter paths on all platforms
expect(pathReplacer.toRemote(phpUnitProject('tests/AssertionsTest.php'))).toEqual(
`/app/sub/tests/AssertionsTest.php`,
);
});
it(`can't replace path when ${VAR_WORKSPACE_FOLDER} is /`, () => {
const pathReplacer = givenPathReplacer(
{
[VAR_WORKSPACE_FOLDER]: '/app',
},
'/',
);
const localPath = phpUnitProject('tests/AssertionsTest.php');
expect(pathReplacer.toRemote(localPath)).toEqual(localPath);
const remotePath = '/app/tests/AssertionsTest.php';
expect(pathReplacer.toLocal(remotePath)).toEqual(remotePath);
});
describe('workspaceFolder differs from cwd (monorepo / nested config)', () => {
const workspace = '/workspace';
const cwd = '/workspace/apps/api';
it(`replaces ${VAR_WORKSPACE_FOLDER} with workspaceFolder, not cwd`, () => {
const pathReplacer = new PathReplacer({ cwd }, undefined, workspace);
expect(
pathReplacer.toRemote(`${VAR_WORKSPACE_FOLDER}/apps/api/phpunit.xml.dist`),
).toEqual('/workspace/apps/api/phpunit.xml.dist');
});
it(`replaces ${VAR_WORKSPACE_FOLDER_BASENAME} with basename of workspaceFolder, not cwd`, () => {
const pathReplacer = new PathReplacer({ cwd }, undefined, workspace);
expect(pathReplacer.replacePathVariables(`${VAR_WORKSPACE_FOLDER_BASENAME}`)).toEqual(
'workspace',
);
});
it(`replaces ${VAR_PWD} with cwd, not workspaceFolder`, () => {
const pathReplacer = new PathReplacer({ cwd }, undefined, workspace);
expect(pathReplacer.toRemote(`${VAR_PWD}/vendor/bin/phpunit`)).toEqual(
'/workspace/apps/api/vendor/bin/phpunit',
);
});
it(`does not double-nest path when ${VAR_WORKSPACE_FOLDER} contains subdirectory`, () => {
const pathReplacer = new PathReplacer({ cwd }, undefined, workspace);
const result = pathReplacer.toRemote(
`${VAR_WORKSPACE_FOLDER}/apps/api/vendor/bin/phpunit`,
);
expect(result).toEqual('/workspace/apps/api/vendor/bin/phpunit');
expect(result).not.toContain('apps/api/apps/api');
});
it(`falls back to cwd when workspaceFolder is not provided`, () => {
const pathReplacer = new PathReplacer({ cwd });
expect(
pathReplacer.toRemote(`${VAR_WORKSPACE_FOLDER}/apps/api/phpunit.xml.dist`),
).toEqual('/workspace/apps/api/apps/api/phpunit.xml.dist');
});
});
});