-
Notifications
You must be signed in to change notification settings - Fork 12k
/
completion.ts
407 lines (361 loc) · 12.4 KB
/
completion.ts
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import { promises as fs } from 'fs';
import * as path from 'path';
import { getGlobalVariable } from '../../../utils/env';
import { mktempd } from '../../../utils/utils';
import {
execAndCaptureError,
execAndWaitForOutputToMatch,
execWithEnv,
silentNpm,
} from '../../../utils/process';
const testRegistry = getGlobalVariable('package-registry');
export default async function () {
// Windows Cmd and Powershell do not support autocompletion. Run a different set of tests to
// confirm autocompletion fails gracefully.
if (process.platform === 'win32') {
await windowsTests();
return;
}
// Generates new `.bashrc` file.
await mockHome(async (home) => {
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);
const rcContents = await fs.readFile(path.join(home, '.bashrc'), 'utf-8');
const expected = `
# Load Angular CLI autocompletion.
source <(ng completion script)
`.trim();
if (!rcContents.includes(expected)) {
throw new Error(`~/.bashrc does not contain autocompletion script. Contents:\n${rcContents}`);
}
});
// Generates new `.zshrc` file.
await mockHome(async (home) => {
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);
const rcContents = await fs.readFile(path.join(home, '.zshrc'), 'utf-8');
const expected = `
# Load Angular CLI autocompletion.
source <(ng completion script)
`.trim();
if (!rcContents.includes(expected)) {
throw new Error(`~/.zshrc does not contain autocompletion script. Contents:\n${rcContents}`);
}
});
// Appends to existing `.bashrc` file.
await mockHome(async (home) => {
const bashrc = path.join(home, '.bashrc');
await fs.writeFile(bashrc, '# Other commands...');
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);
const rcContents = await fs.readFile(bashrc, 'utf-8');
const expected = `# Other commands...
# Load Angular CLI autocompletion.
source <(ng completion script)
`;
if (rcContents !== expected) {
throw new Error(`~/.bashrc does not match expectation. Contents:\n${rcContents}`);
}
});
// Appends to existing `.bash_profile` file.
await mockHome(async (home) => {
const bashProfile = path.join(home, '.bash_profile');
await fs.writeFile(bashProfile, '# Other commands...');
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);
const rcContents = await fs.readFile(bashProfile, 'utf-8');
const expected = `# Other commands...
# Load Angular CLI autocompletion.
source <(ng completion script)
`;
if (rcContents !== expected) {
throw new Error(`~/.bash_profile does not match expectation. Contents:\n${rcContents}`);
}
});
// Appends to existing `.profile` file (using Bash).
await mockHome(async (home) => {
const profile = path.join(home, '.profile');
await fs.writeFile(profile, '# Other commands...');
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);
const rcContents = await fs.readFile(profile, 'utf-8');
const expected = `# Other commands...
# Load Angular CLI autocompletion.
source <(ng completion script)
`;
if (rcContents !== expected) {
throw new Error(`~/.profile does not match expectation. Contents:\n${rcContents}`);
}
});
// Bash shell prefers `.bashrc`.
await mockHome(async (home) => {
const bashrc = path.join(home, '.bashrc');
await fs.writeFile(bashrc, '# `.bashrc` commands...');
const bashProfile = path.join(home, '.bash_profile');
await fs.writeFile(bashProfile, '# `.bash_profile` commands...');
const profile = path.join(home, '.profile');
await fs.writeFile(profile, '# `.profile` commands...');
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/bin/bash',
'HOME': home,
},
);
const bashrcContents = await fs.readFile(bashrc, 'utf-8');
const bashrcExpected = `# \`.bashrc\` commands...
# Load Angular CLI autocompletion.
source <(ng completion script)
`;
if (bashrcContents !== bashrcExpected) {
throw new Error(`~/.bashrc does not match expectation. Contents:\n${bashrcContents}`);
}
const bashProfileContents = await fs.readFile(bashProfile, 'utf-8');
if (bashProfileContents !== '# `.bash_profile` commands...') {
throw new Error(
`~/.bash_profile does not match expectation. Contents:\n${bashProfileContents}`,
);
}
const profileContents = await fs.readFile(profile, 'utf-8');
if (profileContents !== '# `.profile` commands...') {
throw new Error(`~/.profile does not match expectation. Contents:\n${profileContents}`);
}
});
// Appends to existing `.zshrc` file.
await mockHome(async (home) => {
const zshrc = path.join(home, '.zshrc');
await fs.writeFile(zshrc, '# Other commands...');
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);
const rcContents = await fs.readFile(zshrc, 'utf-8');
const expected = `# Other commands...
# Load Angular CLI autocompletion.
source <(ng completion script)
`;
if (rcContents !== expected) {
throw new Error(`~/.zshrc does not match expectation. Contents:\n${rcContents}`);
}
});
// Appends to existing `.zsh_profile` file.
await mockHome(async (home) => {
const zshProfile = path.join(home, '.zsh_profile');
await fs.writeFile(zshProfile, '# Other commands...');
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);
const rcContents = await fs.readFile(zshProfile, 'utf-8');
const expected = `# Other commands...
# Load Angular CLI autocompletion.
source <(ng completion script)
`;
if (rcContents !== expected) {
throw new Error(`~/.zsh_profile does not match expectation. Contents:\n${rcContents}`);
}
});
// Appends to existing `.profile` file (using Zsh).
await mockHome(async (home) => {
const profile = path.join(home, '.profile');
await fs.writeFile(profile, '# Other commands...');
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);
const rcContents = await fs.readFile(profile, 'utf-8');
const expected = `# Other commands...
# Load Angular CLI autocompletion.
source <(ng completion script)
`;
if (rcContents !== expected) {
throw new Error(`~/.profile does not match expectation. Contents:\n${rcContents}`);
}
});
// Zsh prefers `.zshrc`.
await mockHome(async (home) => {
const zshrc = path.join(home, '.zshrc');
await fs.writeFile(zshrc, '# `.zshrc` commands...');
const zshProfile = path.join(home, '.zsh_profile');
await fs.writeFile(zshProfile, '# `.zsh_profile` commands...');
const profile = path.join(home, '.profile');
await fs.writeFile(profile, '# `.profile` commands...');
await execAndWaitForOutputToMatch(
'ng',
['completion'],
/Appended `source <\(ng completion script\)`/,
{
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
},
);
const zshrcContents = await fs.readFile(zshrc, 'utf-8');
const zshrcExpected = `# \`.zshrc\` commands...
# Load Angular CLI autocompletion.
source <(ng completion script)
`;
if (zshrcContents !== zshrcExpected) {
throw new Error(`~/.zshrc does not match expectation. Contents:\n${zshrcContents}`);
}
const zshProfileContents = await fs.readFile(zshProfile, 'utf-8');
if (zshProfileContents !== '# `.zsh_profile` commands...') {
throw new Error(
`~/.zsh_profile does not match expectation. Contents:\n${zshProfileContents}`,
);
}
const profileContents = await fs.readFile(profile, 'utf-8');
if (profileContents !== '# `.profile` commands...') {
throw new Error(`~/.profile does not match expectation. Contents:\n${profileContents}`);
}
});
// Fails for no `$HOME` directory.
{
const err = await execAndCaptureError('ng', ['completion'], {
...process.env,
SHELL: '/bin/bash',
HOME: undefined,
});
if (!err.message.includes('`$HOME` environment variable not set.')) {
throw new Error(`Expected unset \`$HOME\` error message, but got:\n\n${err.message}`);
}
}
// Fails for no `$SHELL`.
await mockHome(async (home) => {
const err = await execAndCaptureError('ng', ['completion'], {
...process.env,
SHELL: undefined,
HOME: home,
});
if (!err.message.includes('`$SHELL` environment variable not set.')) {
throw new Error(`Expected unset \`$SHELL\` error message, but got:\n\n${err.message}`);
}
});
// Fails for unknown `$SHELL`.
await mockHome(async (home) => {
const err = await execAndCaptureError('ng', ['completion'], {
...process.env,
SHELL: '/usr/bin/unknown',
HOME: home,
});
if (!err.message.includes('Unknown `$SHELL` environment variable')) {
throw new Error(`Expected unknown \`$SHELL\` error message, but got:\n\n${err.message}`);
}
});
// Does *not* warn when a global CLI install is present on the system.
await mockHome(async (home) => {
const { stdout } = await execWithEnv('ng', ['completion'], {
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
});
if (stdout.includes('there does not seem to be a global install of the Angular CLI')) {
throw new Error(`CLI warned about missing global install, but one should exist.`);
}
});
// Warns when a global CLI install is *not* present on the system.
await mockHome(async (home) => {
try {
// Temporarily uninstall the global CLI binary from the system.
await silentNpm(['uninstall', '--global', '@angular/cli', `--registry=${testRegistry}`]);
// Setup a fake project directory with a local install of the CLI.
const projectDir = path.join(home, 'project');
await fs.mkdir(projectDir);
await silentNpm(['init', '-y', `--registry=${testRegistry}`], { cwd: projectDir });
await silentNpm(['install', '@angular/cli', `--registry=${testRegistry}`], {
cwd: projectDir,
});
// Invoke the local CLI binary.
const localCliBinary = path.join(projectDir, 'node_modules', '.bin', 'ng');
const { stdout } = await execWithEnv(localCliBinary, ['completion'], {
...process.env,
'SHELL': '/usr/bin/zsh',
'HOME': home,
});
if (stdout.includes('there does not seem to be a global install of the Angular CLI')) {
throw new Error(`CLI warned about missing global install, but one should exist.`);
}
} finally {
// Reinstall global CLI for remainder of the tests.
await silentNpm(['install', '--global', '@angular/cli', `--registry=${testRegistry}`]);
}
});
}
async function windowsTests(): Promise<void> {
// Should fail with a clear error message.
const err = await execAndCaptureError('ng', ['completion']);
if (!err.message.includes("Cmd and Powershell don't support command autocompletion")) {
throw new Error(
`Expected Windows autocompletion to fail with custom error, but got:\n\n${err.message}`,
);
}
}
async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
const tempHome = await mktempd('angular-cli-e2e-home-');
try {
await cb(tempHome);
} finally {
await fs.rm(tempHome, { recursive: true, force: true });
}
}