Skip to content

Commit

Permalink
refactor CLI to make it easier to add pkg opts
Browse files Browse the repository at this point in the history
  • Loading branch information
searls committed Jun 21, 2016
1 parent 3ffa629 commit db854c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
17 changes: 1 addition & 16 deletions bin/teenytest
@@ -1,18 +1,3 @@
#!/usr/bin/env node

var _ = require('lodash'),
teenytest = require('../index'),
argv = require('minimist')(process.argv.slice(2))
testGlob = _.last(argv['_']) || 'test/lib/**/*.js',
helperPath = argv['helper'] || 'test/helper.js',
asyncTimeout = argv['timeout'] || 5000
fs = require('fs')

teenytest(testGlob, {
helperPath: fs.existsSync(helperPath) ? helperPath : null,
asyncTimeout: asyncTimeout
}, function(er, passing) {
process.exit(!er && passing ? 0 : 1)
})


require('../lib/cli')()
24 changes: 14 additions & 10 deletions lib/build-test-helper.js
@@ -1,20 +1,24 @@
var _ = require('lodash')
var fs = require('fs')
var path = require('path')

function noOpHook () {}

module.exports = function (helperPath, cwd) {
return _.assign({}, {
beforeAll: function () {},
afterAll: function () {},
beforeEach: function () {},
afterEach: function () {},
return _.defaults(buildUserHelper, {
beforeAll: noOpHook,
afterAll: noOpHook,
beforeEach: noOpHook,
afterEach: noOpHook,
options: {}
}, buildUserHelper(helperPath, cwd))
})
}

var buildUserHelper = function (helperPath, cwd) {
if (!helperPath) { return {} }
return _.assign({}, require(path.resolve(cwd, helperPath)), {
file: helperPath
})
if (helperPath && fs.existsSync(helperPath)) {
return _.assign({}, require(path.resolve(cwd, helperPath)), {
file: helperPath
})
}
}

29 changes: 29 additions & 0 deletions lib/cli/index.js
@@ -0,0 +1,29 @@
var _ = require('lodash')
var minimist = require('minimist')

var teenytest = require('../../index')

module.exports = function () {
var options = _.defaults(argv(), defaults())
teenytest(options.testGlob, options, function (er, passing) {
process.exit(!er && passing ? 0 : 1)
})
}

function defaults () {
return {
testGlob: 'test/lib/**/*.js',
helperPath: 'test/helper.js',
asyncTimeout: 5000
}
}

function argv () {
var argv = minimist(process.argv.slice(2))

return {
testGlob: _.last(argv['_']),
helperPath: argv['helper'],
asyncTimeout: argv['timeout']
}
}

0 comments on commit db854c2

Please sign in to comment.