Skip to content

Commit

Permalink
Initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Walter committed Jan 27, 2019
1 parent 30a58b8 commit 2632a50
Show file tree
Hide file tree
Showing 8 changed files with 4,829 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import execa from 'execa'

export default function execaHelper (cli, throwOnStdErr = true) {
return async function withCli (t, run) {
await run(t, async (...params) => {
const result = await execa(cli, params)

// Throw an error if stderr has content so that the test fails.
if (throwOnStdErr && result.stderr) {
throw new Error(result.stderr)
}

return result
})
}
}
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@ianwalter/execa-helper",
"description": "An AVA helper that makes it easier to test CLIs using Execa",
"module": "dist/execa-helper.m.js",
"main": "dist/execa-helper.js",
"scripts": {
"lint": "eslint index.js test.js",
"pretest": "dist",
"test": "ava"
},
"dependencies": {
"execa": "^1.0.0"
},
"devDependencies": {
"@ianwalter/dist": "^1.4.0",
"ava": "^1.1.0",
"eslint": "^5.12.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-html": "^5.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"meow": "^5.0.0",
"np": "^4.0.2"
},
"eslintConfig": {
"root": true,
"extends": [
"eslint:recommended",
"standard"
]
}
}
30 changes: 30 additions & 0 deletions test/cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

const meow = require('meow')

const cli = meow(
`
Usage
$ cli <options>
Options
--from, -f Author of the greeting
--greeting, -g Output a greeting
Examples
$ cli --from Ian --greeting Hello World!
[Ian]: Hello World!
`,
{
flags: {
from: { type: 'string', alias: 'f' },
greeting: { type: 'string', alias: 'g' }
}
}
)

if (!cli.flags.from) {
console.error('No author specified!')
}

console.log(`[${cli.flags.from}]: ${cli.flags.greeting}`)
21 changes: 21 additions & 0 deletions test/snapshots/test.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Snapshot report for `test/test.js`

The actual snapshot is saved in `test.js.snap`.

Generated by [AVA](https://ava.li).

## doesn't throw error when configured

> Snapshot 1
'[undefined]: Hello World!'

> Snapshot 2
'No author specified!'

## returns stdout when stderr has no content

> Snapshot 1
'[Ian]: Hello World!'
Binary file added test/snapshots/test.js.snap
Binary file not shown.
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import test from 'ava'
import execaHelper from '..'

const withCli = execaHelper('./test/cli')
const withNoThrowCli = execaHelper('./test/cli', false)

test('throws error when stderr has content', withCli, async (t, cli) => {
await t.throwsAsync(async () => cli('--greeting', 'Hello World!'))
})

test('returns stdout when stderr has no content', withCli, async (t, cli) => {
const { stdout } = await cli('--from', 'Ian', '--greeting', 'Hello World!')
t.snapshot(stdout)
})

test(`doesn't throw error when configured`, withNoThrowCli, async (t, cli) => {
const { stdout, stderr } = await cli('--greeting', 'Hello World!')
t.snapshot(stdout)
t.snapshot(stderr)
})
Loading

0 comments on commit 2632a50

Please sign in to comment.