Skip to content

Commit

Permalink
improve help tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Feb 28, 2024
1 parent 2829975 commit 09058e5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 24 deletions.
17 changes: 11 additions & 6 deletions test/_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,29 @@ export const _verifyCli = (baseFixture = defaultFixture) => test.macro(
async (t, {fixture = baseFixture, args, execaOptions, expected, error}) => {
const assertions = await t.try(async tt => {
const arguments_ = args ? args.split(' ') : [];
const {stdout, stderr, exitCode} = await spawnFixture(fixture, arguments_, {reject: false, ...execaOptions});
const {all: output, exitCode} = await spawnFixture(fixture, arguments_, {reject: false, all: true, ...execaOptions});
tt.log('args:', arguments_);

if (error) {
tt.log(`error (code ${exitCode}):\n`, stderr);
tt.log(`error (code ${exitCode}):\n`, output);

if (typeof error === 'string') {
tt.is(stderr, error);
tt.is(output, error);
tt.is(exitCode, 2);
} else {
const error_ = error.clean ? stackToErrorMessage(stderr) : stderr;
const error_ = error.clean ? stackToErrorMessage(output) : output;

tt.is(error_, error.message);
tt.is(exitCode, error.code);
}
} else {
tt.log('output:\n', stdout);
tt.is(stdout, expected);
tt.log('output:\n', output);

if (expected) {
tt.is(output, expected);
} else {
tt.pass();
}
}
});

Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/help/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node
import meow from '../../../source/index.js';

const cli = meow({
importMeta: import.meta,
help: 'foo',
flags: {
showHelp: {type: 'boolean'},
code: {type: 'number'},
},
});

const {code} = cli.flags;

if (cli.flags.showHelp) {
if (code !== undefined) {
cli.showHelp(code);
}

cli.showHelp();
}
6 changes: 6 additions & 0 deletions test/fixtures/help/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "foo",
"version": "1.0.0",
"bin": "./fixture.js",
"type": "module"
}
61 changes: 43 additions & 18 deletions test/options/help.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import test from 'ava';
import indentString from 'indent-string';
import meow from '../../source/index.js';
import {spawnFixture, stripIndent, stripIndentTrim} from '../_utils.js';
import {
_verifyCli,
spawnFixture,

Check failure on line 6 in test/options/help.js

View workflow job for this annotation

GitHub Actions / Node.js 21

'spawnFixture' is defined but never used. Allowed unused vars must match /^_/u.

Check failure on line 6 in test/options/help.js

View workflow job for this annotation

GitHub Actions / Node.js 20

'spawnFixture' is defined but never used. Allowed unused vars must match /^_/u.

Check failure on line 6 in test/options/help.js

View workflow job for this annotation

GitHub Actions / Node.js 18

'spawnFixture' is defined but never used. Allowed unused vars must match /^_/u.
stripIndent,
stripIndentTrim,
} from '../_utils.js';

const importMeta = import.meta;

const verifyHelp = test.macro(async (t, {cli: cliArguments, args, expected}) => {
const verifyCli = _verifyCli();

const verifyHelp = test.macro(async (t, {cli: cliArguments, expected}) => {
const assertions = await t.try(async tt => {
if (args) {
const {stdout} = await spawnFixture(args.split(' '));

tt.log('help text:\n', stdout);
tt.is(stdout, expected);
} else {
const cli = Array.isArray(cliArguments)
? meow(cliArguments.at(0), {importMeta, ...cliArguments.at(1)})
: meow({importMeta, ...cliArguments});

tt.log('help text:\n', cli.help);
tt.is(cli.help, expected);
}
const cli = Array.isArray(cliArguments)
? meow(cliArguments.at(0), {importMeta, ...cliArguments.at(1)})
: meow({importMeta, ...cliArguments});

tt.log('help text:\n', cli.help);
tt.is(cli.help, expected);
});

assertions.commit({retainLogs: !assertions.passed});
Expand All @@ -33,12 +33,12 @@ test('support help shortcut', verifyHelp, {
expected: indentString('\nCLI app helper\n\nunicorn\ncat\n', 2),
});

test('spawn cli and show help screen', verifyHelp, {
test('spawn cli and show help screen', verifyCli, {
args: '--help',
expected: indentString('\nCustom description\n\nUsage\n foo <input>\n\n', 2),
});

test('spawn cli and disabled autoHelp', verifyHelp, {
test('spawn cli and disabled autoHelp', verifyCli, {
args: '--help --no-auto-help',
expected: stripIndentTrim`
help
Expand All @@ -48,7 +48,7 @@ test('spawn cli and disabled autoHelp', verifyHelp, {
`,
});

test('spawn cli and not show help', verifyHelp, {
test('spawn cli and not show help', verifyCli, {
args: '--help=all',
expected: stripIndentTrim`
help
Expand Down Expand Up @@ -110,3 +110,28 @@ test('no description and no indentation', verifyHelp, {
cat
`,
});

test('exits with code 0 by default', verifyCli, {
args: '--help',
});

test('showHelp exits with code 2 by default', verifyCli, {
fixture: 'help/fixture.js',
args: '--show-help',
error: {
message: stripIndent`
foo
`,
code: 2,
},
});

test('showHelp exits with given code', verifyCli, {
fixture: 'help/fixture.js',
args: '--show-help --code=0',
expected: stripIndent`
foo
`,
});

0 comments on commit 09058e5

Please sign in to comment.