Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

extracted parse-context.js. added require-subvert

  • Loading branch information...
commit ae728d8eac72aee9c66b30178dfd47f64792086e 1 parent 7928da3
Rod Vagg rvagg authored
4 lib/main-add.js
View
@@ -34,13 +34,13 @@
var util = require('./util')
, argsParse = require('./args-parse')
+ , parseContext = require('./parse-context')
, mainBuild = require('./main-build')
- , mainInfoUtil = require('./main-info-util')
, exec = function (options, out, callback) {
var filename = util.getInputFilenameFromOptions(options)
; delete options.use // don't want --use showing up in the 'Build:' context string
- mainInfoUtil.parseContext(filename, function (err, context) {
+ 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)
13 lib/main-info-util.js
View
@@ -31,7 +31,6 @@ var zlib = require('zlib')
, fs = require('fs')
, async = require('async')
, minify = require('./minify')
- , SourceBuild = require('./source-build')
, FilesystemError = require('./errors').FilesystemError
// given a filename, return the 'raw', 'minified' and 'gzipped' sizes
@@ -69,18 +68,6 @@ var zlib = require('zlib')
)
}
- // a simple interface to SourceBuild.parseContext()
- , parseContext = function (filename, callback) {
- SourceBuild.parseContext(filename, function (err, options, packages) {
- if (err) return callback(err) // err wrapped in SourceBuild.parseContext()
- callback(null, {
- options : options
- , packages : packages
- })
- })
- }
-
module.exports = {
sizes : sizes
- , parseContext : parseContext
}
7 lib/main-info.js
View
@@ -30,9 +30,10 @@
* by main-info-output, we just provide the data/model here.
*/
-var util = require('./util')
+var DependencyGraph = require('ender-dependency-graph')
+ , util = require('./util')
, mainInfoUtil = require('./main-info-util')
- , DependencyGraph = require('ender-dependency-graph')
+ , parseContext = require('./parse-context')
, generateAndPrint = function (args, out, filename, options, packages, dependencyGraph, callback) {
// the 2 main processes here is generateSpec() to read the build context from the
@@ -46,7 +47,7 @@ var util = require('./util')
}
// read 'Build: ...' and 'Packages: ...' from the head of the build file
- mainInfoUtil.parseContext(filename, function (err, context) {
+ parseContext(filename, function (err, context) {
if (err) return callback(err) // wrapped in source-build.js
// same process to build a dependency tree as in the original build
5 lib/main-refresh.js
View
@@ -32,14 +32,13 @@
*/
var util = require('./util')
- , argsParse = require('./args-parse')
, mainBuild = require('./main-build')
- , mainInfoUtil = require('./main-info-util')
+ , parseContext = require('./parse-context')
, exec = function (options, out, callback) {
var filename = util.getInputFilenameFromOptions(options)
; delete options.use // don't want --use showing up in the 'Build:' context string
- mainInfoUtil.parseContext(filename, function (err, context) {
+ parseContext(filename, function (err, context) {
if (err) return callback(err) // err wrapped in SourceBuild.parseContext()
// set --force-install but don't leave a trace in the 'Build:' context string
4 lib/main-remove.js
View
@@ -38,8 +38,8 @@ var async = require('async')
, util = require('./util')
, mainBuild = require('./main-build')
, argsParse = require('./args-parse')
- , mainInfoUtil = require('./main-info-util')
, mainBuildUtil = require('./main-build-util')
+ , parseContext = require('./parse-context')
, exec = function (options, out, callback) {
var filename = util.getInputFilenameFromOptions(options)
@@ -54,7 +54,7 @@ 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
- mainInfoUtil.parseContext(filename, function (err, context) {
+ parseContext(filename, function (err, context) {
if (err) return callback(err) // wrapped in source-build.js
// merge the commandline with the ender.js build command
74 lib/parse-context.js
View
@@ -0,0 +1,74 @@
+/*!
+ * 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.
+ */
+
+/******************************************************************************
+ * 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).
+ */
+
+const fs = require('fs')
+ , argsParse = require('./args-parse')
+ , FilesystemError = require('./errors').FilesystemError
+ , BuildParseError = require('./errors').BuildParseError
+
+ // 'Packages:' is optional because it's not in <= 0.8.x Ender builds
+ , buildInfoRegex = /\n {2}\* Build: ender ([^\n]*)\s\S*(?:(?: {2}\* Packages: )([^\n]*))?/
+
+var parseContext = function (file, callback) {
+ fs.open(file, 'r', function (err, fd) {
+ if (err) return callback(new FilesystemError(err))
+
+ var buffer = new Buffer(2048)
+ fs.read(fd, buffer, 0, 2048, null, function (err, bytesRead, buffer) {
+ if (err) return callback(new FilesystemError(err))
+
+ fs.close(fd, function () {
+ // err? who cares, we have our data, let's use it and run for the hills!
+ var options
+ , error
+ , match = String(buffer).match(buildInfoRegex)
+
+ if (!match) {
+ error = 'Could not parse ender spec from "' + file + '" (not an Ender build file?)'
+ return callback(new BuildParseError(error))
+ }
+
+ try {
+ options = argsParse.parseClean(match[1].split(' '))
+ } catch (ex) {
+ error = 'Could not parse ender spec from "' + file + '"'
+ return callback(new BuildParseError(error, ex))
+ }
+
+ callback(null, {
+ options : options
+ , packages : match[2] && match[2].split(' ')
+ })
+ })
+ })
+ })
+ }
+
+module.exports = parseContext
42 lib/source-build.js
View
@@ -22,7 +22,6 @@
* SOFTWARE.
*/
-
/******************************************************************************
* SourceBuild is an object that is created for each Ender build to hold and
* manage multiple SourcePackage objects. It is able to pull together the
@@ -31,16 +30,11 @@
*/
var async = require('async')
- , fs = require('fs')
, util = require('./util')
, minify = require('./minify')
, template = require('./template')
, argsParse = require('./args-parse')
- , BuildParseError = require('./errors').BuildParseError
- , FilesystemError = require('./errors').FilesystemError
- // 'Packages:' is optional because it's not in <= 0.8.x Ender builds
- , buildInfoRegex = /\n {2}\* Build: ender ([^\n]*)\s\S*(?:(?: {2}\* Packages: )([^\n]*))?/
, templateFile = '../resources/build.mustache'
, SourceBuild = {
@@ -95,46 +89,10 @@ var async = require('async')
}
}
- // a utility static method 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).
- , parseContext = function (file, callback) {
- fs.open(file, 'r', function (err, fd) {
- if (err) return callback(new FilesystemError(err))
-
- var buffer = new Buffer(2048)
- fs.read(fd, buffer, 0, 2048, null, function (err, bytesRead, buffer) {
- if (err) return callback(new FilesystemError(err))
-
- fs.close(fd, function () {
- // err? who cares, we have our data, let's use it and run for the hills!
- var options
- , error
- , match = String(buffer).match(buildInfoRegex)
-
- if (!match) {
- error = 'Could not parse ender spec from "' + file + '" (not an Ender build file?)'
- return callback(new BuildParseError(error))
- }
-
- try {
- options = argsParse.parseClean(match[1].split(' '))
- } catch (ex) {
- error = 'Could not parse ender spec from "' + file + '"'
- return callback(new BuildParseError(error, ex))
- }
-
- callback(null, options, match[2] && match[2].split(' '))
- })
- })
- })
- }
-
, create = function (options) {
return Object.create(SourceBuild).init(options)
}
module.exports = {
create : create
- , parseContext : parseContext
}
9 package.json
View
@@ -29,10 +29,11 @@
, "bin" : "./bin"
}
, "devDependencies": {
- "buster" : "~0.6.12"
- , "rimraf" : "~2.1.1"
- , "jshint" : "~0.9.1"
- , "mkfiletree" : "~0.0.0"
+ "buster" : "~0.6.12"
+ , "rimraf" : "~2.1.1"
+ , "jshint" : "~0.9.1"
+ , "mkfiletree" : "~0.0.0"
+ , "require-subvert" : "~0.0.0"
}
, "bin": {
"ender" : "./bin/ender"
18 test/unit/install-test.js
View
@@ -23,10 +23,11 @@
*/
-var testCase = require('buster').testCase
- , repository = require('ender-repository')
- , util = require('../../lib/util')
- , installUtil = require('../../lib/install-util')
+var testCase = require('buster').testCase
+ , repository = require('ender-repository')
+ , requireSubvert = require('require-subvert')(__dirname)
+ , util = require('../../lib/util')
+ , installUtil = require('../../lib/install-util')
, install
require('ender-dependency-graph')
@@ -37,10 +38,8 @@ testCase('Install', {
this.mockInstallUtil = this.mock(installUtil)
this.mockRepository = this.mock(repository)
this.dependencyGraphStub = this.stub()
- this.originalEDG = require.cache[require.resolve('ender-dependency-graph')].exports
- require.cache[require.resolve('ender-dependency-graph')].exports = this.dependencyGraphStub
- require.cache[require.resolve('../../lib/install')] = null
- install = require('../../lib/install')
+ requireSubvert.subvert('ender-dependency-graph', this.dependencyGraphStub)
+ install = requireSubvert.require('../../lib/install')
this.optionsArg = { options: 1 }
this.packagesArg = [ 'yee', 'haw' ] // length 2
@@ -97,8 +96,7 @@ testCase('Install', {
}
, tearDown: function () {
- require.cache[require.resolve('ender-dependency-graph')].exports = this.originalEDG
- require.cache[require.resolve('../../lib/install.js')] = null
+ requireSubvert.cleanUp()
}
, 'test basic one package install, already available': function (done) {
41 test/unit/main-add-test.js
View
@@ -23,32 +23,41 @@
*/
-var testCase = require('buster').testCase
- , util = require('../../lib/util')
- , mainBuild = require('../../lib/main-build')
- , mainInfoUtil = require('../../lib/main-info-util')
- , argsParse = require('../../lib/args-parse')
- , mainAdd = require('../../lib/main-add')
+var testCase = require('buster').testCase
+ , requireSubvert = require('require-subvert')(__dirname)
+ , 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)
- , mainInfoUtilMock = this.mock(mainInfoUtil)
- , argsParseMock = this.mock(argsParse)
- , mainBuildMock = this.mock(mainBuild)
- , optionsArg = { options: 1 }
- , filenameArg = { filename: 1 }
- , contextArg = { options: { contextOptions: 1 } }
+ var utilMock = this.mock(util)
+ , argsParseMock = this.mock(argsParse)
+ , mainBuildMock = this.mock(mainBuild)
+ , parseContextStub = this.stub()
+ , optionsArg = { options: 1 }
+ , filenameArg = { filename: 1 }
+ , contextArg = { options: { contextOptions: 1 } }
, extendedOptionsArg = { extendedOptions: 1 }
- , outArg = { out: 1 }
+ , outArg = { out: 1 }
+
+ requireSubvert.subvert('../../lib/parse-context', parseContextStub)
+ parseContextStub.callsArgWith(1, null, contextArg)
+ mainAdd = requireSubvert.require('../../lib/main-add')
utilMock.expects('getInputFilenameFromOptions').once().withExactArgs(optionsArg).returns(filenameArg)
- mainInfoUtilMock.expects('parseContext').once().withArgs(filenameArg).callsArgWith(1, null, contextArg)
argsParseMock.expects('extend').once().withExactArgs(contextArg.options, optionsArg).returns(extendedOptionsArg)
mainBuildMock.expects('exec').once().withExactArgs(extendedOptionsArg, outArg, done).callsArg(2)
mainAdd.exec(optionsArg, outArg, done)
- assert(true) // required for buster, bug
+ assert.equals(parseContextStub.callCount, 1)
+ assert.equals(parseContextStub.getCall(0).args.length, 2)
+ assert.equals(parseContextStub.getCall(0).args[0], filenameArg)
+ }
+
+ , 'tearDown': function () {
+ requireSubvert.cleanUp()
}
})
28 test/unit/main-info-test.js
View
@@ -23,10 +23,11 @@
*/
-var testCase = require('buster').testCase
+var testCase = require('buster').testCase
+ , requireSubvert = require('require-subvert')(__dirname)
+ , mainInfoOut = require('../../lib/output/main-info-output').create()
+ , mainInfoUtil = require('../../lib/main-info-util')
, mainInfo
- , mainInfoOut = require('../../lib/output/main-info-output').create()
- , mainInfoUtil = require('../../lib/main-info-util')
testCase('Info', {
'setUp': function () {
@@ -35,6 +36,7 @@ testCase('Info', {
, mainInfoUtilMock = this.mock(mainInfoUtil)
, dependencyGraphStub = this.stub()
, archyTreeStub = this.stub()
+ , parseContextStub = this.stub()
, packagesArg = { packages: 1 }
, optionsPackagesArg = { optionsPackages: 1 }
, sizesArg = { sizes: 1 }
@@ -42,27 +44,21 @@ testCase('Info', {
, treeArg = { tree: 1 }
, archyTreeArg = { archyTree: 1 }
- require('ender-dependency-graph')
- this.originalEDG = require.cache[require.resolve('ender-dependency-graph')].exports
- require.cache[require.resolve('../../lib/main-info')] = null
- require.cache[require.resolve('ender-dependency-graph')].exports = dependencyGraphStub
dependencyGraphStub.archyTree = archyTreeStub
- mainInfo = require('../../lib/main-info')
+ requireSubvert.subvert('ender-dependency-graph', dependencyGraphStub)
+ requireSubvert.subvert('../../lib/parse-context', parseContextStub)
+ mainInfo = requireSubvert.require('../../lib/main-info')
mainInfoUtilMock
.expects('sizes')
.once()
.withArgs(contextArg.options, expectedFilename)
.callsArgWith(2, null, sizesArg)
- mainInfoUtilMock
- .expects('parseContext')
- .once()
- .withArgs(expectedFilename)
- .callsArgWith(1, null, contextArg)
dependencyGraphStub.callsArgWith(2, null, treeArg)
// important we use packages from context->options->packages which is the command-line packages
// and not context->packages which is the full list of packages in the build
archyTreeStub.returns(archyTreeArg)
+ parseContextStub.callsArgWith(1, null, contextArg)
mainInfoOutMock
.expects('buildInfo')
.once()
@@ -78,14 +74,16 @@ testCase('Info', {
assert.equals(archyTreeStub.getCall(0).args.length, 2)
assert.equals(archyTreeStub.getCall(0).args[0], optionsPackagesArg)
assert.equals(archyTreeStub.getCall(0).args[1], treeArg)
+ assert.equals(parseContextStub.callCount, 1)
+ assert.equals(parseContextStub.getCall(0).args.length, 2)
+ assert.equals(parseContextStub.getCall(0).args[0], expectedFilename)
done()
})
}
}
, 'tearDown': function () {
- require.cache[require.resolve('ender-dependency-graph')].exports = this.originalEDG
- require.cache[require.resolve('../../lib/main-info')] = null
+ requireSubvert.cleanUp()
}
, 'test no args': function (done) {
21 test/unit/main-info-util-test.js
View
@@ -28,8 +28,6 @@ var testCase = require('buster').testCase
, zlib = require('zlib')
, minify = require('../../lib/minify')
, mainInfoUtil = require('../../lib/main-info-util')
- , mainBuildUtil = require('../../lib/main-build-util')
- , SourceBuild = require('../../lib/source-build')
, FilesystemError = require('../../lib/errors').FilesystemError
, _i = 100
@@ -94,23 +92,4 @@ testCase('Info util', {
done()
})
}
-
- , 'test parseContest': function (done) {
- var sourceBuildMock = this.mock(SourceBuild)
- , optionsArg = { options: 1 }
- , packagesArg = { packages: 1 }
- , filenameArg = { filename: 1 }
-
- sourceBuildMock
- .expects('parseContext')
- .once()
- .withArgs(filenameArg)
- .callsArgWith(1, null, optionsArg, packagesArg)
-
- mainInfoUtil.parseContext(filenameArg, function (err, data) {
- refute(err)
- assert.equals(data, { options: optionsArg, packages: packagesArg })
- done()
- })
- }
})
39 test/unit/main-remove-test.js
View
@@ -23,28 +23,24 @@
*/
-var testCase = require('buster').testCase
- , repository = require('ender-repository')
- , util = require('../../lib/util')
- , mainBuild = require('../../lib/main-build')
- , mainInfoUtil = require('../../lib/main-info-util')
- , mainRemove = require('../../lib/main-remove')
+var testCase = require('buster').testCase
+ , repository = require('ender-repository')
+ , requireSubvert = require('require-subvert')(__dirname)
+ , util = require('../../lib/util')
+ , mainBuild = require('../../lib/main-build')
+ , mainRemove
testCase('Remove', {
'test basic remove': function (done) {
- var utilMock = this.mock(util)
- , mainInfoUtilMock = this.mock(mainInfoUtil)
- , mainBuildMock = this.mock(mainBuild)
- , repositoryMock = this.mock(repository)
- , optionsArg = {
+ var utilMock = this.mock(util)
+ , mainBuildMock = this.mock(mainBuild)
+ , repositoryMock = this.mock(repository)
+ , parseContextStub = this.stub()
+ , optionsArg = {
packages: [ 'bing', 'bar' ]
, main: 'remove'
, noop: true
}
- , expectedModifiedOptions = {
- packages: [] // expect it to not pass through the packages that we want to remove
- , noop: true
- }
, filenameArg = { filename: 1 }
, contextArg = { options: {
packages: [ 'foo', 'bar', 'bing', 'bang' ]
@@ -60,7 +56,10 @@ testCase('Remove', {
, outArg = { out: 1 }
utilMock.expects('getInputFilenameFromOptions').once().withExactArgs(optionsArg).returns(filenameArg)
- mainInfoUtilMock.expects('parseContext').once().withArgs(filenameArg).callsArgWith(1, null, contextArg)
+ requireSubvert.subvert('../../lib/parse-context', parseContextStub)
+ parseContextStub.callsArgWith(1, null, contextArg)
+ mainRemove = requireSubvert.require('../../lib/main-remove')
+
mainBuildMock.expects('exec').once().withArgs(expectedBuildOptions, outArg).callsArg(2)
repositoryMock.expects('setup').once().callsArg(0)
repositoryMock.expects('uninstall').once().withArgs(optionsArg.packages).callsArgWith(1)
@@ -68,6 +67,12 @@ testCase('Remove', {
mainRemove.exec(optionsArg, outArg, done)
- assert(true) // required for buster, bug
+ assert.equals(parseContextStub.callCount, 1)
+ assert.equals(parseContextStub.getCall(0).args.length, 2)
+ assert.equals(parseContextStub.getCall(0).args[0], filenameArg)
+ }
+
+ , 'tearDown': function () {
+ requireSubvert.cleanUp()
}
})
40 test/unit/minify-test.js
View
@@ -23,39 +23,37 @@
*/
-var buster = require('buster')
- , assert = buster.assert
+var buster = require('buster')
+ , assert = buster.assert
+ , requireSubvert = require('require-subvert')(__dirname)
buster.testCase('Minify', {
'setUp': function () {
- this.enderMinifyStub = this.stub()
- this.sourceArg = { source: 1 }
- this.resultArg = { result: 1 }
- require('ender-minify')
- this.originalEM = require.cache[require.resolve('ender-minify')].exports
- require.cache[require.resolve('ender-minify')].exports = this.enderMinifyStub
- this.enderMinifyStub.minifiers = this.originalEM.minifiers
- this.enderMinifyStub.closureLevels = this.originalEM.closureLevels
- this.enderMinifyStub.callsArgWith(3, null, this.resultArg)
+ var enderMinifyStub = this.stub()
+ , sourceArg = { source: 1 }
+ , resultArg = { result: 1 }
+
+ enderMinifyStub.minifiers = require('ender-minify').minifiers
+ enderMinifyStub.closureLevels = require('ender-minify').closureLevels
+ enderMinifyStub.callsArgWith(3, null, resultArg)
+ requireSubvert.subvert('ender-minify', enderMinifyStub)
this.runTest = function (minifier, expectedOptions, parsedArgs, done) {
- require.cache[require.resolve('../../lib/minify.js')] = null
- require('../../lib/minify').minify(parsedArgs, this.sourceArg, function (err, result) {
+ requireSubvert.require('../../lib/minify').minify(parsedArgs, sourceArg, function (err, result) {
refute(err)
- assert.same(result, this.resultArg)
- assert.equals(this.enderMinifyStub.callCount, 1)
- assert.equals(this.enderMinifyStub.getCall(0).args.length, 4)
- assert.equals(this.enderMinifyStub.getCall(0).args[0], minifier)
- assert.same(this.enderMinifyStub.getCall(0).args[1], this.sourceArg)
- assert.equals(this.enderMinifyStub.getCall(0).args[2], expectedOptions)
+ assert.same(result, resultArg)
+ assert.equals(enderMinifyStub.callCount, 1)
+ assert.equals(enderMinifyStub.getCall(0).args.length, 4)
+ assert.equals(enderMinifyStub.getCall(0).args[0], minifier)
+ assert.same(enderMinifyStub.getCall(0).args[1], sourceArg)
+ assert.equals(enderMinifyStub.getCall(0).args[2], expectedOptions)
done()
}.bind(this))
}.bind(this)
}
, tearDown: function () {
- require.cache[require.resolve('ender-minify')].exports = this.originalEM
- require.cache[require.resolve('../../lib/minify.js')] = null
+ requireSubvert.cleanUp()
}
, 'test basic minify, default to uglify': function (done) {
187 test/unit/parse-context-test.js
View
@@ -0,0 +1,187 @@
+/*!
+ * 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 testCase = require('buster').testCase
+ , fs = require('fs')
+ , parseContext = require('../../lib/parse-context')
+ , BuildParseError = require('../../lib/errors').BuildParseError
+ , UnknownMainError = require('../../lib/errors').UnknownMainError
+ , FilesystemError = require('../../lib/errors').FilesystemError
+
+testCase('parseContext', {
+ 'test simple old-skool parse': function (done) {
+ var content = ''
+ + '/*!\n'
+ + ' * =============================================================\n'
+ + ' * Ender: open module JavaScript framework (https://ender.no.de)\n'
+ + ' * Build: ender build foo bar baz --use blah --sandbox foo\n'
+ + ' * =============================================================\n'
+ + ' */\n\n'
+ + arguments.callee.toString()
+
+ , expectedOptions = {
+ main: 'build'
+ , packages: [ 'foo', 'bar', 'baz' ]
+ , use: 'blah'
+ , sandbox: [ 'foo' ]
+ }
+ , filename = 'somefile'
+ , mockFs = this.mock(fs)
+ , fdArg = 99
+
+ mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
+ mockFs.expects('read').withArgs(fdArg).callsArgWith(5, null, 1, new Buffer(content))
+
+ parseContext(filename, function (err, data) {
+ refute(err)
+ assert(data)
+ refute(data.packages)
+ assert.equals(data.options, expectedOptions)
+ done()
+ })
+ }
+
+ , 'test simple new-style parse': function (done) {
+ var expectedPackages = 'ender-js@0.3.7 bean@0.4.9 qwery@3.3.3 bonzo@1.0.1 domready@0.2.11 bowser@0.1.0'.split(' ')
+ , content = ''
+ + '/*!\n'
+ + ' * =============================================================\n'
+ + ' * Ender: open module JavaScript framework (https://ender.no.de)\n'
+ + ' * Build: ender build foo bar baz --use blah --sandbox foo\n'
+ + ' * Packages: ' + expectedPackages.join(' ') + '\n'
+ + ' * =============================================================\n'
+ + ' */\n\n'
+ + arguments.callee.toString()
+
+ , expectedOptions = {
+ main: 'build'
+ , packages: [ 'foo', 'bar', 'baz' ]
+ , use: 'blah'
+ , sandbox: [ 'foo' ]
+ }
+ , filename = 'somefile'
+ , mockFs = this.mock(fs)
+ , fdArg = 99
+
+ mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
+ mockFs.expects('read').withArgs(fdArg).callsArgWith(5, null, 1, new Buffer(content))
+
+ parseContext(filename, function (err, data) {
+ refute(err)
+ assert(data)
+ assert.equals(data.options, expectedOptions)
+ assert.equals(data.packages, expectedPackages)
+ done()
+ })
+ }
+
+ , 'test bad build parse (bad ender spec)': function (done) {
+ var content = ''
+ + '/*!\n'
+ + ' * =============================================================\n'
+ + ' * Ender: open module JavaScript framework (https://ender.no.de)\n'
+ + ' * Build: ender not a real build command\n'
+ + ' * =============================================================\n'
+ + ' */\n\n'
+ + arguments.callee.toString()
+
+ , filename = 'somefile'
+ , mockFs = this.mock(fs)
+ , fdArg = 99
+
+ mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
+ mockFs.expects('read').withArgs(fdArg).callsArgWith(5, null, 1, new Buffer(content))
+
+ parseContext(filename, function (err, options, packages) {
+ assert(err)
+ refute(options)
+ refute(packages)
+ assert.equals(err.name, 'BuildParseError')
+ assert(err.cause)
+ assert(err.cause instanceof UnknownMainError)
+ assert.equals(err.cause.name, 'UnknownMainError')
+ done()
+ })
+ }
+
+ , 'test bad build parse (not an ender file)': function (done) {
+ var content = arguments.callee.toString()
+ , filename = 'somefile'
+ , mockFs = this.mock(fs)
+ , fdArg = 99
+
+ mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
+ mockFs.expects('read').withArgs(fdArg).callsArgWith(5, null, 1, new Buffer(content))
+ mockFs.expects('close').withArgs(fdArg).callsArg(1)
+
+ parseContext(filename, function (err, options, packages) {
+ assert(err)
+ refute(options)
+ refute(packages)
+ assert(err instanceof BuildParseError)
+ assert.equals(err.name, 'BuildParseError')
+ refute(err.cause)
+ done()
+ })
+ }
+
+ , 'test no such file error': function (done) {
+ var filename = 'somefile'
+ , mockFs = this.mock(fs)
+ , errArg = new Error('this is an error')
+
+ mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, errArg)
+
+ parseContext(filename, function (err, options, packages) {
+ assert(err)
+ refute(options)
+ refute(packages)
+ assert(err instanceof FilesystemError)
+ assert.same(err.cause, errArg)
+ assert.same(err.message, errArg.message)
+ done()
+ })
+ }
+
+ , 'test file read error': function (done) {
+ var filename = 'somefile'
+ , mockFs = this.mock(fs)
+ , errArg = new Error('this is an error')
+ , fdArg = 99
+
+ mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
+ mockFs.expects('read').withArgs(fdArg).callsArgWith(5, errArg)
+
+ parseContext(filename, function (err, options, packages) {
+ assert(err)
+ refute(options)
+ refute(packages)
+ assert(err instanceof FilesystemError)
+ assert.same(err.cause, errArg)
+ assert.same(err.message, errArg.message)
+ done()
+ })
+ }
+})
158 test/unit/source-build-test.js
View
@@ -24,14 +24,10 @@
var testCase = require('buster').testCase
- , fs = require('fs')
- , BuildParseError = require('../../lib/errors').BuildParseError
- , UnknownMainError = require('../../lib/errors').UnknownMainError
, SourcePackage = require('../../lib/source-package')
, SourceBuild = require('../../lib/source-build')
, minify = require('../../lib/minify')
, argsParse = require('../../lib/args-parse')
- , FilesystemError = require('../../lib/errors').FilesystemError
var createExpectedHeader = function (context, packageList) {
return [
@@ -197,158 +193,4 @@ testCase('Source build', {
})
}
}
-
- , 'parseContext': {
- 'test simple old-skool parse': function (done) {
- var content = ''
- + '/*!\n'
- + ' * =============================================================\n'
- + ' * Ender: open module JavaScript framework (https://ender.no.de)\n'
- + ' * Build: ender build foo bar baz --use blah --sandbox foo\n'
- + ' * =============================================================\n'
- + ' */\n\n'
- + arguments.callee.toString()
-
- , expectedOptions = {
- main: 'build'
- , packages: [ 'foo', 'bar', 'baz' ]
- , use: 'blah'
- , sandbox: [ 'foo' ]
- }
- , filename = 'somefile'
- , mockFs = this.mock(fs)
- , fdArg = 99
-
- mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
- mockFs.expects('read').withArgs(fdArg).callsArgWith(5, null, 1, new Buffer(content))
-
- SourceBuild.parseContext(filename, function (err, options, packages) {
- refute(err)
- refute(packages)
- assert.equals(options, expectedOptions)
- done()
- })
- }
-
- , 'test simple new-style parse': function (done) {
- var expectedPackages = 'ender-js@0.3.7 bean@0.4.9 qwery@3.3.3 bonzo@1.0.1 domready@0.2.11 bowser@0.1.0'.split(' ')
- , content = ''
- + '/*!\n'
- + ' * =============================================================\n'
- + ' * Ender: open module JavaScript framework (https://ender.no.de)\n'
- + ' * Build: ender build foo bar baz --use blah --sandbox foo\n'
- + ' * Packages: ' + expectedPackages.join(' ') + '\n'
- + ' * =============================================================\n'
- + ' */\n\n'
- + arguments.callee.toString()
-
- , expectedOptions = {
- main: 'build'
- , packages: [ 'foo', 'bar', 'baz' ]
- , use: 'blah'
- , sandbox: [ 'foo' ]
- }
- , filename = 'somefile'
- , mockFs = this.mock(fs)
- , fdArg = 99
-
- mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
- mockFs.expects('read').withArgs(fdArg).callsArgWith(5, null, 1, new Buffer(content))
-
- SourceBuild.parseContext(filename, function (err, options, packages) {
- refute(err)
- assert.equals(options, expectedOptions)
- assert.equals(packages, expectedPackages)
- done()
- })
- }
-
- , 'test bad build parse (bad ender spec)': function (done) {
- var content = ''
- + '/*!\n'
- + ' * =============================================================\n'
- + ' * Ender: open module JavaScript framework (https://ender.no.de)\n'
- + ' * Build: ender not a real build command\n'
- + ' * =============================================================\n'
- + ' */\n\n'
- + arguments.callee.toString()
-
- , filename = 'somefile'
- , mockFs = this.mock(fs)
- , fdArg = 99
-
- mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
- mockFs.expects('read').withArgs(fdArg).callsArgWith(5, null, 1, new Buffer(content))
-
- SourceBuild.parseContext(filename, function (err, options, packages) {
- assert(err)
- refute(options)
- refute(packages)
- assert.equals(err.name, 'BuildParseError')
- assert(err.cause)
- assert(err.cause instanceof UnknownMainError)
- assert.equals(err.cause.name, 'UnknownMainError')
- done()
- })
- }
-
- , 'test bad build parse (not an ender file)': function (done) {
- var content = arguments.callee.toString()
- , filename = 'somefile'
- , mockFs = this.mock(fs)
- , fdArg = 99
-
- mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
- mockFs.expects('read').withArgs(fdArg).callsArgWith(5, null, 1, new Buffer(content))
- mockFs.expects('close').withArgs(fdArg).callsArg(1)
-
- SourceBuild.parseContext(filename, function (err, options, packages) {
- assert(err)
- refute(options)
- refute(packages)
- assert(err instanceof BuildParseError)
- assert.equals(err.name, 'BuildParseError')
- refute(err.cause)
- done()
- })
- }
-
- , 'test no such file error': function (done) {
- var filename = 'somefile'
- , mockFs = this.mock(fs)
- , errArg = new Error('this is an error')
-
- mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, errArg)
-
- SourceBuild.parseContext(filename, function (err, options, packages) {
- assert(err)
- refute(options)
- refute(packages)
- assert(err instanceof FilesystemError)
- assert.same(err.cause, errArg)
- assert.same(err.message, errArg.message)
- done()
- })
- }
-
- , 'test file read error': function (done) {
- var filename = 'somefile'
- , mockFs = this.mock(fs)
- , errArg = new Error('this is an error')
- , fdArg = 99
-
- mockFs.expects('open').withArgs(filename, 'r').callsArgWith(2, null, fdArg)
- mockFs.expects('read').withArgs(fdArg).callsArgWith(5, errArg)
-
- SourceBuild.parseContext(filename, function (err, options, packages) {
- assert(err)
- refute(options)
- refute(packages)
- assert(err instanceof FilesystemError)
- assert.same(err.cause, errArg)
- assert.same(err.message, errArg.message)
- done()
- })
- }
- }
})
Please sign in to comment.
Something went wrong with that request. Please try again.