Add "output" and "stdout" build option unit tests.

Former-commit-id: 2adcdbff4cd1ef6319e33c69fd5ed3b07b205cfe
This commit is contained in:
John-David Dalton
2012-09-09 20:33:08 -07:00
parent 20fcede440
commit 5e04c7f827
3 changed files with 56 additions and 20 deletions

View File

@@ -15,6 +15,9 @@
/** Shortcut used to convert array-like objects to arrays */ /** Shortcut used to convert array-like objects to arrays */
var slice = [].slice; var slice = [].slice;
/** Shorcut to the `stdout` object */
var stdout = process.stdout;
/** Used to associate aliases with their real names */ /** Used to associate aliases with their real names */
var aliasToRealMap = { var aliasToRealMap = {
'all': 'every', 'all': 'every',
@@ -1282,7 +1285,7 @@
if (!_.isEqual(exportsOptions, exportsAll) || filterType || isBackbone || isLegacy || isMobile || isStrict || isUnderscore) { if (!_.isEqual(exportsOptions, exportsAll) || filterType || isBackbone || isLegacy || isMobile || isStrict || isUnderscore) {
// output debug build // output debug build
if (!outputPath && !isStdOut) { if (!outputPath && !isStdOut) {
callback(path.join(cwd, 'lodash.custom.js'), debugSource); callback(debugSource, path.join(cwd, 'lodash.custom.js'));
} }
minify(source, { minify(source, {
'silent': isSilent, 'silent': isSilent,
@@ -1294,9 +1297,10 @@
} }
source = postMinify(source); source = postMinify(source);
if (isStdOut) { if (isStdOut) {
console.log(source); stdout.write(source);
callback(source);
} else { } else {
callback(outputPath || path.join(cwd, 'lodash.custom.min.js'), source); callback(source, outputPath || path.join(cwd, 'lodash.custom.min.js'));
} }
} }
}); });
@@ -1308,9 +1312,10 @@
'onComplete': function(source) { 'onComplete': function(source) {
source = postMinify(source); source = postMinify(source);
if (isStdOut) { if (isStdOut) {
console.log(source); stdout.write(source);
callback(source);
} else { } else {
callback(outputPath || path.join(cwd, 'lodash.min.js'), source); callback(source, outputPath || path.join(cwd, 'lodash.min.js'));
} }
} }
}); });
@@ -1325,8 +1330,8 @@
} }
else { else {
// or invoked directly // or invoked directly
build(process.argv, function(filepath, source) { build(process.argv, function(source, filepath) {
fs.writeFileSync(filepath, source, 'utf8'); filepath && fs.writeFileSync(filepath, source, 'utf8');
}); });
} }
}()); }());

View File

@@ -38,14 +38,14 @@
source = source.replace(/("function")\s*==\s*(typeof define)\s*&&\s*\(?\s*("object")\s*==\s*(typeof define\.amd)\s*&&\s*(define\.amd)\s*\)?/, '$2==$1&&$4==$3&&$5'); source = source.replace(/("function")\s*==\s*(typeof define)\s*&&\s*\(?\s*("object")\s*==\s*(typeof define\.amd)\s*&&\s*(define\.amd)\s*\)?/, '$2==$1&&$4==$3&&$5');
// add trailing semicolon // add trailing semicolon
source = source.replace(/[\s;]*$/, ';'); if (source) {
source = source.replace(/[\s;]*$/, ';');
}
// exit early if version snippet isn't found // exit early if version snippet isn't found
var snippet = /VERSION\s*[=:]\s*([\'"])(.*?)\1/.exec(source); var snippet = /VERSION\s*[=:]\s*([\'"])(.*?)\1/.exec(source);
if (!snippet) { if (!snippet) {
return source; return source;
} }
// add license // add license
return licenseTemplate[/call\(this\);?$/.test(source) ? 'underscore' : 'lodash'] return licenseTemplate[/call\(this\);?$/.test(source) ? 'underscore' : 'lodash']
.replace('@VERSION', snippet[2]) + '\n;' + source; .replace('@VERSION', snippet[2]) + '\n;' + source;

View File

@@ -454,7 +454,7 @@
var start = _.after(2, _.once(QUnit.start)); var start = _.after(2, _.once(QUnit.start));
asyncTest('`lodash ' + command +'`', function() { asyncTest('`lodash ' + command +'`', function() {
build(['--silent'].concat(command.split(' ')), function(filepath, source) { build(['--silent'].concat(command.split(' ')), function(source, filepath) {
var basename = path.basename(filepath, '.js'), var basename = path.basename(filepath, '.js'),
context = createContext(), context = createContext(),
methodNames = []; methodNames = [];
@@ -535,7 +535,7 @@
commands.push('strict'); commands.push('strict');
} }
build(commands, function(filepath, source) { build(commands, function(source, filepath) {
var basename = path.basename(filepath, '.js'), var basename = path.basename(filepath, '.js'),
context = createContext(), context = createContext(),
pass = !index; pass = !index;
@@ -565,7 +565,7 @@
var start = _.once(QUnit.start); var start = _.once(QUnit.start);
asyncTest('should not have deep clone', function() { asyncTest('should not have deep clone', function() {
build(['-s', 'underscore'], function(filepath, source) { build(['-s', 'underscore'], function(source, filepath) {
var array = [{ 'a': 1 }], var array = [{ 'a': 1 }],
basename = path.basename(filepath, '.js'), basename = path.basename(filepath, '.js'),
context = createContext(); context = createContext();
@@ -582,12 +582,7 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('exports command'); QUnit.module('exports command');
var exportsAll = [
'amd',
'commonjs',
'global',
'node'
];
(function() { (function() {
var commands = [ var commands = [
'exports=amd', 'exports=amd',
@@ -600,7 +595,7 @@
var start = _.after(2, _.once(QUnit.start)); var start = _.after(2, _.once(QUnit.start));
asyncTest('`lodash ' + command +'`', function() { asyncTest('`lodash ' + command +'`', function() {
build(['-s', command], function(filepath, source) { build(['-s', command], function(source, filepath) {
var basename = path.basename(filepath, '.js'), var basename = path.basename(filepath, '.js'),
context = createContext(), context = createContext(),
pass = false; pass = false;
@@ -641,4 +636,40 @@
}); });
}()); }());
/*--------------------------------------------------------------------------*/
QUnit.module('output options');
(function() {
['-o a.js', '--output a.js'].forEach(function(command, index) {
var start = _.once(QUnit.start);
asyncTest('`lodash ' + command +'`', function() {
build(['-s'].concat(command.split(' ')), function(source, filepath) {
equal(filepath, 'a.js', command);
start();
});
});
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('stdout options');
(function() {
['-c', '--stdout'].forEach(function(command, index) {
var descriptor = Object.getOwnPropertyDescriptor(global, 'console'),
start = _.once(QUnit.start);
asyncTest('`lodash ' + command +'`', function() {
build([command, 'exports=', 'include='], function(source, filepath) {
equal(source, '');
equal(arguments.length, 1);
start();
});
});
});
}());
}()); }());