Ensure relative paths work with --output and template=… build options.

Former-commit-id: 2447466a006dc8d968969bba70a0e44f91610154
This commit is contained in:
John-David Dalton
2013-02-24 11:40:56 -08:00
parent 8cbbc47043
commit c1e9dd3373
2 changed files with 69 additions and 37 deletions

View File

@@ -19,19 +19,22 @@
* @param {Number|String} mode The permission mode. * @param {Number|String} mode The permission mode.
*/ */
function mkdirpSync(dirname, mode) { function mkdirpSync(dirname, mode) {
var separator = path.sep, var sep = path.sep,
segments = dirname.split(separator),
type = typeof mode; type = typeof mode;
// ensure relative paths are prefixed with `./`
if (!RegExp('^\\.?' + sep).test(dirname)) {
dirname = '.' + sep + dirname;
}
if (!(type == 'number' || type == 'string')) { if (!(type == 'number' || type == 'string')) {
mode = '0777'; mode = '0777';
} }
segments.reduce(function(currPath, segment, index) { dirname.split(sep).reduce(function(currPath, segment, index) {
// skip leading separator of absolute paths // skip leading separator of absolute paths
if (index === 0 && currPath === '') { if (index === 0 && currPath === '') {
return separator; return sep;
} }
segment = currPath + (currPath === separator ? segment : separator + segment); segment = currPath + (currPath === sep ? segment : sep + segment);
try { try {
segment = fs.realpathSync(segment); segment = fs.realpathSync(segment);
} catch(e) { } catch(e) {

View File

@@ -2,16 +2,27 @@
;(function(undefined) { ;(function(undefined) {
'use strict'; 'use strict';
/** Load Node.js modules */
var fs = require('fs'),
path = require('path'),
vm = require('vm');
/** Load other modules */
var build = require('../build.js'),
minify = require('../build/minify'),
_ = require('../lodash.js');
/** Used to avoid `noglobal` false positives caused by `errno` leaked in Node.js */ /** Used to avoid `noglobal` false positives caused by `errno` leaked in Node.js */
global.errno = true; global.errno = true;
/** Load modules */ /** Add `path.sep` for older versions of Node.js */
var fs = require('fs'), path.sep || (path.sep = process.platform == 'win32' ? '\\' : '/');
path = require('path'),
vm = require('vm'), /** The current working directory */
build = require('../build.js'), var cwd = process.cwd();
minify = require('../build/minify'),
_ = require('../lodash.js'); /** Used to prefix relative paths from the current directory */
var relativePrefix = '.' + path.sep;
/** The unit testing framework */ /** The unit testing framework */
var QUnit = ( var QUnit = (
@@ -503,33 +514,46 @@
QUnit.module('template builds'); QUnit.module('template builds');
(function() { (function() {
var templatePath = __dirname + '/template'; var templatePath = path.join(__dirname, 'template');
asyncTest('`lodash template=*.jst`', function() { var commands = [
var start = _.after(2, _.once(QUnit.start)); 'template=' + path.join('template', '*.jst'),
'template=' + relativePrefix + path.join('template', '*.jst'),
'template=' + path.join(templatePath, '*.jst')
];
build(['-s', 'template=' + templatePath + '/*.jst'], function(data) { commands.forEach(function(command) {
var basename = path.basename(data.outputPath, '.js'), asyncTest('`lodash ' + command +'`', function() {
context = createContext(); var start = _.after(2, _.once(function() {
process.chdir(cwd);
QUnit.start();
}));
var object = { process.chdir(__dirname);
'a': { 'people': ['moe', 'larry', 'curly'] },
'b': { 'epithet': 'stooge' },
'c': { 'name': 'ES6' }
};
context._ = _; build(['-s', command], function(data) {
vm.runInContext(data.source, context); var basename = path.basename(data.outputPath, '.js'),
context = createContext();
equal(_.templates.a(object.a).replace(/[\r\n]+/g, ''), '<ul><li>moe</li><li>larry</li><li>curly</li></ul>', basename); var object = {
equal(_.templates.b(object.b), 'Hello stooge.', basename); 'a': { 'people': ['moe', 'larry', 'curly'] },
equal(_.templates.c(object.c), 'Hello ES6!', basename); 'b': { 'epithet': 'stooge' },
delete _.templates; 'c': { 'name': 'ES6' }
start(); };
context._ = _;
vm.runInContext(data.source, context);
equal(_.templates.a(object.a).replace(/[\r\n]+/g, ''), '<ul><li>moe</li><li>larry</li><li>curly</li></ul>', basename);
equal(_.templates.b(object.b), 'Hello stooge.', basename);
equal(_.templates.c(object.c), 'Hello ES6!', basename);
delete _.templates;
start();
});
}); });
}); });
var commands = [ commands = [
'', '',
'moduleId=underscore' 'moduleId=underscore'
]; ];
@@ -540,7 +564,7 @@
asyncTest('`lodash template=*.jst exports=amd' + (command ? ' ' + command : '') + '`', function() { asyncTest('`lodash template=*.jst exports=amd' + (command ? ' ' + command : '') + '`', function() {
var start = _.after(2, _.once(QUnit.start)); var start = _.after(2, _.once(QUnit.start));
build(['-s', 'template=' + templatePath + '/*.jst', 'exports=amd'].concat(command || []), function(data) { build(['-s', 'template=' + path.join(templatePath, '*.jst'), 'exports=amd'].concat(command || []), function(data) {
var moduleId, var moduleId,
basename = path.basename(data.outputPath, '.js'), basename = path.basename(data.outputPath, '.js'),
context = createContext(); context = createContext();
@@ -565,7 +589,7 @@
asyncTest('`lodash settings=...' + (command ? ' ' + command : '') + '`', function() { asyncTest('`lodash settings=...' + (command ? ' ' + command : '') + '`', function() {
var start = _.after(2, _.once(QUnit.start)); var start = _.after(2, _.once(QUnit.start));
build(['-s', 'template=' + templatePath + '/*.tpl', 'settings={interpolate:/{{([\\s\\S]+?)}}/}'].concat(command || []), function(data) { build(['-s', 'template=' + path.join(templatePath, '*.tpl'), 'settings={interpolate:/{{([\\s\\S]+?)}}/}'].concat(command || []), function(data) {
var moduleId, var moduleId,
basename = path.basename(data.outputPath, '.js'), basename = path.basename(data.outputPath, '.js'),
context = createContext(); context = createContext();
@@ -670,9 +694,12 @@
equal(sourceMap.file, basename + '.js', basename); equal(sourceMap.file, basename + '.js', basename);
deepEqual(sourceMap.sources, sources, basename); deepEqual(sourceMap.sources, sources, basename);
process.chdir(cwd);
QUnit.start(); QUnit.start();
}); });
process.chdir(__dirname);
outputCommand = outputCommand ? outputCommand.split(' ') : []; outputCommand = outputCommand ? outputCommand.split(' ') : [];
if (outputCommand.indexOf('-m') < 0) { if (outputCommand.indexOf('-m') < 0) {
callback = _.after(2, callback); callback = _.after(2, callback);
@@ -1112,7 +1139,9 @@
var commands = [ var commands = [
'-o a.js', '-o a.js',
'--output b.js', '--output b.js',
'-o ./a/b/c.js' '-o ' + path.join('a', 'b', 'c.js'),
'-o ' + relativePrefix + path.join('a', 'b', 'c.js'),
'-o ' + path.join(nestedPath, 'c.js')
]; ];
commands.forEach(function(command) { commands.forEach(function(command) {
@@ -1126,12 +1155,12 @@
fs.rmdirSync(nestedPath); fs.rmdirSync(nestedPath);
fs.rmdirSync(path.dirname(nestedPath)); fs.rmdirSync(path.dirname(nestedPath));
} }
process.chdir(cwd);
QUnit.start(); QUnit.start();
})); }));
if (dirs) { process.chdir(__dirname);
command = command.replace('./a/b/c.js', path.join(nestedPath, 'c.js'));
}
build(['-s'].concat(command.split(' ')), function(data) { build(['-s'].concat(command.split(' ')), function(data) {
var basename = path.basename(data.outputPath, '.js'); var basename = path.basename(data.outputPath, '.js');
equal(basename, expected + (counter++ ? '.min' : ''), command); equal(basename, expected + (counter++ ? '.min' : ''), command);