Skip to content

Commit 344ef77

Browse files
jbedarddgp1130
authored andcommitted
test: use common mockHome method
(cherry picked from commit d614a7e)
1 parent d0a0c59 commit 344ef77

File tree

4 files changed

+22
-54
lines changed

4 files changed

+22
-54
lines changed

tests/legacy-cli/e2e/tests/commands/completion/completion-prompt.ts

+1-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from 'fs';
22
import * as path from 'path';
33
import { env } from 'process';
44
import { getGlobalVariable } from '../../../utils/env';
5-
import { mktempd } from '../../../utils/utils';
5+
import { mockHome } from '../../../utils/utils';
66

77
import {
88
execAndCaptureError,
@@ -447,13 +447,3 @@ async function windowsTests(): Promise<void> {
447447
}
448448
});
449449
}
450-
451-
async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
452-
const tempHome = await mktempd('angular-cli-e2e-home-');
453-
454-
try {
455-
await cb(tempHome);
456-
} finally {
457-
await fs.rm(tempHome, { recursive: true, force: true });
458-
}
459-
}

tests/legacy-cli/e2e/tests/commands/completion/completion.ts

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promises as fs } from 'fs';
22
import * as path from 'path';
33
import { getGlobalVariable } from '../../../utils/env';
4-
import { mktempd } from '../../../utils/utils';
4+
import { mockHome } from '../../../utils/utils';
55
import {
66
execAndCaptureError,
77
execAndWaitForOutputToMatch,
@@ -29,7 +29,6 @@ export default async function () {
2929
{
3030
...process.env,
3131
'SHELL': '/bin/bash',
32-
'HOME': home,
3332
},
3433
);
3534

@@ -52,7 +51,6 @@ source <(ng completion script)
5251
{
5352
...process.env,
5453
'SHELL': '/usr/bin/zsh',
55-
'HOME': home,
5654
},
5755
);
5856

@@ -77,7 +75,6 @@ source <(ng completion script)
7775
{
7876
...process.env,
7977
'SHELL': '/bin/bash',
80-
'HOME': home,
8178
},
8279
);
8380

@@ -103,7 +100,6 @@ source <(ng completion script)
103100
{
104101
...process.env,
105102
'SHELL': '/bin/bash',
106-
'HOME': home,
107103
},
108104
);
109105

@@ -129,7 +125,6 @@ source <(ng completion script)
129125
{
130126
...process.env,
131127
'SHELL': '/bin/bash',
132-
'HOME': home,
133128
},
134129
);
135130

@@ -160,7 +155,6 @@ source <(ng completion script)
160155
{
161156
...process.env,
162157
'SHELL': '/bin/bash',
163-
'HOME': home,
164158
},
165159
);
166160

@@ -196,7 +190,6 @@ source <(ng completion script)
196190
{
197191
...process.env,
198192
'SHELL': '/usr/bin/zsh',
199-
'HOME': home,
200193
},
201194
);
202195

@@ -222,7 +215,6 @@ source <(ng completion script)
222215
{
223216
...process.env,
224217
'SHELL': '/usr/bin/zsh',
225-
'HOME': home,
226218
},
227219
);
228220

@@ -248,7 +240,6 @@ source <(ng completion script)
248240
{
249241
...process.env,
250242
'SHELL': '/usr/bin/zsh',
251-
'HOME': home,
252243
},
253244
);
254245

@@ -279,7 +270,6 @@ source <(ng completion script)
279270
{
280271
...process.env,
281272
'SHELL': '/usr/bin/zsh',
282-
'HOME': home,
283273
},
284274
);
285275

@@ -322,7 +312,6 @@ source <(ng completion script)
322312
const err = await execAndCaptureError('ng', ['completion'], {
323313
...process.env,
324314
SHELL: undefined,
325-
HOME: home,
326315
});
327316
if (!err.message.includes('`$SHELL` environment variable not set.')) {
328317
throw new Error(`Expected unset \`$SHELL\` error message, but got:\n\n${err.message}`);
@@ -334,7 +323,6 @@ source <(ng completion script)
334323
const err = await execAndCaptureError('ng', ['completion'], {
335324
...process.env,
336325
SHELL: '/usr/bin/unknown',
337-
HOME: home,
338326
});
339327
if (!err.message.includes('Unknown `$SHELL` environment variable')) {
340328
throw new Error(`Expected unknown \`$SHELL\` error message, but got:\n\n${err.message}`);
@@ -346,7 +334,6 @@ source <(ng completion script)
346334
const { stdout } = await execWithEnv('ng', ['completion'], {
347335
...process.env,
348336
'SHELL': '/usr/bin/zsh',
349-
'HOME': home,
350337
});
351338

352339
if (stdout.includes('there does not seem to be a global install of the Angular CLI')) {
@@ -373,7 +360,6 @@ source <(ng completion script)
373360
const { stdout } = await execWithEnv(localCliBinary, ['completion'], {
374361
...process.env,
375362
'SHELL': '/usr/bin/zsh',
376-
'HOME': home,
377363
});
378364

379365
if (stdout.includes('there does not seem to be a global install of the Angular CLI')) {
@@ -395,13 +381,3 @@ async function windowsTests(): Promise<void> {
395381
);
396382
}
397383
}
398-
399-
async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
400-
const tempHome = await mktempd('angular-cli-e2e-home-');
401-
402-
try {
403-
await cb(tempHome);
404-
} finally {
405-
await fs.rm(tempHome, { recursive: true, force: true });
406-
}
407-
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { promises as fs } from 'fs';
21
import { execWithEnv } from '../../utils/process';
2+
import { mockHome } from '../../utils/utils';
33

44
const ANALYTICS_PROMPT = /Would you like to share anonymous usage data/;
55

66
export default async function () {
77
// CLI should prompt for analytics permissions.
8-
await mockHome(async (home) => {
8+
await mockHome(async () => {
99
const { stdout } = await execWithEnv(
1010
'ng',
1111
['version'],
1212
{
1313
...process.env,
14-
HOME: home,
1514
NG_FORCE_TTY: '1',
1615
NG_FORCE_AUTOCOMPLETE: 'false',
1716
},
@@ -24,10 +23,9 @@ export default async function () {
2423
});
2524

2625
// CLI should skip analytics prompt with `NG_CLI_ANALYTICS=false`.
27-
await mockHome(async (home) => {
26+
await mockHome(async () => {
2827
const { stdout } = await execWithEnv('ng', ['version'], {
2928
...process.env,
30-
HOME: home,
3129
NG_FORCE_TTY: '1',
3230
NG_CLI_ANALYTICS: 'false',
3331
NG_FORCE_AUTOCOMPLETE: 'false',
@@ -39,10 +37,9 @@ export default async function () {
3937
});
4038

4139
// CLI should skip analytics prompt during `ng update`.
42-
await mockHome(async (home) => {
40+
await mockHome(async () => {
4341
const { stdout } = await execWithEnv('ng', ['update', '--help'], {
4442
...process.env,
45-
HOME: home,
4643
NG_FORCE_TTY: '1',
4744
NG_FORCE_AUTOCOMPLETE: 'false',
4845
});
@@ -54,13 +51,3 @@ export default async function () {
5451
}
5552
});
5653
}
57-
58-
async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
59-
const tempHome = await fs.mkdtemp('angular-cli-e2e-home-');
60-
61-
try {
62-
await cb(tempHome);
63-
} finally {
64-
await fs.rm(tempHome, { recursive: true, force: true });
65-
}
66-
}

tests/legacy-cli/e2e/utils/utils.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdtemp, realpath } from 'fs/promises';
1+
import { mkdtemp, realpath, rm } from 'fs/promises';
22
import { tmpdir } from 'os';
33
import path from 'path';
44

@@ -26,3 +26,18 @@ export function wait(msecs: number): Promise<void> {
2626
export async function mktempd(prefix: string): Promise<string> {
2727
return realpath(await mkdtemp(path.join(tmpdir(), prefix)));
2828
}
29+
30+
export async function mockHome(cb: (home: string) => Promise<void>): Promise<void> {
31+
const tempHome = await mktempd('angular-cli-e2e-home-');
32+
33+
const oldHome = process.env.HOME;
34+
process.env.HOME = tempHome;
35+
36+
try {
37+
await cb(tempHome);
38+
} finally {
39+
process.env.HOME = oldHome;
40+
41+
await rm(tempHome, { recursive: true, force: true });
42+
}
43+
}

0 commit comments

Comments
 (0)