|
| 1 | +import { promises as fs } from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { env } from 'process'; |
| 5 | +import { execWithEnv } from '../../utils/process'; |
| 6 | + |
| 7 | +const AUTOCOMPLETION_PROMPT = /Would you like to enable autocompletion\?/; |
| 8 | +const DEFAULT_ENV = Object.freeze({ |
| 9 | + ...env, |
| 10 | + // Shell should be mocked for each test that cares about it. |
| 11 | + SHELL: '/bin/bash', |
| 12 | + // Even if the actual test process is run on CI, we're testing user flows which aren't on CI. |
| 13 | + CI: undefined, |
| 14 | + // Tests run on CI technically don't have a TTY, but the autocompletion prompt requires it, so we |
| 15 | + // force a TTY by default. |
| 16 | + NG_FORCE_TTY: '1', |
| 17 | + // Analytics wants to prompt for a first command as well, but we don't care about that here. |
| 18 | + NG_CLI_ANALYTICS: 'false', |
| 19 | +}); |
| 20 | + |
| 21 | +export default async function () { |
| 22 | + // Sets up autocompletion after user accepts a prompt from any command. |
| 23 | + await mockHome(async (home) => { |
| 24 | + const bashrc = path.join(home, '.bashrc'); |
| 25 | + await fs.writeFile(bashrc, `# Other content...`); |
| 26 | + |
| 27 | + const { stdout } = await execWithEnv( |
| 28 | + 'ng', |
| 29 | + ['version'], |
| 30 | + { |
| 31 | + ...DEFAULT_ENV, |
| 32 | + SHELL: '/bin/bash', |
| 33 | + HOME: home, |
| 34 | + }, |
| 35 | + 'y' /* stdin: accept prompt */, |
| 36 | + ); |
| 37 | + |
| 38 | + if (!AUTOCOMPLETION_PROMPT.test(stdout)) { |
| 39 | + throw new Error('CLI execution did not prompt for autocompletion setup when it should have.'); |
| 40 | + } |
| 41 | + |
| 42 | + const bashrcContents = await fs.readFile(bashrc, 'utf-8'); |
| 43 | + if (!bashrcContents.includes('source <(ng completion script)')) { |
| 44 | + throw new Error( |
| 45 | + 'Autocompletion was *not* added to `~/.bashrc` after accepting the setup' + ' prompt.', |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + if (!stdout.includes('Appended `source <(ng completion script)`')) { |
| 50 | + throw new Error('CLI did not print that it successfully set up autocompletion.'); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + // Does nothing if the user rejects the autocompletion prompt. |
| 55 | + await mockHome(async (home) => { |
| 56 | + const bashrc = path.join(home, '.bashrc'); |
| 57 | + await fs.writeFile(bashrc, `# Other content...`); |
| 58 | + |
| 59 | + const { stdout } = await execWithEnv( |
| 60 | + 'ng', |
| 61 | + ['version'], |
| 62 | + { |
| 63 | + ...DEFAULT_ENV, |
| 64 | + SHELL: '/bin/bash', |
| 65 | + HOME: home, |
| 66 | + }, |
| 67 | + 'n' /* stdin: reject prompt */, |
| 68 | + ); |
| 69 | + |
| 70 | + if (!AUTOCOMPLETION_PROMPT.test(stdout)) { |
| 71 | + throw new Error('CLI execution did not prompt for autocompletion setup when it should have.'); |
| 72 | + } |
| 73 | + |
| 74 | + const bashrcContents = await fs.readFile(bashrc, 'utf-8'); |
| 75 | + if (bashrcContents.includes('ng completion')) { |
| 76 | + throw new Error( |
| 77 | + 'Autocompletion was incorrectly added to `~/.bashrc` after refusing the setup' + ' prompt.', |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + if (stdout.includes('Appended `source <(ng completion script)`')) { |
| 82 | + throw new Error( |
| 83 | + 'CLI printed that it successfully set up autocompletion when it actually' + " didn't.", |
| 84 | + ); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + // Does *not* prompt user for CI executions. |
| 89 | + { |
| 90 | + const { stdout } = await execWithEnv('ng', ['version'], { |
| 91 | + ...DEFAULT_ENV, |
| 92 | + CI: 'true', |
| 93 | + NG_FORCE_TTY: undefined, |
| 94 | + }); |
| 95 | + |
| 96 | + if (AUTOCOMPLETION_PROMPT.test(stdout)) { |
| 97 | + throw new Error('CI execution prompted for autocompletion setup but should not have.'); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Does *not* prompt user for non-TTY executions. |
| 102 | + { |
| 103 | + const { stdout } = await execWithEnv('ng', ['version'], { |
| 104 | + ...DEFAULT_ENV, |
| 105 | + NG_FORCE_TTY: 'false', |
| 106 | + }); |
| 107 | + |
| 108 | + if (AUTOCOMPLETION_PROMPT.test(stdout)) { |
| 109 | + throw new Error('Non-TTY execution prompted for autocompletion setup but should not have.'); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Does *not* prompt user for executions without a `$HOME`. |
| 114 | + { |
| 115 | + const { stdout } = await execWithEnv('ng', ['version'], { |
| 116 | + ...DEFAULT_ENV, |
| 117 | + HOME: undefined, |
| 118 | + }); |
| 119 | + |
| 120 | + if (AUTOCOMPLETION_PROMPT.test(stdout)) { |
| 121 | + throw new Error( |
| 122 | + 'Execution without a `$HOME` value prompted for autocompletion setup but' + |
| 123 | + ' should not have.', |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Does *not* prompt user for executions without a `$SHELL`. |
| 129 | + { |
| 130 | + const { stdout } = await execWithEnv('ng', ['version'], { |
| 131 | + ...DEFAULT_ENV, |
| 132 | + SHELL: undefined, |
| 133 | + }); |
| 134 | + |
| 135 | + if (AUTOCOMPLETION_PROMPT.test(stdout)) { |
| 136 | + throw new Error( |
| 137 | + 'Execution without a `$SHELL` value prompted for autocompletion setup but' + |
| 138 | + ' should not have.', |
| 139 | + ); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // Does *not* prompt user for executions from unknown shells. |
| 144 | + { |
| 145 | + const { stdout } = await execWithEnv('ng', ['version'], { |
| 146 | + ...DEFAULT_ENV, |
| 147 | + SHELL: '/usr/bin/unknown', |
| 148 | + }); |
| 149 | + |
| 150 | + if (AUTOCOMPLETION_PROMPT.test(stdout)) { |
| 151 | + throw new Error( |
| 152 | + 'Execution with an unknown `$SHELL` value prompted for autocompletion setup' + |
| 153 | + ' but should not have.', |
| 154 | + ); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Does *not* prompt user when an RC file already uses `ng completion`. |
| 159 | + await mockHome(async (home) => { |
| 160 | + await fs.writeFile( |
| 161 | + path.join(home, '.bashrc'), |
| 162 | + ` |
| 163 | +# Some stuff... |
| 164 | +
|
| 165 | +source <(ng completion script) |
| 166 | +
|
| 167 | +# Some other stuff... |
| 168 | + `.trim(), |
| 169 | + ); |
| 170 | + |
| 171 | + const { stdout } = await execWithEnv('ng', ['version'], { |
| 172 | + ...DEFAULT_ENV, |
| 173 | + SHELL: '/bin/bash', |
| 174 | + HOME: home, |
| 175 | + }); |
| 176 | + |
| 177 | + if (AUTOCOMPLETION_PROMPT.test(stdout)) { |
| 178 | + throw new Error( |
| 179 | + "Execution with an existing `ng completion` line in the user's RC file" + |
| 180 | + ' prompted for autocompletion setup but should not have.', |
| 181 | + ); |
| 182 | + } |
| 183 | + }); |
| 184 | +} |
| 185 | + |
| 186 | +async function mockHome(cb: (home: string) => Promise<void>): Promise<void> { |
| 187 | + const tempHome = await fs.mkdtemp(path.join(os.tmpdir(), 'angular-cli-e2e-home-')); |
| 188 | + |
| 189 | + try { |
| 190 | + await cb(tempHome); |
| 191 | + } finally { |
| 192 | + await fs.rm(tempHome, { recursive: true, force: true }); |
| 193 | + } |
| 194 | +} |
0 commit comments