[Pkg-javascript-commits] [node-resolve] 03/06: Imported Upstream version 1.1.7

Thorsten Alteholz alteholz at moszumanska.debian.org
Fri Jun 17 17:57:32 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-resolve.

commit dd7fd14543ae913672b2d5153f9e68aa860559d2
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Fri Jun 17 19:42:45 2016 +0200

    Imported Upstream version 1.1.7
---
 lib/async.js                                       | 183 ++++++++++++++-------
 lib/caller.js                                      |   8 +
 lib/node-modules-paths.js                          |  38 +++++
 lib/sync.js                                        |  40 ++---
 package.json                                       |  53 +++---
 readme.markdown                                    |  32 +++-
 test/core.js                                       |   2 +-
 test/dotdot.js                                     |  29 ++++
 test/dotdot/abc/index.js                           |   2 +
 test/dotdot/index.js                               |   1 +
 test/faulty_basedir.js                             |  17 ++
 test/filter.js                                     |   7 +-
 test/filter_sync.js                                |   2 +-
 test/mock.js                                       |  90 +++++++++-
 test/mock_sync.js                                  |   2 +-
 test/module_dir.js                                 |  56 +++++++
 test/module_dir/xmodules/aaa/index.js              |   1 +
 test/module_dir/ymodules/aaa/index.js              |   1 +
 test/module_dir/zmodules/bbb/main.js               |   1 +
 test/module_dir/zmodules/bbb/package.json          |   3 +
 test/node_path.js                                  |  48 ++++++
 test/node_path/x/aaa/index.js                      |   1 +
 test/node_path/x/ccc/index.js                      |   1 +
 test/node_path/y/bbb/index.js                      |   1 +
 test/node_path/y/ccc/index.js                      |   1 +
 test/nonstring.js                                  |   9 +
 test/pathfilter.js                                 |  35 ++++
 test/pathfilter/deep_ref/main.js                   |   0
 test/pathfilter/deep_ref/node_modules/deep/alt.js  |   0
 .../deep_ref/node_modules/deep/deeper/ref.js       |   0
 .../deep_ref/node_modules/deep/package.json        |   4 +
 test/pathfilter/deep_ref/node_modules/deep/ref.js  |   0
 test/precedence.js                                 |  23 +++
 test/precedence/aaa.js                             |   1 +
 test/precedence/aaa/index.js                       |   1 +
 test/precedence/aaa/main.js                        |   1 +
 test/precedence/bbb.js                             |   1 +
 test/precedence/bbb/main.js                        |   1 +
 test/resolver.js                                   | 175 +++++++++++++++++---
 test/resolver/biz/node_modules/garply/lib/index.js |   1 +
 test/resolver/biz/node_modules/garply/package.json |   3 +
 test/resolver/incorrect_main/index.js              |   2 +
 test/resolver/incorrect_main/package.json          |   3 +
 .../punycode/node_modules/punycode/index.js        |   0
 test/resolver/quux/foo/index.js                    |   1 +
 test/resolver/without_basedir/main.js              |   6 +
 .../without_basedir/node_modules/mymodule.js       |   1 +
 test/resolver_sync.js                              |  25 ++-
 test/subdirs.js                                    |  13 ++
 test/subdirs/node_modules/a/b/c/x.json             |   1 +
 test/subdirs/node_modules/a/package.json           |   1 +
 51 files changed, 769 insertions(+), 159 deletions(-)

diff --git a/lib/async.js b/lib/async.js
index 7c5a01a..0f0eeca 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -1,15 +1,21 @@
 var core = require('./core');
 var fs = require('fs');
 var path = require('path');
+var caller = require('./caller.js');
+var nodeModulesPaths = require('./node-modules-paths.js');
+var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
 
 module.exports = function resolve (x, opts, cb) {
-    if (core[x]) return cb(null, x);
-    
     if (typeof opts === 'function') {
         cb = opts;
         opts = {};
     }
     if (!opts) opts = {};
+    if (typeof x !== 'string') {
+        return process.nextTick(function () {
+            cb(new Error('path must be a string'));
+        });
+    }
     
     var isFile = opts.isFile || function (file, cb) {
         fs.stat(file, function (err, stat) {
@@ -21,47 +27,111 @@ module.exports = function resolve (x, opts, cb) {
     var readFile = opts.readFile || fs.readFile;
     
     var extensions = opts.extensions || [ '.js' ];
-    var y = opts.basedir
-        || path.dirname(require.cache[__filename].parent.filename)
-    ;
+    var y = opts.basedir || path.dirname(caller());
     
     opts.paths = opts.paths || [];
     
-    if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
-        loadAsFile(path.resolve(y, x), function (err, m) {
-            if (err) cb(err)
-            else if (m) cb(null, m)
-            else loadAsDirectory(path.resolve(y, x), function (err, d) {
-                if (err) cb(err)
-                else if (d) cb(null, d)
-                else cb(new Error("Cannot find module '" + x + "'"))
-            })
-        });
+    if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {
+        var res = path.resolve(y, x);
+        if (x === '..') res += '/';
+        if (/\/$/.test(x) && res === y) {
+            loadAsDirectory(res, opts.package, onfile);
+        }
+        else loadAsFile(res, opts.package, onfile);
     }
-    else loadNodeModules(x, y, function (err, n) {
+    else loadNodeModules(x, y, function (err, n, pkg) {
         if (err) cb(err)
-        else if (n) cb(null, n)
-        else cb(new Error("Cannot find module '" + x + "'"))
+        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 loadAsFile (x, cb) {
-        (function load (exts) {
-            if (exts.length === 0) return cb(null, undefined);
+    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 + "'"))
+        })
+    }
+    
+    function loadAsFile (x, pkg, cb) {
+        if (typeof pkg === 'function') {
+            cb = pkg;
+            pkg = undefined;
+        }
+        
+        var exts = [''].concat(extensions);
+        load(exts, x, pkg)
+		
+		function load (exts, x, pkg) {
+            if (exts.length === 0) return cb(null, undefined, pkg);
             var file = x + exts[0];
             
-            isFile(file, function (err, ex) {
+            if (pkg) onpkg(null, pkg)
+            else loadpkg(path.dirname(file), onpkg);
+            
+            function onpkg (err, pkg_, dir) {
+                pkg = pkg_;
+                if (err) return cb(err)
+                if (dir && pkg && opts.pathFilter) {
+                    var rfile = path.relative(dir, file);
+                    var rel = rfile.slice(0, rfile.length - exts[0].length);
+                    var r = opts.pathFilter(pkg, x, rel);
+                    if (r) return load(
+                        [''].concat(extensions.slice()),
+                        path.resolve(dir, r),
+                        pkg
+                    );
+                }
+                isFile(file, onex);
+            }
+            function onex (err, ex) {
                 if (err) cb(err)
-                else if (ex) cb(null, file)
-                else load(exts.slice(1))
+                else if (!ex) load(exts.slice(1), x, pkg)
+                else cb(null, file, pkg)
+            }
+        }
+    }
+    
+    function loadpkg (dir, cb) {
+        if (dir === '' || dir === '/') return cb(null);
+        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
+            );
+            
+            readFile(pkgfile, function (err, body) {
+                if (err) cb(err);
+                try { var pkg = JSON.parse(body) }
+                catch (err) {}
+                
+                if (pkg && opts.packageFilter) {
+                    pkg = opts.packageFilter(pkg, pkgfile);
+                }
+                cb(null, pkg, dir);
             });
-        })([''].concat(extensions));
+        });
     }
     
-    function loadAsDirectory (x, cb) {
+    function loadAsDirectory (x, fpkg, cb) {
+        if (typeof fpkg === 'function') {
+            cb = fpkg;
+            fpkg = opts.package;
+        }
+        
         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'), cb);
+            if (!ex) return loadAsFile(path.join(x, '/index'), fpkg, cb);
             
             readFile(pkgfile, function (err, body) {
                 if (err) return cb(err);
@@ -71,24 +141,29 @@ module.exports = function resolve (x, opts, cb) {
                 catch (err) {}
                 
                 if (opts.packageFilter) {
-                    pkg = opts.packageFilter(pkg, x);
+                    pkg = opts.packageFilter(pkg, pkgfile);
                 }
                 
                 if (pkg.main) {
-                    loadAsFile(path.resolve(x, pkg.main), function (err, m) {
+                    if (pkg.main === '.' || pkg.main === './'){
+                        pkg.main = 'index'
+                    }
+                    loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
                         if (err) return cb(err);
-                        if (m) return cb(null, m);
+                        if (m) return cb(null, m, pkg);
+                        if (!pkg) return loadAsFile(path.join(x, '/index'), pkg, cb);
+
                         var dir = path.resolve(x, pkg.main);
-                        loadAsDirectory(dir, function (err, n) {
+                        loadAsDirectory(dir, pkg, function (err, n, pkg) {
                             if (err) return cb(err);
-                            if (n) return cb(null, n);
-                            loadAsFile(path.join(x, '/index'), cb);
+                            if (n) return cb(null, n, pkg);
+                            loadAsFile(path.join(x, '/index'), pkg, cb);
                         });
                     });
                     return;
                 }
                 
-                loadAsFile(path.join(x, '/index'), cb);
+                loadAsFile(path.join(x, '/index'), pkg, cb);
             });
         });
     }
@@ -98,34 +173,20 @@ module.exports = function resolve (x, opts, cb) {
             if (dirs.length === 0) return cb(null, undefined);
             var dir = dirs[0];
             
-            loadAsFile(path.join(dir, '/', x), function (err, m) {
+            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);
-                loadAsDirectory(path.join(dir, '/', x), function (err, n) {
-                    if (err) return cb(err);
-                    if (n) return cb(null, n);
-                    process(dirs.slice(1));
-                });
-            });
-        })(nodeModulesPaths(start));
-    }
-    
-    function nodeModulesPaths (start, cb) {
-        var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
-        var parts = start.split(splitRe);
-        
-        var dirs = [];
-        for (var i = parts.length - 1; i >= 0; i--) {
-            if (parts[i] === 'node_modules') continue;
-            var dir = path.join(
-                path.join.apply(path, parts.slice(0, i + 1)),
-                'node_modules'
-            );
-            if (!parts[0].match(/([A-Za-z]:)/)) {
-                dir = '/' + dir;    
+                if (m) return cb(null, m, pkg);
+                loadAsDirectory(path.join(dir, '/', x), undefined, ondir);
             }
-            dirs.push(dir);
-        }
-        return dirs.concat(opts.paths);
+            
+            function ondir (err, n, pkg) {
+                if (err) return cb(err);
+                if (n) return cb(null, n, pkg);
+                process(dirs.slice(1));
+            }
+        })(nodeModulesPaths(start, opts));
     }
 };
diff --git a/lib/caller.js b/lib/caller.js
new file mode 100644
index 0000000..5536549
--- /dev/null
+++ b/lib/caller.js
@@ -0,0 +1,8 @@
+module.exports = function () {
+    // see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
+    var origPrepareStackTrace = Error.prepareStackTrace;
+    Error.prepareStackTrace = function (_, stack) { return stack };
+    var stack = (new Error()).stack;
+    Error.prepareStackTrace = origPrepareStackTrace;
+    return stack[2].getFileName();
+};
diff --git a/lib/node-modules-paths.js b/lib/node-modules-paths.js
new file mode 100644
index 0000000..ce0a0d9
--- /dev/null
+++ b/lib/node-modules-paths.js
@@ -0,0 +1,38 @@
+var path = require('path');
+
+module.exports = function (start, opts) {
+    var modules = opts.moduleDirectory
+        ? [].concat(opts.moduleDirectory)
+        : ['node_modules']
+    ;
+
+    // ensure that `start` is an absolute path at this point,
+    // resolving against the process' current working directory
+    start = path.resolve(start);
+
+    var prefix = '/';
+    if (/^([A-Za-z]:)/.test(start)) {
+        prefix = '';
+    } else if (/^\\\\/.test(start)) {
+        prefix = '\\\\';
+    }
+
+    var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
+
+    var parts = start.split(splitRe);
+
+    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);
+}
diff --git a/lib/sync.js b/lib/sync.js
index 6b8576b..ef91edd 100644
--- a/lib/sync.js
+++ b/lib/sync.js
@@ -1,10 +1,10 @@
 var core = require('./core');
 var fs = require('fs');
 var path = require('path');
+var caller = require('./caller.js');
+var nodeModulesPaths = require('./node-modules-paths.js');
 
 module.exports = function (x, opts) {
-    if (core[x]) return x;
-    
     if (!opts) opts = {};
     var isFile = opts.isFile || function (file) {
         try { var stat = fs.statSync(file) }
@@ -14,22 +14,23 @@ module.exports = function (x, opts) {
     var readFileSync = opts.readFileSync || fs.readFileSync;
     
     var extensions = opts.extensions || [ '.js' ];
-    var y = opts.basedir
-        || path.dirname(require.cache[__filename].parent.filename)
-    ;
+    var y = opts.basedir || path.dirname(caller());
 
     opts.paths = opts.paths || [];
 
-    if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
-        var m = loadAsFileSync(path.resolve(y, x))
-            || loadAsDirectorySync(path.resolve(y, x));
+    if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {
+        var res = path.resolve(y, x);
+        if (x === '..') res += '/';
+        var m = loadAsFileSync(res) || loadAsDirectorySync(res);
         if (m) return m;
     } else {
         var n = loadNodeModulesSync(x, y);
         if (n) return n;
     }
     
-    throw new Error("Cannot find module '" + x + "'");
+    if (core[x]) return x;
+    
+    throw new Error("Cannot find module '" + x + "' from '" + y + "'");
     
     function loadAsFileSync (x) {
         if (isFile(x)) {
@@ -68,7 +69,7 @@ module.exports = function (x, opts) {
     }
     
     function loadNodeModulesSync (x, start) {
-        var dirs = nodeModulesPathsSync(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));
@@ -77,23 +78,4 @@ module.exports = function (x, opts) {
             if (n) return n;
         }
     }
-    
-    function nodeModulesPathsSync (start) {
-        var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
-        var parts = start.split(splitRe);
-        
-        var dirs = [];
-        for (var i = parts.length - 1; i >= 0; i--) {
-            if (parts[i] === 'node_modules') continue;
-            var dir = path.join(
-                path.join.apply(path, parts.slice(0, i + 1)),
-                'node_modules'
-            );
-            if (!parts[0].match(/([A-Za-z]:)/)) {
-                dir = '/' + dir;    
-            }
-            dirs.push(dir);
-        }
-        return dirs.concat(opts.paths);
-    }
 };
diff --git a/package.json b/package.json
index 2626cff..507fe8b 100644
--- a/package.json
+++ b/package.json
@@ -1,28 +1,29 @@
 {
-    "name" : "resolve",
-    "description" : "resolve like require.resolve() on behalf of files asynchronously and synchronously",
-    "version" : "0.3.1",
-    "repository" : {
-        "type" : "git",
-        "url" : "git://github.com/substack/node-resolve.git"
-    },
-    "main" : "index.js",
-    "keywords" : [
-        "resolve",
-        "require",
-        "node",
-        "module"
-    ],
-    "scripts" : {
-        "test" : "tap test/*.js"
-    },
-    "devDependencies" : {
-        "tap" : "~0.4.0"
-    },
-    "license" : "MIT",
-    "author" : {
-        "name" : "James Halliday",
-        "email" : "mail at substack.net",
-        "url" : "http://substack.net"
-    }
+  "name": "resolve",
+  "description": "resolve like require.resolve() on behalf of files asynchronously and synchronously",
+  "version": "1.1.7",
+  "repository": {
+    "type": "git",
+    "url": "git://github.com/substack/node-resolve.git"
+  },
+  "main": "index.js",
+  "keywords": [
+    "resolve",
+    "require",
+    "node",
+    "module"
+  ],
+  "scripts": {
+    "test": "tape test/*.js"
+  },
+  "devDependencies": {
+    "tape": "^3.5.0",
+    "tap": "0.4.13"
+  },
+  "license": "MIT",
+  "author": {
+    "name": "James Halliday",
+    "email": "mail at substack.net",
+    "url": "http://substack.net"
+  }
 }
diff --git a/readme.markdown b/readme.markdown
index 8da922c..4fab9b0 100644
--- a/readme.markdown
+++ b/readme.markdown
@@ -43,14 +43,16 @@ $ node example/sync.js
 var resolve = require('resolve')
 ```
 
-## resolve(pkg, opts={}, cb)
+## resolve(id, opts={}, cb)
 
-Asynchronously resolve the module path string `pkg` into `cb(err, res)`.
+Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.
 
 options are:
 
 * opts.basedir - directory to begin resolving from
 
+* opts.package - `package.json` data applicable to the module being loaded
+
 * opts.extensions - array of file extensions to search in order
 
 * opts.readFile - how to read files asynchronously
@@ -60,9 +62,17 @@ options are:
 * opts.packageFilter - transform the parsed package.json contents before looking
 at the "main" field
 
+* opts.pathFilter(pkg, path, relativePath) - transform a path within a package
+  * pkg - package data
+  * path - the path being resolved
+  * relativePath - the path relative from the package.json location
+  * returns - a relative path that will be joined from the package.json location
+
 * opts.paths - require.paths array to use if nothing is found on the normal
 node_modules recursive walk (probably don't use this)
 
+* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
+
 default `opts` values:
 
 ``` javascript
@@ -77,14 +87,15 @@ default `opts` values:
             else if (err) cb(err)
             else cb(null, stat.isFile())
         });
-    }
+    },
+    moduleDirectory: 'node_modules'
 }
 ```
 
-## resolve.sync(pkg, opts)
+## resolve.sync(id, opts)
 
-Synchronously resolve the module path string `pkg`, returning the result and
-throwing an error when `pkg` can't be resolved.
+Synchronously resolve the module path string `id`, returning the result and
+throwing an error when `id` can't be resolved.
 
 options are:
 
@@ -96,12 +107,14 @@ options are:
 
 * opts.isFile - function to synchronously test whether a file exists
 
-* opts.packageFilter - transform the parsed package.json contents before looking
-at the "main" field
+* `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json
+* contents before looking at the "main" field
 
 * opts.paths - require.paths array to use if nothing is found on the normal
 node_modules recursive walk (probably don't use this)
 
+* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
+
 default `opts` values:
 
 ``` javascript
@@ -113,7 +126,8 @@ default `opts` values:
     isFile: function (file) {
         try { return fs.statSync(file).isFile() }
         catch (e) { return false }
-    }
+    },
+    moduleDirectory: 'node_modules'
 }
 ````
 
diff --git a/test/core.js b/test/core.js
index 88a510c..4a56682 100644
--- a/test/core.js
+++ b/test/core.js
@@ -1,4 +1,4 @@
-var test = require('tap').test;
+var test = require('tape');
 var resolve = require('../');
 
 test('core modules', function (t) {
diff --git a/test/dotdot.js b/test/dotdot.js
new file mode 100644
index 0000000..b876772
--- /dev/null
+++ b/test/dotdot.js
@@ -0,0 +1,29 @@
+var path = require('path');
+var test = require('tape');
+var resolve = require('../');
+
+test('dotdot', function (t) {
+    t.plan(4);
+    var dir = __dirname + '/dotdot/abc';
+    
+    resolve('..', { basedir : dir }, function (err, res, pkg) {
+        t.ifError(err);
+        t.equal(res, __dirname + '/dotdot/index.js');
+    });
+    
+    resolve('.', { basedir : dir }, function (err, res, pkg) {
+        t.ifError(err);
+        t.equal(res, dir + '/index.js');
+    });
+});
+
+test('dotdot sync', function (t) {
+    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');
+});
diff --git a/test/dotdot/abc/index.js b/test/dotdot/abc/index.js
new file mode 100644
index 0000000..67f2534
--- /dev/null
+++ b/test/dotdot/abc/index.js
@@ -0,0 +1,2 @@
+var x = require('..');
+console.log(x);
diff --git a/test/dotdot/index.js b/test/dotdot/index.js
new file mode 100644
index 0000000..afec736
--- /dev/null
+++ b/test/dotdot/index.js
@@ -0,0 +1 @@
+module.exports = 'whatever'
diff --git a/test/faulty_basedir.js b/test/faulty_basedir.js
new file mode 100644
index 0000000..2440818
--- /dev/null
+++ b/test/faulty_basedir.js
@@ -0,0 +1,17 @@
+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) {
+    t.plan(1);
+
+    var resolverDir = 'C:\\a\\b\\c\\d';
+
+    resolve('tape/lib/test.js', { basedir : resolverDir }, function (err, res, pkg) {
+        t.equal(true, !!err);
+    });
+
+});
diff --git a/test/filter.js b/test/filter.js
index eb64642..07c38f3 100644
--- a/test/filter.js
+++ b/test/filter.js
@@ -1,8 +1,8 @@
-var test = require('tap').test;
+var test = require('tape');
 var resolve = require('../');
 
 test('filter', function (t) {
-    t.plan(1);
+    t.plan(2);
     var dir = __dirname + '/resolver';
     resolve('./baz', {
         basedir : dir,
@@ -10,8 +10,9 @@ test('filter', function (t) {
             pkg.main = 'doom';
             return pkg;
         }
-    }, function (err, res) {
+    }, function (err, res, pkg) {
         if (err) t.fail(err);
         t.equal(res, dir + '/baz/doom.js');
+        t.equal(pkg.main, 'doom');
     });
 });
diff --git a/test/filter_sync.js b/test/filter_sync.js
index 8856c01..3f89b79 100644
--- a/test/filter_sync.js
+++ b/test/filter_sync.js
@@ -1,4 +1,4 @@
-var test = require('tap').test;
+var test = require('tape');
 var resolve = require('../');
 
 test('filter', function (t) {
diff --git a/test/mock.js b/test/mock.js
index 76bdd75..1cf3b12 100644
--- a/test/mock.js
+++ b/test/mock.js
@@ -1,8 +1,8 @@
-var test = require('tap').test;
+var test = require('tape');
 var resolve = require('../');
 
 test('mock', function (t) {
-    t.plan(4);
+    t.plan(6);
     
     var files = {
         '/foo/bar/baz.js' : 'beep'
@@ -20,27 +20,99 @@ test('mock', function (t) {
         }
     }
     
-    resolve('./baz', opts('/foo/bar'), function (err, res) {
+    resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
         if (err) t.fail(err);
         t.equal(res, '/foo/bar/baz.js');
+        t.equal(pkg, undefined);
     });
     
-    resolve('./baz.js', opts('/foo/bar'), function (err, res) {
+    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, undefined);
     });
     
     resolve('baz', opts('/foo/bar'), function (err, res) {
-        t.equal(err.message, "Cannot find module 'baz'");
+        t.equal(err.message, "Cannot find module 'baz' from '/foo/bar'");
     });
     
     resolve('../baz', opts('/foo/bar'), function (err, res) {
-        t.equal(err.message, "Cannot find module '../baz'");
+        t.equal(err.message, "Cannot find module '../baz' from '/foo/bar'");
+    });
+});
+
+test('mock from package', function (t) {
+    t.plan(6);
+    
+    var files = {
+        '/foo/bar/baz.js' : 'beep'
+    };
+    
+    function opts (basedir) {
+        return {
+            basedir : basedir,
+            package : { main: 'bar' },
+            isFile : function (file, cb) {
+                cb(null, files.hasOwnProperty(file));
+            },
+            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');
+    });
+    
+    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');
+    });
+    
+    resolve('baz', opts('/foo/bar'), function (err, res) {
+        t.equal(err.message, "Cannot find module 'baz' from '/foo/bar'");
+    });
+    
+    resolve('../baz', opts('/foo/bar'), function (err, res) {
+        t.equal(err.message, "Cannot find module '../baz' from '/foo/bar'");
     });
 });
 
 test('mock package', function (t) {
-    t.plan(1);
+    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) {
+        return {
+            basedir : basedir,
+            isFile : function (file, cb) {
+                cb(null, files.hasOwnProperty(file));
+            },
+            readFile : function (file, cb) {
+                cb(null, files[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');
+    });
+});
+
+test('mock package from package', function (t) {
+    t.plan(2);
     
     var files = {
         '/foo/node_modules/bar/baz.js' : 'beep',
@@ -52,6 +124,7 @@ test('mock package', function (t) {
     function opts (basedir) {
         return {
             basedir : basedir,
+            package : { main: 'bar' },
             isFile : function (file, cb) {
                 cb(null, files.hasOwnProperty(file));
             },
@@ -61,8 +134,9 @@ test('mock package', function (t) {
         }
     }
     
-    resolve('bar', opts('/foo'), function (err, res) {
+    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');
     });
 });
diff --git a/test/mock_sync.js b/test/mock_sync.js
index 963afa5..abfd289 100644
--- a/test/mock_sync.js
+++ b/test/mock_sync.js
@@ -1,4 +1,4 @@
-var test = require('tap').test;
+var test = require('tape');
 var resolve = require('../');
 
 test('mock', function (t) {
diff --git a/test/module_dir.js b/test/module_dir.js
new file mode 100644
index 0000000..06395d8
--- /dev/null
+++ b/test/module_dir.js
@@ -0,0 +1,56 @@
+var path = require('path');
+var test = require('tape');
+var resolve = require('../');
+
+test('moduleDirectory strings', function (t) {
+    t.plan(4);
+    var dir = __dirname + '/module_dir';
+    var xopts = {
+        basedir : dir,
+        moduleDirectory: 'xmodules'
+    };
+    resolve('aaa', xopts, function (err, res, pkg) {
+        t.ifError(err);
+        t.equal(res, dir + '/xmodules/aaa/index.js');
+    });
+    
+    var yopts = {
+        basedir : dir,
+        moduleDirectory: 'ymodules'
+    };
+    resolve('aaa', yopts, function (err, res, pkg) {
+        t.ifError(err);
+        t.equal(res, dir + '/ymodules/aaa/index.js');
+    });
+});
+
+test('moduleDirectory array', function (t) {
+    t.plan(6);
+    var dir = __dirname + '/module_dir';
+    var aopts = {
+        basedir : dir,
+        moduleDirectory: [ 'xmodules', 'ymodules', 'zmodules' ]
+    };
+    resolve('aaa', aopts, function (err, res, pkg) {
+        t.ifError(err);
+        t.equal(res, dir + '/xmodules/aaa/index.js');
+    });
+    
+    var bopts = {
+        basedir : dir,
+        moduleDirectory: [ 'zmodules', 'ymodules', 'xmodules' ]
+    };
+    resolve('aaa', bopts, function (err, res, pkg) {
+        t.ifError(err);
+        t.equal(res, dir + '/ymodules/aaa/index.js');
+    });
+    
+    var copts = {
+        basedir : dir,
+        moduleDirectory: [ 'xmodules', 'ymodules', 'zmodules' ]
+    };
+    resolve('bbb', copts, function (err, res, pkg) {
+        t.ifError(err);
+        t.equal(res, dir + '/zmodules/bbb/main.js');
+    });
+});
diff --git a/test/module_dir/xmodules/aaa/index.js b/test/module_dir/xmodules/aaa/index.js
new file mode 100644
index 0000000..55cd18c
--- /dev/null
+++ b/test/module_dir/xmodules/aaa/index.js
@@ -0,0 +1 @@
+module.exports = function (x) { return x * 100 }
diff --git a/test/module_dir/ymodules/aaa/index.js b/test/module_dir/ymodules/aaa/index.js
new file mode 100644
index 0000000..651aca8
--- /dev/null
+++ b/test/module_dir/ymodules/aaa/index.js
@@ -0,0 +1 @@
+module.exports = function (x) { return x + 100 }
diff --git a/test/module_dir/zmodules/bbb/main.js b/test/module_dir/zmodules/bbb/main.js
new file mode 100644
index 0000000..4325a0b
--- /dev/null
+++ b/test/module_dir/zmodules/bbb/main.js
@@ -0,0 +1 @@
+module.exports = function (n) { return n * 111 }
diff --git a/test/module_dir/zmodules/bbb/package.json b/test/module_dir/zmodules/bbb/package.json
new file mode 100644
index 0000000..c13b8cf
--- /dev/null
+++ b/test/module_dir/zmodules/bbb/package.json
@@ -0,0 +1,3 @@
+{
+  "main": "main.js"
+}
diff --git a/test/node_path.js b/test/node_path.js
new file mode 100644
index 0000000..2407189
--- /dev/null
+++ b/test/node_path.js
@@ -0,0 +1,48 @@
+var path = require('path');
+var test = require('tape');
+var resolve = require('../');
+
+test('$NODE_PATH', function (t) {
+    t.plan(4);
+    
+    resolve('aaa', {
+        paths: [
+            __dirname + '/node_path/x',
+            __dirname + '/node_path/y'
+        ],
+        basedir: __dirname,
+    }, function (err, res) {
+        t.equal(res, __dirname + '/node_path/x/aaa/index.js');
+    });
+    
+    resolve('bbb', {
+        paths: [
+            __dirname + '/node_path/x',
+            __dirname + '/node_path/y'
+        ],
+        basedir: __dirname,
+    }, function (err, res) {
+        t.equal(res, __dirname + '/node_path/y/bbb/index.js');
+    });
+    
+    resolve('ccc', {
+        paths: [
+            __dirname + '/node_path/x',
+            __dirname + '/node_path/y'
+        ],
+        basedir: __dirname,
+    }, function (err, res) {
+        t.equal(res, __dirname + '/node_path/x/ccc/index.js');
+    });
+
+    // ensure that relative paths still resolve against the
+    // regular `node_modules` correctly
+    resolve('tap', {
+        paths: [
+            'node_path',
+        ],
+        basedir: 'node_path/x',
+    }, function (err, res) {
+        t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js'));
+    });
+});
diff --git a/test/node_path/x/aaa/index.js b/test/node_path/x/aaa/index.js
new file mode 100644
index 0000000..1ea5913
--- /dev/null
+++ b/test/node_path/x/aaa/index.js
@@ -0,0 +1 @@
+module.exports = 'A'
diff --git a/test/node_path/x/ccc/index.js b/test/node_path/x/ccc/index.js
new file mode 100644
index 0000000..f186fa7
--- /dev/null
+++ b/test/node_path/x/ccc/index.js
@@ -0,0 +1 @@
+module.exports = 'C'
diff --git a/test/node_path/y/bbb/index.js b/test/node_path/y/bbb/index.js
new file mode 100644
index 0000000..e22dd83
--- /dev/null
+++ b/test/node_path/y/bbb/index.js
@@ -0,0 +1 @@
+module.exports = 'B'
diff --git a/test/node_path/y/ccc/index.js b/test/node_path/y/ccc/index.js
new file mode 100644
index 0000000..d0043d1
--- /dev/null
+++ b/test/node_path/y/ccc/index.js
@@ -0,0 +1 @@
+module.exports = 'CY'
diff --git a/test/nonstring.js b/test/nonstring.js
new file mode 100644
index 0000000..ef63c40
--- /dev/null
+++ b/test/nonstring.js
@@ -0,0 +1,9 @@
+var test = require('tape');
+var resolve = require('../');
+
+test('nonstring', function (t) {
+    t.plan(1);
+    resolve(555, function (err, res, pkg) {
+        t.ok(err);
+    });
+});
diff --git a/test/pathfilter.js b/test/pathfilter.js
new file mode 100644
index 0000000..142f94d
--- /dev/null
+++ b/test/pathfilter.js
@@ -0,0 +1,35 @@
+var test = require('tape');
+var resolve = require('../');
+
+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";
+	};
+	
+	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');
+    });
+
+    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');
+    });
+});
diff --git a/test/pathfilter/deep_ref/main.js b/test/pathfilter/deep_ref/main.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/pathfilter/deep_ref/node_modules/deep/alt.js b/test/pathfilter/deep_ref/node_modules/deep/alt.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/pathfilter/deep_ref/node_modules/deep/deeper/ref.js b/test/pathfilter/deep_ref/node_modules/deep/deeper/ref.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/pathfilter/deep_ref/node_modules/deep/package.json b/test/pathfilter/deep_ref/node_modules/deep/package.json
new file mode 100644
index 0000000..fe4b408
--- /dev/null
+++ b/test/pathfilter/deep_ref/node_modules/deep/package.json
@@ -0,0 +1,4 @@
+{
+  "name": "deep",
+  "version": "1.2.3"
+}
diff --git a/test/pathfilter/deep_ref/node_modules/deep/ref.js b/test/pathfilter/deep_ref/node_modules/deep/ref.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/precedence.js b/test/precedence.js
new file mode 100644
index 0000000..c716f0e
--- /dev/null
+++ b/test/precedence.js
@@ -0,0 +1,23 @@
+var path = require('path');
+var test = require('tape');
+var resolve = require('../');
+
+test('precedence', function (t) {
+    t.plan(3);
+    var dir = path.join(__dirname, 'precedence/aaa');
+    
+    resolve('./', { basedir : dir }, function (err, res, pkg) {
+        t.ifError(err);
+        t.equal(res, path.join(dir, 'index.js'));
+        t.equal(pkg.name, 'resolve');
+    });
+});
+
+test('./ should not load ${dir}.js', function (t) {
+    t.plan(1);
+    var dir = path.join(__dirname, 'precedence/bbb');
+    
+    resolve('./', { basedir : dir }, function (err, res, pkg) {
+        t.ok(err);
+    });
+});
diff --git a/test/precedence/aaa.js b/test/precedence/aaa.js
new file mode 100644
index 0000000..a182397
--- /dev/null
+++ b/test/precedence/aaa.js
@@ -0,0 +1 @@
+module.exports = 'wtf'
diff --git a/test/precedence/aaa/index.js b/test/precedence/aaa/index.js
new file mode 100644
index 0000000..993b03c
--- /dev/null
+++ b/test/precedence/aaa/index.js
@@ -0,0 +1 @@
+module.exports = 'okok'
diff --git a/test/precedence/aaa/main.js b/test/precedence/aaa/main.js
new file mode 100644
index 0000000..db38959
--- /dev/null
+++ b/test/precedence/aaa/main.js
@@ -0,0 +1 @@
+console.log(require('./'))
diff --git a/test/precedence/bbb.js b/test/precedence/bbb.js
new file mode 100644
index 0000000..c8a9996
--- /dev/null
+++ b/test/precedence/bbb.js
@@ -0,0 +1 @@
+module.exports '>_<'
diff --git a/test/precedence/bbb/main.js b/test/precedence/bbb/main.js
new file mode 100644
index 0000000..716b81d
--- /dev/null
+++ b/test/precedence/bbb/main.js
@@ -0,0 +1 @@
+console.log(require('./')); // should throw
diff --git a/test/resolver.js b/test/resolver.js
index 3fb64f5..5bbb05f 100644
--- a/test/resolver.js
+++ b/test/resolver.js
@@ -1,77 +1,176 @@
-var test = require('tap').test;
+var path = require('path');
+var test = require('tape');
 var resolve = require('../');
 
 test('async foo', function (t) {
-    t.plan(3);
+    t.plan(9);
     var dir = __dirname + '/resolver';
     
-    resolve('./foo', { basedir : dir }, function (err, res) {
+    resolve('./foo', { basedir : dir }, function (err, res, pkg) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/foo.js');
+        t.equal(pkg.name, 'resolve');
+    });
+    
+    resolve('./foo.js', { basedir : dir }, function (err, res, pkg) {
         if (err) t.fail(err);
         t.equal(res, dir + '/foo.js');
+        t.equal(pkg.name, 'resolve');
     });
     
-    resolve('./foo.js', { basedir : dir }, function (err, res) {
+    resolve('./foo', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
         if (err) t.fail(err);
         t.equal(res, dir + '/foo.js');
+        t.equal(pkg.main, 'resolver');
+    });
+    
+    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(pkg.main, 'resolver');
     });
     
     resolve('foo', { basedir : dir }, function (err) {
-        t.equal(err.message, "Cannot find module 'foo'");
+        t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
     });
 });
 
 test('bar', function (t) {
-    t.plan(2);
+    t.plan(6);
     var dir = __dirname + '/resolver';
     
-    resolve('foo', { basedir : dir + '/bar' }, function (err, res) {
+    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(pkg, undefined);
     });
     
-    resolve('foo', { basedir : dir + '/bar' }, function (err, res) {
+    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(pkg, undefined);
+    });
+    
+    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(pkg, undefined);
     });
 });
 
 test('baz', function (t) {
-    t.plan(1);
+    t.plan(4);
     var dir = __dirname + '/resolver';
     
-    resolve('./baz', { basedir : dir }, function (err, res) {
+    resolve('./baz', { basedir : dir }, function (err, res, pkg) {
         if (err) t.fail(err);
         t.equal(res, dir + '/baz/quux.js');
+        t.equal(pkg.main, 'quux.js');
+    });
+    
+    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(pkg.main, 'quux.js');
     });
 });
 
 test('biz', function (t) {
-    t.plan(3);
+    t.plan(24);
     var dir = __dirname + '/resolver/biz/node_modules';
     
-    resolve('./grux', { basedir : dir }, function (err, res) {
+    resolve('./grux', { basedir : dir }, function (err, res, pkg) {
         if (err) t.fail(err);
         t.equal(res, dir + '/grux/index.js');
+        t.equal(pkg, undefined);
+    });
+    
+    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(pkg.main, 'biz');
+    });
+    
+    resolve('./garply', { basedir : dir }, function (err, res, pkg) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/garply/lib/index.js');
+        t.equal(pkg.main, './lib');
+    });
+    
+    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(pkg.main, './lib');
+    });
+    
+    resolve('tiv', { basedir : dir + '/grux' }, function (err, res, pkg) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/tiv/index.js');
+        t.equal(pkg, undefined);
+    });
+    
+    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(pkg, undefined);
+    });
+    
+    resolve('tiv', { basedir : dir + '/garply' }, function (err, res, pkg) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/tiv/index.js');
+        t.equal(pkg, undefined);
     });
     
-    resolve('tiv', { basedir : dir + '/grux' }, function (err, res) {
+    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(pkg, undefined);
+    });
+    
+    resolve('grux', { basedir : dir + '/tiv' }, function (err, res, pkg) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/grux/index.js');
+        t.equal(pkg, undefined);
     });
     
-    resolve('grux', { basedir : dir + '/tiv' }, function (err, res) {
+    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(pkg, undefined);
+    });
+    
+    resolve('garply', { basedir : dir + '/tiv' }, function (err, res, pkg) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/garply/lib/index.js');
+        t.equal(pkg.main, './lib');
+    });
+    
+    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(pkg.main, './lib');
+    });
+});
+
+test('quux', function (t) {
+    t.plan(2);
+    var dir = __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(pkg.main, 'quux');
     });
 });
 
 test('normalize', function (t) {
-    t.plan(1);
+    t.plan(2);
     var dir = __dirname + '/resolver/biz/node_modules/grux';
     
-    resolve('../grux', { basedir : dir }, function (err, res) {
+    resolve('../grux', { basedir : dir }, function (err, res, pkg) {
         if (err) t.fail(err);
         t.equal(res, dir + '/index.js');
+        t.equal(pkg, undefined);
     });
 });
 
@@ -92,7 +191,7 @@ test('cup', function (t) {
     
     resolve('./cup', { basedir : dir, extensions : [ '.js' ] },
     function (err, res) {
-        t.equal(err.message, "Cannot find module './cup'");
+        t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
     });
 });
 
@@ -135,10 +234,48 @@ test('other path', function (t) {
     });
     
     resolve('root', { basedir : dir, }, function (err, res) {
-        t.equal(err.message, "Cannot find module 'root'");
+        t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
     });
     
     resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) {
-        t.equal(err.message, "Cannot find module 'zzz'");
+        t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
+    });
+});
+
+test('incorrect main', function (t) {
+    t.plan(1)
+
+    var resolverDir = __dirname + '/resolver';
+    var dir = resolverDir + '/incorrect_main';
+
+    resolve('./incorrect_main', { basedir : resolverDir }, function (err, res, pkg) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/index.js');
+    });
+});
+
+test('without basedir', function (t) {
+    t.plan(1);
+
+    var dir = __dirname + '/resolver/without_basedir';
+    var tester = require(dir + '/main.js');
+
+    tester(t, function (err, res, pkg){
+        if (err) {
+	  t.fail(err);
+	} else {
+          t.equal(res, dir + '/node_modules/mymodule.js');
+	}
+    });
+});
+
+test('#25: node modules with the same name as node stdlib modules', function (t) {
+    t.plan(1);
+
+    var resolverDir = __dirname + '/resolver/punycode';
+
+    resolve('punycode', { basedir : resolverDir }, function (err, res, pkg) {
+        if (err) t.fail(err);
+        t.equal(res, resolverDir + '/node_modules/punycode/index.js');
     });
 });
diff --git a/test/resolver/biz/node_modules/garply/lib/index.js b/test/resolver/biz/node_modules/garply/lib/index.js
new file mode 100644
index 0000000..0379e29
--- /dev/null
+++ b/test/resolver/biz/node_modules/garply/lib/index.js
@@ -0,0 +1 @@
+module.exports = 'hello garply';
diff --git a/test/resolver/biz/node_modules/garply/package.json b/test/resolver/biz/node_modules/garply/package.json
new file mode 100644
index 0000000..babaac5
--- /dev/null
+++ b/test/resolver/biz/node_modules/garply/package.json
@@ -0,0 +1,3 @@
+{
+    "main" : "./lib"
+}
diff --git a/test/resolver/incorrect_main/index.js b/test/resolver/incorrect_main/index.js
new file mode 100644
index 0000000..bc1fb0a
--- /dev/null
+++ b/test/resolver/incorrect_main/index.js
@@ -0,0 +1,2 @@
+// this is the actual main file 'index.js', not 'wrong.js' like the package.json would indicate
+module.exports = 1;
diff --git a/test/resolver/incorrect_main/package.json b/test/resolver/incorrect_main/package.json
new file mode 100644
index 0000000..1592ed3
--- /dev/null
+++ b/test/resolver/incorrect_main/package.json
@@ -0,0 +1,3 @@
+{
+    "main" : "wrong.js"
+}
diff --git a/test/resolver/punycode/node_modules/punycode/index.js b/test/resolver/punycode/node_modules/punycode/index.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/resolver/quux/foo/index.js b/test/resolver/quux/foo/index.js
new file mode 100644
index 0000000..bd816ea
--- /dev/null
+++ b/test/resolver/quux/foo/index.js
@@ -0,0 +1 @@
+module.exports = 1;
diff --git a/test/resolver/without_basedir/main.js b/test/resolver/without_basedir/main.js
new file mode 100644
index 0000000..5f211e9
--- /dev/null
+++ b/test/resolver/without_basedir/main.js
@@ -0,0 +1,6 @@
+resolve = require('../../../');
+
+module.exports = function(t, cb) {
+  resolve('mymodule', null, cb);
+}
+
diff --git a/test/resolver/without_basedir/node_modules/mymodule.js b/test/resolver/without_basedir/node_modules/mymodule.js
new file mode 100644
index 0000000..2b58aa4
--- /dev/null
+++ b/test/resolver/without_basedir/node_modules/mymodule.js
@@ -0,0 +1 @@
+module.exports = "The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities.- E. Dijkstra"
diff --git a/test/resolver_sync.js b/test/resolver_sync.js
index 80f5aa5..5982531 100644
--- a/test/resolver_sync.js
+++ b/test/resolver_sync.js
@@ -1,4 +1,4 @@
-var test = require('tap').test;
+var test = require('tape');
 var resolve = require('../');
 
 test('foo', function (t) {
@@ -155,3 +155,26 @@ test('other path', function (t) {
     
     t.end();
 });
+
+test('incorrect main', function (t) {
+    var resolverDir = __dirname + '/resolver';
+    var dir = resolverDir + '/incorrect_main';
+
+    t.equal(
+        resolve.sync('./incorrect_main', { basedir : resolverDir }),
+        dir + '/index.js'
+    )
+
+    t.end()
+});
+
+test('#25: node modules with the same name as node stdlib modules', function (t) {
+    var resolverDir = __dirname + '/resolver/punycode';
+
+    t.equal(
+        resolve.sync('punycode', { basedir : resolverDir }),
+        resolverDir + '/node_modules/punycode/index.js'
+    )
+
+    t.end()
+});
diff --git a/test/subdirs.js b/test/subdirs.js
new file mode 100644
index 0000000..957abfe
--- /dev/null
+++ b/test/subdirs.js
@@ -0,0 +1,13 @@
+var test = require('tape');
+var resolve = require('../');
+var path = require('path');
+
+test('subdirs', function (t) {
+    t.plan(2);
+    
+    var dir = path.join(__dirname, '/subdirs');
+    resolve('a/b/c/x.json', { basedir: dir }, function (err, res) {
+        t.ifError(err);
+        t.equal(res, path.join(dir, 'node_modules/a/b/c/x.json'));
+    });
+});
diff --git a/test/subdirs/node_modules/a/b/c/x.json b/test/subdirs/node_modules/a/b/c/x.json
new file mode 100644
index 0000000..3cc0ecb
--- /dev/null
+++ b/test/subdirs/node_modules/a/b/c/x.json
@@ -0,0 +1 @@
+[1,2,3]
diff --git a/test/subdirs/node_modules/a/package.json b/test/subdirs/node_modules/a/package.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/test/subdirs/node_modules/a/package.json
@@ -0,0 +1 @@
+{}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-resolve.git



More information about the Pkg-javascript-commits mailing list