Skip to content

Commit

Permalink
Improve tests (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Feb 28, 2024
1 parent c97c031 commit 7b9e739
Show file tree
Hide file tree
Showing 39 changed files with 1,361 additions and 1,137 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
fail-fast: false
matrix:
node-version:
- 21
- 20
- 18
- 16
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand Down
24 changes: 0 additions & 24 deletions estest/index.js

This file was deleted.

5 changes: 0 additions & 5 deletions estest/package.json

This file was deleted.

22 changes: 9 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,29 @@
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/minimist": "^1.2.5",
"ava": "^6.0.1",
"camelcase-keys": "^9.1.2",
"ava": "^6.1.1",
"camelcase-keys": "^9.1.3",
"common-tags": "^2.0.0-alpha.1",
"decamelize": "^6.0.0",
"decamelize-keys": "^2.0.1",
"delete_comments": "^0.0.2",
"execa": "^8.0.1",
"globby": "^14.0.0",
"globby": "^14.0.1",
"indent-string": "^5.0.0",
"minimist-options": "4.1.0",
"normalize-package-data": "^6.0.0",
"read-package-up": "^11.0.0",
"read-pkg": "^9.0.1",
"redent": "^4.0.0",
"rollup": "^4.9.2",
"rollup": "^4.12.0",
"rollup-plugin-dts": "^6.1.0",
"rollup-plugin-license": "^3.2.0",
"stack-utils": "^2.0.6",
"trim-newlines": "^5.0.0",
"tsd": "^0.30.3",
"type-fest": "^4.9.0",
"typescript": "^5.3.3",
"xo": "^0.56.0",
"tsd": "^0.30.7",
"type-fest": "^4.10.3",
"typescript": "~5.3.3",
"xo": "^0.57.0",
"yargs-parser": "^21.1.1"
},
"xo": {
Expand All @@ -96,10 +97,5 @@
"ignores": [
"build"
]
},
"ava": {
"files": [
"test/*"
]
}
}
5 changes: 1 addition & 4 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ const buildResult = (options, parserOptions) => {

normalizePackageData(package_);

let {description} = options;
if (!description && description !== false) {
({description} = package_);
}
let description = options.description ?? package_.description;

description &&= help ? redent(`\n${description}\n`, options.helpIndent) : `\n${description}`;
help = `${description || ''}${help}\n`;
Expand Down
2 changes: 1 addition & 1 deletion source/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const buildParserFlags = ({flags, booleanDefault}) => {

if (flag.isMultiple) {
flag.type = flag.type ? `${flag.type}-array` : 'array';
flag.default = flag.default ?? [];
flag.default ??= [];
delete flag.isMultiple;
}

Expand Down
64 changes: 55 additions & 9 deletions test/_utils.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,70 @@
import path from 'node:path';
/* eslint-disable ava/no-ignored-test-files */
import {fileURLToPath} from 'node:url';
import test from 'ava';
import {execa} from 'execa';
import {readPackage} from 'read-pkg';
import {createTag, stripIndentTransformer, trimResultTransformer} from 'common-tags';
import StackUtils from 'stack-utils';

export const __dirname = path.dirname(fileURLToPath(import.meta.url));
export const defaultFixture = 'fixture.js';

const getFixture = fixture => path.join(__dirname, 'fixtures', fixture);
const getFixture = fixture => fileURLToPath(new URL(`fixtures/${fixture}`, import.meta.url));

export const spawnFixture = async (fixture = 'fixture.js', args = []) => {
// Allow calling with args first
export const spawnFixture = async (fixture = defaultFixture, arguments_ = [], options = {}) => {
// Allow calling with arguments first
if (Array.isArray(fixture)) {
args = fixture;
fixture = 'fixture.js';
arguments_ = fixture;
fixture = defaultFixture;
}

return execa(getFixture(fixture), args);
return execa(getFixture(fixture), arguments_, options);
};

export {stripIndent} from 'common-tags';

// Use old behavior prior to zspecza/common-tags#165
export const stripIndent = createTag(
export const stripIndentTrim = createTag(
stripIndentTransformer(),
trimResultTransformer(),
);

export const meowPackage = await readPackage();
export const meowVersion = meowPackage.version;

const stackUtils = new StackUtils();

export const stackToErrorMessage = stack => stackUtils.clean(stack).split('\n').at(0);

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 {all: output, exitCode} = await spawnFixture(fixture, arguments_, {reject: false, all: true, ...execaOptions});
tt.log('args:', arguments_);

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

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

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

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

assertions.commit({retainLogs: !assertions.passed});
},
);
68 changes: 0 additions & 68 deletions test/allow-unknown-flags.js

This file was deleted.

12 changes: 6 additions & 6 deletions test/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import test from 'ava';
import {readPackage} from 'read-pkg';
import meow from '../build/index.js';
import {spawnFixture} from './_utils.js';
import {_verifyCli, meowVersion} from './_utils.js';

const verifyCli = _verifyCli();

test('main', t => {
const cli = meow(`
Expand Down Expand Up @@ -39,8 +40,7 @@ test('main', t => {
});
});

test('spawn cli and show version', async t => {
const pkg = await readPackage();
const {stdout} = await spawnFixture(['--version']);
t.is(stdout, pkg.version);
test('spawn cli and show version', verifyCli, {
args: '--version',
expected: meowVersion,
});
Loading

0 comments on commit 7b9e739

Please sign in to comment.