From 10d060f9a192d9c789da9c0ba044513486e920bb Mon Sep 17 00:00:00 2001 From: Rod Vagg Date: Thu, 27 Dec 2012 17:15:16 +1100 Subject: [PATCH] extracted args-parse.js to ender-args-parser lib --- lib/args-parse.js | 203 --------------------- lib/main-add.js | 6 +- lib/main-remove.js | 6 +- lib/main.js | 7 +- lib/output/main-info-output.js | 14 +- lib/parse-context.js | 6 +- lib/source-build.js | 12 +- package.json | 4 +- test/unit/args-parse-test.js | 309 -------------------------------- test/unit/main-add-test.js | 6 +- test/unit/main-test.js | 27 ++- test/unit/parse-context-test.js | 4 +- test/unit/source-build-test.js | 22 +-- 13 files changed, 56 insertions(+), 570 deletions(-) delete mode 100644 lib/args-parse.js delete mode 100644 test/unit/args-parse-test.js diff --git a/lib/args-parse.js b/lib/args-parse.js deleted file mode 100644 index 2cd5987..0000000 --- a/lib/args-parse.js +++ /dev/null @@ -1,203 +0,0 @@ -/*! - * ENDER - The open module JavaScript framework - * - * Copyright (c) 2011-2012 @ded, @fat, @rvagg and other contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is furnished - * to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - - -/****************************************************************************** - * Our own argument-parser. It's unfortunate that we have to do this but - * the argument structure of Ender rules out most other argument parser - * modules. Specifically the fact that --sandbox can have multiple arguments - * to it, normally you'd only have one per --/-. - * See the tests for this for a more comprehensive treatment of what we expect - * from this code. - */ - -var UnknownMainError = require('./errors').UnknownMainError - , UnknownOptionError = require('./errors').UnknownOptionError - - // the default array option to collect arguments for ('packages'), so all non - // --/- prefixed arguments will be sent to this list until we receive a --/- - // that is an 'Array' type. - // e.g. `ender build foo bar baz`, our 'main' is 'build', 'foo bar baz' are - // all collected into 'packages'. Whereas `ender build foo bar --sandbox foo baz` - // we get 'foo bar' in packages and 'foo baz' in 'sandbox'. - , defaultArray - - // available options - , options = (function () { - var arr = [] - , add = function (name, short, type, altMain) { - var o - - if (typeof short != 'string') { - altMain = type - type = short - short = null - } - - o = { - name : name - , short : short - , type : type - , altMain : altMain - } - - if (short === '' && type === Array) defaultArray = name - arr.push(o) - } - - // --foo -f [type] [alternate main cmd] - add('packages' , '' , Array) - add('output' , 'o' , String) - add('use' , 'u' , String) - add('max' , Number) - add('sandbox' , Array) - add('noop' , 'x' , Boolean) - add('silent' , 's' , Boolean) - add('help' , 'h' , Boolean, true) - add('sans' , Boolean) - add('debug' , Boolean) - add('version' , 'v' , Boolean, true) - add('externs' , Array) - add('client-lib' , String) - add('level' , String) - add('quiet' , Boolean) - add('force-install' , Boolean) - add('minifier' , String) - - return arr - }()) - - // allowable first-arguments that resolve to our main-X.js modules. - , mains = { - 'help' : 'help' - , 'build' : 'build' - , 'add' : 'add' - , 'set' : 'add' - , 'refresh' : 'refresh' - , 'remove' : 'remove' - , 'rm' : 'remove' - , 'info' : 'info' - , 'ls' : 'info' - , 'list' : 'info' - , 'search' : 'search' - , 'compile' : 'compile' - , 'version' : 'version' - } - - , findOption = function (s) { - var i - , option - , match = s.match(/^(--?)([a-z\-]+)$/) - - if (!match) return - - for (i = 0; i < options.length; i++) { - var hasOption - - option = options[i] - hasOption = (match[1] === '--' && match[2] === option.name) - || (match[1] === '-' && option.short && match[2] === option.short) - - if (hasOption) return option - } - throw new UnknownOptionError('Unknown option "' + s + '"') - } - - // reverse a parse! Turn an `options` object into a parsable commandline string - // mainly so we can include it in the 'Build:' string in the source header but - // it has other uses. - , toContextString = function (options) { - var str = options.main - , p - - if (options.packages.length) str += ' ' + options.packages.join(' ') - - for (p in options) { - if (p === 'packages' || p === 'main') continue - str += ' --' + p - if (Array.isArray(options[p])) str += ' ' + options[p].join(' ') - else if (typeof options[p] !== 'boolean') str += ' ' + options[p] - } - return str - } - - , parse = function (argv, slice, options) { - var args = Array.prototype.slice.call(argv, slice) // slice might be 0 for clean or 2 for raw - , options = options || {} - , currentArray - , arg - , o - , setCurrentArray = function (name) { - currentArray = name - options[currentArray] = options[currentArray] - ? Array.prototype.slice.call(options[currentArray], 0) // clone - : [] - } - - setCurrentArray(defaultArray) - - while (arg = args.shift()) { - o = findOption(arg) - if (o) { - currentArray = defaultArray - - if (o.type === Boolean) { - options[o.name] = true - if (o.altMain && !options.main) options.main = o.name - } else if ((o.type === String || o.type === Number) && args.length) { - // converstion through type constructor - options[o.name] = o.type(args.shift()) - } else if (o.type === Array) { - setCurrentArray(o.name) - } - - } else if (!options.main) { - options.main = arg - } else { - options[currentArray].push(arg) - } - } - - if (!options.main) { - if (Object.keys(options).length > 1) throw new UnknownMainError('No main command supplied') - options.main = 'help' - } - - if (!mains[options.main]) throw new UnknownMainError('Unknown main command "' + options.main + '"') - options.main = mains[options.main] - - return options - } - - // merge two options objects together, for `add` and `remove`. - , extend = function (originalArgs, newArgs) { - return parse(toContextString(newArgs).split(' '), 1, originalArgs) - } - -module.exports = { - parse : function (argv) { return parse(argv, 2) } // with 2 additional args 'node script.js' - , parseClean : function (argv) { return parse(argv, 0) } // without - , extend : extend - , toContextString : toContextString -} diff --git a/lib/main-add.js b/lib/main-add.js index 6774211..79962d0 100644 --- a/lib/main-add.js +++ b/lib/main-add.js @@ -32,8 +32,8 @@ * module which does all the hard work. */ -var util = require('./util') - , argsParse = require('./args-parse') +var argsParser = require('ender-args-parser') + , util = require('./util') , parseContext = require('./parse-context') , mainBuild = require('./main-build') @@ -43,7 +43,7 @@ var util = require('./util') parseContext(filename, function (err, context) { if (err) return callback(err) // err wrapped in SourceBuild.parseContext() // merge commandline args with the build command in ender.js - options = argsParse.extend(context.options, options) + options = argsParser.extend(context.options, options) mainBuild.exec(options, out, callback) }) } diff --git a/lib/main-remove.js b/lib/main-remove.js index c53115e..c42d230 100644 --- a/lib/main-remove.js +++ b/lib/main-remove.js @@ -35,9 +35,9 @@ var async = require('async') , repository = require('ender-repository') + , argsParser = require('ender-args-parser') , util = require('./util') , mainBuild = require('./main-build') - , argsParse = require('./args-parse') , mainBuildUtil = require('./main-build-util') , parseContext = require('./parse-context') @@ -52,13 +52,13 @@ var async = require('async') } ; delete options.use // don't want --use showing up in the 'Build:' context string - options.packages = [] // reset the packages list so argsParse.extend() doesn't include them + options.packages = [] // reset the packages list so argsParser.extend() doesn't include them parseContext(filename, function (err, context) { if (err) return callback(err) // wrapped in source-build.js // merge the commandline with the ender.js build command - options = argsParse.extend(context.options, options) + options = argsParser.extend(context.options, options) options.packages = options.packages.filter(function (p) { return packages.indexOf(p) == -1 // do the remove! }) diff --git a/lib/main.js b/lib/main.js index 1128f77..25697f2 100644 --- a/lib/main.js +++ b/lib/main.js @@ -42,9 +42,8 @@ process.title = 'Ender' var sysUtil = require('util') + , argsParser = require('ender-args-parser') , Output = require('./output/output') - , argsParse = require('./args-parse') - , EnderError = require('./errors').EnderError // basic error handler, differentiates between 'known' EnderErrors and everything else , complete = function (out, callback, err) { @@ -66,7 +65,7 @@ var sysUtil = require('util') } try { - options = argsParse[parseType](argv) + options = argsParser[parseType](argv) // get the module to execute and it's partner output module exe = options && require('./main-' + options.main) @@ -74,7 +73,7 @@ var sysUtil = require('util') if (exe && out) { exe.exec(options, out, complete.bind(null, out, callback)) - } // else err? argsParse should take care of this if it's list of mains corresponds to the modules we have + } // else err? argsParser should take care of this if it's list of mains corresponds to the modules we have } catch (ex) { // create a generic/base 'out' module which can do the error printing out = Output.create(sysUtil, argv.indexOf('--debug') != -1) diff --git a/lib/output/main-info-output.js b/lib/output/main-info-output.js index 97a86ef..7ab5567 100644 --- a/lib/output/main-info-output.js +++ b/lib/output/main-info-output.js @@ -23,12 +23,12 @@ */ -var archy = require('archy') - , colors = require('colors') - , extend = require('../util').extend - , toKb = require('../util').toKb - , Output = require('./output') - , argsParse = require('../args-parse') +var archy = require('archy') + , colors = require('colors') + , argsParser = require('ender-args-parser') + , extend = require('../util').extend + , toKb = require('../util').toKb + , Output = require('./output') , InfoOutput = extend(Output, { // inherit from Output @@ -45,7 +45,7 @@ var archy = require('archy') prepareTree(archyTree) //this.log('Your current build type is ' + ('"' + options.main + '"').yellow) - this.log('Your current build command is: ' + ('ender ' + argsParse.toContextString(options)).yellow) + this.log('Your current build command is: ' + ('ender ' + argsParser.toContextString(options)).yellow) this.log( 'Your current build size is: ' + toKb(sizes.raw).yellow + ' raw' diff --git a/lib/parse-context.js b/lib/parse-context.js index 05bd9b6..ac39041 100644 --- a/lib/parse-context.js +++ b/lib/parse-context.js @@ -25,11 +25,11 @@ /****************************************************************************** * a utility to partially read an ender build file and parse the head comment * to pull out the 'Build:' and 'Packages:' lines. Returns the build command as a properly - * parsed options object (via argsParse). + * parsed options object (via argsParser). */ const fs = require('fs') - , argsParse = require('./args-parse') + , argsParser = require('ender-args-parser') , FilesystemError = require('./errors').FilesystemError , BuildParseError = require('./errors').BuildParseError @@ -56,7 +56,7 @@ var parseContext = function (file, callback) { } try { - options = argsParse.parseClean(match[1].split(' ')) + options = argsParser.parseClean(match[1].split(' ')) } catch (ex) { error = 'Could not parse ender spec from "' + file + '"' return callback(new BuildParseError(error, ex)) diff --git a/lib/source-build.js b/lib/source-build.js index 603f1bf..7d08c19 100644 --- a/lib/source-build.js +++ b/lib/source-build.js @@ -29,11 +29,11 @@ * on the list of SourcePackages. */ -var async = require('async') - , util = require('./util') - , minify = require('./minify') - , template = require('./template') - , argsParse = require('./args-parse') +var async = require('async') + , argsParser = require('ender-args-parser') + , util = require('./util') + , minify = require('./minify') + , template = require('./template') , templateFile = '../resources/build.mustache' @@ -59,7 +59,7 @@ var async = require('async') , tmplData = function (sources) { return { source: sources.join('\n\n') - , context: argsParse.toContextString(this.options) + , context: argsParser.toContextString(this.options) , sandbox: !!this.options.sandbox , packages: this.packages.map(function (p) { return p.identifier diff --git a/package.json b/package.json index faab68c..f7c10ed 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ender" , "description": "Open Module JavaScript Framework" - , "version": "0.9.9-dev" + , "version": "0.9.10-dev" , "authors": [ "Jacob Thornton @fat (https://github.com/fat)" , "Dustin Diaz @ded (https://github.com/ded)" @@ -15,6 +15,8 @@ , "ender-minify" : "0.1.0" , "ender-dependency-graph" : "0.0.1" + , "ender-args-parser" + : "0.0.1" , "colors" : "~0.6.0" , "async" : "~0.1.22" , "hogan.js" : "~2.0.0" diff --git a/test/unit/args-parse-test.js b/test/unit/args-parse-test.js deleted file mode 100644 index 4e27e71..0000000 --- a/test/unit/args-parse-test.js +++ /dev/null @@ -1,309 +0,0 @@ -/*! - * ENDER - The open module JavaScript framework - * - * Copyright (c) 2011-2012 @ded, @fat, @rvagg and other contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is furnished - * to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - - -var buster = require('buster') - , assert = buster.assert - , argsParse = require('../../lib/args-parse') - , buildargs = function (s) { - return [ 'node', '/some/path/to/bin' ].concat(s.split(' ')) - } - -buster.testCase('Args parser', { - 'parse': { - 'test parse() exists': function () { - assert.isFunction(argsParse.parse) - } - - , 'test parse finds main command': function () { - var actual = argsParse.parse(buildargs('help')) - assert.isString(actual.main) - assert.equals(actual.main, 'help') - } - - , 'test parse finds main command with trailling cruft': function () { - var actual = argsParse.parse(buildargs('build --debug --noop stuff here')) - assert.isString(actual.main) - assert.equals(actual.main, 'build') - } - - , 'test parse finds main command with leading and trailling cruft': function () { - var actual = argsParse.parse(buildargs('--debug info --sandbox --noop --output stuff here')) - assert.isString(actual.main) - assert.equals(actual.main, 'info') - } - - , 'test parse provides help when no arguments present': function () { - var actual = argsParse.parse(buildargs('')) - assert.equals( - actual - , { main: 'help', packages: [] } - ) - } - - , 'test parse throws exception on only dashed (--) arguments arguments': function () { - assert.exception(function () { - argsParse.parse(buildargs('--debug --noop')) - }, 'UnknownMainError') - } - - , 'test parse throws exception on unknown build commands': function () { - assert.exception(function () { - argsParse.parse(buildargs('unknown')) - }, 'UnknownMainError') - - assert.exception(function () { - argsParse.parse(buildargs('--output bar --noop')) - }, 'UnknownMainError') - } - - , 'test parse returns packages non dashed arguments': function () { - var actual = argsParse.parse(buildargs('search --output foo bar woo hoo')) - assert.isArray(actual.packages) - assert.equals(actual.packages, [ 'bar', 'woo', 'hoo' ]) - } - - , 'test parse returns packages as empty array if none provided': function () { - var actual = argsParse.parse(buildargs('search')) - assert.isArray(actual.packages) - assert.equals(actual.packages.length, 0) - } - - , 'test parse returns packages as empty array if only dashed (--) provided': function () { - var actual = argsParse.parse(buildargs('search --noop')) - assert.isArray(actual.packages) - assert.equals(actual.packages.length, 0) - } - - , 'test parse returns expected object (no specials)': function () { - var actual = argsParse.parse(buildargs('build fee fie foe fum')) - assert.equals( - actual - , { - main: 'build' - , packages: [ 'fee', 'fie', 'foe', 'fum' ] - } - ) - } - - , 'test parse returns expected object (-- long form)': function () { - var actual = argsParse.parse(buildargs( - 'build fee fie foe fum --output foobar --use yeehaw --max 10 --sandbox foo bar --noop --silent --help --sans --debug --externs what tha --client-lib BOOM --quiet --force-install --minifier none' - )) - assert.equals( - actual - , { - main : 'build' - , packages : [ 'fee', 'fie', 'foe', 'fum' ] - , output : 'foobar' - , use : 'yeehaw' - , max : 10 - , sandbox : [ 'foo', 'bar' ] - , noop : true - , silent : true - , help : true - , sans : true - , debug : true - , externs : [ 'what', 'tha' ] - , 'client-lib' : 'BOOM' - , quiet : true - , 'force-install' : true - , 'minifier' : 'none' - } - ) - } - - , 'test parse returns expected object (- short form)': function () { - var actual = argsParse.parse(buildargs('build fee fie foe fum -o foobar -u yeehaw -x -s -h')) - assert.equals( - actual - , { - main : 'build' - , packages : [ 'fee', 'fie', 'foe', 'fum' ] - , output : 'foobar' - , use : 'yeehaw' - , noop : true - , silent : true - , help : true - } - ) - } - - , 'test parse returns expected object (array arg stops at next -/--)': function () { - var actual = argsParse.parse(buildargs('build fee fie --sandbox foo bar --noop foe fum')) - assert.equals( - actual - , { - main: 'build' - , packages: [ 'fee', 'fie', 'foe', 'fum' ] - , sandbox: [ 'foo', 'bar' ] - , noop: true - } - ) - } - - , 'test parse can handle compact args': function () { - // normally parse knows to split off the first 2 args, "node script.js" - // but we want it to be able to handle arrays without it - var actual = argsParse.parseClean('build fee fie foe fum -o foobar -u yeehaw -x -s -h'.split(' ')) - assert.equals( - actual - , { - main: 'build' - , packages: [ 'fee', 'fie', 'foe', 'fum' ] - , output: 'foobar' - , use: 'yeehaw' - , noop: true - , silent: true - , help: true - } - ) - } - } - - , 'aliases': { - 'test set = add': function () { - var actual = argsParse.parse(buildargs('set')) - assert.isString(actual.main) - assert.equals(actual.main, 'add') - } - - , 'test rm = remove': function () { - var actual = argsParse.parse(buildargs('rm')) - assert.isString(actual.main) - assert.equals(actual.main, 'remove') - } - - , 'test ls = info': function () { - var actual = argsParse.parse(buildargs('ls')) - assert.isString(actual.main) - assert.equals(actual.main, 'info') - } - - , 'test list = info': function () { - var actual = argsParse.parse(buildargs('list')) - assert.isString(actual.main) - assert.equals(actual.main, 'info') - } - } - - , 'extend': { - 'test no specials': function () { - var originalArgs = { - main: 'build' - , packages: [ 'fee', 'fie', 'foe', 'fum' ] - } - , newArgs = { - main: 'add' - , packages: [ 'baz', 'bing' ] - } - , expectedArgs = { - main: 'build' - , packages: [ 'fee', 'fie', 'foe', 'fum', 'baz', 'bing' ] - } - - assert.equals(argsParse.extend(originalArgs, newArgs), expectedArgs) - } - - , 'test with specials': function () { - var originalArgs = { - main: 'build' - , packages: [ 'fee', 'fie', 'foe', 'fum' ] - , sandbox: [ 'foo' ] - , use: 'yeehaw' - , silent: true - , help: true - } - , newArgs = { - main: 'add' - , packages: [ 'baz', 'bing' ] - , sandbox: [ 'bar', 'baz' ] - , noop: true - , silent: true - } - , expectedArgs = { - main: 'build' - , packages: [ 'fee', 'fie', 'foe', 'fum', 'baz', 'bing' ] - , sandbox: [ 'foo', 'bar', 'baz' ] - , use: 'yeehaw' - , silent: true - , help: true - , noop: true - } - - assert.equals(argsParse.extend(originalArgs, newArgs), expectedArgs) - } - } - - , 'toContextString': { - 'test no specials': function () { - var actual = argsParse.toContextString(argsParse.parse(buildargs('build fee fie foe fum'))) - assert.equals(actual, 'build fee fie foe fum') - } - - , 'test "-" short form': function () { - var ctx = argsParse.toContextString(argsParse.parse(buildargs('build fee fie foe fum -o foobar -u yeehaw -x -s -h'))) - assert(ctx) - assert.equals(ctx.split(' ').length, 12) - ctx += ' ' // for convenience so we can match spaces around each element, even at the end - assert.match(ctx, /^build fee fie foe fum /) - assert.match(ctx, / --output foobar /) - assert.match(ctx, / --use yeehaw /) - assert.match(ctx, / --noop /) - assert.match(ctx, / --silent /) - assert.match(ctx, / --help /) - } - - , 'test array arg stops at next "-/--"': function () { - // this test doesn't really need to be here but we may as well confirm - var ctx = argsParse.toContextString(argsParse.parse(buildargs('build fee fie --sandbox foo bar --noop foe fum'))) - assert.equals(ctx, 'build fee fie foe fum --sandbox foo bar --noop') - } - - , 'test "--" long form': function () { - var ctx = argsParse.toContextString( - argsParse.parse( - buildargs( - 'build fee fie foe fum --output foobar --use yeehaw --max 10 --sandbox foo bar --noop --silent --help --sans --debug --externs what tha --client-lib BOOM --quiet --force-install' - ))) - assert(ctx) - assert.equals(ctx.split(' ').length, 26) - ctx += ' ' // for convenience so we can match spaces around each element, even at the end - assert.match(ctx, /^build fee fie foe fum /) - assert.match(ctx, / --output foobar /) - assert.match(ctx, / --use yeehaw /) - assert.match(ctx, / --max 10 /) - assert.match(ctx, / --sandbox foo bar /) - assert.match(ctx, / --noop /) - assert.match(ctx, / --silent /) - assert.match(ctx, / --help /) - assert.match(ctx, / --sans /) - assert.match(ctx, / --debug /) - assert.match(ctx, / --externs what tha /) - assert.match(ctx, / --client-lib BOOM /) - assert.match(ctx, / --quiet /) - assert.match(ctx, / --force-install /) - } - } -}) diff --git a/test/unit/main-add-test.js b/test/unit/main-add-test.js index adc7dca..2f181eb 100644 --- a/test/unit/main-add-test.js +++ b/test/unit/main-add-test.js @@ -25,15 +25,15 @@ var testCase = require('buster').testCase , requireSubvert = require('require-subvert')(__dirname) + , argsParser = require('ender-args-parser') , util = require('../../lib/util') , mainBuild = require('../../lib/main-build') - , argsParse = require('../../lib/args-parse') , mainAdd testCase('Add', { 'test basic add': function (done) { var utilMock = this.mock(util) - , argsParseMock = this.mock(argsParse) + , argsParserMock = this.mock(argsParser) , mainBuildMock = this.mock(mainBuild) , parseContextStub = this.stub() , optionsArg = { options: 1 } @@ -47,7 +47,7 @@ testCase('Add', { mainAdd = requireSubvert.require('../../lib/main-add') utilMock.expects('getInputFilenameFromOptions').once().withExactArgs(optionsArg).returns(filenameArg) - argsParseMock.expects('extend').once().withExactArgs(contextArg.options, optionsArg).returns(extendedOptionsArg) + argsParserMock.expects('extend').once().withExactArgs(contextArg.options, optionsArg).returns(extendedOptionsArg) mainBuildMock.expects('exec').once().withExactArgs(extendedOptionsArg, outArg, done).callsArg(2) mainAdd.exec(optionsArg, outArg, done) diff --git a/test/unit/main-test.js b/test/unit/main-test.js index 4178235..8c0ebed 100644 --- a/test/unit/main-test.js +++ b/test/unit/main-test.js @@ -23,35 +23,33 @@ */ -var buster = require('buster') - , main = require('../../lib/main') - , mainSearch = require('../../lib/main-search') - , mainSearchOut = require('../../lib/output/main-search-output') - , argsParse = require('../../lib/args-parse') +var buster = require('buster') + , argsParser = require('ender-args-parser') + , main = require('../../lib/main') + , mainSearch = require('../../lib/main-search') buster.testCase('Main program', { 'test main has exec': function () { assert.isFunction(main.exec) } - , 'test main calls argsParse to parse arguments': function () { - var argsParseMock = this.mock(argsParse) + , 'test main calls argsParser to parse arguments': function () { + var argsParserMock = this.mock(argsParser) , expectedArgs = [ 'foo', 'bar', 'search' ] - argsParseMock.expects('parse').once().withArgs(expectedArgs).returns(null) + argsParserMock.expects('parse').once().withArgs(expectedArgs).returns(null) main.exec(expectedArgs) assert(true) } - , 'test main loads main module as specified by args-parse': function (done) { - var argsParseMock = this.mock(argsParse) + , 'test main loads main module as specified by args-parser': function (done) { + var argsParserMock = this.mock(argsParser) , mainSearchMock = this.mock(mainSearch) - , mainSearchOutMock = this.mock(mainSearchOut) , expectedArgs = [ 'foo', 'bar', 'search' ] , argsArg = { main: 'search' } - argsParseMock.expects('parse').once().withArgs(expectedArgs).returns(argsArg) + argsParserMock.expects('parse').once().withArgs(expectedArgs).returns(argsArg) mainSearchMock.expects('exec').once().withArgs(argsArg).callsArg(2) main.exec(expectedArgs, done) @@ -59,15 +57,14 @@ buster.testCase('Main program', { } , 'test API exec(string, cb) call': function (done) { - var argsParseMock = this.mock(argsParse) + var argsParserMock = this.mock(argsParser) , mainSearchMock = this.mock(mainSearch) - , mainSearchOutMock = this.mock(mainSearchOut) , expectedArgs = [ 'search', 'foo', 'bar' ] , argsArg = { main: 'search' } // note the difference hear is that the first 2 elements of our args array aren't // discarded, only the unnecessary 'ender' element (below) - argsParseMock.expects('parseClean').once().withArgs(expectedArgs).returns(argsArg) + argsParserMock.expects('parseClean').once().withArgs(expectedArgs).returns(argsArg) mainSearchMock.expects('exec').once().withArgs(argsArg).callsArg(2) main.exec('ender ' + expectedArgs.join(' '), done) diff --git a/test/unit/parse-context-test.js b/test/unit/parse-context-test.js index c19dde8..5bc1037 100644 --- a/test/unit/parse-context-test.js +++ b/test/unit/parse-context-test.js @@ -25,9 +25,9 @@ var testCase = require('buster').testCase , fs = require('fs') + , argsParser = require('ender-args-parser') , parseContext = require('../../lib/parse-context') , BuildParseError = require('../../lib/errors').BuildParseError - , UnknownMainError = require('../../lib/errors').UnknownMainError , FilesystemError = require('../../lib/errors').FilesystemError testCase('parseContext', { @@ -120,7 +120,7 @@ testCase('parseContext', { refute(packages) assert.equals(err.name, 'BuildParseError') assert(err.cause) - assert(err.cause instanceof UnknownMainError) + assert(err.cause instanceof argsParser.UnknownMainError) assert.equals(err.cause.name, 'UnknownMainError') done() }) diff --git a/test/unit/source-build-test.js b/test/unit/source-build-test.js index 4eecf45..1b76d47 100644 --- a/test/unit/source-build-test.js +++ b/test/unit/source-build-test.js @@ -23,11 +23,11 @@ */ -var testCase = require('buster').testCase +var testCase = require('buster').testCase + , argsParser = require('ender-args-parser') , SourcePackage = require('../../lib/source-package') - , SourceBuild = require('../../lib/source-build') - , minify = require('../../lib/minify') - , argsParse = require('../../lib/args-parse') + , SourceBuild = require('../../lib/source-build') + , minify = require('../../lib/minify') var createExpectedHeader = function (context, packageList) { return [ @@ -51,9 +51,9 @@ testCase('Source build', { pkg.__defineGetter__('identifier', function () { return identifier }) // sinon can't mock getters return pkg } - this.createArgsParseMock = function (optionsArg, contextArg) { - var argsParseMock = this.mock(argsParse) - argsParseMock.expects('toContextString').withExactArgs(optionsArg).once().returns(contextArg) + this.createArgsParserMock = function (optionsArg, contextArg) { + var argsParserMock = this.mock(argsParser) + argsParserMock.expects('toContextString').withExactArgs(optionsArg).once().returns(contextArg) } } @@ -75,7 +75,7 @@ testCase('Source build', { + pkg3Content , mockMinify = this.mock(minify) - this.createArgsParseMock(optionsArg, contextArg) + this.createArgsParserMock(optionsArg, contextArg) srcBuild.addPackage(pkg1) srcBuild.addPackage(pkg2) srcBuild.addPackage(pkg3) @@ -107,7 +107,7 @@ testCase('Source build', { , minifiedSource = 'this is minified, these are not the droids you are looking for' , mockMinify = this.mock(minify) - this.createArgsParseMock(optionsArg, contextArg) + this.createArgsParserMock(optionsArg, contextArg) srcBuild.addPackage(pkg1) srcBuild.addPackage(pkg2) srcBuild.addPackage(pkg3) @@ -138,7 +138,7 @@ testCase('Source build', { + '\n\n}.call({});' , mockMinify = this.mock(minify) - this.createArgsParseMock(optionsArg, contextArg) + this.createArgsParserMock(optionsArg, contextArg) srcBuild.addPackage(pkg1) srcBuild.addPackage(pkg2) srcBuild.addPackage(pkg3) @@ -169,7 +169,7 @@ testCase('Source build', { , minifiedSource = 'this is minified, these are not the droids you are looking for' , mockMinify = this.mock(minify) - this.createArgsParseMock(optionsArg, contextArg) + this.createArgsParserMock(optionsArg, contextArg) srcBuild.addPackage(pkg1) srcBuild.addPackage(pkg2) srcBuild.addPackage(pkg3)