|
| 1 | +import path from 'node:path'; |
| 2 | +import {fileURLToPath} from 'node:url'; |
| 3 | +import test from 'ava'; |
| 4 | +import indentString from 'indent-string'; |
| 5 | +import {execa} from 'execa'; |
| 6 | +import {readPackage} from 'read-pkg'; |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const fixtureAllowUnknownFlags = path.join(__dirname, 'fixtures', 'fixture-allow-unknown-flags.js'); |
| 10 | + |
| 11 | +test('spawn CLI and test specifying unknown flags', async t => { |
| 12 | + const error = await t.throwsAsync( |
| 13 | + execa(fixtureAllowUnknownFlags, ['--foo', 'bar', '--unspecified-a', '--unspecified-b', 'input-is-allowed']), |
| 14 | + { |
| 15 | + message: /^Command failed with exit code 2/, |
| 16 | + }, |
| 17 | + ); |
| 18 | + const {stderr} = error; |
| 19 | + t.regex(stderr, /Unknown flags/); |
| 20 | + t.regex(stderr, /--unspecified-a/); |
| 21 | + t.regex(stderr, /--unspecified-b/); |
| 22 | + t.notRegex(stderr, /input-is-allowed/); |
| 23 | +}); |
| 24 | + |
| 25 | +test('spawn CLI and test specifying known flags', async t => { |
| 26 | + const {stdout} = await execa(fixtureAllowUnknownFlags, ['--foo', 'bar']); |
| 27 | + t.is(stdout, 'bar'); |
| 28 | +}); |
| 29 | + |
| 30 | +test('spawn CLI and test help as a known flag', async t => { |
| 31 | + const {stdout} = await execa(fixtureAllowUnknownFlags, ['--help']); |
| 32 | + t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>\n\n', 2)); |
| 33 | +}); |
| 34 | + |
| 35 | +test('spawn CLI and test version as a known flag', async t => { |
| 36 | + const pkg = await readPackage(); |
| 37 | + const {stdout} = await execa(fixtureAllowUnknownFlags, ['--version']); |
| 38 | + t.is(stdout, pkg.version); |
| 39 | +}); |
| 40 | + |
| 41 | +test('spawn CLI and test help as an unknown flag', async t => { |
| 42 | + const error = await t.throwsAsync( |
| 43 | + execa(fixtureAllowUnknownFlags, ['--help', '--no-auto-help']), |
| 44 | + { |
| 45 | + message: /^Command failed with exit code 2/, |
| 46 | + }, |
| 47 | + ); |
| 48 | + const {stderr} = error; |
| 49 | + t.regex(stderr, /Unknown flag/); |
| 50 | + t.regex(stderr, /--help/); |
| 51 | +}); |
| 52 | + |
| 53 | +test('spawn CLI and test version as an unknown flag', async t => { |
| 54 | + const error = await t.throwsAsync( |
| 55 | + execa(fixtureAllowUnknownFlags, ['--version', '--no-auto-version']), |
| 56 | + { |
| 57 | + message: /^Command failed with exit code 2/, |
| 58 | + }, |
| 59 | + ); |
| 60 | + const {stderr} = error; |
| 61 | + t.regex(stderr, /Unknown flag/); |
| 62 | + t.regex(stderr, /--version/); |
| 63 | +}); |
0 commit comments