Skip to content

Commit

Permalink
argufy
Browse files Browse the repository at this point in the history
  • Loading branch information
zavr-1 committed Apr 24, 2019
1 parent 4d4a73a commit 762b09b
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 52 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "artdeco"
"extends": "artdeco",
"rules": {
"quote-props": "off"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"d": "yarn-s d1",
"dev": "node src/bin",
"lint": "eslint .",
"args": "argufy -o src/bin/get-args.js",
"example/simple.js": "node src/bin example/simple.js -b",
"example/Zoroaster/": "node src/bin example/Zoroaster/test/spec -b"
},
Expand Down
112 changes: 112 additions & 0 deletions src/bin/get-args.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import argufy from 'argufy'

export const argsConfig = {
'tests': {
description: 'The location of the test suite directory or file.',
command: true,
multiple: true,
},
'alamode': {
description: 'Enable import/export transpilation with ÀLaMode.',
boolean: true,
short: 'a',
},
'babel': {
description: 'Require `@babel/register` (needs to be installed).',
boolean: true,
short: 'b',
},
'watch': {
description: 'Start the runner in _watch_ mode (rerun on changes).',
boolean: true,
short: 'w',
},
'timeout': {
description: 'Timeout for tests in ms.',
number: true,
default: '2000',
short: 't',
},
'snapshot': {
description: 'The location of the snapshot dir.',
default: 'test/snapshot',
short: 's',
},
'snapshotRoot': {
description: 'The list of folders that will be roots in the snapshot dir.',
default: 'test/spec,test/mask',
short: 'r',
},
'interactive': {
description: 'Run in interactive mode, allowing to update snapshots\nand mask results when they don\'t match currently expected.',
boolean: true,
short: 'i',
},
'version': {
description: 'Show the current version.',
boolean: true,
short: 'v',
},
'help': {
description: 'Display help information.',
boolean: true,
short: 'h',
},
}
const args = argufy(argsConfig)

/**
* The location of the test suite directory or file.
*/
export const _tests = /** @type {string} */ (args['tests'])

/**
* Enable import/export transpilation with ÀLaMode.
*/
export const _alamode = /** @type {boolean} */ (args['alamode'])

/**
* Require `@babel/register` (needs to be installed).
*/
export const _babel = /** @type {boolean} */ (args['babel'])

/**
* Start the runner in _watch_ mode (rerun on changes).
*/
export const _watch = /** @type {boolean} */ (args['watch'])

/**
* Timeout for tests in ms. Default `2000`.
*/
export const _timeout = /** @type {number} */ (args['timeout']) || 2000

/**
* The location of the snapshot dir. Default `test/snapshot`.
*/
export const _snapshot = /** @type {string} */ (args['snapshot']) || 'test/snapshot'

/**
* The list of folders that will be roots in the snapshot dir. Default `test/spec,test/mask`.
*/
export const _snapshotRoot = /** @type {string} */ (args['snapshotRoot']) || 'test/spec,test/mask'

/**
* Run in interactive mode, allowing to update snapshots
and mask results when they don't match currently expected.
*/
export const _interactive = /** @type {boolean} */ (args['interactive'])

/**
* Show the current version.
*/
export const _version = /** @type {boolean} */ (args['version'])

/**
* Display help information.
*/
export const _help = /** @type {boolean} */ (args['help'])

/**
* The additional arguments passed to the program.
*/
export const _argv = /** @type {!Array<string>} */ (args._argv)
24 changes: 8 additions & 16 deletions src/bin/usage.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import usually from 'usually'

export default () => {
const usage = usually({
usage: {
pathToSpec: 'The path to a test suite file.',
'-w, --watch': 'Monitor files for changes and re-run tests.',
'-a, --alamode': 'Require ÀLaMode to enable import/export & JSX.',
'-b, --babel': 'Require babel/register.',
'-s, --snapshot': 'The path to the snapshot dir.\nDefault test/snapshot.',
'-i, --interactive': 'Enable prompts to update snapshots.',
'-r, --snapshotRoot': 'Comma-separated snapshot roots.\nDefault test/spec,test/mask.',
'-v, --version': 'Print version number and exit.',
'-h, --help': 'Display this usage information.',
},
description: 'A testing framework with support for test contexts.',
line: 'zoroaster pathToSpec [pathToSpecN] [-w] [-ab] [-sr] [-vh]',
export default (usage) => {
const res = usually({
usage,
description: `A testing framework with support for test contexts and masks.
Automatically transpiles import/export and JSX with ÀLaMode.
https://artdecocode.com/zoroaster/`,
line: 'zoroaster path [pathN] [-w] [-ab] [-sr] [-vh]',
example: 'zoroaster test/spec test/mask -a',
})
return usage
return res
}
46 changes: 11 additions & 35 deletions src/bin/zoroaster.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,22 @@
#!/usr/bin/env node
import cleanStack from '@artdeco/clean-stack'
import argufy from 'argufy'
import { _version, _help, _alamode, _babel, _tests, _argv, _watch, _timeout, _snapshot, _snapshotRoot, _interactive, argsConfig } from './get-args'
import { reduceUsage } from 'argufy'
import { resolve } from 'path'
import run from '../lib/run'
import getUsage from './usage'
import { version } from '../../package.json'

const {
babel,
alamode,
watch: _watch,
version: _version,
help: _help,
paths: _paths = [],
timeout: _timeout = 2000,
snapshot = 'test/snapshot',
snapshotRoot = 'test/spec,test/mask',
interactive = false,
_argv,
} = argufy({
paths: { command: true, multiple: true },
babel: { short: 'b', boolean: true },
alamode: { short: 'a', boolean: true },
watch: { short: 'w', boolean: true },
version: { short: 'v', boolean: true },
help: { short: 'h', boolean: true },
timeout: { short: 't', number: true },
snapshot: { short: 's' },
snapshotRoot: { short: 'r' },
interactive: { short: 'i', boolean: true },
})

if (_version) {
console.log(version)
process.exit()
} else if (_help) {
const usage = getUsage()
const usage = getUsage(reduceUsage(argsConfig))
console.log(usage)
process.exit()
}

if (babel) {
if (_babel) {
try {
require('@babel/register')
} catch (err) {
Expand All @@ -49,22 +25,22 @@ if (babel) {
}
}

if (alamode) {
if (_alamode) {
require('alamode')()
}

(async () => {
try {
await run({
paths: [..._paths, ..._argv],
paths: [..._tests || [], ..._argv],
watch: _watch,
timeout: _timeout,
snapshot,
snapshotRoot: snapshotRoot.split(','),
interactive,
snapshot: _snapshot,
snapshotRoot: _snapshotRoot.split(','),
interactive: _interactive,
})
} catch ({ stack }) {
console.log(cleanStack(stack)) // eslint-disable-line no-console
} catch (error) {
console.log(cleanStack(error.stack)) // eslint-disable-line no-console
process.exit(1)
}
})()
34 changes: 34 additions & 0 deletions types/arguments.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<arguments>
<arg command multiple name="tests">
The location of the test suite directory or file.
</arg>
<arg boolean name="alamode" short="a">
Enable import/export transpilation with ÀLaMode.
</arg>
<arg boolean name="babel" short="b">
Require `@babel/register` (needs to be installed).
</arg>
<arg boolean name="watch" short="w">
Start the runner in _watch_ mode (rerun on changes).
</arg>
<arg number name="timeout" short="t" default="2000">
Timeout for tests in ms.
</arg>
<arg name="snapshot" short="s" default="test/snapshot">
The location of the snapshot dir.
</arg>
<arg name="snapshotRoot" short="r"
default="test/spec,test/mask">
The list of folders that will be roots in the snapshot dir.
</arg>
<arg boolean name="interactive" short="i" >
Run in interactive mode, allowing to update snapshots
and mask results when they don't match currently expected.
</arg>
<arg boolean name="version" short="v">
Show the current version.
</arg>
<arg boolean name="help" short="h">
Display help information.
</arg>
</arguments>

0 comments on commit 762b09b

Please sign in to comment.