|
1 |
| -import { execWithEnv, killAllProcesses, waitForAnyProcessOutputToMatch } from '../../utils/process'; |
2 |
| -import { expectToFail } from '../../utils/utils'; |
| 1 | +import { promises as fs } from 'fs'; |
| 2 | +import { execWithEnv } from '../../utils/process'; |
| 3 | + |
| 4 | +const ANALYTICS_PROMPT = /Would you like to share anonymous usage data/; |
3 | 5 |
|
4 | 6 | export default async function () {
|
5 |
| - try { |
6 |
| - // Execute a command with TTY force enabled |
7 |
| - execWithEnv('ng', ['version'], { |
8 |
| - ...process.env, |
9 |
| - NG_FORCE_TTY: '1', |
10 |
| - }); |
| 7 | + // CLI should prompt for analytics permissions. |
| 8 | + await mockHome(async (home) => { |
| 9 | + const { stdout } = await execWithEnv( |
| 10 | + 'ng', |
| 11 | + ['version'], |
| 12 | + { |
| 13 | + ...process.env, |
| 14 | + HOME: home, |
| 15 | + NG_FORCE_TTY: '1', |
| 16 | + }, |
| 17 | + 'y' /* stdin */, |
| 18 | + ); |
11 | 19 |
|
12 |
| - // Check if the prompt is shown |
13 |
| - await waitForAnyProcessOutputToMatch(/Would you like to share anonymous usage data/); |
14 |
| - } finally { |
15 |
| - killAllProcesses(); |
16 |
| - } |
| 20 | + if (!ANALYTICS_PROMPT.test(stdout)) { |
| 21 | + throw new Error('CLI did not prompt for analytics permission.'); |
| 22 | + } |
| 23 | + }); |
17 | 24 |
|
18 |
| - try { |
19 |
| - // Execute a command with TTY force enabled |
20 |
| - execWithEnv('ng', ['version'], { |
| 25 | + // CLI should skip analytics prompt with `NG_CLI_ANALYTICS=false`. |
| 26 | + await mockHome(async (home) => { |
| 27 | + const { stdout } = await execWithEnv('ng', ['version'], { |
21 | 28 | ...process.env,
|
| 29 | + HOME: home, |
22 | 30 | NG_FORCE_TTY: '1',
|
23 | 31 | NG_CLI_ANALYTICS: 'false',
|
24 | 32 | });
|
25 | 33 |
|
26 |
| - // Check if the prompt is shown |
27 |
| - await expectToFail(() => |
28 |
| - waitForAnyProcessOutputToMatch(/Would you like to share anonymous usage data/, 5), |
29 |
| - ); |
30 |
| - } finally { |
31 |
| - killAllProcesses(); |
32 |
| - } |
| 34 | + if (ANALYTICS_PROMPT.test(stdout)) { |
| 35 | + throw new Error('CLI prompted for analytics permission when it should be forced off.'); |
| 36 | + } |
| 37 | + }); |
33 | 38 |
|
34 |
| - // Should not show a prompt when using update |
35 |
| - try { |
36 |
| - // Execute a command with TTY force enabled |
37 |
| - execWithEnv('ng', ['update'], { |
| 39 | + // CLI should skip analytics prompt during `ng update`. |
| 40 | + await mockHome(async (home) => { |
| 41 | + const { stdout } = await execWithEnv('ng', ['update', '--help'], { |
38 | 42 | ...process.env,
|
| 43 | + HOME: home, |
39 | 44 | NG_FORCE_TTY: '1',
|
40 | 45 | });
|
41 | 46 |
|
42 |
| - // Check if the prompt is shown |
43 |
| - await expectToFail(() => |
44 |
| - waitForAnyProcessOutputToMatch(/Would you like to share anonymous usage data/, 5), |
45 |
| - ); |
| 47 | + if (ANALYTICS_PROMPT.test(stdout)) { |
| 48 | + throw new Error( |
| 49 | + 'CLI prompted for analytics permission during an update where it should not' + ' have.', |
| 50 | + ); |
| 51 | + } |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +async function mockHome(cb: (home: string) => Promise<void>): Promise<void> { |
| 56 | + const tempHome = await fs.mkdtemp('angular-cli-e2e-home-'); |
| 57 | + |
| 58 | + try { |
| 59 | + await cb(tempHome); |
46 | 60 | } finally {
|
47 |
| - killAllProcesses(); |
| 61 | + await fs.rm(tempHome, { recursive: true, force: true }); |
48 | 62 | }
|
49 | 63 | }
|
0 commit comments