Skip to content

Commit

Permalink
feat: use fancy-mocha
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 21, 2018
1 parent 9b2b2be commit b7a1d25
Show file tree
Hide file tree
Showing 18 changed files with 869 additions and 380 deletions.
16 changes: 10 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ jobs:
- checkout
- restore_cache: &restore_cache
keys:
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/test"}}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/test"}}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/test"}}-{{checksum ".circleci/config.yml"}}-master-
- run: ./.circleci/test
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-
- v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/config.yml"}}-master-
- run: ./.circleci/setup_git
- run: ./.circleci/greenkeeper
- run: yarn exec nps ci.test
- store_test_results:
path: ~/cli/reports
- save_cache: &save_cache
key: v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/test"}}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
key: v0-yarn-{{ .Environment.CIRCLE_JOB }}-{{checksum ".circleci/config.yml"}}-{{ .Branch }}-{{checksum "yarn.lock"}}
paths:
- ~/cli/node_modules
- /usr/local/share/.cache/yarn
Expand All @@ -28,10 +30,12 @@ jobs:
release:
<<: *test
steps:
- add_ssh_keys
- checkout
- restore_cache: *restore_cache
- run: ./.circleci/setup_git
- run: yarn --frozen-lockfile
- run: ./node_modules/.bin/nps ci.release
- run: yarn exec nps ci.release
- save_cache: *save_cache

workflows:
Expand Down
22 changes: 22 additions & 0 deletions .circleci/greenkeeper
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -e

PATH=/usr/local/share/.config/yarn/global/node_modules/.bin:$PATH

CLI_ENGINE_UTIL_YARN_ARGS="--frozen-lockfile"

if [[ "$CIRCLE_BRANCH" == greenkeeper/* ]]; then
CLI_ENGINE_GREENKEEPER_BRANCH=1
CLI_ENGINE_UTIL_YARN_ARGS=""
if [[ ! -x "$(command -v greenkeeper-lockfile-update)" ]]; then
yarn global add greenkeeper-lockfile@1
fi
greenkeeper-lockfile-update
fi

yarn install $CLI_ENGINE_UTIL_YARN_ARGS

if [[ "$CLI_ENGINE_GREENKEEPER_BRANCH" == 1 ]]; then
greenkeeper-lockfile-upload
fi
12 changes: 12 additions & 0 deletions .circleci/setup_git
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

set -e

if [[ ! -z "$GIT_EMAIL" ]] & [[ ! -z "$GIT_USERNAME" ]]; then
git config --global push.default simple
git config --global user.email "$GIT_EMAIL"
git config --global user.user "$GIT_USERNAME"
fi

git submodule sync
git submodule update --init --recursive
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/lib
/test/fixtures
/docs
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ install:
- git config --global user.name "dxcli"
- yarn
test_script:
- yarn test
- .\node_modules\.bin\nps ci.test
after_test:
- ps: |
$env:PATH = 'C:\msys64\usr\bin;' + $env:PATH
Expand Down
123 changes: 66 additions & 57 deletions package-scripts.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,84 @@
const {concurrent, series} = require('nps-utils')
const {
concurrent,
crossEnv,
mkdirp,
series,
ifWindows,
ifNotWindows,
} = require('nps-utils')
const pjson = require('./package.json')
const release = pjson.devDependencies.typedoc ? ['ci.release.semantic-release', 'ci.release.typedoc'] : ['ci.release.semantic-release']
const script = (script, description) => description ? {script, description} : {script}
const hidden = script => ({script, hiddenFromHelp: true})
const unixOrWindows = (unix, windows) => series(ifNotWindows(unix), ifWindows(windows))

let ciTests = [
'ci.test.eslint',
'ci.test.mocha',
'ci.test.tslint',
]

module.exports = {
scripts: {
build: 'rm -rf lib && tsc',
lint: {
default: concurrent.nps('lint.eslint', 'lint.commitlint', 'lint.tsc', 'lint.tslint'),
eslint: {
script: 'eslint .',
description: 'lint js files',
},
commitlint: {
script: 'commitlint --from origin/master',
description: 'ensure that commits are in valid conventional-changelog format',
},
tsc: {
script: 'tsc -p test --noEmit',
description: 'syntax check with tsc',
},
tslint: {
script: 'tslint -p test',
description: 'lint ts files',
},
eslint: script('eslint .', 'lint js files'),
commitlint: script('commitlint --from origin/master', 'ensure that commits are in valid conventional-changelog format'),
tsc: script('tsc -p test --noEmit', 'syntax check with tsc'),
tslint: script('tslint -p test', 'lint ts files'),
},
test: {
default: {
script: concurrent.nps('lint', 'test.mocha'),
description: 'lint and run all tests',
},
default: script(concurrent.nps('lint', 'test.mocha'), 'lint and run all tests'),
series: script(series.nps('lint', 'test.mocha'), 'lint and run all tests in series'),
mocha: {
script: 'mocha "test/**/*.test.ts"',
description: 'run all mocha tests',
default: script('mocha --forbid-only "test/**/*.test.ts"', 'run all mocha tests'),
coverage: hidden(series.nps('test.mocha.nyc nps test.mocha')),
junit: hidden(series(
crossEnv('MOCHA_FILE="reports/mocha.xml" ') + series.nps('test.mocha.nyc nps \\"test.mocha --reporter mocha-junit-reporter\\"'),
series.nps('test.mocha.nyc report text-lcov > coverage.lcov'),
)),
nyc: hidden('nyc --nycrc-path node_modules/@dxcli/dev-nyc-config/.nycrc'),
},
},
ci: {
default: {
script: concurrent.nps(
'ci.mocha',
'ci.eslint',
'ci.tslint',
test: {
default: hidden(series(
mkdirp('reports'),
unixOrWindows(
concurrent.nps(...ciTests),
series.nps(...ciTests),
),
)),
mocha: hidden(
unixOrWindows(
series.nps('test.mocha.junit'),
series.nps('test.mocha'),
)
),
eslint: hidden(
unixOrWindows(
series.nps('lint.eslint --format junit --output-file reports/eslint.xml'),
series.nps('lint.eslint'),
)
),
tslint: hidden(
unixOrWindows(
series.nps('lint.tslint --format junit > reports/tslint.xml'),
series.nps('lint.tslint'),
)
),
hiddenFromHelp: true,
},
mocha: {
default: {
script: series.nps('ci.mocha.test', 'ci.mocha.report'),
hiddenFromHelp: true,
},
test: {
script: 'MOCHA_FILE="reports/mocha.xml" nps "ci.mocha.nyc nps \\"test.mocha --reporter mocha-junit-reporter\\""',
hiddenFromHelp: true,
},
report: {
script: series.nps('ci.mocha.nyc report --reporter text-lcov > coverage.lcov'),
hiddenFromHelp: true,
},
nyc: {
script: 'nyc --nycrc-path node_modules/@dxcli/dev-nyc-config/.nycrc',
hiddenFromHelp: true,
},
},
eslint: {
script: series.nps('lint.eslint --format junit --output-file reports/eslint.xml'),
hiddenFromHelp: true,
},
tslint: {
script: series.nps('lint.tslint --format junit > reports/tslint.xml'),
hiddenFromHelp: true,
},
release: {
script: 'dxcli-dev-semantic-release',
hiddenFromHelp: true,
default: hidden(series.nps(...release)),
'semantic-release': hidden('semantic-release -e @dxcli/dev-semantic-release'),
typedoc: hidden(series(
'git clone -b gh-pages $CIRCLE_REPOSITORY_URL gh-pages',
'typedoc --out /tmp/docs src/index.ts --excludeNotExported --mode file',
'rm -rf ./gh-pages/*',
'mv /tmp/docs/* ./gh-pages',
'cd gh-pages && git add . && git commit -m "updates from $CIRCLE_SHA1 [skip ci]" && git push',
)),
},
},
},
Expand Down
29 changes: 18 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,42 @@
"author": "Jeff Dickey @jdxcode",
"bugs": "https://github.com/dxcli/dev-test/issues",
"dependencies": {
"debug": "^3.1.0",
"lodash": "^4.17.4",
"strip-ansi": "^4.0.0"
},
"devDependencies": {
"@dxcli/dev-nyc-config": "^0.0.3",
"@dxcli/dev-semantic-release": "^0.1.0",
"@dxcli/dev-test": "^0.7.0",
"@dxcli/dev-tslint": "^0.0.15",
"@dxcli/engine": "^0.1.5",
"@dxcli/version": "^0.1.4",
"@types/ansi-styles": "^2.0.30",
"@types/chai": "^4.1.1",
"@types/chai-as-promised": "^7.1.0",
"@types/lodash": "^4.14.93",
"@types/mocha": "^2.2.46",
"@types/node": "^9.3.0",
"@types/read-pkg": "^3.0.0",
"@types/strip-ansi": "^3.0.0",
"chai": "^4.1.2",
"chai-as-promised": "^7.1.1",
"lodash": "^4.17.4",
"mocha-junit-reporter": "^1.16.0",
"std-mocks": "^1.0.1",
"strip-ansi": "^4.0.0",
"ts-node": "^4.1.0"
},
"devDependencies": {
"@dxcli/dev-semantic-release": "^0.0.3",
"@dxcli/dev-tslint": "^0.0.15",
"@dxcli/version": "^0.1.4",
"@types/node": "^9.3.0",
"cli-ux": "^3.1.5",
"esdoc": "^1.0.4",
"esdoc-standard-plugin": "^1.0.0",
"esdoc-undocumented-identifier-plugin": "^1.0.0",
"eslint": "^4.16.0",
"eslint-config-dxcli": "^1.1.4",
"fancy-mocha": "^0.0.6",
"husky": "^0.14.3",
"mocha": "^5.0.0",
"mocha-junit-reporter": "^1.16.0",
"nps": "^5.7.1",
"nps-utils": "^1.5.0",
"nyc": "^11.4.1",
"ts-node": "^4.1.0",
"typedoc": "^0.9.0",
"typescript": "^2.6.2"
},
"engines": {
Expand Down
59 changes: 16 additions & 43 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,22 @@
import run from '@dxcli/engine'
import * as _ from 'lodash'

import {expect, it, output} from '.'
import {Options} from '.'

export interface TestCommandOptions {
description?: string
stdout?: string | boolean
stderr?: string | boolean
exit?: number
root?: string
}

export type TestCommandCallback<T> = (output: T) => Promise<void> | void

export interface TestCommand {
(args: string[], opts: TestCommandOptions & {stdout: true, stderr: true}, fn: TestCommandCallback<{stdout: string, stderr: string}>): void
(args: string[], opts: TestCommandOptions & {stdout: true}, fn: TestCommandCallback<{stdout: string}>): void
(args: string[], opts: TestCommandOptions & {stderr: true}, fn: TestCommandCallback<{stderr: string}>): void
(args: string[], opts: TestCommandOptions, fn?: TestCommandCallback<{}>): void
}

export const testCommand: TestCommand = (
args: string[],
opts: TestCommandOptions,
fn?: TestCommandCallback<any>
) => {
const description = opts.description || args.join(' ')
let test = it
if (opts.stdout) test = test.stdout
if (opts.stderr) test = test.stderr
test(description, async () => {
const exit = opts.exit || 0
try {
export default (args: string[] | string, opts: Options = {}) => {
return {
async before() {
args = _.castArray(args)
await run(args, {root: opts.root || module.parent!.parent!.filename})
} catch (err) {
if (err.code !== 'EEXIT') throw err
if (err['cli-ux'].exit !== exit) {
throw new Error(`Expected exit code to be ${exit} but got ${err['cli-ux'].exit}`)
}
}
if (typeof opts.stdout === 'string') expect(output.stdout).to.equal(opts.stdout)
if (typeof opts.stderr === 'string') expect(output.stderr).to.equal(opts.stderr)
if (!fn) return
let o: any = {}
if (opts.stdout) o.stdout = output.stdout
if (opts.stderr) o.stderr = output.stderr
await fn(o)
})
}
}
// const exit = opts.exit || 0
// try {
// await run(args, {root: opts.root || module.parent!.parent!.filename})
// } catch (err) {
// if (err.code !== 'EEXIT') throw err
// if (err['cli-ux'].exit !== exit) {
// throw new Error(`Expected exit code to be ${exit} but got ${err['cli-ux'].exit}`)
// }
// }
23 changes: 23 additions & 0 deletions src/exit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {CLIError} from 'cli-ux'

/**
* ensures that a dxcli command or hook exits
*
* @param code - expected code
* @default 0
*/
export default (code = 0) => {
let err: CLIError
return {
catch(_: any, e: CLIError) {
err = e
if (!err['cli-ux'] || typeof err['cli-ux'].exit !== 'number') throw err
if (err['cli-ux'].exit !== code) {
throw new Error(`Expected hook to exit with ${code} but exited with ${err['cli-ux'].exit}`)
}
},
finally() {
if (!err) throw new Error(`Expected hook to exit with code ${code} but it ran without exiting`)
}
}
}
Loading

0 comments on commit b7a1d25

Please sign in to comment.