resolve
Advanced tools
Comparing version 1.2.0 to 1.3.0
var resolve = require('../'); | ||
resolve('tap', { basedir: __dirname }, function (err, res) { | ||
if (err) console.error(err) | ||
else console.log(res) | ||
if (err) console.error(err); | ||
else console.log(res); | ||
}); |
var core = require('./lib/core'); | ||
exports = module.exports = require('./lib/async'); | ||
exports.core = core; | ||
exports.isCore = function (x) { return core[x] }; | ||
exports.isCore = function isCore(x) { return core[x]; }; | ||
exports.sync = require('./lib/sync'); |
180
lib/async.js
@@ -6,5 +6,7 @@ var core = require('./core'); | ||
var nodeModulesPaths = require('./node-modules-paths.js'); | ||
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//; | ||
var format = require('util').format; | ||
module.exports = function resolve (x, opts, cb) { | ||
module.exports = function resolve(x, options, callback) { | ||
var cb = callback; | ||
var opts = options || {}; | ||
if (typeof opts === 'function') { | ||
@@ -14,3 +16,2 @@ cb = opts; | ||
} | ||
if (!opts) opts = {}; | ||
if (typeof x !== 'string') { | ||
@@ -22,17 +23,17 @@ var err = new TypeError('path must be a string'); | ||
} | ||
var isFile = opts.isFile || function (file, cb) { | ||
fs.stat(file, function (err, stat) { | ||
if (err && err.code === 'ENOENT') cb(null, false) | ||
else if (err) cb(err) | ||
else cb(null, stat.isFile() || stat.isFIFO()) | ||
if (err && err.code === 'ENOENT') cb(null, false); | ||
else if (err) cb(err); | ||
else cb(null, stat.isFile() || stat.isFIFO()); | ||
}); | ||
}; | ||
var readFile = opts.readFile || fs.readFile; | ||
var extensions = opts.extensions || [ '.js' ]; | ||
var extensions = opts.extensions || ['.js']; | ||
var y = opts.basedir || path.dirname(caller()); | ||
opts.paths = opts.paths || []; | ||
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) { | ||
@@ -43,23 +44,30 @@ var res = path.resolve(y, x); | ||
loadAsDirectory(res, opts.package, onfile); | ||
} else loadAsFile(res, opts.package, onfile); | ||
} else loadNodeModules(x, y, function (err, n, pkg) { | ||
if (err) cb(err); | ||
else if (n) cb(null, n, pkg); | ||
else if (core[x]) return cb(null, x); | ||
else { | ||
var moduleError = new Error(format("Cannot find module '%s' from '%s' with extension in %j", x, y, extensions)); | ||
moduleError.code = 'MODULE_NOT_FOUND'; | ||
cb(moduleError); | ||
} | ||
else loadAsFile(res, opts.package, onfile); | ||
} | ||
else loadNodeModules(x, y, function (err, n, pkg) { | ||
if (err) cb(err) | ||
else if (n) cb(null, n, pkg) | ||
else if (core[x]) return cb(null, x); | ||
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'")) | ||
}); | ||
function onfile (err, m, pkg) { | ||
if (err) cb(err) | ||
else if (m) cb(null, m, pkg) | ||
function onfile(err, m, pkg) { | ||
if (err) cb(err); | ||
else if (m) cb(null, m, pkg); | ||
else loadAsDirectory(res, function (err, d, pkg) { | ||
if (err) cb(err) | ||
else if (d) cb(null, d, pkg) | ||
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'")) | ||
}) | ||
if (err) cb(err); | ||
else if (d) cb(null, d, pkg); | ||
else { | ||
var moduleError = new Error(format("Cannot find module '%s' from '%s' with extension in %j", x, y, extensions)); | ||
moduleError.code = 'MODULE_NOT_FOUND'; | ||
cb(moduleError); | ||
} | ||
}); | ||
} | ||
function loadAsFile (x, pkg, cb) { | ||
function loadAsFile(x, pkg, callback) { | ||
var cb = callback; | ||
if (typeof pkg === 'function') { | ||
@@ -69,16 +77,16 @@ cb = pkg; | ||
} | ||
var exts = [''].concat(extensions); | ||
load(exts, x, pkg) | ||
load(exts, x, pkg); | ||
function load (exts, x, pkg) { | ||
function load(exts, x, pkg) { | ||
if (exts.length === 0) return cb(null, undefined, pkg); | ||
var file = x + exts[0]; | ||
if (pkg) onpkg(null, pkg) | ||
if (pkg) onpkg(null, pkg); | ||
else loadpkg(path.dirname(file), onpkg); | ||
function onpkg (err, pkg_, dir) { | ||
function onpkg(err, pkg_, dir) { | ||
pkg = pkg_; | ||
if (err) return cb(err) | ||
if (err) return cb(err); | ||
if (dir && pkg && opts.pathFilter) { | ||
@@ -96,29 +104,26 @@ var rfile = path.relative(dir, file); | ||
} | ||
function onex (err, ex) { | ||
if (err) cb(err) | ||
else if (!ex) load(exts.slice(1), x, pkg) | ||
else cb(null, file, pkg) | ||
function onex(err, ex) { | ||
if (err) cb(err); | ||
else if (!ex) load(exts.slice(1), x, pkg); | ||
else cb(null, file, pkg); | ||
} | ||
} | ||
} | ||
function loadpkg (dir, cb) { | ||
function loadpkg(dir, cb) { | ||
if (dir === '' || dir === '/') return cb(null); | ||
if (process.platform === 'win32' && /^\w:[\\\/]*$/.test(dir)) { | ||
if (process.platform === 'win32' && (/^\w:[\\\/]*$/).test(dir)) { | ||
return cb(null); | ||
} | ||
if (/[\\\/]node_modules[\\\/]*$/.test(dir)) return cb(null); | ||
var pkgfile = path.join(dir, 'package.json'); | ||
isFile(pkgfile, function (err, ex) { | ||
// on err, ex is false | ||
if (!ex) return loadpkg( | ||
path.dirname(dir), cb | ||
); | ||
if (!ex) return loadpkg(path.dirname(dir), cb); | ||
readFile(pkgfile, function (err, body) { | ||
if (err) cb(err); | ||
try { var pkg = JSON.parse(body) } | ||
catch (err) {} | ||
try { var pkg = JSON.parse(body); } catch (jsonErr) {} | ||
if (pkg && opts.packageFilter) { | ||
@@ -131,4 +136,5 @@ pkg = opts.packageFilter(pkg, pkgfile); | ||
} | ||
function loadAsDirectory (x, fpkg, cb) { | ||
function loadAsDirectory(x, fpkg, callback) { | ||
var cb = callback; | ||
if (typeof fpkg === 'function') { | ||
@@ -138,8 +144,8 @@ cb = fpkg; | ||
} | ||
var pkgfile = path.join(x, '/package.json'); | ||
var pkgfile = path.join(x, 'package.json'); | ||
isFile(pkgfile, function (err, ex) { | ||
if (err) return cb(err); | ||
if (!ex) return loadAsFile(path.join(x, '/index'), fpkg, cb); | ||
if (!ex) return loadAsFile(path.join(x, 'index'), fpkg, cb); | ||
readFile(pkgfile, function (err, body) { | ||
@@ -149,12 +155,11 @@ if (err) return cb(err); | ||
var pkg = JSON.parse(body); | ||
} | ||
catch (err) {} | ||
} catch (jsonErr) {} | ||
if (opts.packageFilter) { | ||
pkg = opts.packageFilter(pkg, pkgfile); | ||
} | ||
if (pkg.main) { | ||
if (pkg.main === '.' || pkg.main === './'){ | ||
pkg.main = 'index' | ||
if (pkg.main === '.' || pkg.main === './') { | ||
pkg.main = 'index'; | ||
} | ||
@@ -164,3 +169,3 @@ loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) { | ||
if (m) return cb(null, m, pkg); | ||
if (!pkg) return loadAsFile(path.join(x, '/index'), pkg, cb); | ||
if (!pkg) return loadAsFile(path.join(x, 'index'), pkg, cb); | ||
@@ -171,3 +176,3 @@ var dir = path.resolve(x, pkg.main); | ||
if (n) return cb(null, n, pkg); | ||
loadAsFile(path.join(x, '/index'), pkg, cb); | ||
loadAsFile(path.join(x, 'index'), pkg, cb); | ||
}); | ||
@@ -177,3 +182,3 @@ }); | ||
} | ||
loadAsFile(path.join(x, '/index'), pkg, cb); | ||
@@ -183,24 +188,25 @@ }); | ||
} | ||
function loadNodeModules (x, start, cb) { | ||
(function process (dirs) { | ||
if (dirs.length === 0) return cb(null, undefined); | ||
var dir = dirs[0]; | ||
var file = path.join(dir, '/', x); | ||
loadAsFile(file, undefined, onfile); | ||
function onfile (err, m, pkg) { | ||
if (err) return cb(err); | ||
if (m) return cb(null, m, pkg); | ||
loadAsDirectory(path.join(dir, '/', x), undefined, ondir); | ||
} | ||
function ondir (err, n, pkg) { | ||
if (err) return cb(err); | ||
if (n) return cb(null, n, pkg); | ||
process(dirs.slice(1)); | ||
} | ||
})(nodeModulesPaths(start, opts)); | ||
function processDirs(cb, dirs) { | ||
if (dirs.length === 0) return cb(null, undefined); | ||
var dir = dirs[0]; | ||
var file = path.join(dir, x); | ||
loadAsFile(file, undefined, onfile); | ||
function onfile(err, m, pkg) { | ||
if (err) return cb(err); | ||
if (m) return cb(null, m, pkg); | ||
loadAsDirectory(path.join(dir, x), undefined, ondir); | ||
} | ||
function ondir(err, n, pkg) { | ||
if (err) return cb(err); | ||
if (n) return cb(null, n, pkg); | ||
processDirs(cb, dirs.slice(1)); | ||
} | ||
} | ||
function loadNodeModules(x, start, cb) { | ||
processDirs(cb, nodeModulesPaths(start, opts)); | ||
} | ||
}; |
module.exports = function () { | ||
// see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi | ||
var origPrepareStackTrace = Error.prepareStackTrace; | ||
Error.prepareStackTrace = function (_, stack) { return stack }; | ||
Error.prepareStackTrace = function (_, stack) { return stack; }; | ||
var stack = (new Error()).stack; | ||
@@ -6,0 +6,0 @@ Error.prepareStackTrace = origPrepareStackTrace; |
@@ -7,3 +7,3 @@ var current = process.versions.node.split('.'); | ||
for (var i = 0; i < 3; ++i) { | ||
if ((current[i] || 0) >= (versionParts[i] || 0)) return true; | ||
if ((current[i] || 0) >= (versionParts[i] || 0)) return true; | ||
} | ||
@@ -10,0 +10,0 @@ return false; |
var path = require('path'); | ||
var parse = path.parse || require('path-parse'); | ||
module.exports = function (start, opts) { | ||
var modules = opts.moduleDirectory | ||
module.exports = function nodeModulesPaths(start, opts) { | ||
var modules = opts && opts.moduleDirectory | ||
? [].concat(opts.moduleDirectory) | ||
@@ -20,20 +21,16 @@ : ['node_modules'] | ||
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/; | ||
var paths = [start]; | ||
var parsed = parse(start); | ||
while (parsed.dir !== paths[paths.length - 1]) { | ||
paths.push(parsed.dir); | ||
parsed = parse(parsed.dir); | ||
} | ||
var parts = start.split(splitRe); | ||
var dirs = paths.reduce(function (dirs, aPath) { | ||
return dirs.concat(modules.map(function (moduleDir) { | ||
return path.join(prefix, aPath, moduleDir); | ||
})); | ||
}, []); | ||
var dirs = []; | ||
for (var i = parts.length - 1; i >= 0; i--) { | ||
if (modules.indexOf(parts[i]) !== -1) continue; | ||
dirs = dirs.concat(modules.map(function(module_dir) { | ||
return prefix + path.join( | ||
path.join.apply(path, parts.slice(0, i + 1)), | ||
module_dir | ||
); | ||
})); | ||
} | ||
if (process.platform === 'win32'){ | ||
dirs[dirs.length-1] = dirs[dirs.length-1].replace(":", ":\\"); | ||
} | ||
return dirs.concat(opts.paths); | ||
} | ||
return opts && opts.paths ? dirs.concat(opts.paths) : dirs; | ||
}; |
@@ -6,10 +6,12 @@ var core = require('./core'); | ||
var nodeModulesPaths = require('./node-modules-paths.js'); | ||
var format = require('util').format; | ||
module.exports = function (x, opts) { | ||
if (!opts) opts = {}; | ||
module.exports = function (x, options) { | ||
var opts = options || {}; | ||
var isFile = opts.isFile || function (file) { | ||
try { var stat = fs.statSync(file) } | ||
catch (err) { | ||
if (err && err.code === 'ENOENT') return false; | ||
throw err; | ||
try { | ||
var stat = fs.statSync(file); | ||
} catch (e) { | ||
if (e && e.code === 'ENOENT') return false; | ||
throw e; | ||
} | ||
@@ -19,4 +21,4 @@ return stat.isFile() || stat.isFIFO(); | ||
var readFileSync = opts.readFileSync || fs.readFileSync; | ||
var extensions = opts.extensions || [ '.js' ]; | ||
var extensions = opts.extensions || ['.js']; | ||
var y = opts.basedir || path.dirname(caller()); | ||
@@ -35,12 +37,14 @@ | ||
} | ||
if (core[x]) return x; | ||
throw new Error("Cannot find module '" + x + "' from '" + y + "'"); | ||
function loadAsFileSync (x) { | ||
var err = new Error(format("Cannot find module '%s' from '%s' with extension in %j", x, y, extensions)); | ||
err.code = 'MODULE_NOT_FOUND'; | ||
throw err; | ||
function loadAsFileSync(x) { | ||
if (isFile(x)) { | ||
return x; | ||
} | ||
for (var i = 0; i < extensions.length; i++) { | ||
@@ -53,4 +57,4 @@ var file = x + extensions[i]; | ||
} | ||
function loadAsDirectorySync (x) { | ||
function loadAsDirectorySync(x) { | ||
var pkgfile = path.join(x, '/package.json'); | ||
@@ -64,3 +68,3 @@ if (isFile(pkgfile)) { | ||
} | ||
if (pkg.main) { | ||
@@ -72,16 +76,15 @@ var m = loadAsFileSync(path.resolve(x, pkg.main)); | ||
} | ||
} | ||
catch (err) {} | ||
} catch (e) {} | ||
} | ||
return loadAsFileSync(path.join( x, '/index')); | ||
return loadAsFileSync(path.join(x, '/index')); | ||
} | ||
function loadNodeModulesSync (x, start) { | ||
function loadNodeModulesSync(x, start) { | ||
var dirs = nodeModulesPaths(start, opts); | ||
for (var i = 0; i < dirs.length; i++) { | ||
var dir = dirs[i]; | ||
var m = loadAsFileSync(path.join( dir, '/', x)); | ||
var m = loadAsFileSync(path.join(dir, '/', x)); | ||
if (m) return m; | ||
var n = loadAsDirectorySync(path.join( dir, '/', x )); | ||
var n = loadAsDirectorySync(path.join(dir, '/', x)); | ||
if (n) return n; | ||
@@ -88,0 +91,0 @@ } |
{ | ||
"name": "resolve", | ||
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"repository": { | ||
@@ -22,5 +22,6 @@ "type": "git", | ||
"devDependencies": { | ||
"tape": "^4.6.3", | ||
"object-keys": "^1.0.11", | ||
"safe-publish-latest": "^1.1.1", | ||
"tap": "0.4.13", | ||
"safe-publish-latest": "^1.1.1" | ||
"tape": "^4.6.3" | ||
}, | ||
@@ -32,3 +33,6 @@ "license": "MIT", | ||
"url": "http://substack.net" | ||
}, | ||
"dependencies": { | ||
"path-parse": "^1.0.5" | ||
} | ||
} |
@@ -7,12 +7,12 @@ var path = require('path'); | ||
t.plan(4); | ||
var dir = __dirname + '/dotdot/abc'; | ||
resolve('..', { basedir : dir }, function (err, res, pkg) { | ||
var dir = path.join(__dirname, '/dotdot/abc'); | ||
resolve('..', { basedir: dir }, function (err, res, pkg) { | ||
t.ifError(err); | ||
t.equal(res, __dirname + '/dotdot/index.js'); | ||
t.equal(res, path.join(__dirname, 'dotdot/index.js')); | ||
}); | ||
resolve('.', { basedir : dir }, function (err, res, pkg) { | ||
resolve('.', { basedir: dir }, function (err, res, pkg) { | ||
t.ifError(err); | ||
t.equal(res, dir + '/index.js'); | ||
t.equal(res, path.join(dir, 'index.js')); | ||
}); | ||
@@ -23,9 +23,9 @@ }); | ||
t.plan(2); | ||
var dir = __dirname + '/dotdot/abc'; | ||
var a = resolve.sync('..', { basedir : dir }); | ||
t.equal(a, __dirname + '/dotdot/index.js'); | ||
var b = resolve.sync('.', { basedir : dir }); | ||
t.equal(b, dir + '/index.js'); | ||
var dir = path.join(__dirname, '/dotdot/abc'); | ||
var a = resolve.sync('..', { basedir: dir }); | ||
t.equal(a, path.join(__dirname, 'dotdot/index.js')); | ||
var b = resolve.sync('.', { basedir: dir }); | ||
t.equal(b, path.join(dir, 'index.js')); | ||
}); |
@@ -1,1 +0,1 @@ | ||
module.exports = 'whatever' | ||
module.exports = 'whatever'; |
@@ -1,9 +0,5 @@ | ||
var path = require('path'); | ||
var test = require('tape'); | ||
var resolve = require('../'); | ||
// not sure what's up with this test anymore | ||
if (process.platform !== 'win32') return; | ||
test('faulty basedir must produce error in windows', function (t) { | ||
test('faulty basedir must produce error in windows', { skip: process.platform !== 'win32' }, function (t) { | ||
t.plan(1); | ||
@@ -13,3 +9,3 @@ | ||
resolve('tape/lib/test.js', { basedir : resolverDir }, function (err, res, pkg) { | ||
resolve('tape/lib/test.js', { basedir: resolverDir }, function (err, res, pkg) { | ||
t.equal(true, !!err); | ||
@@ -16,0 +12,0 @@ }); |
@@ -0,1 +1,2 @@ | ||
var path = require('path'); | ||
var test = require('tape'); | ||
@@ -5,12 +6,12 @@ var resolve = require('../'); | ||
test('filter', function (t) { | ||
var dir = __dirname + '/resolver'; | ||
var dir = path.join(__dirname, 'resolver'); | ||
var res = resolve.sync('./baz', { | ||
basedir : dir, | ||
packageFilter : function (pkg) { | ||
pkg.main = 'doom' | ||
basedir: dir, | ||
packageFilter: function (pkg) { | ||
pkg.main = 'doom'; | ||
return pkg; | ||
} | ||
}); | ||
t.equal(res, dir + '/baz/doom.js'); | ||
t.equal(res, path.join(dir, 'baz/doom.js')); | ||
t.end(); | ||
}); |
@@ -0,1 +1,2 @@ | ||
var path = require('path'); | ||
var test = require('tape'); | ||
@@ -6,6 +7,6 @@ var resolve = require('../'); | ||
t.plan(2); | ||
var dir = __dirname + '/resolver'; | ||
var dir = path.join(__dirname, 'resolver'); | ||
resolve('./baz', { | ||
basedir : dir, | ||
packageFilter : function (pkg) { | ||
basedir: dir, | ||
packageFilter: function (pkg) { | ||
pkg.main = 'doom'; | ||
@@ -16,5 +17,5 @@ return pkg; | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/baz/doom.js'); | ||
t.equal(res, path.join(dir, 'baz/doom.js')); | ||
t.equal(pkg.main, 'doom'); | ||
}); | ||
}); |
@@ -0,1 +1,2 @@ | ||
var path = require('path'); | ||
var test = require('tape'); | ||
@@ -6,29 +7,28 @@ var resolve = require('../'); | ||
t.plan(4); | ||
var files = { | ||
'/foo/bar/baz.js' : 'beep' | ||
}; | ||
function opts (basedir) { | ||
var files = {}; | ||
files[path.resolve('/foo/bar/baz.js')] = 'beep'; | ||
function opts(basedir) { | ||
return { | ||
basedir : basedir, | ||
isFile : function (file) { | ||
return files.hasOwnProperty(file) | ||
basedir: path.resolve(basedir), | ||
isFile: function (file) { | ||
return Object.prototype.hasOwnProperty.call(files, file); | ||
}, | ||
readFileSync : function (file) { | ||
return files[file] | ||
readFileSync: function (file) { | ||
return files[file]; | ||
} | ||
} | ||
}; | ||
} | ||
t.equal( | ||
resolve.sync('./baz', opts('/foo/bar')), | ||
'/foo/bar/baz.js' | ||
path.resolve('/foo/bar/baz.js') | ||
); | ||
t.equal( | ||
resolve.sync('./baz.js', opts('/foo/bar')), | ||
'/foo/bar/baz.js' | ||
path.resolve('/foo/bar/baz.js') | ||
); | ||
t.throws(function () { | ||
@@ -45,26 +45,25 @@ resolve.sync('baz', opts('/foo/bar')); | ||
t.plan(1); | ||
var files = { | ||
'/foo/node_modules/bar/baz.js' : 'beep', | ||
'/foo/node_modules/bar/package.json' : JSON.stringify({ | ||
main : './baz.js' | ||
}) | ||
}; | ||
function opts (basedir) { | ||
var files = {}; | ||
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep'; | ||
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({ | ||
main: './baz.js' | ||
}); | ||
function opts(basedir) { | ||
return { | ||
basedir : basedir, | ||
isFile : function (file) { | ||
return files.hasOwnProperty(file) | ||
basedir: path.resolve(basedir), | ||
isFile: function (file) { | ||
return Object.prototype.hasOwnProperty.call(files, file); | ||
}, | ||
readFileSync : function (file) { | ||
return files[file] | ||
readFileSync: function (file) { | ||
return files[file]; | ||
} | ||
} | ||
}; | ||
} | ||
t.equal( | ||
resolve.sync('bar', opts('/foo')), | ||
'/foo/node_modules/bar/baz.js' | ||
path.resolve('/foo/node_modules/bar/baz.js') | ||
); | ||
}); |
175
test/mock.js
@@ -0,1 +1,2 @@ | ||
var path = require('path'); | ||
var test = require('tape'); | ||
@@ -5,38 +6,39 @@ var resolve = require('../'); | ||
test('mock', function (t) { | ||
t.plan(6); | ||
var files = { | ||
'/foo/bar/baz.js' : 'beep' | ||
}; | ||
function opts (basedir) { | ||
t.plan(8); | ||
var files = {}; | ||
files[path.resolve('/foo/bar/baz.js')] = 'beep'; | ||
function opts(basedir) { | ||
return { | ||
basedir : basedir, | ||
isFile : function (file, cb) { | ||
cb(null, files.hasOwnProperty(file)); | ||
basedir: path.resolve(basedir), | ||
isFile: function (file, cb) { | ||
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file))); | ||
}, | ||
readFile : function (file, cb) { | ||
cb(null, files[file]); | ||
readFile: function (file, cb) { | ||
cb(null, files[path.resolve(file)]); | ||
} | ||
} | ||
}; | ||
} | ||
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/bar/baz.js'); | ||
if (err) return t.fail(err); | ||
t.equal(res, path.resolve('/foo/bar/baz.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/bar/baz.js'); | ||
if (err) return t.fail(err); | ||
t.equal(res, path.resolve('/foo/bar/baz.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('baz', opts('/foo/bar'), function (err, res) { | ||
t.equal(err.message, "Cannot find module 'baz' from '/foo/bar'"); | ||
t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "' with extension in [\".js\"]"); | ||
t.equal(err.code, 'MODULE_NOT_FOUND'); | ||
}); | ||
resolve('../baz', opts('/foo/bar'), function (err, res) { | ||
t.equal(err.message, "Cannot find module '../baz' from '/foo/bar'"); | ||
t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "' with extension in [\".js\"]"); | ||
t.equal(err.code, 'MODULE_NOT_FOUND'); | ||
}); | ||
@@ -46,39 +48,40 @@ }); | ||
test('mock from package', function (t) { | ||
t.plan(6); | ||
var files = { | ||
'/foo/bar/baz.js' : 'beep' | ||
}; | ||
function opts (basedir) { | ||
t.plan(8); | ||
var files = {}; | ||
files[path.resolve('/foo/bar/baz.js')] = 'beep'; | ||
function opts(basedir) { | ||
return { | ||
basedir : basedir, | ||
package : { main: 'bar' }, | ||
isFile : function (file, cb) { | ||
cb(null, files.hasOwnProperty(file)); | ||
basedir: path.resolve(basedir), | ||
isFile: function (file, cb) { | ||
cb(null, Object.prototype.hasOwnProperty.call(files, file)); | ||
}, | ||
readFile : function (file, cb) { | ||
'package': { main: 'bar' }, | ||
readFile: function (file, cb) { | ||
cb(null, files[file]); | ||
} | ||
} | ||
}; | ||
} | ||
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/bar/baz.js'); | ||
t.equal(pkg.main, 'bar'); | ||
if (err) return t.fail(err); | ||
t.equal(res, path.resolve('/foo/bar/baz.js')); | ||
t.equal(pkg && pkg.main, 'bar'); | ||
}); | ||
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/bar/baz.js'); | ||
t.equal(pkg.main, 'bar'); | ||
if (err) return t.fail(err); | ||
t.equal(res, path.resolve('/foo/bar/baz.js')); | ||
t.equal(pkg && pkg.main, 'bar'); | ||
}); | ||
resolve('baz', opts('/foo/bar'), function (err, res) { | ||
t.equal(err.message, "Cannot find module 'baz' from '/foo/bar'"); | ||
t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "' with extension in [\".js\"]"); | ||
t.equal(err.code, 'MODULE_NOT_FOUND'); | ||
}); | ||
resolve('../baz', opts('/foo/bar'), function (err, res) { | ||
t.equal(err.message, "Cannot find module '../baz' from '/foo/bar'"); | ||
t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "' with extension in [\".js\"]"); | ||
t.equal(err.code, 'MODULE_NOT_FOUND'); | ||
}); | ||
@@ -89,26 +92,25 @@ }); | ||
t.plan(2); | ||
var files = { | ||
'/foo/node_modules/bar/baz.js' : 'beep', | ||
'/foo/node_modules/bar/package.json' : JSON.stringify({ | ||
main : './baz.js' | ||
}) | ||
}; | ||
function opts (basedir) { | ||
var files = {}; | ||
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep'; | ||
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({ | ||
main: './baz.js' | ||
}); | ||
function opts(basedir) { | ||
return { | ||
basedir : basedir, | ||
isFile : function (file, cb) { | ||
cb(null, files.hasOwnProperty(file)); | ||
basedir: path.resolve(basedir), | ||
isFile: function (file, cb) { | ||
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file))); | ||
}, | ||
readFile : function (file, cb) { | ||
cb(null, files[file]); | ||
readFile: function (file, cb) { | ||
cb(null, files[path.resolve(file)]); | ||
} | ||
} | ||
}; | ||
} | ||
resolve('bar', opts('/foo'), function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/node_modules/bar/baz.js'); | ||
t.equal(pkg.main, './baz.js'); | ||
if (err) return t.fail(err); | ||
t.equal(res, path.resolve('/foo/node_modules/bar/baz.js')); | ||
t.equal(pkg && pkg.main, './baz.js'); | ||
}); | ||
@@ -119,28 +121,27 @@ }); | ||
t.plan(2); | ||
var files = { | ||
'/foo/node_modules/bar/baz.js' : 'beep', | ||
'/foo/node_modules/bar/package.json' : JSON.stringify({ | ||
main : './baz.js' | ||
}) | ||
}; | ||
function opts (basedir) { | ||
var files = {}; | ||
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep'; | ||
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({ | ||
main: './baz.js' | ||
}); | ||
function opts(basedir) { | ||
return { | ||
basedir : basedir, | ||
package : { main: 'bar' }, | ||
isFile : function (file, cb) { | ||
cb(null, files.hasOwnProperty(file)); | ||
basedir: path.resolve(basedir), | ||
isFile: function (file, cb) { | ||
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file))); | ||
}, | ||
readFile : function (file, cb) { | ||
cb(null, files[file]); | ||
'package': { main: 'bar' }, | ||
readFile: function (file, cb) { | ||
cb(null, files[path.resolve(file)]); | ||
} | ||
} | ||
}; | ||
} | ||
resolve('bar', opts('/foo'), function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/node_modules/bar/baz.js'); | ||
t.equal(pkg.main, './baz.js'); | ||
if (err) return t.fail(err); | ||
t.equal(res, path.resolve('/foo/node_modules/bar/baz.js')); | ||
t.equal(pkg && pkg.main, './baz.js'); | ||
}); | ||
}); |
@@ -7,5 +7,5 @@ var path = require('path'); | ||
t.plan(4); | ||
var dir = __dirname + '/module_dir'; | ||
var dir = path.join(__dirname, 'module_dir'); | ||
var xopts = { | ||
basedir : dir, | ||
basedir: dir, | ||
moduleDirectory: 'xmodules' | ||
@@ -15,7 +15,7 @@ }; | ||
t.ifError(err); | ||
t.equal(res, dir + '/xmodules/aaa/index.js'); | ||
t.equal(res, path.join(dir, '/xmodules/aaa/index.js')); | ||
}); | ||
var yopts = { | ||
basedir : dir, | ||
basedir: dir, | ||
moduleDirectory: 'ymodules' | ||
@@ -25,3 +25,3 @@ }; | ||
t.ifError(err); | ||
t.equal(res, dir + '/ymodules/aaa/index.js'); | ||
t.equal(res, path.join(dir, '/ymodules/aaa/index.js')); | ||
}); | ||
@@ -32,29 +32,29 @@ }); | ||
t.plan(6); | ||
var dir = __dirname + '/module_dir'; | ||
var dir = path.join(__dirname, 'module_dir'); | ||
var aopts = { | ||
basedir : dir, | ||
moduleDirectory: [ 'xmodules', 'ymodules', 'zmodules' ] | ||
basedir: dir, | ||
moduleDirectory: ['xmodules', 'ymodules', 'zmodules'] | ||
}; | ||
resolve('aaa', aopts, function (err, res, pkg) { | ||
t.ifError(err); | ||
t.equal(res, dir + '/xmodules/aaa/index.js'); | ||
t.equal(res, path.join(dir, '/xmodules/aaa/index.js')); | ||
}); | ||
var bopts = { | ||
basedir : dir, | ||
moduleDirectory: [ 'zmodules', 'ymodules', 'xmodules' ] | ||
basedir: dir, | ||
moduleDirectory: ['zmodules', 'ymodules', 'xmodules'] | ||
}; | ||
resolve('aaa', bopts, function (err, res, pkg) { | ||
t.ifError(err); | ||
t.equal(res, dir + '/ymodules/aaa/index.js'); | ||
t.equal(res, path.join(dir, '/ymodules/aaa/index.js')); | ||
}); | ||
var copts = { | ||
basedir : dir, | ||
moduleDirectory: [ 'xmodules', 'ymodules', 'zmodules' ] | ||
basedir: dir, | ||
moduleDirectory: ['xmodules', 'ymodules', 'zmodules'] | ||
}; | ||
resolve('bbb', copts, function (err, res, pkg) { | ||
t.ifError(err); | ||
t.equal(res, dir + '/zmodules/bbb/main.js'); | ||
t.equal(res, path.join(dir, '/zmodules/bbb/main.js')); | ||
}); | ||
}); |
@@ -1,1 +0,1 @@ | ||
module.exports = function (x) { return x * 100 } | ||
module.exports = function (x) { return x * 100; }; |
@@ -1,1 +0,1 @@ | ||
module.exports = function (x) { return x + 100 } | ||
module.exports = function (x) { return x + 100; }; |
@@ -1,1 +0,1 @@ | ||
module.exports = function (n) { return n * 111 } | ||
module.exports = function (n) { return n * 111; }; |
@@ -7,31 +7,31 @@ var path = require('path'); | ||
t.plan(4); | ||
resolve('aaa', { | ||
paths: [ | ||
__dirname + '/node_path/x', | ||
__dirname + '/node_path/y' | ||
path.join(__dirname, '/node_path/x'), | ||
path.join(__dirname, '/node_path/y') | ||
], | ||
basedir: __dirname, | ||
basedir: __dirname | ||
}, function (err, res) { | ||
t.equal(res, __dirname + '/node_path/x/aaa/index.js'); | ||
t.equal(res, path.join(__dirname, '/node_path/x/aaa/index.js')); | ||
}); | ||
resolve('bbb', { | ||
paths: [ | ||
__dirname + '/node_path/x', | ||
__dirname + '/node_path/y' | ||
path.join(__dirname, '/node_path/x'), | ||
path.join(__dirname, '/node_path/y') | ||
], | ||
basedir: __dirname, | ||
basedir: __dirname | ||
}, function (err, res) { | ||
t.equal(res, __dirname + '/node_path/y/bbb/index.js'); | ||
t.equal(res, path.join(__dirname, '/node_path/y/bbb/index.js')); | ||
}); | ||
resolve('ccc', { | ||
paths: [ | ||
__dirname + '/node_path/x', | ||
__dirname + '/node_path/y' | ||
path.join(__dirname, '/node_path/x'), | ||
path.join(__dirname, '/node_path/y') | ||
], | ||
basedir: __dirname, | ||
basedir: __dirname | ||
}, function (err, res) { | ||
t.equal(res, __dirname + '/node_path/x/ccc/index.js'); | ||
t.equal(res, path.join(__dirname, '/node_path/x/ccc/index.js')); | ||
}); | ||
@@ -43,8 +43,9 @@ | ||
paths: [ | ||
'node_path', | ||
'node_path' | ||
], | ||
basedir: 'node_path/x', | ||
basedir: 'node_path/x' | ||
}, function (err, res) { | ||
t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js')); | ||
var root = require('tap/package.json').main; | ||
t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap', root)); | ||
}); | ||
}); |
@@ -1,1 +0,1 @@ | ||
module.exports = 'A' | ||
module.exports = 'A'; |
@@ -1,1 +0,1 @@ | ||
module.exports = 'C' | ||
module.exports = 'C'; |
@@ -1,1 +0,1 @@ | ||
module.exports = 'B' | ||
module.exports = 'B'; |
@@ -1,1 +0,1 @@ | ||
module.exports = 'CY' | ||
module.exports = 'CY'; |
@@ -0,35 +1,42 @@ | ||
var path = require('path'); | ||
var test = require('tape'); | ||
var resolve = require('../'); | ||
test('#62: deep module references and the pathFilter', function(t){ | ||
test('#62: deep module references and the pathFilter', function (t) { | ||
t.plan(9); | ||
var resolverDir = __dirname + '/pathfilter/deep_ref'; | ||
var pathFilter = function(pkg, x, remainder){ | ||
t.equal(pkg.version, "1.2.3"); | ||
t.equal(x, resolverDir + '/node_modules/deep/ref'); | ||
t.equal(remainder, "ref"); | ||
return "alt"; | ||
var resolverDir = path.join(__dirname, '/pathfilter/deep_ref'); | ||
var pathFilter = function (pkg, x, remainder) { | ||
t.equal(pkg.version, '1.2.3'); | ||
t.equal(x, path.join(resolverDir, 'node_modules/deep/ref')); | ||
t.equal(remainder, 'ref'); | ||
return 'alt'; | ||
}; | ||
resolve('deep/ref', { basedir : resolverDir }, function (err, res, pkg) { | ||
resolve('deep/ref', { basedir: resolverDir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(pkg.version, "1.2.3"); | ||
t.equal(res, resolverDir + '/node_modules/deep/ref.js'); | ||
t.equal(pkg.version, '1.2.3'); | ||
t.equal(res, path.join(resolverDir, 'node_modules/deep/ref.js')); | ||
}); | ||
resolve('deep/deeper/ref', { basedir: resolverDir }, | ||
function(err, res, pkg) { | ||
if(err) t.fail(err); | ||
t.notEqual(pkg, undefined); | ||
t.equal(pkg.version, "1.2.3"); | ||
t.equal(res, resolverDir + '/node_modules/deep/deeper/ref.js'); | ||
}); | ||
resolve('deep/ref', { basedir : resolverDir, pathFilter : pathFilter }, | ||
function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, resolverDir + '/node_modules/deep/alt.js'); | ||
}); | ||
resolve( | ||
'deep/deeper/ref', | ||
{ basedir: resolverDir }, | ||
function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.notEqual(pkg, undefined); | ||
t.equal(pkg.version, '1.2.3'); | ||
t.equal(res, path.join(resolverDir, 'node_modules/deep/deeper/ref.js')); | ||
} | ||
); | ||
resolve( | ||
'deep/ref', | ||
{ basedir: resolverDir, pathFilter: pathFilter }, | ||
function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js')); | ||
} | ||
); | ||
}); |
@@ -8,4 +8,4 @@ var path = require('path'); | ||
var dir = path.join(__dirname, 'precedence/aaa'); | ||
resolve('./', { basedir : dir }, function (err, res, pkg) { | ||
resolve('./', { basedir: dir }, function (err, res, pkg) { | ||
t.ifError(err); | ||
@@ -20,6 +20,6 @@ t.equal(res, path.join(dir, 'index.js')); | ||
var dir = path.join(__dirname, 'precedence/bbb'); | ||
resolve('./', { basedir : dir }, function (err, res, pkg) { | ||
resolve('./', { basedir: dir }, function (err, res, pkg) { | ||
t.ok(err); | ||
}); | ||
}); |
@@ -1,1 +0,1 @@ | ||
module.exports = 'wtf' | ||
module.exports = 'wtf'; |
@@ -1,1 +0,1 @@ | ||
module.exports = 'okok' | ||
module.exports = 'okok'; |
@@ -1,1 +0,1 @@ | ||
console.log(require('./')) | ||
console.log(require('./')); |
@@ -1,1 +0,1 @@ | ||
module.exports '>_<' | ||
module.exports = '>_<'; |
@@ -0,1 +1,2 @@ | ||
var path = require('path'); | ||
var test = require('tape'); | ||
@@ -5,18 +6,18 @@ var resolve = require('../'); | ||
test('foo', function (t) { | ||
var dir = __dirname + '/resolver'; | ||
var dir = path.join(__dirname, 'resolver'); | ||
t.equal( | ||
resolve.sync('./foo', { basedir : dir }), | ||
dir + '/foo.js' | ||
resolve.sync('./foo', { basedir: dir }), | ||
path.join(dir, 'foo.js') | ||
); | ||
t.equal( | ||
resolve.sync('./foo.js', { basedir : dir }), | ||
dir + '/foo.js' | ||
resolve.sync('./foo.js', { basedir: dir }), | ||
path.join(dir, 'foo.js') | ||
); | ||
t.throws(function () { | ||
resolve.sync('foo', { basedir : dir }); | ||
resolve.sync('foo', { basedir: dir }); | ||
}); | ||
t.end(); | ||
@@ -26,7 +27,7 @@ }); | ||
test('bar', function (t) { | ||
var dir = __dirname + '/resolver'; | ||
var dir = path.join(__dirname, 'resolver'); | ||
t.equal( | ||
resolve.sync('foo', { basedir : dir + '/bar' }), | ||
dir + '/bar/node_modules/foo/index.js' | ||
resolve.sync('foo', { basedir: path.join(dir, 'bar') }), | ||
path.join(dir, 'bar/node_modules/foo/index.js') | ||
); | ||
@@ -37,7 +38,7 @@ t.end(); | ||
test('baz', function (t) { | ||
var dir = __dirname + '/resolver'; | ||
var dir = path.join(__dirname, 'resolver'); | ||
t.equal( | ||
resolve.sync('./baz', { basedir : dir }), | ||
dir + '/baz/quux.js' | ||
resolve.sync('./baz', { basedir: dir }), | ||
path.join(dir, 'baz/quux.js') | ||
); | ||
@@ -48,16 +49,16 @@ t.end(); | ||
test('biz', function (t) { | ||
var dir = __dirname + '/resolver/biz/node_modules'; | ||
var dir = path.join(__dirname, 'resolver/biz/node_modules'); | ||
t.equal( | ||
resolve.sync('./grux', { basedir : dir }), | ||
dir + '/grux/index.js' | ||
resolve.sync('./grux', { basedir: dir }), | ||
path.join(dir, 'grux/index.js') | ||
); | ||
t.equal( | ||
resolve.sync('tiv', { basedir : dir + '/grux' }), | ||
dir + '/tiv/index.js' | ||
resolve.sync('tiv', { basedir: path.join(dir, 'grux') }), | ||
path.join(dir, 'tiv/index.js') | ||
); | ||
t.equal( | ||
resolve.sync('grux', { basedir : dir + '/tiv' }), | ||
dir + '/grux/index.js' | ||
resolve.sync('grux', { basedir: path.join(dir, 'tiv') }), | ||
path.join(dir, 'grux/index.js') | ||
); | ||
@@ -68,6 +69,6 @@ t.end(); | ||
test('normalize', function (t) { | ||
var dir = __dirname + '/resolver/biz/node_modules/grux'; | ||
var dir = path.join(__dirname, 'resolver/biz/node_modules/grux'); | ||
t.equal( | ||
resolve.sync('../grux', { basedir : dir }), | ||
dir + '/index.js' | ||
resolve.sync('../grux', { basedir: dir }), | ||
path.join(dir, 'index.js') | ||
); | ||
@@ -78,25 +79,23 @@ t.end(); | ||
test('cup', function (t) { | ||
var dir = __dirname + '/resolver'; | ||
var dir = path.join(__dirname, 'resolver'); | ||
t.equal( | ||
resolve.sync('./cup', { | ||
basedir : dir, | ||
extensions : [ '.js', '.coffee' ] | ||
basedir: dir, | ||
extensions: ['.js', '.coffee'] | ||
}), | ||
dir + '/cup.coffee' | ||
path.join(dir, 'cup.coffee') | ||
); | ||
t.equal( | ||
resolve.sync('./cup.coffee', { | ||
basedir : dir | ||
}), | ||
dir + '/cup.coffee' | ||
resolve.sync('./cup.coffee', { basedir: dir }), | ||
path.join(dir, 'cup.coffee') | ||
); | ||
t.throws(function () { | ||
resolve.sync('./cup', { | ||
basedir : dir, | ||
extensions : [ '.js' ] | ||
}) | ||
basedir: dir, | ||
extensions: ['.js'] | ||
}); | ||
}); | ||
t.end(); | ||
@@ -106,24 +105,24 @@ }); | ||
test('mug', function (t) { | ||
var dir = __dirname + '/resolver'; | ||
var dir = path.join(__dirname, 'resolver'); | ||
t.equal( | ||
resolve.sync('./mug', { basedir : dir }), | ||
dir + '/mug.js' | ||
resolve.sync('./mug', { basedir: dir }), | ||
path.join(dir, 'mug.js') | ||
); | ||
t.equal( | ||
resolve.sync('./mug', { | ||
basedir : dir, | ||
extensions : [ '.coffee', '.js' ] | ||
basedir: dir, | ||
extensions: ['.coffee', '.js'] | ||
}), | ||
dir + '/mug.coffee' | ||
path.join(dir, 'mug.coffee') | ||
); | ||
t.equal( | ||
resolve.sync('./mug', { | ||
basedir : dir, | ||
extensions : [ '.js', '.coffee' ] | ||
basedir: dir, | ||
extensions: ['.js', '.coffee'] | ||
}), | ||
dir + '/mug.js' | ||
path.join(dir, 'mug.js') | ||
); | ||
t.end(); | ||
@@ -133,32 +132,33 @@ }); | ||
test('other path', function (t) { | ||
var resolverDir = __dirname + '/resolver'; | ||
var dir = resolverDir + '/bar'; | ||
var otherDir = resolverDir + '/other_path'; | ||
var resolverDir = path.join(__dirname, 'resolver'); | ||
var dir = path.join(resolverDir, 'bar'); | ||
var otherDir = path.join(resolverDir, 'other_path'); | ||
var path = require('path'); | ||
t.equal( | ||
resolve.sync('root', { | ||
basedir : dir, | ||
paths: [otherDir] }), | ||
resolverDir + '/other_path/root.js' | ||
basedir: dir, | ||
paths: [otherDir] | ||
}), | ||
path.join(resolverDir, 'other_path/root.js') | ||
); | ||
t.equal( | ||
resolve.sync('lib/other-lib', { | ||
basedir : dir, | ||
paths: [otherDir] }), | ||
resolverDir + '/other_path/lib/other-lib.js' | ||
basedir: dir, | ||
paths: [otherDir] | ||
}), | ||
path.join(resolverDir, 'other_path/lib/other-lib.js') | ||
); | ||
t.throws(function () { | ||
resolve.sync('root', { basedir : dir, }); | ||
resolve.sync('root', { basedir: dir }); | ||
}); | ||
t.throws(function () { | ||
resolve.sync('zzz', { | ||
basedir : dir, | ||
paths: [otherDir] }); | ||
basedir: dir, | ||
paths: [otherDir] | ||
}); | ||
}); | ||
t.end(); | ||
@@ -168,48 +168,48 @@ }); | ||
test('incorrect main', function (t) { | ||
var resolverDir = __dirname + '/resolver'; | ||
var dir = resolverDir + '/incorrect_main'; | ||
var resolverDir = path.join(__dirname, 'resolver'); | ||
var dir = path.join(resolverDir, 'incorrect_main'); | ||
t.equal( | ||
resolve.sync('./incorrect_main', { basedir : resolverDir }), | ||
dir + '/index.js' | ||
) | ||
resolve.sync('./incorrect_main', { basedir: resolverDir }), | ||
path.join(dir, 'index.js') | ||
); | ||
t.end() | ||
t.end(); | ||
}); | ||
test('#25: node modules with the same name as node stdlib modules', function (t) { | ||
var resolverDir = __dirname + '/resolver/punycode'; | ||
var resolverDir = path.join(__dirname, 'resolver/punycode'); | ||
t.equal( | ||
resolve.sync('punycode', { basedir : resolverDir }), | ||
resolverDir + '/node_modules/punycode/index.js' | ||
) | ||
resolve.sync('punycode', { basedir: resolverDir }), | ||
path.join(resolverDir, 'node_modules/punycode/index.js') | ||
); | ||
t.end() | ||
t.end(); | ||
}); | ||
function stubStatSync(fn) { | ||
var fs = require('fs') | ||
var stubStatSync = function stubStatSync(fn) { | ||
var fs = require('fs'); | ||
var statSync = fs.statSync; | ||
try { | ||
fs.statSync = function () { | ||
throw new EvalError('Unknown Error'); | ||
throw new EvalError('Unknown Error'); | ||
}; | ||
return fn(); | ||
} finally { | ||
fs.statSync = statSync; | ||
fs.statSync = statSync; | ||
} | ||
} | ||
}; | ||
test('#79 - re-throw non ENOENT errors from stat', function (t) { | ||
var dir = __dirname + '/resolver'; | ||
var dir = path.join(__dirname, 'resolver'); | ||
stubStatSync(function () { | ||
t.throws(function () { | ||
resolve.sync('foo', { basedir : dir }); | ||
}, /Unknown Error/); | ||
}) | ||
t.throws(function () { | ||
resolve.sync('foo', { basedir: dir }); | ||
}, /Unknown Error/); | ||
}); | ||
t.end() | ||
t.end(); | ||
}); | ||
@@ -6,31 +6,32 @@ var path = require('path'); | ||
test('async foo', function (t) { | ||
t.plan(9); | ||
var dir = __dirname + '/resolver'; | ||
resolve('./foo', { basedir : dir }, function (err, res, pkg) { | ||
t.plan(10); | ||
var dir = path.join(__dirname, 'resolver'); | ||
resolve('./foo', { basedir: dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo.js'); | ||
t.equal(res, path.join(dir, 'foo.js')); | ||
t.equal(pkg.name, 'resolve'); | ||
}); | ||
resolve('./foo.js', { basedir : dir }, function (err, res, pkg) { | ||
resolve('./foo.js', { basedir: dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo.js'); | ||
t.equal(res, path.join(dir, 'foo.js')); | ||
t.equal(pkg.name, 'resolve'); | ||
}); | ||
resolve('./foo', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) { | ||
resolve('./foo', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo.js'); | ||
t.equal(res, path.join(dir, 'foo.js')); | ||
t.equal(pkg.main, 'resolver'); | ||
}); | ||
resolve('./foo.js', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) { | ||
resolve('./foo.js', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo.js'); | ||
t.equal(res, path.join(dir, 'foo.js')); | ||
t.equal(pkg.main, 'resolver'); | ||
}); | ||
resolve('foo', { basedir : dir }, function (err) { | ||
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'"); | ||
resolve('foo', { basedir: dir }, function (err) { | ||
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "' with extension in [\".js\"]"); | ||
t.equal(err.code, 'MODULE_NOT_FOUND'); | ||
}); | ||
@@ -41,19 +42,19 @@ }); | ||
t.plan(6); | ||
var dir = __dirname + '/resolver'; | ||
resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) { | ||
var dir = path.join(__dirname, 'resolver'); | ||
resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/bar/node_modules/foo/index.js'); | ||
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) { | ||
resolve('foo', { basedir: dir + '/bar' }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/bar/node_modules/foo/index.js'); | ||
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('foo', { basedir : dir + '/bar', package: { main: 'bar' } }, function (err, res, pkg) { | ||
resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/bar/node_modules/foo/index.js'); | ||
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js')); | ||
t.equal(pkg, undefined); | ||
@@ -65,13 +66,13 @@ }); | ||
t.plan(4); | ||
var dir = __dirname + '/resolver'; | ||
resolve('./baz', { basedir : dir }, function (err, res, pkg) { | ||
var dir = path.join(__dirname, 'resolver'); | ||
resolve('./baz', { basedir: dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/baz/quux.js'); | ||
t.equal(res, path.join(dir, 'baz/quux.js')); | ||
t.equal(pkg.main, 'quux.js'); | ||
}); | ||
resolve('./baz', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) { | ||
resolve('./baz', { basedir: dir, 'package': { main: 'resolver' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/baz/quux.js'); | ||
t.equal(res, path.join(dir, 'baz/quux.js')); | ||
t.equal(pkg.main, 'quux.js'); | ||
@@ -83,73 +84,73 @@ }); | ||
t.plan(24); | ||
var dir = __dirname + '/resolver/biz/node_modules'; | ||
resolve('./grux', { basedir : dir }, function (err, res, pkg) { | ||
var dir = path.join(__dirname, 'resolver/biz/node_modules'); | ||
resolve('./grux', { basedir: dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/grux/index.js'); | ||
t.equal(res, path.join(dir, 'grux/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('./grux', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) { | ||
resolve('./grux', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/grux/index.js'); | ||
t.equal(res, path.join(dir, 'grux/index.js')); | ||
t.equal(pkg.main, 'biz'); | ||
}); | ||
resolve('./garply', { basedir : dir }, function (err, res, pkg) { | ||
resolve('./garply', { basedir: dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/garply/lib/index.js'); | ||
t.equal(res, path.join(dir, 'garply/lib/index.js')); | ||
t.equal(pkg.main, './lib'); | ||
}); | ||
resolve('./garply', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) { | ||
resolve('./garply', { basedir: dir, 'package': { main: 'biz' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/garply/lib/index.js'); | ||
t.equal(res, path.join(dir, 'garply/lib/index.js')); | ||
t.equal(pkg.main, './lib'); | ||
}); | ||
resolve('tiv', { basedir : dir + '/grux' }, function (err, res, pkg) { | ||
resolve('tiv', { basedir: dir + '/grux' }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/tiv/index.js'); | ||
t.equal(res, path.join(dir, 'tiv/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('tiv', { basedir : dir + '/grux', package: { main: 'grux' } }, function (err, res, pkg) { | ||
resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/tiv/index.js'); | ||
t.equal(res, path.join(dir, 'tiv/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('tiv', { basedir : dir + '/garply' }, function (err, res, pkg) { | ||
resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/tiv/index.js'); | ||
t.equal(res, path.join(dir, 'tiv/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('tiv', { basedir : dir + '/garply', package: { main: './lib' } }, function (err, res, pkg) { | ||
resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/tiv/index.js'); | ||
t.equal(res, path.join(dir, 'tiv/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('grux', { basedir : dir + '/tiv' }, function (err, res, pkg) { | ||
resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/grux/index.js'); | ||
t.equal(res, path.join(dir, 'grux/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('grux', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) { | ||
resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/grux/index.js'); | ||
t.equal(res, path.join(dir, 'grux/index.js')); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('garply', { basedir : dir + '/tiv' }, function (err, res, pkg) { | ||
resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/garply/lib/index.js'); | ||
t.equal(res, path.join(dir, 'garply/lib/index.js')); | ||
t.equal(pkg.main, './lib'); | ||
}); | ||
resolve('garply', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) { | ||
resolve('garply', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/garply/lib/index.js'); | ||
t.equal(res, path.join(dir, 'garply/lib/index.js')); | ||
t.equal(pkg.main, './lib'); | ||
@@ -161,7 +162,7 @@ }); | ||
t.plan(2); | ||
var dir = __dirname + '/resolver/quux'; | ||
resolve('./foo', { basedir : dir, package: { main: 'quux' } }, function (err, res, pkg) { | ||
var dir = path.join(__dirname, 'resolver/quux'); | ||
resolve('./foo', { basedir: dir, 'package': { main: 'quux' } }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo/index.js'); | ||
t.equal(res, path.join(dir, 'foo/index.js')); | ||
t.equal(pkg.main, 'quux'); | ||
@@ -173,7 +174,7 @@ }); | ||
t.plan(2); | ||
var dir = __dirname + '/resolver/biz/node_modules/grux'; | ||
resolve('../grux', { basedir : dir }, function (err, res, pkg) { | ||
var dir = path.join(__dirname, 'resolver/biz/node_modules/grux'); | ||
resolve('../grux', { basedir: dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/index.js'); | ||
t.equal(res, path.join(dir, 'index.js')); | ||
t.equal(pkg, undefined); | ||
@@ -184,19 +185,18 @@ }); | ||
test('cup', function (t) { | ||
t.plan(3); | ||
var dir = __dirname + '/resolver'; | ||
resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] }, | ||
function (err, res) { | ||
t.plan(4); | ||
var dir = path.join(__dirname, 'resolver'); | ||
resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/cup.coffee'); | ||
t.equal(res, path.join(dir, 'cup.coffee')); | ||
}); | ||
resolve('./cup.coffee', { basedir : dir }, function (err, res) { | ||
resolve('./cup.coffee', { basedir: dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/cup.coffee'); | ||
t.equal(res, path.join(dir, 'cup.coffee')); | ||
}); | ||
resolve('./cup', { basedir : dir, extensions : [ '.js' ] }, | ||
function (err, res) { | ||
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'"); | ||
resolve('./cup', { basedir: dir, extensions: ['.js'] }, function (err, res) { | ||
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "' with extension in [\".js\"]"); | ||
t.equal(err.code, 'MODULE_NOT_FOUND'); | ||
}); | ||
@@ -207,18 +207,16 @@ }); | ||
t.plan(3); | ||
var dir = __dirname + '/resolver'; | ||
resolve('./mug', { basedir : dir }, function (err, res) { | ||
var dir = path.join(__dirname, 'resolver'); | ||
resolve('./mug', { basedir: dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/mug.js'); | ||
t.equal(res, path.join(dir, 'mug.js')); | ||
}); | ||
resolve('./mug', { basedir : dir, extensions : [ '.coffee', '.js' ] }, | ||
function (err, res) { | ||
resolve('./mug', { basedir: dir, extensions: ['.coffee', '.js'] }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/mug.coffee'); | ||
t.equal(res, path.join(dir, '/mug.coffee')); | ||
}); | ||
resolve('./mug', { basedir : dir, extensions : [ '.js', '.coffee' ] }, | ||
function (err, res) { | ||
t.equal(res, dir + '/mug.js'); | ||
resolve('./mug', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) { | ||
t.equal(res, path.join(dir, '/mug.js')); | ||
}); | ||
@@ -228,24 +226,25 @@ }); | ||
test('other path', function (t) { | ||
t.plan(4); | ||
var resolverDir = __dirname + '/resolver'; | ||
var dir = resolverDir + '/bar'; | ||
var otherDir = resolverDir + '/other_path'; | ||
resolve('root', { basedir : dir, paths: [otherDir] }, function (err, res) { | ||
t.plan(6); | ||
var resolverDir = path.join(__dirname, 'resolver'); | ||
var dir = path.join(resolverDir, 'bar'); | ||
var otherDir = path.join(resolverDir, 'other_path'); | ||
resolve('root', { basedir: dir, paths: [otherDir] }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, resolverDir + '/other_path/root.js'); | ||
t.equal(res, path.join(resolverDir, 'other_path/root.js')); | ||
}); | ||
resolve('lib/other-lib', { basedir : dir, paths: [otherDir] }, | ||
function (err, res) { | ||
resolve('lib/other-lib', { basedir: dir, paths: [otherDir] }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, resolverDir + '/other_path/lib/other-lib.js'); | ||
t.equal(res, path.join(resolverDir, 'other_path/lib/other-lib.js')); | ||
}); | ||
resolve('root', { basedir : dir, }, function (err, res) { | ||
t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'"); | ||
resolve('root', { basedir: dir }, function (err, res) { | ||
t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "' with extension in [\".js\"]"); | ||
t.equal(err.code, 'MODULE_NOT_FOUND'); | ||
}); | ||
resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) { | ||
t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'"); | ||
resolve('zzz', { basedir: dir, paths: [otherDir] }, function (err, res) { | ||
t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "' with extension in [\".js\"]"); | ||
t.equal(err.code, 'MODULE_NOT_FOUND'); | ||
}); | ||
@@ -255,10 +254,10 @@ }); | ||
test('incorrect main', function (t) { | ||
t.plan(1) | ||
t.plan(1); | ||
var resolverDir = __dirname + '/resolver'; | ||
var dir = resolverDir + '/incorrect_main'; | ||
var resolverDir = path.join(__dirname, 'resolver'); | ||
var dir = path.join(resolverDir, 'incorrect_main'); | ||
resolve('./incorrect_main', { basedir : resolverDir }, function (err, res, pkg) { | ||
resolve('./incorrect_main', { basedir: resolverDir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/index.js'); | ||
t.equal(res, path.join(dir, 'index.js')); | ||
}); | ||
@@ -270,10 +269,10 @@ }); | ||
var dir = __dirname + '/resolver/without_basedir'; | ||
var tester = require(dir + '/main.js'); | ||
var dir = path.join(__dirname, 'resolver/without_basedir'); | ||
var tester = require(path.join(dir, 'main.js')); | ||
tester(t, function (err, res, pkg){ | ||
tester(t, function (err, res, pkg) { | ||
if (err) { | ||
t.fail(err); | ||
} else { | ||
t.equal(res, dir + '/node_modules/mymodule.js'); | ||
t.equal(res, path.join(dir, 'node_modules/mymodule.js')); | ||
} | ||
@@ -286,8 +285,8 @@ }); | ||
var resolverDir = __dirname + '/resolver/punycode'; | ||
var resolverDir = path.join(__dirname, 'resolver/punycode'); | ||
resolve('punycode', { basedir : resolverDir }, function (err, res, pkg) { | ||
resolve('punycode', { basedir: resolverDir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, resolverDir + '/node_modules/punycode/index.js'); | ||
t.equal(res, path.join(resolverDir, 'node_modules/punycode/index.js')); | ||
}); | ||
}); |
{ | ||
"main" : "quux.js" | ||
"main": "quux.js" | ||
} |
{ | ||
"main" : "wrong.js" | ||
"main": "wrong.js" | ||
} |
@@ -1,6 +0,5 @@ | ||
resolve = require('../../../'); | ||
var resolve = require('../../../'); | ||
module.exports = function(t, cb) { | ||
resolve('mymodule', null, cb); | ||
} | ||
module.exports = function (t, cb) { | ||
resolve('mymodule', null, cb); | ||
}; |
@@ -7,3 +7,3 @@ var test = require('tape'); | ||
t.plan(2); | ||
var dir = path.join(__dirname, '/subdirs'); | ||
@@ -10,0 +10,0 @@ resolve('a/b/c/x.json', { basedir: dir }, function (err, res) { |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
56715
63
1290
0
1
4
+ Addedpath-parse@^1.0.5
+ Addedpath-parse@1.0.7(transitive)