-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathutils.spec.ts
More file actions
373 lines (312 loc) · 14.1 KB
/
Copy pathutils.spec.ts
File metadata and controls
373 lines (312 loc) · 14.1 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
deriveDefaultPackageName,
ensureGitignoreNodeModules,
ensureGitignoreVsCodeEditorConfigs,
formatTargetDir,
getProjectDirFromPackageName,
normalizeEditorOption,
renameFiles,
shouldConfigureEditorsForCreate,
} from '../utils.js';
describe('getProjectDirFromPackageName', () => {
it('should get project dir from package name', () => {
expect(getProjectDirFromPackageName('@my/package')).toBe('package');
expect(getProjectDirFromPackageName('my-package')).toBe('my-package');
});
});
describe('editor configuration policy', () => {
it('normalizes repeated editor options to a single editor value', () => {
expect(normalizeEditorOption('vscode')).toBe('vscode');
expect(normalizeEditorOption(['vscode', 'zed'])).toBe('zed');
expect(normalizeEditorOption(['vscode', false])).toBe(false);
expect(normalizeEditorOption([undefined, 'vscode'])).toBe('vscode');
});
it('allows automatic editor configuration outside existing monorepos', () => {
expect(shouldConfigureEditorsForCreate({ isMonorepo: false, editor: undefined })).toBe(true);
});
it('skips automatic editor configuration inside existing monorepos', () => {
expect(shouldConfigureEditorsForCreate({ isMonorepo: true, editor: undefined })).toBe(false);
});
it('allows explicit editor opt-in inside existing monorepos', () => {
expect(shouldConfigureEditorsForCreate({ isMonorepo: true, editor: 'vscode' })).toBe(true);
expect(shouldConfigureEditorsForCreate({ isMonorepo: true, editor: ' ' })).toBe(false);
});
it('keeps --no-editor as an explicit opt-out in every workspace mode', () => {
expect(shouldConfigureEditorsForCreate({ isMonorepo: false, editor: false })).toBe(false);
expect(shouldConfigureEditorsForCreate({ isMonorepo: true, editor: false })).toBe(false);
});
});
describe('formatTargetDir', () => {
it('should format "." as current directory with empty package name', () => {
expect(formatTargetDir('.')).toEqual({
directory: '.',
packageName: '',
});
});
it('should format "./" as current directory with empty package name', () => {
expect(formatTargetDir('./')).toEqual({
directory: '.',
packageName: '',
});
});
it('should format target dir with invalid input', () => {
expect(formatTargetDir('/foo/bar')).matchSnapshot();
expect(formatTargetDir('@scope/')).matchSnapshot();
expect(formatTargetDir('../../foo/bar')).matchSnapshot();
});
// Should work on all platforms (including Windows) - directory must always use forward slashes
it('should format target dir with valid input', () => {
expect(formatTargetDir('./my-package')).matchSnapshot();
expect(formatTargetDir('my-package')).matchSnapshot();
expect(formatTargetDir('@my-scope/my-package')).matchSnapshot();
expect(formatTargetDir('foo/@my-scope/my-package')).matchSnapshot();
expect(formatTargetDir('./foo/@my-scope/my-package')).matchSnapshot();
expect(formatTargetDir('./foo/bar/@scope/my-package')).matchSnapshot();
expect(formatTargetDir('./foo/bar/@scope/my-package/')).matchSnapshot();
expect(formatTargetDir('./foo/bar/@scope/my-package/sub-package')).matchSnapshot();
});
// Regression test for https://github.com/voidzero-dev/vite-plus/issues/938
// On Windows, path.join/normalize produce backslashes which break when passed as CLI args.
// Nested paths are the critical cases since they involve path separators.
it('should always use forward slashes in directory (issue #938)', () => {
expect(formatTargetDir('foo/@my-scope/my-package').directory).toBe('foo/my-package');
expect(formatTargetDir('./foo/bar/@scope/my-package').directory).toBe('foo/bar/my-package');
expect(formatTargetDir('./foo/bar/@scope/my-package/sub-package').directory).toBe(
'foo/bar/@scope/my-package/sub-package',
);
});
it('should format target dir with invalid package name', () => {
expect(formatTargetDir('my-package@').error).matchSnapshot();
expect(formatTargetDir('my-package@1.0.0').error).matchSnapshot();
});
});
describe('deriveDefaultPackageName', () => {
it('should derive package name from directory basename', () => {
expect(deriveDefaultPackageName('/home/user/my-app', undefined, 'fallback')).toBe('my-app');
});
it('should derive scoped package name when scope is provided', () => {
expect(deriveDefaultPackageName('/home/user/my-app', '@my-scope', 'fallback')).toBe(
'@my-scope/my-app',
);
});
it('should fallback to random name when directory name is invalid', () => {
const result = deriveDefaultPackageName('/home/user/.hidden', undefined, 'vite-plus-app');
// directory name starts with '.', so a random name is generated instead
expect(result).not.toBe('.hidden');
expect(result.length).toBeGreaterThan(0);
});
it('should fallback when directory is filesystem root', () => {
const result = deriveDefaultPackageName('/', undefined, 'vite-plus-app');
// basename of '/' is empty, so a random name is generated
expect(result.length).toBeGreaterThan(0);
});
});
describe('ensureGitignoreNodeModules', () => {
let projectDir: string;
beforeEach(() => {
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-gitignore-'));
});
afterEach(() => {
fs.rmSync(projectDir, { recursive: true, force: true });
});
function gitignore(): string {
return fs.readFileSync(path.join(projectDir, '.gitignore'), 'utf-8');
}
it('creates a fresh `.gitignore` with `node_modules` when none exists', () => {
ensureGitignoreNodeModules(projectDir);
expect(gitignore()).toBe('node_modules\n');
});
it('appends `node_modules` to an existing `.gitignore` that omits it', () => {
fs.writeFileSync(path.join(projectDir, '.gitignore'), 'dist\n*.log\n');
ensureGitignoreNodeModules(projectDir);
expect(gitignore()).toBe('dist\n*.log\nnode_modules\n');
});
it('terminates the last line first when the existing file lacks a trailing newline', () => {
fs.writeFileSync(path.join(projectDir, '.gitignore'), 'dist');
ensureGitignoreNodeModules(projectDir);
expect(gitignore()).toBe('dist\nnode_modules\n');
});
it('is a no-op when `node_modules` already appears as a standalone line', () => {
const existing = '# Logs\n*.log\nnode_modules\ndist\n';
fs.writeFileSync(path.join(projectDir, '.gitignore'), existing);
ensureGitignoreNodeModules(projectDir);
expect(gitignore()).toBe(existing);
});
it('treats `node_modules/` (with trailing slash) as a match', () => {
const existing = 'node_modules/\ndist\n';
fs.writeFileSync(path.join(projectDir, '.gitignore'), existing);
ensureGitignoreNodeModules(projectDir);
expect(gitignore()).toBe(existing);
});
it('handles CRLF line endings without re-appending', () => {
const existing = 'node_modules\r\ndist\r\n';
fs.writeFileSync(path.join(projectDir, '.gitignore'), existing);
ensureGitignoreNodeModules(projectDir);
expect(gitignore()).toBe(existing);
});
it('does not consider a `node_modules/sub` subpath as already excluded', () => {
fs.writeFileSync(path.join(projectDir, '.gitignore'), 'node_modules/sub\n');
ensureGitignoreNodeModules(projectDir);
expect(gitignore()).toBe('node_modules/sub\nnode_modules\n');
});
it('does not match `!node_modules` (an explicit un-ignore override)', () => {
fs.writeFileSync(path.join(projectDir, '.gitignore'), '!node_modules\n');
ensureGitignoreNodeModules(projectDir);
expect(gitignore()).toBe('!node_modules\nnode_modules\n');
});
});
describe('ensureGitignoreVsCodeEditorConfigs', () => {
let projectDir: string;
beforeEach(() => {
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-vscode-gitignore-'));
});
afterEach(() => {
fs.rmSync(projectDir, { recursive: true, force: true });
});
function gitignore(): string {
return fs.readFileSync(path.join(projectDir, '.gitignore'), 'utf-8');
}
function writeGitignore(content: string): void {
fs.writeFileSync(path.join(projectDir, '.gitignore'), content);
}
function writeVsCodeSettings(): void {
fs.mkdirSync(path.join(projectDir, '.vscode'), { recursive: true });
fs.writeFileSync(path.join(projectDir, '.vscode', 'settings.json'), '{}\n');
}
const vscodeUnignoreBlock = '!.vscode/\n!.vscode/settings.json\n!.vscode/extensions.json\n';
it('unignores VS Code settings when `.vscode/*` is ignored', () => {
writeVsCodeSettings();
writeGitignore('.vscode/*\n!.vscode/extensions.json\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`.vscode/*\n!.vscode/extensions.json\n${vscodeUnignoreBlock}`);
});
it('unignores generated VS Code config files for root-anchored contents ignores', () => {
writeVsCodeSettings();
writeGitignore('/.vscode/*\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`/.vscode/*\n${vscodeUnignoreBlock}`);
});
it('appends VS Code directory and config unignores for directory-level VS Code ignores', () => {
writeVsCodeSettings();
writeGitignore('.vscode/\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`.vscode/\n${vscodeUnignoreBlock}`);
});
it('appends VS Code directory and config unignores for root-anchored directory-level VS Code ignores', () => {
writeVsCodeSettings();
writeGitignore('/.vscode\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`/.vscode\n${vscodeUnignoreBlock}`);
});
it('appends VS Code config unignores after explicit VS Code settings ignores', () => {
writeVsCodeSettings();
writeGitignore('.vscode/*\n.vscode/settings.json\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`.vscode/*\n.vscode/settings.json\n${vscodeUnignoreBlock}`);
});
it('appends VS Code config unignores after explicit VS Code extensions ignores', () => {
writeVsCodeSettings();
writeGitignore('.vscode/*\n/.vscode/extensions.json\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`.vscode/*\n/.vscode/extensions.json\n${vscodeUnignoreBlock}`);
});
it('appends VS Code config unignores when all generated config files are explicitly ignored', () => {
writeVsCodeSettings();
writeGitignore('.vscode/*\n.vscode/settings.json\n.vscode/extensions.json\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(
`.vscode/*\n.vscode/settings.json\n.vscode/extensions.json\n${vscodeUnignoreBlock}`,
);
});
it('appends the full block when settings are already unignored without the EOF block', () => {
writeVsCodeSettings();
writeGitignore('.vscode/*\n!.vscode/settings.json\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`.vscode/*\n!.vscode/settings.json\n${vscodeUnignoreBlock}`);
});
it('re-appends the full block when later ignore rules override generated config unignores', () => {
writeVsCodeSettings();
writeGitignore('!.vscode/settings.json\n!.vscode/extensions.json\n.vscode/*\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(
`!.vscode/settings.json\n!.vscode/extensions.json\n.vscode/*\n${vscodeUnignoreBlock}`,
);
});
it('appends VS Code config unignores even without a broad VS Code ignore', () => {
writeVsCodeSettings();
writeGitignore('dist\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`dist\n${vscodeUnignoreBlock}`);
});
it('does not create `.gitignore` when none exists', () => {
writeVsCodeSettings();
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(fs.existsSync(path.join(projectDir, '.gitignore'))).toBe(false);
});
it('does not change `.gitignore` when VS Code settings do not exist', () => {
const existing = '.vscode/*\n';
writeGitignore(existing);
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(existing);
});
it('terminates the last line before appending VS Code config unignores', () => {
writeVsCodeSettings();
writeGitignore('.vscode/*');
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(`.vscode/*\n${vscodeUnignoreBlock}`);
});
it('is idempotent', () => {
writeVsCodeSettings();
writeGitignore('.vscode/*\n');
ensureGitignoreVsCodeEditorConfigs(projectDir);
const afterFirstRun = gitignore();
ensureGitignoreVsCodeEditorConfigs(projectDir);
expect(gitignore()).toBe(afterFirstRun);
});
});
describe('renameFiles', () => {
let projectDir: string;
beforeEach(() => {
projectDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-rename-'));
});
afterEach(() => {
fs.rmSync(projectDir, { recursive: true, force: true });
});
function write(name: string, content: string): void {
fs.writeFileSync(path.join(projectDir, name), content);
}
function read(name: string): string {
return fs.readFileSync(path.join(projectDir, name), 'utf-8');
}
function exists(name: string): boolean {
return fs.existsSync(path.join(projectDir, name));
}
it('renames `_gitignore` to `.gitignore`', () => {
write('_gitignore', 'node_modules\n');
renameFiles(projectDir);
expect(exists('_gitignore')).toBe(false);
expect(read('.gitignore')).toBe('node_modules\n');
});
it('renames `_npmrc` and `_yarnrc.yml`', () => {
write('_npmrc', 'auto-install-peers=true\n');
write('_yarnrc.yml', 'nodeLinker: pnpm\n');
renameFiles(projectDir);
expect(exists('_npmrc')).toBe(false);
expect(exists('_yarnrc.yml')).toBe(false);
expect(read('.npmrc')).toBe('auto-install-peers=true\n');
expect(read('.yarnrc.yml')).toBe('nodeLinker: pnpm\n');
});
it('is a no-op when no source files exist', () => {
expect(() => renameFiles(projectDir)).not.toThrow();
expect(fs.readdirSync(projectDir)).toEqual([]);
});
it('leaves unmapped underscore files untouched', () => {
write('_foo', 'bar\n');
renameFiles(projectDir);
expect(read('_foo')).toBe('bar\n');
});
});