Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

extracted ender-builder package

  • Loading branch information...
commit 7761da787b9df17a56ef3eea413382a0b05e6844 1 parent 10d060f
@rvagg rvagg authored
View
8 lib/install.js
@@ -38,10 +38,10 @@
* by a path (i.e. local).
*/
-var async = require('async')
- , repository = require('ender-repository')
- , packageUtil = repository.util
- , installUtil = require('./install-util')
+var async = require('async')
+ , repository = require('ender-repository')
+ , packageUtil = repository.util
+ , installUtil = require('./install-util')
, DependencyGraph = require('ender-dependency-graph')
, installPackages = function (options, packages, callback) {
View
44 lib/main-build.js
@@ -31,36 +31,14 @@
* the assembling work while the write module outputs the results.
*/
-var path = require('path')
- , packageUtil = require('ender-repository').util
- , util = require('./util')
- , write = require('./write')
- , mainInfo = require('./main-info')
- , buildUtil = require('./main-build-util')
- , install = require('./install')
- , SourceBuild = require('./source-build')
- , SourcePackage = require('./source-package')
+var builder = require('ender-builder')
+ , mainInfo = require('./main-info')
+ , buildUtil = require('./main-build-util')
+ , install = require('./install')
- , handle = function (options, packages, out, installedPackages, dependencyTree, callback) {
+ , handle = function (options, packages, out, installedPackages, dependencyGraph, callback) {
if (out && installedPackages) out.installedFromRepository(installedPackages.length)
-
- // new SourceBuild object to store each package in
- var srcBuild = SourceBuild.create(options)
- // sanitise and localise the names from relative paths
- , localizedPackages = dependencyTree.localizePackageList(packages)
- , rootPackageName = util.getRootPackageName(options)
-
- // DependencyTree does all the hard work of collecting and ordering dependencies for us
- dependencyTree.forEachUniqueOrderedDependency(localizedPackages, function (packageName, parents, data) {
- var pidx = localizedPackages.indexOf(packageName)
- , isRoot = (pidx != -1 && packages[pidx] == rootPackageName) || packageName == rootPackageName
-
- // each package that we need, add it to SourceBuild as a SourcePackage object
- srcBuild.addPackage(SourcePackage.create(packageName, parents, isRoot, data.packageJSON, options))
- })
-
- // write the output files!
- write.write(options, srcBuild, out, function (err) {
+ builder(options, packages, dependencyGraph, function (err, outputFilename) {
if (err) return callback(err) // wrapped in write.js
out.finishedAssembly()
@@ -71,10 +49,10 @@ var path = require('path')
mainInfo.generateAndPrint(
options
, out
- , util.getOutputFilenameFromOptions(options)
+ , outputFilename
, options
- , localizedPackages
- , dependencyTree
+ , dependencyGraph.localizePackageList(packages)
+ , dependencyGraph
, callback
)
}
@@ -88,10 +66,10 @@ var path = require('path')
// install takes care of collecting and organising dependencies for us and simply returns
// a DependencyTree object that we can use to assemble a build
- install.installPackages(options, packages, function (err, installedPackages, dependencyTree) {
+ install.installPackages(options, packages, function (err, installedPackages, dependencyGraph) {
if (err) return callback(err) // wrapped in repository.js
- handle(options, packages, out, installedPackages, dependencyTree, callback)
+ handle(options, packages, out, installedPackages, dependencyGraph, callback)
})
}
View
8 lib/main-info-util.js
@@ -30,7 +30,7 @@
var zlib = require('zlib')
, fs = require('fs')
, async = require('async')
- , minify = require('./minify')
+ , enderBuilder = require('ender-builder')
, FilesystemError = require('./errors').FilesystemError
// given a filename, return the 'raw', 'minified' and 'gzipped' sizes
@@ -52,7 +52,7 @@ var zlib = require('zlib')
if (options.minifier != 'none') {
jobs.push(function (data, callback) {
- minify.minify(options, data, mkcb('minify', callback))
+ enderBuilder.minify(options, data, mkcb('minify', callback))
})
jobs.push(function (data, callback) {
zlib.gzip(data, mkcb('gzip', callback))
@@ -68,6 +68,4 @@ var zlib = require('zlib')
)
}
-module.exports = {
- sizes : sizes
-}
+module.exports.sizes = sizes
View
54 lib/minify.js
@@ -1,54 +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.
- */
-
-
-/******************************************************************************
- * An interface to UglifyJS. Preserves copyright comments, which UglifyJS
- * currently doesn't do: https://github.com/mishoo/UglifyJS/issues/85
- */
-
-var enderMinify = require('ender-minify')
- , MinifyError = require('./errors').MinifyError
-
- , minify = function (options, source, callback) {
- var minifier = enderMinify.minifiers.indexOf(options.minifier) > -1 ? options.minifier : 'uglify'
- , minifyOptions = {}
-
- if (minifier == 'closure') {
- if (enderMinify.closureLevels.indexOf(options.level) > -1) {
- minifyOptions.level = options.level
- }
- if (Array.isArray(options.externs)) {
- minifyOptions.externs = options.externs
- }
- }
-
- enderMinify(minifier, source, minifyOptions, function (err, minifiedSource) {
- if (err) return callback(new MinifyError(err))
-
- callback(null, minifiedSource)
- })
- }
-
-module.exports.minify = minify
View
98 lib/source-build.js
@@ -1,98 +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.
- */
-
-/******************************************************************************
- * 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
- * source files through its asString() method which in turn invokes asString()
- * on the list of SourcePackages.
- */
-
-var async = require('async')
- , argsParser = require('ender-args-parser')
- , util = require('./util')
- , minify = require('./minify')
- , template = require('./template')
-
- , templateFile = '../resources/build.mustache'
-
- , SourceBuild = {
- init: function (options) {
- this.options = options
- this.packages = []
- return this
- }
-
- , addPackage: function (srcPackage) { // add a SourcePackage object for each package
- this.packages.push(srcPackage)
- }
-
- , asString: function (options, callback) {
- //options.type == plain||minified
- var finish = function (err, source) {
- if (err) return callback(err) // wrapped in template.js
- if (options.type != 'minified') return callback(null, source)
- minify.minify(this.completeOptions(), source, callback)
- }.bind(this)
-
- , tmplData = function (sources) {
- return {
- source: sources.join('\n\n')
- , context: argsParser.toContextString(this.options)
- , sandbox: !!this.options.sandbox
- , packages: this.packages.map(function (p) {
- return p.identifier
- }).join(' ')
- }
- }.bind(this)
-
- , readComplete = function (err, sources) {
- if (err) return callback(err) // wrapped in source-package.js
- template.generateSource(templateFile, tmplData(sources), finish)
- }.bind(this)
-
- , packageToString = function (srcPackage, callback) {
- srcPackage.asString(callback)
- }
-
- // async.map, oh my! do an asString() on each SourcePackage async but reassemble them in order
- async.map(this.packages, packageToString, readComplete)
- }
-
- , completeOptions: function () { // options + any additional options child packages may wish to add
- var options = util.extend(this.options, {})
- this.packages.forEach(function (pkg) {
- pkg.extendOptions(options)
- })
- return options
- }
- }
-
- , create = function (options) {
- return Object.create(SourceBuild).init(options)
- }
-
-module.exports = {
- create : create
-}
View
79 lib/source-package-util.js
@@ -1,79 +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.
- */
-
-/******************************************************************************
- * Utility functions for source-package.js
- */
-
-var fs = require('fs')
- , path = require('path')
- , async = require('async')
- , glob = require('glob')
- , FilesystemError = require('./errors').FilesystemError
-
- , collectFiles = function (root, files, callback) {
- async.map(
- files
- , function (file, callback) {
- // use glob.Glob because it's easier to stub for tests
- new glob.Glob(file, { cwd: root, root: root, nomount: true }, function (err, files) {
- if (err) return callback(new FilesystemError(err))
- files.sort()
- callback(null, files)
- })
- }
- , function (err, files) {
- if (err) return callback(err)
- // flatten files array
- files = files.reduce(function (p, c) { return p.concat(c) }, [])
- callback(null, files)
- }
- )
- }
-
- // utility to read multiple files and return them in order
- , loadFiles = function (root, files, callback) {
- if (!Array.isArray(files)) files = [ files ]
- if (!files.length || (files.length == 1 && files[0] == 'noop')) return callback()
-
- collectFiles(root, files, function (err, files) {
- // read each source file in parallel and assemble them together
- // in order, async.map() FTW!
- async.map(
- files
- , function (file, callback) {
- file = path.join(root, file).replace(/(\.js)?$/, '.js')
- fs.readFile(file, 'utf-8', function (err, contents) {
- if (err) return callback(new FilesystemError(err))
- callback(null, { file: file, contents: contents })
- })
- }
- , callback
- )
- })
- }
-
-module.exports = {
- loadFiles: loadFiles
-}
View
194 lib/source-package.js
@@ -1,194 +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.
- */
-
-
-/******************************************************************************
- * The SourcePackage object, each instance is associated with an npm package.
- * The object uses the package.json and commandline options to figure out how
- * to assemble an output via the asString() method.
- * Internally we use EJS templates to augment the source to provide an Ender-
- * compatible output (the less screwing with strings here the better).
- */
-
-var path = require('path')
- , async = require('async')
- , packageUtil = require('ender-repository').util
- , template = require('./template')
- , sourcePackageUtil = require('./source-package-util')
-
- , templateFiles = {
- 'standard' : '../resources/source-package.mustache'
- , 'root' : '../resources/root-package.mustache' // a special template for the root package
- , 'file' : '../resources/source-file.mustache'
- }
-
- // pass the source through
- , generateSourcePackage = function (root, data, callback) {
- template.generateSource(templateFiles[root ? 'root' : 'standard'], data, callback)
- }
-
- , generateMainSourceFile = function (fileData, callback) {
- template.generateSource(templateFiles.file, fileData, callback)
- }
-
- , indent = function (str) {
- // was this: return str.replace(/^(?!\s*$)/gm, ' ')
- // but in some odd cases ^ was matching other things and inserting ' ' in unhelpful places
- // unfortunately I can't easily replicate the problem to write a test for it!
- // one example is this line in the ender-json package:
- // https://github.com/douglascrockford/JSON-js/blob/master/json2.js#L372
- // which starts with: // If the space parameter...
- // and gets converted to: / / If the space parameter
- return str.split('\n').map(function (line) {
- return (/^\s*$/).test(line) ? line : (' ' + line)
- }).join('\n')
- }
-
- // generate an object that can be fed to the templates
- , makeTemplateData = function (sources) {
- // if this package is in the `--sandbox <packages>` list, or, if we are
- // the root package and a --sandbox option is passed, then set this to true
- var isSandbox = Array.isArray(this.options.sandbox)
- && (this.options.sandbox.indexOf(this.packageJSON.name) != -1 || this.isRoot)
-
- return {
- packageName : this.packageJSON.name
- , options : { noop: this.options.noop, sandbox: isSandbox }
- // these objects have lazy methods so we don't do unnecessary indent()ing
- , mainSource: sources.main && {
- raw : function () { return sources.main }
- , indented : function () { return indent(sources.main) }
- }
- , enderSource: sources.ender && {
- raw : function () { return sources.ender }
- , indented : function () { return indent(sources.ender) }
- }
- }
- }
-
- , SourcePackage = {
- init: function (packageName, parents, isRoot, packageJSON, options) {
- this.parents = parents
- this.options = options
- this.isRoot = isRoot
- this.packageName = packageName
- this.packageJSON = packageJSON
-
- // custom hasher function for async.memoize so we have a single key, default will use
- // first arg (callback) as hash key which won't work
- this.asString = async.memoize(this.asString.bind(this), function () { return '_' })
- return this
- }
-
- // not the overridden name, the name from the package.json
- , get realName () {
- return this.packageJSON && Object.getPrototypeOf(this.packageJSON).name
- ? Object.getPrototypeOf(this.packageJSON).name
- : this.packageName
- }
-
- // get the `name` from the original json data, available as proto (see package-descriptor.js)
- , get identifier () {
- return this.realName + '@' + this.packageJSON.version
- }
-
- // the root of this package on the filesystem
- , get packageRoot () {
- return packageUtil.getPackageRoot(this.parents, this.realName)
- }
-
- // this method supports multiple calls but a single execution, hence the async.memoize in init()
- , asString: function (callback) {
- // note that "main" and "ender" are processed in the same way so they can both be just
- // a string pointing to a source file or an array of source files that are concatenated
- // or be left unspecified
- var root = this.packageRoot
- , asValidType = function (value) {
- return typeof value == 'string' || Array.isArray(value) ? value : []
- }
- , mainSources = asValidType(this.packageJSON.main)
- , enderBridgeSources = asValidType(this.packageJSON.ender)
-
- , processMainSources = function (data, callback) {
- if (this.isRoot || !data.main) return callback(null, data)
- async.map(
- data.main
- , function (fileData, callback) {
- generateMainSourceFile(fileData, function (err, source) {
- if (err) return callback(err)
- fileData.contents = source
- callback(null, fileData)
- })
- }
- , function (err, sources) {
- if (err) return callback(err)
- data.main = sources
- callback(null, data)
- }
- )
- }.bind(this)
-
- , handleSourceData = function (err, sources) {
- if (err) return callback(err)
-
- // take the array of { file: 'path', contents: 'source' } and just join the contents
- // for more complex handling later
- sources.ender =
- sources.ender && sources.ender.map(function (s) { return s.contents }).join('\n\n')
- sources.main =
- sources.main && sources.main.map(function (s) { return s.contents }).join('\n\n')
-
- generateSourcePackage(
- this.isRoot
- , makeTemplateData.call(this, sources)
- , callback
- )
- }.bind(this)
-
- , sourceLoaders = {
- main : sourcePackageUtil.loadFiles.bind(this, root, mainSources)
- , ender : sourcePackageUtil.loadFiles.bind(this, root, enderBridgeSources)
- }
-
- async.waterfall([ async.parallel.bind(null, sourceLoaders), processMainSources ], handleSourceData)
- }
-
- , extendOptions: function (options) {
- var externs = this.packageJSON && this.packageJSON.externs
- , root
-
- if (externs) {
- if (!Array.isArray(externs)) externs = [ externs ]
- root = this.packageRoot
- if (!options.externs) options.externs = []
- options.externs = options.externs.concat(externs.map(function (e) {
- return path.join(root, e)
- }))
- }
- }
- }
-
-module.exports.create = function (packageName, parents, isRoot, packageJSON, options) {
- return Object.create(SourcePackage).init(packageName, parents, isRoot, packageJSON, options)
-}
View
48 lib/template.js
@@ -1,48 +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 fs = require('fs')
- , path = require('path')
- , async = require('async')
- , hogan = require('hogan.js')
- , FilesystemError = require('./errors').FilesystemError
- , templateCache = {}
-
- // async.memoize lets us read each file once only, no matter how many calls before & after the file is read
- , readTemplate = async.memoize(function (file, callback) {
- fs.readFile(path.resolve(__dirname, file), 'utf-8', function (err, templateContents) {
- if (err) return callback(new FilesystemError(err))
- callback(null, templateContents)
- })
- })
-
- , generateSource = function (file, data, callback) {
- readTemplate(file, function (err, templateContents) {
- if (err) return callback(err) // wrapped above
- var tmpl = templateCache[file] || (templateCache[file] = hogan.compile(templateContents))
- callback(null, tmpl.render(data))
- })
- }
-
-module.exports.generateSource = generateSource
View
64 lib/write.js
@@ -1,64 +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.
- */
-
-
-/******************************************************************************
- * A simple utility to write out the source files, both plain and minified.
- * The source comes from a SourceBuild object which has an asString() method
- * to pull together the component parts.
- */
-
-var fs = require('fs')
- , path = require('path')
- , async = require('async')
- , util = require('./util')
- , FilesystemError = require('./errors').FilesystemError
-
- , writeFile = function (file, callback, err, data) {
- if (err) return callback(err) // wrapped in source-build.js
- fs.writeFile(file, data, 'utf-8', function (err) {
- if (err) return callback(new FilesystemError(err))
- callback.apply(null, arguments)
- })
- }
-
- , writePlainFile = function (options, sourceBuild, out, callback) {
- var filename = util.getOutputFilenameFromOptions(options)
- sourceBuild.asString({ type: 'plain' }, writeFile.bind(null, filename, callback))
- }
-
- , writeMinifiedFile = function (options, sourceBuild, out, callback) {
- var filename = util.getOutputFilenameFromOptions(options).replace(/(\.min)?\.js/, '.min.js')
- sourceBuild.asString({ type: 'minified' }, writeFile.bind(null, filename, callback))
- }
-
- , write = function (options, sourceBuild, out, callback) {
- var jobs = [ writePlainFile.bind(null, options, sourceBuild, out) ]
- if (options.minifier != 'none')
- jobs.push(writeMinifiedFile.bind(null, options, sourceBuild, out))
-
- async.parallel(jobs, callback)
- }
-
-module.exports.write = write
View
17 package.json
@@ -1,7 +1,7 @@
{
"name": "ender"
, "description": "Open Module JavaScript Framework"
- , "version": "0.9.10-dev"
+ , "version": "0.9.11-dev"
, "authors": [
"Jacob Thornton @fat <jacob@twitter.com> (https://github.com/fat)"
, "Dustin Diaz @ded <dustin@obvious.com> (https://github.com/ded)"
@@ -11,12 +11,13 @@
, "main": "./lib/main.js"
, "homepage": "https://ender.jit.su"
, "dependencies": {
- "ender-repository" : "0.0.2"
- , "ender-minify" : "0.1.0"
- , "ender-dependency-graph"
- : "0.0.1"
- , "ender-args-parser"
- : "0.0.1"
+
+ "ender-repository" : "~0.0.2"
+ , "ender-minify" : "~0.1.0"
+ , "ender-dependency-graph" : "~0.0.1"
+ , "ender-args-parser" : "~0.0.1"
+ , "ender-builder" : "~0.0.2"
+
, "colors" : "~0.6.0"
, "async" : "~0.1.22"
, "hogan.js" : "~2.0.0"
@@ -35,7 +36,7 @@
, "rimraf" : "~2.1.1"
, "jshint" : "~0.9.1"
, "mkfiletree" : "~0.0.0"
- , "require-subvert" : "~0.0.0"
+ , "require-subvert" : "~0.0.1"
}
, "bin": {
"ender" : "./bin/ender"
View
14 test/unit/main-add-test.js
@@ -26,15 +26,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')
+ , util
+ , mainBuild
, mainAdd
testCase('Add', {
'test basic add': function (done) {
- var utilMock = this.mock(util)
+ var utilMock
, argsParserMock = this.mock(argsParser)
- , mainBuildMock = this.mock(mainBuild)
+ , mainBuildMock
, parseContextStub = this.stub()
, optionsArg = { options: 1 }
, filenameArg = { filename: 1 }
@@ -44,7 +44,11 @@ testCase('Add', {
requireSubvert.subvert('../../lib/parse-context', parseContextStub)
parseContextStub.callsArgWith(1, null, contextArg)
- mainAdd = requireSubvert.require('../../lib/main-add')
+ util = require('../../lib/util')
+ mainBuild = require('../../lib/main-build')
+ utilMock = this.mock(util)
+ mainBuildMock = this.mock(mainBuild)
+ mainAdd = requireSubvert.require('../../lib/main-add')
utilMock.expects('getInputFilenameFromOptions').once().withExactArgs(optionsArg).returns(filenameArg)
argsParserMock.expects('extend').once().withExactArgs(contextArg.options, optionsArg).returns(extendedOptionsArg)
View
65 test/unit/main-build-test.js
@@ -23,42 +23,40 @@
*/
-var testCase = require('buster').testCase
- , buildUtil = require('../../lib/main-build-util')
- , install = require('../../lib/install')
- , util = require('../../lib/util')
- , SourcePackage = require('../../lib/source-package')
- , SourceBuild = require('../../lib/source-build')
- , build = require('../../lib/main-build')
- , buildOutput = require('../../lib/output/main-build-output').create()
- , info = require('../../lib/main-info')
- , write = require('../../lib/write')
+var testCase = require('buster').testCase
+ , requireSubvert = require('require-subvert')(__dirname)
+ , buildUtil = require('../../lib/main-build-util')
+ , install
+ , info
+ , build
+
+require('../../lib/output/main-build-output').create()
testCase('Build', {
// OK, this is a bit of a mess, more of an integration test, but it tests the ful
// build process and that it calls everything we expect it to
'test standard main-build interaction': function (done) {
- var mockUtil = this.mock(util)
- , mockBuildUtil = this.mock(buildUtil)
- , mockInstall = this.mock(install)
- , mockInfo = this.mock(info)
+ var mockBuildUtil = this.mock(buildUtil)
+ , mockInstall
+ , mockInfo
, out = require('../../lib/output/main-build-output').create(1)
, outMock = this.mock(out)
- , sourcePackage = SourcePackage.create()
- , SourcePackageMock = this.mock(SourcePackage)
- , sourceBuild = SourceBuild.create()
- , sourceBuildMock = this.mock(sourceBuild)
- , SourceBuildMock = this.mock(SourceBuild)
- , writeMock = this.mock(write)
+ , enderBuilderStub = this.stub()
, optionsArg = { options: 1 }
, packagesArg = [ 'foobarbang' ]
, installedPackagesArg = [ 1, 2, 3 ]
, dependencyTreeArg = { dependencyTree: 1 }
, localizedArg = [ 'foobar' ]
- , packageNameArg = { packageName: 1 }
- , parentsArg = { parents: 1 }
- , dataArg = { packageJSON: { packageJSON: 1, name: 'foobar' } }
+ , filenameArg = { filename: 1 }
+
+ enderBuilderStub.callsArgWith(3, null, filenameArg)
+ requireSubvert.subvert('ender-builder', enderBuilderStub)
+ info = requireSubvert.require('../../lib/main-info')
+ install = requireSubvert.require('../../lib/install')
+ mockInfo = this.mock(info)
+ mockInstall = this.mock(install)
+ build = requireSubvert.require('../../lib/main-build')
mockBuildUtil.expects('packageList').once().withExactArgs(optionsArg).returns(packagesArg)
outMock.expects('buildInit').once()
@@ -68,32 +66,21 @@ testCase('Build', {
.withArgs(optionsArg, packagesArg)
.callsArgWith(2, null, installedPackagesArg, dependencyTreeArg)
outMock.expects('installedFromRepository').once().withArgs(installedPackagesArg.length)
- SourceBuildMock.expects('create').once().withExactArgs(optionsArg).returns(sourceBuild)
dependencyTreeArg.localizePackageList = this.stub().returns(localizedArg)
- mockUtil.expects('getRootPackageName').once().withExactArgs(optionsArg).returns(localizedArg)
- dependencyTreeArg.forEachUniqueOrderedDependency = this.stub()
- SourcePackageMock
- .expects('create')
- .once()
- .withExactArgs(packageNameArg, parentsArg, false, dataArg.packageJSON, optionsArg)
- .returns(sourcePackage)
- sourceBuildMock.expects('addPackage').once().withArgs(sourcePackage)
outMock.expects('finishedAssembly').once()
- writeMock.expects('write').once().withArgs(optionsArg, sourceBuild, out).callsArg(3)
mockInfo
.expects('generateAndPrint')
.once()
- .withArgs(optionsArg, out, 'ender.js', optionsArg, localizedArg, dependencyTreeArg)
+ .withArgs(optionsArg, out, filenameArg, optionsArg, localizedArg, dependencyTreeArg)
.callsArg(6)
// execute
build.exec(optionsArg, out, done)
- assert(dependencyTreeArg.forEachUniqueOrderedDependency.calledOnce)
- assert(dependencyTreeArg.forEachUniqueOrderedDependency.calledWith(localizedArg))
- dependencyTreeArg.forEachUniqueOrderedDependency.lastCall.args[1](packageNameArg, parentsArg, dataArg)
-
assert(true) // required, buster bug
}
-})
+ , 'tearDown': function () {
+ requireSubvert.cleanUp()
+ }
+})
View
14 test/unit/main-info-util-test.js
@@ -23,11 +23,11 @@
*/
-var testCase = require('buster').testCase
- , fs = require('fs')
- , zlib = require('zlib')
- , minify = require('../../lib/minify')
- , mainInfoUtil = require('../../lib/main-info-util')
+var testCase = require('buster').testCase
+ , enderBuilder = require('ender-builder')
+ , fs = require('fs')
+ , zlib = require('zlib')
+ , mainInfoUtil = require('../../lib/main-info-util')
, FilesystemError = require('../../lib/errors').FilesystemError
, _i = 100
@@ -36,7 +36,7 @@ testCase('Info util', {
'test sizes': function (done) {
var fsMock = this.mock(fs)
, zlibMock = this.mock(zlib)
- , minifyMock = this.mock(minify)
+ , enderBuilderMock = this.mock(enderBuilder)
, filenameArg = { filename: 1 }
, optionsArg = { options: 1 }
, fileContentsArg = { fileContents: 1, length: _i++ }
@@ -49,7 +49,7 @@ testCase('Info util', {
}
fsMock.expects('readFile').once().withArgs(filenameArg, 'utf-8').callsArgWith(2, null, fileContentsArg)
- minifyMock.expects('minify').once().withArgs(optionsArg, fileContentsArg).callsArgWith(2, null, minifyContentsArg)
+ enderBuilderMock.expects('minify').once().withArgs(optionsArg, fileContentsArg).callsArgWith(2, null, minifyContentsArg)
zlibMock.expects('gzip').once().withArgs(minifyContentsArg).callsArgWith(1, null, gzipContentsArg)
mainInfoUtil.sizes(optionsArg, filenameArg, function (err, sizes) {
View
16 test/unit/main-remove-test.js
@@ -26,14 +26,14 @@
var testCase = require('buster').testCase
, repository = require('ender-repository')
, requireSubvert = require('require-subvert')(__dirname)
- , util = require('../../lib/util')
- , mainBuild = require('../../lib/main-build')
+ , util
+ , mainBuild
, mainRemove
testCase('Remove', {
'test basic remove': function (done) {
- var utilMock = this.mock(util)
- , mainBuildMock = this.mock(mainBuild)
+ var utilMock
+ , mainBuildMock
, repositoryMock = this.mock(repository)
, parseContextStub = this.stub()
, optionsArg = {
@@ -55,11 +55,15 @@ testCase('Remove', {
}
, outArg = { out: 1 }
- utilMock.expects('getInputFilenameFromOptions').once().withExactArgs(optionsArg).returns(filenameArg)
requireSubvert.subvert('../../lib/parse-context', parseContextStub)
parseContextStub.callsArgWith(1, null, contextArg)
- mainRemove = requireSubvert.require('../../lib/main-remove')
+ util = requireSubvert.require('../../lib/util')
+ mainBuild = requireSubvert.require('../../lib/main-build')
+ utilMock = this.mock(util)
+ mainBuildMock = this.mock(mainBuild)
+ mainRemove = requireSubvert.require('../../lib/main-remove')
+ utilMock.expects('getInputFilenameFromOptions').once().withExactArgs(optionsArg).returns(filenameArg)
mainBuildMock.expects('exec').once().withArgs(expectedBuildOptions, outArg).callsArg(2)
repositoryMock.expects('setup').once().callsArg(0)
repositoryMock.expects('uninstall').once().withArgs(optionsArg.packages).callsArgWith(1)
View
101 test/unit/minify-test.js
@@ -1,101 +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
- , requireSubvert = require('require-subvert')(__dirname)
-
-buster.testCase('Minify', {
- 'setUp': function () {
- 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) {
- requireSubvert.require('../../lib/minify').minify(parsedArgs, sourceArg, function (err, result) {
- refute(err)
- 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 () {
- requireSubvert.cleanUp()
- }
-
- , 'test basic minify, default to uglify': function (done) {
- var parsedArgs = { foo: 'bar' } // shouldn't see this come through
- , expectedOptions = {}
- this.runTest('uglify', expectedOptions, parsedArgs, done)
- }
-
- , 'test closure default options': function (done) {
- var parsedArgs = { foo: 'bar', minifier: 'closure' }
- , expectedOptions = {}
- this.runTest('closure', expectedOptions, parsedArgs, done)
- }
-
- , 'test closure level option (whitespace)': function (done) {
- var parsedArgs = { foo: 'bar', minifier: 'closure', level: 'whitespace' }
- , expectedOptions = { level: 'whitespace' }
- this.runTest('closure', expectedOptions, parsedArgs, done)
- }
-
- , 'test closure level option (simple)': function (done) {
- var parsedArgs = { foo: 'bar', minifier: 'closure', level: 'simple' }
- , expectedOptions = { level: 'simple' }
- this.runTest('closure', expectedOptions, parsedArgs, done)
- }
-
- , 'test closure level option (advanced)': function (done) {
- var parsedArgs = { foo: 'bar', minifier: 'closure', level: 'advanced' }
- , expectedOptions = { level: 'advanced' }
- this.runTest('closure', expectedOptions, parsedArgs, done)
- }
-
- , 'test closure externs option': function (done) {
- var parsedArgs = { foo: 'bar', minifier: 'closure', externs: [ 'bing', 'bang' ] }
- , expectedOptions = { externs: [ 'bing', 'bang' ] }
- this.runTest('closure', expectedOptions, parsedArgs, done)
- }
-
- , 'test closure level and externs options': function (done) {
- var parsedArgs = { foo: 'bar', minifier: 'closure', level: 'advanced', externs: [ 'woo', 'hoo' ] }
- , expectedOptions = { level: 'advanced', externs: [ 'woo', 'hoo' ] }
-
- this.runTest('closure', expectedOptions, parsedArgs, done)
- }
-})
View
196 test/unit/source-build-test.js
@@ -1,196 +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 testCase = require('buster').testCase
- , argsParser = require('ender-args-parser')
- , SourcePackage = require('../../lib/source-package')
- , SourceBuild = require('../../lib/source-build')
- , minify = require('../../lib/minify')
-
-var createExpectedHeader = function (context, packageList) {
- return [
- "/*!"
- , " * ============================================================="
- , " * Ender: open module JavaScript framework (https://ender.no.de)"
- , " * Build: ender " + context
- , " * Packages: " + packageList
- , " * ============================================================="
- , " */"
- ].join('\n') + '\n\n'
- }
-
-testCase('Source build', {
- 'setUp': function () {
- this.createPackageMock = function (content, identifier) {
- var pkg = SourcePackage.create()
- , pkgMock = this.mock(pkg)
-
- pkgMock.expects('asString').once().callsArgWith(0, null, content)
- pkg.__defineGetter__('identifier', function () { return identifier }) // sinon can't mock getters
- return pkg
- }
- this.createArgsParserMock = function (optionsArg, contextArg) {
- var argsParserMock = this.mock(argsParser)
- argsParserMock.expects('toContextString').withExactArgs(optionsArg).once().returns(contextArg)
- }
- }
-
- , 'asString': {
- 'plain': function (done) {
- var pkg1Content = 'package 1\ncontents'
- , pkg1 = this.createPackageMock(pkg1Content, "pkg1@0.1.1")
- , pkg2Content = 'package 2\n\ncontents'
- , pkg2 = this.createPackageMock(pkg2Content, "pkg2@1.1.1")
- , pkg3Content = 'package 3\n\ncontents\nright\nhere\n'
- , pkg3 = this.createPackageMock(pkg3Content, "pkg3@1.2.3")
- , optionsArg = { options: 1 }
- , srcBuild = SourceBuild.create(optionsArg)
- , contextArg = 'some context here & don\'t escape <this>'
- , plainSource =
- createExpectedHeader(contextArg, "pkg1@0.1.1 pkg2@1.1.1 pkg3@1.2.3")
- + pkg1Content + '\n\n'
- + pkg2Content + '\n\n'
- + pkg3Content
- , mockMinify = this.mock(minify)
-
- this.createArgsParserMock(optionsArg, contextArg)
- srcBuild.addPackage(pkg1)
- srcBuild.addPackage(pkg2)
- srcBuild.addPackage(pkg3)
-
- mockMinify.expects('minify').never()
-
- srcBuild.asString({ type: 'plain' }, function (err, actual) {
- refute(err)
- assert.equals(actual, plainSource)
- done()
- })
- }
-
- , 'minify': function (done) {
- var pkg1Content = 'package 1\ncontents'
- , pkg1 = this.createPackageMock(pkg1Content, "pkg1@0.1.1")
- , pkg2Content = 'package 2\n\ncontents'
- , pkg2 = this.createPackageMock(pkg2Content, "pkg2@1.1.1")
- , pkg3Content = 'package 3\n\ncontents\nright\nhere\n'
- , pkg3 = this.createPackageMock(pkg3Content, "pkg3@1.2.3")
- , optionsArg = { options: 1 }
- , srcBuild = SourceBuild.create(optionsArg)
- , contextArg = 'some minified context here & don\'t escape <this>'
- , plainSource =
- createExpectedHeader(contextArg, "pkg1@0.1.1 pkg2@1.1.1 pkg3@1.2.3")
- + pkg1Content + '\n\n'
- + pkg2Content + '\n\n'
- + pkg3Content
- , minifiedSource = 'this is minified, these are not the droids you are looking for'
- , mockMinify = this.mock(minify)
-
- this.createArgsParserMock(optionsArg, contextArg)
- srcBuild.addPackage(pkg1)
- srcBuild.addPackage(pkg2)
- srcBuild.addPackage(pkg3)
-
- mockMinify.expects('minify').once().withArgs(optionsArg, plainSource).callsArgWith(2, null, minifiedSource)
-
- srcBuild.asString({ type: 'minified' }, function (err, actual) {
- refute(err)
- assert.equals(actual, minifiedSource)
- done()
- })
- }
-
- , 'sandboxed': function (done) {
- var pkg1Content = 'package 1\ncontents'
- , pkg1 = this.createPackageMock(pkg1Content, "pkg1@0.1.1")
- , pkg2Content = 'package 2\n\ncontents'
- , pkg2 = this.createPackageMock(pkg2Content, "pkg2@1.1.1")
- , pkg3Content = 'package 3\n\ncontents\nright\nhere\n'
- , pkg3 = this.createPackageMock(pkg3Content, "pkg3@1.2.3")
- , optionsArg = { sandbox: [ 'foo', 'bar' ] }
- , srcBuild = SourceBuild.create(optionsArg)
- , contextArg = 'some context here & don\'t escape <this>'
- , plainSource =
- createExpectedHeader(contextArg, "pkg1@0.1.1 pkg2@1.1.1 pkg3@1.2.3")
- + '!function () {\n\n'
- + pkg1Content + '\n\n' + pkg2Content + '\n\n' + pkg3Content
- + '\n\n}.call({});'
- , mockMinify = this.mock(minify)
-
- this.createArgsParserMock(optionsArg, contextArg)
- srcBuild.addPackage(pkg1)
- srcBuild.addPackage(pkg2)
- srcBuild.addPackage(pkg3)
-
- mockMinify.expects('minify').never()
-
- srcBuild.asString({ type: 'plain' }, function (err, actual) {
- refute(err)
- assert.equals(actual, plainSource)
- done()
- })
- }
-
- // the minifier function should be passed an options object that has been extended by
- // each of the packages, this allows for packages to add on options such as 'externs'
- // which are passed to the minifier
- , 'minify extends options for each package (externs)': function (done) {
- var pkg1 = this.createPackageMock('p1', "pkg1@0.1.1")
- , pkg2 = this.createPackageMock('p2', "pkg2@1.1.1")
- , pkg3 = this.createPackageMock('p3', "pkg3@1.2.3")
- , optionsArg = { options: 1, externs: [ 'extern0' ] }
- , expectedOptionsArg
- , srcBuild = SourceBuild.create(optionsArg)
- , contextArg = 'some minified context here & don\'t escape <this>'
- , plainSource =
- createExpectedHeader(contextArg, "pkg1@0.1.1 pkg2@1.1.1 pkg3@1.2.3")
- + 'p1\n\np2\n\np3'
- , minifiedSource = 'this is minified, these are not the droids you are looking for'
- , mockMinify = this.mock(minify)
-
- this.createArgsParserMock(optionsArg, contextArg)
- srcBuild.addPackage(pkg1)
- srcBuild.addPackage(pkg2)
- srcBuild.addPackage(pkg3)
-
- // sort of mock out the extendOptions() function
- pkg2.extendOptions = function (options) {
- options.externs.push('extern1')
- options.externs.push('extern2')
- }
- pkg3.extendOptions = function (options) {
- options.externs.push('extern3')
- }
- expectedOptionsArg = { options: 1, externs: [ 'extern0', 'extern1', 'extern2', 'extern3' ] }
-
- mockMinify.expects('minify').once().withArgs(expectedOptionsArg, plainSource).callsArgWith(2, null, minifiedSource)
-
- srcBuild.asString({ type: 'minified' }, function (err, actual) {
- refute(err)
- assert.equals(actual, minifiedSource)
- done()
- })
- }
- }
-})
View
664 test/unit/source-package-test.js
@@ -1,664 +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 testCase = require('buster').testCase
- , fs = require('fs')
- , path = require('path')
- , async = require('async')
- , SourcePackage = require('../../lib/source-package')
- , FilesystemError = require('../../lib/errors').FilesystemError
-
- , templateFiles = {
- 'standard' : path.join(__dirname, '/../../resources/source-package.mustache')
- , 'root' : path.join(__dirname, '/../../resources/root-package.mustache')
- , 'file' : path.join(__dirname, '/../../resources/source-file.mustache')
- }
- , templateFileContents
-
-require('glob')
-
-testCase('Source package', {
- 'setUp': function (done) {
-
- // a very hacky stub for glob, replace glob.Glob with a simple func that returns the first arg
- // restore it in tearDown
- var exp = require.cache[require.resolve('glob')].exports
- this._oldGlob = exp.Glob
- exp.Glob = function (f, opt, cb) {
- cb(null, [ f ])
- }
-
- // TODO: demystify and clean this mess up
- this.runAsStringTest = function (options, done) {
- // options: expectedFileReads, fileContents, readDelays, parents, pkg, json, expectedResult
-
- var fsMock = this.mock(fs)
- , tmplType = options.json.name == 'ender-js' ? 'root' : 'standard'
- , srcPkg
-
- options.expectedFileReads.forEach(function (file, index) {
- var exp = fsMock.expects('readFile')
- .withArgs(
- path.resolve(file)
- , 'utf-8'
- )
- if (!options.readDelays)
- exp.callsArgWith(2, null, options.fileContents[index])
- else {
- setTimeout(function () {
- exp.args[0][2].call(null, null, options.fileContents[index])
- }, options.readDelays[index])
- }
- })
-
- if (typeof templateFileContents[tmplType] == 'string') {
- fsMock
- .expects('readFile')
- .withArgs(path.resolve(templateFiles[tmplType]), 'utf-8')
- .callsArgWith(2, null, templateFileContents[tmplType])
- templateFileContents[tmplType] = -1 // i.e. only run this branch once
- }
- if (tmplType == 'standard' && options.json.main && typeof templateFileContents.file == 'string') {
- fsMock
- .expects('readFile')
- .withArgs(path.resolve(templateFiles.file), 'utf-8')
- .callsArgWith(2, null, templateFileContents.file)
- templateFileContents.file = -1 // i.e. only run this branch once
- }
-
- srcPkg = SourcePackage.create(
- options.pkg
- , options.parents || []
- , tmplType == 'root'
- , options.json
- , options.options || {}
- )
-
- srcPkg.asString(function (err, actual) {
- refute(err)
- assert.equals(actual, options.expectedResult)
- done && done()
- })
-
- return srcPkg
- }
-
- this.buildExpectedResult = function (options) {
- // don't be too clever here so we can be clever in the code we're testing.
- // source indenting should be done by the caller, not here.
- var src = ''
- src += '(function () {\n\n var module = { exports: {} }, exports = module.exports;\n'
- if (options.main)
- src += '\n' + options.main
- src += '\n provide("' + options.name + '", module.exports);'
- if (options.sandbox)
- src += '\n\n window["' + options.name + '"] = module.exports;'
- if (options.ender)
- src += '\n\n' + options.ender
- else
- src += '\n $.ender(module.exports);'
- src += '\n}());'
- return src
- }
-
- if (!templateFileContents) {
- // unfortunately we have to mock this out as we're mocking out the whole `fs`
- async.map(
- [ 'standard', 'root', 'file' ]
- , function (type, callback) {
- fs.readFile(templateFiles[type], 'utf8', callback)
- }
- , function (err, templates) {
- if (err)
- throw err
- templateFileContents = {
- 'standard' : templates[0]
- , 'root' : templates[1]
- , 'file' : templates[2]
- }
- done()
- }
- )
- } else
- done()
- }
-
- , 'tearDown': function () {
- require.cache[require.resolve('glob')].exports.Glob = this._oldGlob
- }
-
- // test our internal methods, declared in setUp
- , 'internal tests': {
- 'buildExpectedResult': {
- 'test main with no ender': function () {
- assert.equals(this.buildExpectedResult({
- name: 'foobar'
- , main:
- ' this is a test\n\n'
- + ' 1\n'
- + ' 2\n'
- + ' 3\n'
- })
- , '(function () {\n\n'
- + ' var module = { exports: {} }, exports = module.exports;\n\n'
- + ' this is a test\n\n'
- + ' 1\n'
- + ' 2\n'
- + ' 3\n\n'
- + ' provide("foobar", module.exports);\n'
- + ' $.ender(module.exports);\n'
- + '}());'
- )
- }
-
- , 'test ender with no main': function () {
- assert.equals(this.buildExpectedResult({
- name: 'foobar'
- , ender:
- ' this is a test\n\n'
- + ' 1\n'
- + ' 2\n'
- + ' 3\n'
- })
- , '(function () {\n\n'
- + ' var module = { exports: {} }, exports = module.exports;\n\n'
- + ' provide("foobar", module.exports);\n\n'
- + ' this is a test\n\n'
- + ' 1\n'
- + ' 2\n'
- + ' 3\n\n'
- + '}());'
- )
- }
-
- , 'test main and ender': function () {
- assert.equals(this.buildExpectedResult({
- name: 'foobar'
- , main:
- ' main\n\n'
- + ' source\n'
- + ' here\n'
- , ender:
- ' this is a test\n\n'
- + ' 1\n'
- + ' 2\n'
- + ' 3\n'
- })
- , '(function () {\n\n'
- + ' var module = { exports: {} }, exports = module.exports;\n\n'
- + ' main\n\n'
- + ' source\n'
- + ' here\n\n'
- + ' provide("foobar", module.exports);\n\n'
- + ' this is a test\n\n'
- + ' 1\n'
- + ' 2\n'
- + ' 3\n\n'
- + '}());'
- )
- }
- }
- }
-
- , 'main-only': {
- 'test (single) main-only asString without .js extension': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/parent1/node_modules/parent2/node_modules/apkg/lib/mainsrc.js' ]
- , fileContents: [ 'mainsrc contents' ]
- , parents: [ 'parent1', 'parent2' ]
- , pkg: 'apkg'
- , json: { name: 'apkg-name', main: 'lib/mainsrc' }
- , expectedResult: this.buildExpectedResult({ name: 'apkg-name', main: ' mainsrc contents' })
- }, done)
- }
-
- , 'test (single) main-only asString with .js extension': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/parent1/node_modules/parent2/node_modules/apkg/lib/mainsrc.js' ]
- , fileContents: [ 'mainsrc.js contents' ]
- , parents: [ 'parent1', 'parent2' ]
- , pkg: 'apkg'
- , json: { name: 'apkg-name', main: 'lib/mainsrc.js' }
- , expectedResult: this.buildExpectedResult({ name: 'apkg-name', main: ' mainsrc.js contents' })
- }, done)
- }
-
- , 'test (multiple) main-only asString (mixed extensions)': function (done) {
- this.runAsStringTest({
- expectedFileReads: [
- 'node_modules/mypkg/lib/mainsrc.js'
- , 'node_modules/mypkg/lib/foo/bar.js'
- , 'node_modules/mypkg/lib/foo/bar/baz.js'
- ]
- , fileContents: [
- 'mainsrc.js contents'
- , 'BAR!'
- , 'BAZ!'
- ]
- , pkg: 'mypkg'
- , json: {
- name: 'mypkg-name'
- , main: [
- 'lib/mainsrc.js'
- , 'lib/foo/bar'
- , 'lib/foo/bar/baz'
- ]
- }
- , expectedResult: this.buildExpectedResult({ name: 'mypkg-name', main: ' mainsrc.js contents\n\n BAR!\n\n BAZ!' })
- }, done)
- }
-
- , 'test (multiple) main-only asString (mixed extensions) with out-of-order read returns': function (done) {
- // test that even though we read the source files in parallel that they get stitched together
- // in the right order in the end. Delay the callbacks from the reads to emulate out-of-order
- // filesystem reads
- this.runAsStringTest({
- expectedFileReads: [
- 'node_modules/mypkg/lib/mainsrc.js'
- , 'node_modules/mypkg/lib/foo/bar.js'
- , 'node_modules/mypkg/lib/foo/bar/baz.js'
- ]
- , fileContents: [
- 'mainsrc.js contents'
- , 'BAR!'
- , 'BAZ!'
- ]
- , readDelays: [ 50, 25, 10 ]
- , pkg: 'mypkg'
- , json: {
- name: 'mypkg-name'
- , main: [
- 'lib/mainsrc.js'
- , 'lib/foo/bar'
- , 'lib/foo/bar/baz'
- ]
- }
- , expectedResult: this.buildExpectedResult({ name: 'mypkg-name', main: ' mainsrc.js contents\n\n BAR!\n\n BAZ!' })
- }, done)
- }
-
- , 'test (single) main-only asString, with ender=noop': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/parent1/node_modules/parent2/node_modules/apkg/lib/mainsrc.js' ]
- , fileContents: [ 'mainsrc contents' ]
- , parents: [ 'parent1', 'parent2' ]
- , pkg: 'apkg'
- , json: { name: 'apkg-name', main: 'lib/mainsrc', ender: 'noop' }
- , expectedResult: this.buildExpectedResult({ name: 'apkg-name', main: ' mainsrc contents' })
- }, done)
- }
-
- , 'test (single) main-only asString, with ender={}': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/parent1/node_modules/parent2/node_modules/apkg/lib/mainsrc.js' ]
- , fileContents: [ 'mainsrc contents' ]
- , parents: [ 'parent1', 'parent2' ]
- , pkg: 'apkg'
- , json: { name: 'apkg-name', main: 'lib/mainsrc', ender: { } }
- , expectedResult: this.buildExpectedResult({ name: 'apkg-name', main: ' mainsrc contents' })
- }, done)
- }
-
- }
-
- , 'ender-only': {
- 'test (single) ender-only asString without .js extension': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/parent1/node_modules/parent2/node_modules/apkg/lib/endersrc.js' ]
- , fileContents: [ 'endersrc contents' ]
- , parents: [ 'parent1', 'parent2' ]
- , pkg: 'apkg'
- , json: { name: 'apkg-name', ender: 'lib/endersrc' }
- , expectedResult: this.buildExpectedResult({ name: 'apkg-name', ender: ' endersrc contents' })
- }, done)
- }
-
- , 'test (single) ender-only asString with .js extension': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/parent1/node_modules/parent2/node_modules/apkg/lib/endersrc.js' ]
- , fileContents: [ 'endersrc.js contents' ]
- , parents: [ 'parent1', 'parent2' ]
- , pkg: 'apkg'
- , json: { name: 'apkg-name', ender: 'lib/endersrc.js' }
- , expectedResult: this.buildExpectedResult({ name: 'apkg-name', ender: ' endersrc.js contents' })
- }, done)
- }
-
- , 'test (multiple) ender-only asString (mixed extensions)': function (done) {
- this.runAsStringTest({
- expectedFileReads: [
- 'node_modules/mypkg/lib/endersrc.js'
- , 'node_modules/mypkg/lib/foo/bar.js'
- , 'node_modules/mypkg/lib/foo/bar/baz.js'
- ]
- , fileContents: [
- 'endersrc.js contents'
- , 'BAR!'
- , 'BAZ!'
- ]
- , pkg: 'mypkg'
- , json: {
- name: 'mypkg-name'
- , ender: [
- 'lib/endersrc.js'
- , 'lib/foo/bar'
- , 'lib/foo/bar/baz'
- ]
- }
- , expectedResult: this.buildExpectedResult({ name: 'mypkg-name', ender: ' endersrc.js contents\n\n BAR!\n\n BAZ!' })
- }, done)
- }
-
- , 'test (multiple) ender-only asString (mixed extensions) with out-of-order read returns': function (done) {
- // test that even though we read the source files in parallel that they get stitched together
- // in the right order in the end. Delay the callbacks from the reads to emulate out-of-order
- // filesystem reads
- this.runAsStringTest({
- expectedFileReads: [
- 'node_modules/mypkg/lib/endersrc.js'
- , 'node_modules/mypkg/lib/foo/bar.js'
- , 'node_modules/mypkg/lib/foo/bar/baz.js'
- ]
- , fileContents: [
- 'endersrc.js contents'
- , 'BAR!'
- , 'BAZ!'
- ]
- , readDelays: [ 50, 25, 10 ]
- , pkg: 'mypkg'
- , json: {
- name: 'mypkg-name'
- , ender: [
- 'lib/endersrc.js'
- , 'lib/foo/bar'
- , 'lib/foo/bar/baz'
- ]
- }
- , expectedResult: this.buildExpectedResult({ name: 'mypkg-name', ender: ' endersrc.js contents\n\n BAR!\n\n BAZ!' })
- }, done)
- }
- }
-
- , 'test (multiple) main and ender asString (mixed extensions) with out-of-order read returns': function (done) {
- // crazytown!
- this.runAsStringTest({
- expectedFileReads: [
- 'node_modules/mypkg/mainsrc.js'
- , 'node_modules/mypkg/lib/foo/bar.js'
- , 'node_modules/mypkg/lib/foo/bar/baz.js'
- , 'node_modules/mypkg/endersrc.js'
- , 'node_modules/mypkg/ender/foo/bar.js'
- , 'node_modules/mypkg/ender/foo/bar/baz.js'
- ]
- , fileContents: [
- 'mainsrc.js contents'
- , 'BAR!'
- , 'BAZ!'
- , 'endersrc.js contents'
- , 'ENDERBAR!'
- , 'ENDERBAZ!'
- ]
- , readDelays: [ 50, 10, 25, 40, 10, 20 ]
- , pkg: 'mypkg'
- , json: {
- name: 'mypkg-name'
- , main: [
- './mainsrc.js'
- , 'lib/foo/bar'
- , 'lib/foo/bar/baz'
- ]
- , ender: [
- './endersrc.js'
- , 'ender/foo/bar'
- , 'ender/foo/bar/baz'
- ]
- }
- , expectedResult: this.buildExpectedResult({
- name: 'mypkg-name'
- , main: ' mainsrc.js contents\n\n BAR!\n\n BAZ!'
- , ender: ' endersrc.js contents\n\n ENDERBAR!\n\n ENDERBAZ!'
- })
- }, done)
- }
-
- , 'test multiple calls to asString on same build before complete': function (done) {
- // test that if we call asString twice prior to it finishing that we'll only
- // process once.
- var expectedResult = this.buildExpectedResult({
- name: 'mypkg-name'
- , main: ' mainsrc.js contents'
- , ender: ' endersrc.js contents'
- })
- , srcPkg = this.runAsStringTest({
- expectedFileReads: [
- 'node_modules/mypkg/mainsrc.js'
- , 'node_modules/mypkg/endersrc.js'
- ]
- , fileContents: [
- 'mainsrc.js contents'
- , 'endersrc.js contents'
- ]
- , readDelays: [ 25, 25 ]
- , pkg: 'mypkg'
- , json: {
- name: 'mypkg-name'
- , main: './mainsrc.js'
- , ender: './endersrc.js'
- }
- , expectedResult: expectedResult
- })
-
- // second call
- srcPkg.asString(function (err, actual) {
- refute(err)
- assert.equals(actual, expectedResult)
- })
-
- setTimeout(function () {
- // third call, after 'generated'
- srcPkg.asString(function (err, actual) {
- refute(err)
- assert.equals(actual, expectedResult)
- done()
- })
- }, 50)
- }
-
- , 'test ender-js package': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/ender-js/main.js' ]
- , fileContents: [ 'ender-js\ncontents' ]
- , pkg: 'ender-js'
- , json: { name: 'ender-js', main: './main.js' }
- , expectedResult: 'ender-js\ncontents'
- }, done)
- }
-
- , 'test noop option': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/foobar/main.js' ]
- , fileContents: [ 'main\nsource\ncontents' ]
- , pkg: 'foobar'
- , json: { name: 'foobar', main: './main.js' }
- , options: { noop: true }
- , expectedResult: 'main\nsource\ncontents'
- }, done)
- }
-
- , 'test sandbox option (not on this package)': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/foobar/main.js' ]
- , fileContents: [ 'main\nsource\ncontents' ]
- , pkg: 'foobar'
- , json: {
- name: 'foobar'
- , main: './main.js'
- }
- , options: { sandbox: [ 'foo', 'bar' ] }
- , expectedResult: this.buildExpectedResult({
- name: 'foobar'
- , main: ' main\n source\n contents'
- })
- }, done)
- }
-
- , 'test sandbox option (on this package)': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/foobar/main.js' ]
- , fileContents: [ 'main\nsource\ncontents' ]
- , pkg: 'foobar'
- , json: {
- name: 'foobar'
- , main: './main.js'
- }
- , options: { sandbox: [ 'foobar', 'bar' ] }
- , expectedResult: this.buildExpectedResult({
- name: 'foobar'
- , main: ' main\n source\n contents'
- , sandbox: true
- })
- }, done)
- }
-
- , 'test sandbox option for root package (ender-js)': function (done) {
- this.runAsStringTest({
- expectedFileReads: [ './foobar/main.js' ]
- , fileContents: [ 'main\nsource\ncontents\n' ]
- , pkg: './foobar'
- , json: {
- name: 'ender-js'
- , main: './main.js'
- }
- , options: { sandbox: [ 'foo', 'bar' ] }
- , expectedResult:
- '/* Declare local API */\n'
- + 'var require, provide, $, ender;\n\n'
- + 'main\nsource\ncontents\n\n\n'
- + '/* Set Local API */\n'
- + 'require = this.require;\n'
- + 'provide = this.provide;\n'
- + 'ender = $ = this.ender;'
-
- }, done)
- }
-
- , 'test special characters not escaped': function (done) {
- // don't want a templating engine to escape for us
- this.runAsStringTest({
- expectedFileReads: [ 'node_modules/foobar/main.js' ]
- , fileContents: [ '!@#$%^&*()_+=-\\][{}|\';:"/.,<>?' ]
- , pkg: 'foobar'
- , json: {
- name: 'foobar'
- , main: './main.js'
- }
- , options: { }
- , expectedResult: this.buildExpectedResult({
- name: 'foobar'
- , main: ' !@#$%^&*()_+=-\\][{}|\';:"/.,<>?'
- })
- }, done)
- }
-
- , 'test identifier': function () {
- var json = { name: 'foobar', version: '1.2.3' }
- , srcPackage = SourcePackage.create('foobar', null, false, json)
-
- json.__proto__ = { name: 'barfoo' } // original json, see package-descriptor.js
- assert.equals(srcPackage.identifier, 'barfoo@1.2.3')
- }
-
- , 'test fs error': function (done) {
- var fsMock = this.mock(fs)
- , errArg = new Error('this is an error')
-
- fsMock.expects('readFile').once().callsArgWith(2, errArg)
-
- SourcePackage
- .create('whatevs', [], false, { name: 'whatevs', main: './main.js' }, {})
- .asString(function (err, arg) {
- assert(err)
- refute(arg)
- assert(err instanceof FilesystemError)
- assert.same(err.cause, errArg)
- assert.same(err.message, errArg.message)
- done()
- })
- }
-
- , 'extendOptions': {
- 'test nothing to extend': function () {
- var pkg = SourcePackage.create('whatevs', [], false, { name: 'whatevs', main: './main.js' }, {})
- , opts = { foo: 'bar' }
-
- pkg.extendOptions(opts)
- assert.equals(opts, { foo: 'bar' }) // shoudn't be touched
- }
-
- , 'test externs': function () {
- var pkg = SourcePackage.create('whatevs', [], false, { name: 'whatevs', main: './main.js', externs: 'lib/foo.js' }, {})
- , opts = { foo: 'bar' }
-
- pkg.extendOptions(opts)
- assert.equals(opts, { foo: 'bar', externs: [ path.resolve('node_modules/whatevs/lib/foo.js') ] }) // shoudn't be touched
- }
-
- , 'test externs with overridden pkg name': function () {
- // just to make sure we're pointing to the right dir, not using the overridden name
-
- var json = { name: 'whatevs', main: './main.js', externs: 'lib/foo.js' }
- , pkg = SourcePackage.create('whatevs', [], false, json, {})
- , opts = { foo: 'bar' }
- json.__proto__ = { name: 'whoa' }
-
- pkg.extendOptions(opts)
- assert.equals(opts, { foo: 'bar', externs: [ path.resolve('node_modules/whoa/lib/foo.js') ] })
- }
-
- , 'test externs array and nested pkg': function () {
- var pkg = SourcePackage.create('whatevs', [ 'boom', 'bang' ], false, { name: 'whatevs', main: './main.js', externs: [ 'lib/foo.js', 'BOOM.js' ] }, {})
- , opts = { foo: 'bar' }
-
- pkg.extendOptions(opts)
- assert.equals(opts, { foo: 'bar', externs: [
- path.resolve('node_modules/boom/node_modules/bang/node_modules/whatevs/lib/foo.js')
- , path.resolve('node_modules/boom/node_modules/bang/node_modules/whatevs/BOOM.js')
- ] })
- }
-
- , 'test externs array over existing externs': function () {
- var pkg = SourcePackage.create('whatevs', [ ], false, { name: 'whatevs', main: './main.js', externs: [ 'lib/foo.js', 'BOOM.js' ] }, {})
- , opts = { foo: 'bar', externs: [ 'existing1.js', 'existing2.js' ] }
-
- pkg.extendOptions(opts)
- assert.equals(opts, { foo: 'bar', externs: [
- 'existing1.js'
- , 'existing2.js'
- , path.resolve('node_modules/whatevs/lib/foo.js')
- , path.resolve('node_modules/whatevs/BOOM.js')
- ] })
- }
- }
-})
View
199 test/unit/source-package-util-test.js
@@ -1,199 +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 testCase = require('buster').testCase
- , fs = require('fs')
- , path = require('path')
- , async = require('async')
- , mkfiletree = require('mkfiletree')
- , sourcePackageUtil = require('../../lib/source-package-util')
-
-
-testCase('Source package util', {
- 'tearDown': function (done) {
- mkfiletree.cleanUp(done)
- }
-
- , 'loadFiles': {
- // note: we expect globs to return a sorted list of files (dirname(s) included)
-
- 'test single file with exact name': function (done) {
- mkfiletree.makeTemp(
- 'ender-test-source-package-util'
- , { 'foo.js': 'fooish!' }
- , function (err, dir) {
- refute(err)
- sourcePackageUtil.loadFiles(dir, [ 'foo.js' ], function (err, contents) {
- refute(err)
- assert.equals(
- contents
- , [ { file: path.join(dir, 'foo.js'), contents: 'fooish!' } ]
- )
- done()
- })
- }
- )
- }
-
- , 'test single file with glob': function (done) {
- mkfiletree.makeTemp(
- 'ender-test-source-package-util'
- , { 'foo.js': 'fooish!' }
- , function (err, dir) {
- refute(err)
- sourcePackageUtil.loadFiles(dir, [ '**/*.js' ], function (err, contents) {
- refute(err)
- assert.equals(
- contents
- , [ { file: path.join(dir, 'foo.js'), contents: 'fooish!' } ]
- )
- done()
- })
- }
- )
- }
-
- , 'test lots of files, one glob to rule them all': function (done) {
- mkfiletree.makeTemp(
- 'ender-test-source-package-util'
- , {
- 'foo.js': 'fooish!'
- , 'bar': {
- 'baz.js': 'baz!'
- , 'bang.js': 'bang!'
- , 'boo': { '1.js': 'boo!!', '2.js': 'boo!!!' }
- }
- }
- , function (err, dir) {
- refute(err)
-
- sourcePackageUtil.loadFiles(dir, [ '**/*.js' ], function (err, contents) {
- refute(err)
- assert.equals(
- contents
- , [
- { file: path.join(dir, 'bar/bang.js'), contents: 'bang!' }
- , { file: path.join(dir, 'bar/baz.js'), contents: 'baz!' }
- , { file: path.join(dir, 'bar/boo/1.js'), contents: 'boo!!' }
- , { file: path.join(dir, 'bar/boo/2.js'), contents: 'boo!!!' }
- , { file: path.join(dir, 'foo.js'), contents: 'fooish!' }
- ]
- )
- done()
- })
- }
- )
- }
-
- , 'test lots of files, cherry pick specifics': function (done) {
- mkfiletree.makeTemp(
- 'ender-test-source-package-util'
- , {
- 'foo.js': 'fooish!'
- , 'bar': {
- 'baz.js': 'baz!'
- , 'bang.js': 'bang!'
- , 'boo': { '1.js': 'boo!!', '2.js': 'boo!!!' }
- }
- }
- , function (err, dir) {
- refute(err)
-
- sourcePackageUtil.loadFiles(dir, [ 'foo.js', './bar/bang.js', '/bar/boo/2.js', 'bar/baz.js' ], function (err, contents) {
- refute(err)
- assert.equals(
- contents
- , [
- { file: path.join(dir, 'foo.js'), contents: 'fooish!' }
- , { file: path.join(dir, 'bar/bang.js'), contents: 'bang!' }
- , { file: path.join(dir, 'bar/boo/2.js'), contents: 'boo!!!' }
- , { file: path.join(dir, 'bar/baz.js'), contents: 'baz!' }
- ]
- )
- done()
- })
- }
- )
- }
-
- , 'test lots of files, shallow glob': function (done) {
- mkfiletree.makeTemp(
- 'ender-test-source-package-util'
- , {
- 'foo.js': 'fooish!'
- , 'bar': {
- 'baz.js': 'baz!'
- , 'bang.js': 'bang!'
- , 'boo': { '1.js': 'boo!!', '2.js': 'boo!!!' }
- }
- }
- , function (err, dir) {
- refute(err)
-
- sourcePackageUtil.loadFiles(dir, [ '*.js', 'bar/baz.js' ], function (err, contents) {
- refute(err)
- assert.equals(
- contents
- , [
- { file: path.join(dir, 'foo.js'), contents: 'fooish!' }
- , { file: path.join(dir, 'bar/baz.js'), contents: 'baz!' }
- ]
- )
- done()
- })
- }
- )
- }
-
- , 'test lots of files, deep, specific glob': function (done) {
- mkfiletree.makeTemp(
- 'ender-test-source-package-util'
- , {
- 'foo.js': 'fooish!'
- , 'bar': {
- 'baz.js': 'baz!'
- , 'bang.js': 'bang!'
- , 'boo': { '1.js': 'boo!!', '2.js': 'boo!!!' }
- }
- }
- , function (err, dir) {
- refute(err)
-
- sourcePackageUtil.loadFiles(dir, [ '*/*/*.js' ], function (err, contents) {
- refute(err)
- assert.equals(
- contents
- , [
- { file: path.join(dir, 'bar/boo/1.js'), contents: 'boo!!' }
- , { file: path.join(dir, 'bar/boo/2.js'), contents: 'boo!!!' }
- ]
- )
- done()
- })
- }
- )
- }
- }
-})
View
96 test/unit/write-test.js
@@ -1,96 +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
- , fs = require('fs')
- , path = require('path')
- , SourceBuild = require('../../lib/source-build')
- , write = require('../../lib/write')
- , buildOutput = require('../../lib/output/main-build-output')
- , FilesystemError = require('../../lib/errors').FilesystemError
-
-buster.testCase('Write', {
- 'test standard write': function (done) {
- var sourceBuild = SourceBuild.create()
- , mockFs = this.mock(fs)
- , mockSourceBuild = this.mock(sourceBuild)
- , sourceArg = 'source contents'
- , compressedSourceArg = 'compressed source'
- , fileArg = 'ender.js'
- , compressedFileArg = 'ender.min.js'
-
- mockSourceBuild.expects('asString').once().withArgs({ type: 'plain' }).callsArgWith(1, null, sourceArg)
- mockFs.expects('writeFile').once().withArgs(fileArg, sourceArg, 'utf-8').callsArg(3)
- mockSourceBuild.expects('asString').once().withArgs({ type: 'minified' }).callsArgWith(1, null, compressedSourceArg)
- mockFs.expects('writeFile').once().withArgs(compressedFileArg, compressedSourceArg, 'utf-8').callsArg(3)
-
- write.write({}, sourceBuild, buildOutput, function (err) {
- refute(err)
- done()
- })
- }
-
- , 'test write with --minifier none': function (done) {
- var sourceBuild = SourceBuild.create()
- , mockFs = this.mock(fs)
- , mockSourceBuild = this.mock(sourceBuild)
- , sourceArg = 'source contents'
- , fileArg = 'ender.js'
-
- mockSourceBuild.expects('asString').once().withArgs({ type: 'plain' }).callsArgWith(1, null, sourceArg)
- mockFs.expects('writeFile').once().withArgs(fileArg, sourceArg, 'utf-8').callsArg(3)
-
- write.write({ minifier: 'none' }, sourceBuild, buildOutput, function (err) {
- refute(err)
- done()
- })
- }
-
- , 'test fs error': function (done) {
- var sourceBuild = SourceBuild.create()
- , mockFs = this.mock(fs)
- , mockSourceBuild = this.mock(sourceBuild)
- , sourceArg = 'source contents'
- , compressedSourceArg = 'compressed source'
- , fileArg = 'ender.js'
- , compressedFileArg = 'ender.min.js'
- , errArg = new Error('this is an error')
-
- mockSourceBuild.expects('asString').once().withArgs({ type: 'plain' }).callsArgWith(1, null, sourceArg)
- mockFs.expects('writeFile').once().withArgs(fileArg, sourceArg, 'utf-8').callsArg(3)
- mockSourceBuild.expects('asString').once().withArgs({ type: 'minified' }).callsArgWith(1, null, compressedSourceArg)
- mockFs.expects('writeFile').once().withArgs(compressedFileArg, compressedSourceArg, 'utf-8').callsArgWith(3, errArg)
-
- write.write({}, sourceBuild, buildOutput, function (err) {
- assert(err)
- 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.