Skip to content

Commit

Permalink
feat: added loadConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2018
1 parent 12c28eb commit 9e620ed
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 33 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@dxcli/config": "^0.1.27",
"@dxcli/dev": "^2.0.12",
"@dxcli/engine": "^0.1.12",
"@dxcli/loader": "^0.2.8",
"@dxcli/semantic-release": "^0.2.4",
"@dxcli/tslint": "^0.0.23",
"@dxcli/version": "^0.1.6",
Expand All @@ -38,6 +39,9 @@
],
"license": "MIT",
"main": "lib/index.js",
"peerDependencies": {
"@dxcli/loader": "^0.2.8"
},
"repository": "dxcli/test",
"scripts": {
"commitmsg": "dxcli-commitlint",
Expand Down
9 changes: 4 additions & 5 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {IConfig} from '@dxcli/config'
import run from '@dxcli/engine'
import * as _ from 'lodash'

import {Options} from '.'

export default (args: string[] | string | undefined, opts: Options = {}) => ({
async run(ctx: {expectation: string}) {
export default (args: string[] | string | undefined) => ({
async run(ctx: {config: IConfig, expectation: string}) {
args = _.castArray(args)
ctx.expectation = ctx.expectation || `runs ${args.join(' ')}`
await run(args, {root: opts.root || module.parent!.parent!.filename})
await run(args, ctx.config)
}
})
10 changes: 4 additions & 6 deletions src/hook.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {IConfig} from '@dxcli/config'
import {Engine} from '@dxcli/engine'

import {Options} from './options'

/**
* tests a dxcli hook
*
Expand All @@ -12,13 +11,12 @@ import {Options} from './options'
*
* @param event - hook to run
* @param hookOpts - options to pass to hook. Config object will be passed automatically.
* @param opts - test options
*/
export default (event?: string, hookOpts: object = {}, opts: Options = {}) => ({
async run(ctx: {expectation: string}) {
export default (event?: string, hookOpts: object = {}) => ({
async run(ctx: {config: IConfig, expectation: string}) {
ctx.expectation = ctx.expectation || `runs ${event} hook`
const engine = new Engine()
await engine.load(opts.root || module.parent!.parent!.filename)
await engine.load(ctx.config.root)
await engine.runHook(event!, hookOpts || {})
}
})
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// tslint:disable no-var-before-return
import {IConfig} from '@dxcli/config'
import {expect, fancy, FancyTypes, NockScope} from 'fancy-test'

import command from './command'
import exit from './exit'
import hook from './hook'

import {Options} from './options'
import loadConfig from './load_config'

export const test = fancy
.register('loadConfig', loadConfig)
.register('command', command)
.register('exit', exit)
.register('hook', hook)
Expand All @@ -18,5 +20,5 @@ export {

FancyTypes,
NockScope,
Options,
IConfig,
}
14 changes: 14 additions & 0 deletions src/load_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as path from 'path'

import {IConfig} from '@dxcli/config'
import {load} from '@dxcli/loader'

/**
* loads CLI plugin/multi config
*/
export default (opts: {root?: string} = {}) => ({
async run(ctx: {config: IConfig}) {
const {config} = await load({root: opts.root || path.dirname(module.parent!.filename)})
ctx.config = config
}
})
9 changes: 0 additions & 9 deletions src/options.ts

This file was deleted.

11 changes: 7 additions & 4 deletions test/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import * as path from 'path'

import fancy, {expect} from '../src'
import {expect, test} from '../src'

const root = path.join(__dirname, 'fixtures/multi')

describe('command', () => {
fancy
test
.loadConfig({root})
.stdout()
.command(['foo:bar'], {root})
.do(output => expect(output.stdout).to.equal('hello world!\n'))
.it()

fancy
test
.loadConfig({root})
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.do(output => expect(output.stdout).to.equal('hello foo!\n'))
.it()

fancy
test
.loadConfig({root})
.stdout()
.command(['foo:bar', '--name=foo'], {root})
.do(output => expect(output.stdout).to.equal('hello foo!\n'))
Expand Down
14 changes: 9 additions & 5 deletions test/hook.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import * as path from 'path'

import fancy, {expect} from '../src'
import {expect, test} from '../src'

const root = path.join(__dirname, 'fixtures/multi')

describe('hooks', () => {
const stdout = `test/0.0.0 (${process.platform}-${process.arch}) node-${process.version}\n`
const root = path.join(__dirname, 'fixtures/multi')

fancy
test
.loadConfig({root})
.stdout()
.hook('init', {id: '-v'}, {root})
.exit(0)
.do(output => expect(output.stdout).to.equal(stdout))
.it('catches -v')

fancy
test
.loadConfig({root})
.stdout()
.hook('init', {id: '--version'}, {root})
.exit(0)
.do(output => expect(output.stdout).to.equal(stdout))
.it('catches --version')

fancy
test
.loadConfig({root})
.stdout()
.hook('init', {}, {root})
.it()
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
cli-ux "^3.2.1"
debug "^3.1.0"

"@dxcli/loader@^0.2.5":
"@dxcli/loader@^0.2.5", "@dxcli/loader@^0.2.8":
version "0.2.8"
resolved "https://registry.yarnpkg.com/@dxcli/loader/-/loader-0.2.8.tgz#a704e704d0682e82ab0ad2d1393cbe326986c922"
dependencies:
Expand Down

0 comments on commit 9e620ed

Please sign in to comment.