[Pkg-javascript-commits] [node-resolve] 01/06: Imported Upstream version 0.3.1

Thorsten Alteholz alteholz at moszumanska.debian.org
Fri Jun 17 17:57:31 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 af61d239497053473fdad2abf7a37f5fedfb2f31
Author: Thorsten Alteholz <debian at alteholz.de>
Date:   Fri Jun 17 19:42:04 2016 +0200

    Imported Upstream version 0.3.1
---
 .travis.yml                                  |   4 +
 LICENSE                                      |  18 +++
 example/async.js                             |   5 +
 example/sync.js                              |   3 +
 index.js                                     |   5 +
 lib/async.js                                 | 131 ++++++++++++++++++++++
 lib/core.js                                  |   4 +
 lib/core.json                                |  38 +++++++
 lib/sync.js                                  |  99 +++++++++++++++++
 package.json                                 |  28 +++++
 readme.markdown                              | 134 +++++++++++++++++++++++
 test/core.js                                 |  12 ++
 test/filter.js                               |  17 +++
 test/filter_sync.js                          |  15 +++
 test/mock.js                                 |  68 ++++++++++++
 test/mock_sync.js                            |  68 ++++++++++++
 test/resolver.js                             | 144 ++++++++++++++++++++++++
 test/resolver/bar/node_modules/foo/index.js  |   1 +
 test/resolver/baz/doom.js                    |   0
 test/resolver/baz/package.json               |   3 +
 test/resolver/baz/quux.js                    |   1 +
 test/resolver/biz/node_modules/grux/index.js |   1 +
 test/resolver/biz/node_modules/tiv/index.js  |   1 +
 test/resolver/cup.coffee                     |   1 +
 test/resolver/foo.js                         |   1 +
 test/resolver/mug.coffee                     |   0
 test/resolver/mug.js                         |   0
 test/resolver/other_path/lib/other-lib.js    |   0
 test/resolver/other_path/root.js             |   0
 test/resolver_sync.js                        | 157 +++++++++++++++++++++++++++
 30 files changed, 959 insertions(+)

diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..895dbd3
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+  - 0.6
+  - 0.8
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..ee27ba4
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,18 @@
+This software is released under the MIT license:
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/example/async.js b/example/async.js
new file mode 100644
index 0000000..6624ff7
--- /dev/null
+++ b/example/async.js
@@ -0,0 +1,5 @@
+var resolve = require('../');
+resolve('tap', { basedir: __dirname }, function (err, res) {
+    if (err) console.error(err)
+    else console.log(res)
+});
diff --git a/example/sync.js b/example/sync.js
new file mode 100644
index 0000000..54b2cc1
--- /dev/null
+++ b/example/sync.js
@@ -0,0 +1,3 @@
+var resolve = require('../');
+var res = resolve.sync('tap', { basedir: __dirname });
+console.log(res);
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..51f194b
--- /dev/null
+++ b/index.js
@@ -0,0 +1,5 @@
+var core = require('./lib/core');
+exports = module.exports = require('./lib/async');
+exports.core = core;
+exports.isCore = function (x) { return core[x] };
+exports.sync = require('./lib/sync');
diff --git a/lib/async.js b/lib/async.js
new file mode 100644
index 0000000..7c5a01a
--- /dev/null
+++ b/lib/async.js
@@ -0,0 +1,131 @@
+var core = require('./core');
+var fs = require('fs');
+var path = require('path');
+
+module.exports = function resolve (x, opts, cb) {
+    if (core[x]) return cb(null, x);
+    
+    if (typeof opts === 'function') {
+        cb = opts;
+        opts = {};
+    }
+    if (!opts) opts = {};
+    
+    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())
+        });
+    };
+    var readFile = opts.readFile || fs.readFile;
+    
+    var extensions = opts.extensions || [ '.js' ];
+    var y = opts.basedir
+        || path.dirname(require.cache[__filename].parent.filename)
+    ;
+    
+    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 + "'"))
+            })
+        });
+    }
+    else loadNodeModules(x, y, function (err, n) {
+        if (err) cb(err)
+        else if (n) cb(null, n)
+        else cb(new Error("Cannot find module '" + x + "'"))
+    });
+    
+    function loadAsFile (x, cb) {
+        (function load (exts) {
+            if (exts.length === 0) return cb(null, undefined);
+            var file = x + exts[0];
+            
+            isFile(file, function (err, ex) {
+                if (err) cb(err)
+                else if (ex) cb(null, file)
+                else load(exts.slice(1))
+            });
+        })([''].concat(extensions));
+    }
+    
+    function loadAsDirectory (x, cb) {
+        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);
+            
+            readFile(pkgfile, function (err, body) {
+                if (err) return cb(err);
+                try {
+                    var pkg = JSON.parse(body);
+                }
+                catch (err) {}
+                
+                if (opts.packageFilter) {
+                    pkg = opts.packageFilter(pkg, x);
+                }
+                
+                if (pkg.main) {
+                    loadAsFile(path.resolve(x, pkg.main), function (err, m) {
+                        if (err) return cb(err);
+                        if (m) return cb(null, m);
+                        var dir = path.resolve(x, pkg.main);
+                        loadAsDirectory(dir, function (err, n) {
+                            if (err) return cb(err);
+                            if (n) return cb(null, n);
+                            loadAsFile(path.join(x, '/index'), cb);
+                        });
+                    });
+                    return;
+                }
+                
+                loadAsFile(path.join(x, '/index'), cb);
+            });
+        });
+    }
+    
+    function loadNodeModules (x, start, cb) {
+        (function process (dirs) {
+            if (dirs.length === 0) return cb(null, undefined);
+            var dir = dirs[0];
+            
+            loadAsFile(path.join(dir, '/', x), function (err, m) {
+                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;    
+            }
+            dirs.push(dir);
+        }
+        return dirs.concat(opts.paths);
+    }
+};
diff --git a/lib/core.js b/lib/core.js
new file mode 100644
index 0000000..ea4a6c8
--- /dev/null
+++ b/lib/core.js
@@ -0,0 +1,4 @@
+module.exports = require('./core.json').reduce(function (acc, x) {
+    acc[x] = true;
+    return acc;
+}, {});
diff --git a/lib/core.json b/lib/core.json
new file mode 100644
index 0000000..28560f7
--- /dev/null
+++ b/lib/core.json
@@ -0,0 +1,38 @@
+[
+    "assert",
+    "buffer_ieee754",
+    "buffer",
+    "child_process",
+    "cluster",
+    "console",
+    "constants",
+    "crypto",
+    "_debugger",
+    "dgram",
+    "dns",
+    "domain",
+    "events",
+    "freelist",
+    "fs",
+    "http",
+    "https",
+    "_linklist",
+    "module",
+    "net",
+    "os",
+    "path",
+    "punycode",
+    "querystring",
+    "readline",
+    "repl",
+    "stream",
+    "string_decoder",
+    "sys",
+    "timers",
+    "tls",
+    "tty",
+    "url",
+    "util",
+    "vm",
+    "zlib"
+]
diff --git a/lib/sync.js b/lib/sync.js
new file mode 100644
index 0000000..6b8576b
--- /dev/null
+++ b/lib/sync.js
@@ -0,0 +1,99 @@
+var core = require('./core');
+var fs = require('fs');
+var path = require('path');
+
+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) }
+        catch (err) { if (err && err.code === 'ENOENT') return false }
+        return stat.isFile() || stat.isFIFO();
+    };
+    var readFileSync = opts.readFileSync || fs.readFileSync;
+    
+    var extensions = opts.extensions || [ '.js' ];
+    var y = opts.basedir
+        || path.dirname(require.cache[__filename].parent.filename)
+    ;
+
+    opts.paths = opts.paths || [];
+
+    if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
+        var m = loadAsFileSync(path.resolve(y, x))
+            || loadAsDirectorySync(path.resolve(y, x));
+        if (m) return m;
+    } else {
+        var n = loadNodeModulesSync(x, y);
+        if (n) return n;
+    }
+    
+    throw new Error("Cannot find module '" + x + "'");
+    
+    function loadAsFileSync (x) {
+        if (isFile(x)) {
+            return x;
+        }
+        
+        for (var i = 0; i < extensions.length; i++) {
+            var file = x + extensions[i];
+            if (isFile(file)) {
+                return file;
+            }
+        }
+    }
+    
+    function loadAsDirectorySync (x) {
+        var pkgfile = path.join(x, '/package.json');
+        if (isFile(pkgfile)) {
+            var body = readFileSync(pkgfile, 'utf8');
+            try {
+                var pkg = JSON.parse(body);
+                if (opts.packageFilter) {
+                    pkg = opts.packageFilter(pkg, x);
+                }
+                
+                if (pkg.main) {
+                    var m = loadAsFileSync(path.resolve(x, pkg.main));
+                    if (m) return m;
+                    var n = loadAsDirectorySync(path.resolve(x, pkg.main));
+                    if (n) return n;
+                }
+            }
+            catch (err) {}
+        }
+        
+        return loadAsFileSync(path.join( x, '/index'));
+    }
+    
+    function loadNodeModulesSync (x, start) {
+        var dirs = nodeModulesPathsSync(start);
+        for (var i = 0; i < dirs.length; i++) {
+            var dir = dirs[i];
+            var m = loadAsFileSync(path.join( dir, '/', x));
+            if (m) return m;
+            var n = loadAsDirectorySync(path.join( dir, '/', x ));
+            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
new file mode 100644
index 0000000..2626cff
--- /dev/null
+++ b/package.json
@@ -0,0 +1,28 @@
+{
+    "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"
+    }
+}
diff --git a/readme.markdown b/readme.markdown
new file mode 100644
index 0000000..8da922c
--- /dev/null
+++ b/readme.markdown
@@ -0,0 +1,134 @@
+# resolve
+
+implements the [node `require.resolve()`
+algorithm](http://nodejs.org/docs/v0.4.8/api/all.html#all_Together...)
+such that you can `require.resolve()` on behalf of a file asynchronously and
+synchronously
+
+[![build status](https://secure.travis-ci.org/substack/node-resolve.png)](http://travis-ci.org/substack/node-resolve)
+
+# example
+
+asynchronously resolve:
+
+``` js
+var resolve = require('resolve');
+resolve('tap', { basedir: __dirname }, function (err, res) {
+    if (err) console.error(err)
+    else console.log(res)
+});
+```
+
+```
+$ node example/async.js
+/home/substack/projects/node-resolve/node_modules/tap/lib/main.js
+```
+
+synchronously resolve:
+
+``` js
+var resolve = require('resolve');
+var res = resolve.sync('tap', { basedir: __dirname });
+console.log(res);
+```
+
+```
+$ node example/sync.js
+/home/substack/projects/node-resolve/node_modules/tap/lib/main.js
+```
+
+# methods
+
+``` js
+var resolve = require('resolve')
+```
+
+## resolve(pkg, opts={}, cb)
+
+Asynchronously resolve the module path string `pkg` into `cb(err, res)`.
+
+options are:
+
+* opts.basedir - directory to begin resolving from
+
+* opts.extensions - array of file extensions to search in order
+
+* opts.readFile - how to read files asynchronously
+
+* opts.isFile - function to asynchronously test whether a file exists
+
+* opts.packageFilter - 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)
+
+default `opts` values:
+
+``` javascript
+{
+    paths: [],
+    basedir: __dirname,
+    extensions: [ '.js' ],
+    readFile: fs.readFile,
+    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())
+        });
+    }
+}
+```
+
+## resolve.sync(pkg, opts)
+
+Synchronously resolve the module path string `pkg`, returning the result and
+throwing an error when `pkg` can't be resolved.
+
+options are:
+
+* opts.basedir - directory to begin resolving from
+
+* opts.extensions - array of file extensions to search in order
+
+* opts.readFile - how to read files synchronously
+
+* 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.paths - require.paths array to use if nothing is found on the normal
+node_modules recursive walk (probably don't use this)
+
+default `opts` values:
+
+``` javascript
+{
+    paths: [],
+    basedir: __dirname,
+    extensions: [ '.js' ],
+    readFileSync: fs.readFileSync,
+    isFile: function (file) {
+        try { return fs.statSync(file).isFile() }
+        catch (e) { return false }
+    }
+}
+````
+
+## resolve.isCore(pkg)
+
+Return whether a package is in core.
+
+# install
+
+With [npm](https://npmjs.org) do:
+
+```
+npm install resolve
+```
+
+# license
+
+MIT
diff --git a/test/core.js b/test/core.js
new file mode 100644
index 0000000..88a510c
--- /dev/null
+++ b/test/core.js
@@ -0,0 +1,12 @@
+var test = require('tap').test;
+var resolve = require('../');
+
+test('core modules', function (t) {
+    t.ok(resolve.isCore('fs'));
+    t.ok(resolve.isCore('net'));
+    t.ok(resolve.isCore('http'));
+    
+    t.ok(!resolve.isCore('seq'));
+    t.ok(!resolve.isCore('../'));
+    t.end();
+});
diff --git a/test/filter.js b/test/filter.js
new file mode 100644
index 0000000..eb64642
--- /dev/null
+++ b/test/filter.js
@@ -0,0 +1,17 @@
+var test = require('tap').test;
+var resolve = require('../');
+
+test('filter', function (t) {
+    t.plan(1);
+    var dir = __dirname + '/resolver';
+    resolve('./baz', {
+        basedir : dir,
+        packageFilter : function (pkg) {
+            pkg.main = 'doom';
+            return pkg;
+        }
+    }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/baz/doom.js');
+    });
+});
diff --git a/test/filter_sync.js b/test/filter_sync.js
new file mode 100644
index 0000000..8856c01
--- /dev/null
+++ b/test/filter_sync.js
@@ -0,0 +1,15 @@
+var test = require('tap').test;
+var resolve = require('../');
+
+test('filter', function (t) {
+    var dir = __dirname + '/resolver';
+    var res = resolve.sync('./baz', {
+        basedir : dir,
+        packageFilter : function (pkg) {
+            pkg.main = 'doom'
+            return pkg;
+        }
+    });
+    t.equal(res, dir + '/baz/doom.js');
+    t.end();
+});
diff --git a/test/mock.js b/test/mock.js
new file mode 100644
index 0000000..76bdd75
--- /dev/null
+++ b/test/mock.js
@@ -0,0 +1,68 @@
+var test = require('tap').test;
+var resolve = require('../');
+
+test('mock', function (t) {
+    t.plan(4);
+    
+    var files = {
+        '/foo/bar/baz.js' : 'beep'
+    };
+    
+    function opts (basedir) {
+        return {
+            basedir : basedir,
+            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) {
+        if (err) t.fail(err);
+        t.equal(res, '/foo/bar/baz.js');
+    });
+    
+    resolve('./baz.js', opts('/foo/bar'), function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, '/foo/bar/baz.js');
+    });
+    
+    resolve('baz', opts('/foo/bar'), function (err, res) {
+        t.equal(err.message, "Cannot find module 'baz'");
+    });
+    
+    resolve('../baz', opts('/foo/bar'), function (err, res) {
+        t.equal(err.message, "Cannot find module '../baz'");
+    });
+});
+
+test('mock package', function (t) {
+    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) {
+        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) {
+        if (err) t.fail(err);
+        t.equal(res, '/foo/node_modules/bar/baz.js');
+    });
+});
diff --git a/test/mock_sync.js b/test/mock_sync.js
new file mode 100644
index 0000000..963afa5
--- /dev/null
+++ b/test/mock_sync.js
@@ -0,0 +1,68 @@
+var test = require('tap').test;
+var resolve = require('../');
+
+test('mock', function (t) {
+    t.plan(4);
+    
+    var files = {
+        '/foo/bar/baz.js' : 'beep'
+    };
+    
+    function opts (basedir) {
+        return {
+            basedir : basedir,
+            isFile : function (file) {
+                return files.hasOwnProperty(file)
+            },
+            readFileSync : function (file) {
+                return files[file]
+            }
+        }
+    }
+    
+    t.equal(
+        resolve.sync('./baz', opts('/foo/bar')),
+        '/foo/bar/baz.js'
+    );
+    
+    t.equal(
+        resolve.sync('./baz.js', opts('/foo/bar')),
+        '/foo/bar/baz.js'
+    );
+    
+    t.throws(function () {
+        resolve.sync('baz', opts('/foo/bar'));
+    });
+
+    t.throws(function () {
+        resolve.sync('../baz', opts('/foo/bar'));
+    });
+});
+
+test('mock package', function (t) {
+    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) {
+        return {
+            basedir : basedir,
+            isFile : function (file) {
+                return files.hasOwnProperty(file)
+            },
+            readFileSync : function (file) {
+                return files[file]
+            }
+        }
+    }
+    
+    t.equal(
+        resolve.sync('bar', opts('/foo')),
+        '/foo/node_modules/bar/baz.js'
+    );
+});
diff --git a/test/resolver.js b/test/resolver.js
new file mode 100644
index 0000000..3fb64f5
--- /dev/null
+++ b/test/resolver.js
@@ -0,0 +1,144 @@
+var test = require('tap').test;
+var resolve = require('../');
+
+test('async foo', function (t) {
+    t.plan(3);
+    var dir = __dirname + '/resolver';
+    
+    resolve('./foo', { basedir : dir }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/foo.js');
+    });
+    
+    resolve('./foo.js', { basedir : dir }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/foo.js');
+    });
+    
+    resolve('foo', { basedir : dir }, function (err) {
+        t.equal(err.message, "Cannot find module 'foo'");
+    });
+});
+
+test('bar', function (t) {
+    t.plan(2);
+    var dir = __dirname + '/resolver';
+    
+    resolve('foo', { basedir : dir + '/bar' }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/bar/node_modules/foo/index.js');
+    });
+    
+    resolve('foo', { basedir : dir + '/bar' }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/bar/node_modules/foo/index.js');
+    });
+});
+
+test('baz', function (t) {
+    t.plan(1);
+    var dir = __dirname + '/resolver';
+    
+    resolve('./baz', { basedir : dir }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/baz/quux.js');
+    });
+});
+
+test('biz', function (t) {
+    t.plan(3);
+    var dir = __dirname + '/resolver/biz/node_modules';
+    
+    resolve('./grux', { basedir : dir }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/grux/index.js');
+    });
+    
+    resolve('tiv', { basedir : dir + '/grux' }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/tiv/index.js');
+    });
+    
+    resolve('grux', { basedir : dir + '/tiv' }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/grux/index.js');
+    });
+});
+
+test('normalize', function (t) {
+    t.plan(1);
+    var dir = __dirname + '/resolver/biz/node_modules/grux';
+    
+    resolve('../grux', { basedir : dir }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/index.js');
+    });
+});
+
+test('cup', function (t) {
+    t.plan(3);
+    var dir = __dirname + '/resolver';
+    
+    resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] },
+    function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/cup.coffee');
+    });
+    
+    resolve('./cup.coffee', { basedir : dir }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/cup.coffee');
+    });
+    
+    resolve('./cup', { basedir : dir, extensions : [ '.js' ] },
+    function (err, res) {
+        t.equal(err.message, "Cannot find module './cup'");
+    });
+});
+
+test('mug', function (t) {
+    t.plan(3);
+    var dir = __dirname + '/resolver';
+    
+    resolve('./mug', { basedir : dir }, function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/mug.js');
+    });
+    
+    resolve('./mug', { basedir : dir, extensions : [ '.coffee', '.js' ] },
+    function (err, res) {
+        if (err) t.fail(err);
+        t.equal(res, dir + '/mug.coffee');
+    });
+    
+    resolve('./mug', { basedir : dir, extensions : [ '.js', '.coffee' ] },
+    function (err, res) {
+        t.equal(res, dir + '/mug.js');
+    });
+});
+
+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) {
+        if (err) t.fail(err);
+        t.equal(res, resolverDir + '/other_path/root.js');
+    });
+    
+    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');
+    });
+    
+    resolve('root', { basedir : dir, }, function (err, res) {
+        t.equal(err.message, "Cannot find module 'root'");
+    });
+    
+    resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) {
+        t.equal(err.message, "Cannot find module 'zzz'");
+    });
+});
diff --git a/test/resolver/bar/node_modules/foo/index.js b/test/resolver/bar/node_modules/foo/index.js
new file mode 100644
index 0000000..bd816ea
--- /dev/null
+++ b/test/resolver/bar/node_modules/foo/index.js
@@ -0,0 +1 @@
+module.exports = 1;
diff --git a/test/resolver/baz/doom.js b/test/resolver/baz/doom.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/resolver/baz/package.json b/test/resolver/baz/package.json
new file mode 100644
index 0000000..6b81dcd
--- /dev/null
+++ b/test/resolver/baz/package.json
@@ -0,0 +1,3 @@
+{
+    "main" : "quux.js"
+}
diff --git a/test/resolver/baz/quux.js b/test/resolver/baz/quux.js
new file mode 100644
index 0000000..bd816ea
--- /dev/null
+++ b/test/resolver/baz/quux.js
@@ -0,0 +1 @@
+module.exports = 1;
diff --git a/test/resolver/biz/node_modules/grux/index.js b/test/resolver/biz/node_modules/grux/index.js
new file mode 100644
index 0000000..4996055
--- /dev/null
+++ b/test/resolver/biz/node_modules/grux/index.js
@@ -0,0 +1 @@
+module.exports = require('tiv') * 100;
diff --git a/test/resolver/biz/node_modules/tiv/index.js b/test/resolver/biz/node_modules/tiv/index.js
new file mode 100644
index 0000000..690aad3
--- /dev/null
+++ b/test/resolver/biz/node_modules/tiv/index.js
@@ -0,0 +1 @@
+module.exports = 3;
diff --git a/test/resolver/cup.coffee b/test/resolver/cup.coffee
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/test/resolver/cup.coffee
@@ -0,0 +1 @@
+
diff --git a/test/resolver/foo.js b/test/resolver/foo.js
new file mode 100644
index 0000000..bd816ea
--- /dev/null
+++ b/test/resolver/foo.js
@@ -0,0 +1 @@
+module.exports = 1;
diff --git a/test/resolver/mug.coffee b/test/resolver/mug.coffee
new file mode 100644
index 0000000..e69de29
diff --git a/test/resolver/mug.js b/test/resolver/mug.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/resolver/other_path/lib/other-lib.js b/test/resolver/other_path/lib/other-lib.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/resolver/other_path/root.js b/test/resolver/other_path/root.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/resolver_sync.js b/test/resolver_sync.js
new file mode 100644
index 0000000..80f5aa5
--- /dev/null
+++ b/test/resolver_sync.js
@@ -0,0 +1,157 @@
+var test = require('tap').test;
+var resolve = require('../');
+
+test('foo', function (t) {
+    var dir = __dirname + '/resolver';
+    
+    t.equal(
+        resolve.sync('./foo', { basedir : dir }),
+        dir + '/foo.js'
+    );
+    
+    t.equal(
+        resolve.sync('./foo.js', { basedir : dir }),
+        dir + '/foo.js'
+    );
+    
+    t.throws(function () {
+        resolve.sync('foo', { basedir : dir });
+    });
+    
+    t.end();
+});
+
+test('bar', function (t) {
+    var dir = __dirname + '/resolver';
+    
+    t.equal(
+        resolve.sync('foo', { basedir : dir + '/bar' }),
+        dir + '/bar/node_modules/foo/index.js'
+    );
+    t.end();
+});
+
+test('baz', function (t) {
+    var dir = __dirname + '/resolver';
+    
+    t.equal(
+        resolve.sync('./baz', { basedir : dir }),
+        dir + '/baz/quux.js'
+    );
+    t.end();
+});
+
+test('biz', function (t) {
+    var dir = __dirname + '/resolver/biz/node_modules';
+    t.equal(
+        resolve.sync('./grux', { basedir : dir }),
+        dir + '/grux/index.js'
+    );
+    
+    t.equal(
+        resolve.sync('tiv', { basedir : dir + '/grux' }),
+        dir + '/tiv/index.js'
+    );
+    
+    t.equal(
+        resolve.sync('grux', { basedir : dir + '/tiv' }),
+        dir + '/grux/index.js'
+    );
+    t.end();
+});
+
+test('normalize', function (t) {
+    var dir = __dirname + '/resolver/biz/node_modules/grux';
+    t.equal(
+        resolve.sync('../grux', { basedir : dir }),
+        dir + '/index.js'
+    );
+    t.end();
+});
+
+test('cup', function (t) {
+    var dir = __dirname + '/resolver';
+    t.equal(
+        resolve.sync('./cup', {
+            basedir : dir,
+            extensions : [ '.js', '.coffee' ]
+        }),
+        dir + '/cup.coffee'
+    );
+    
+    t.equal(
+        resolve.sync('./cup.coffee', {
+            basedir : dir
+        }),
+        dir + '/cup.coffee'
+    );
+    
+    t.throws(function () {
+        resolve.sync('./cup', {
+            basedir : dir,
+            extensions : [ '.js' ]
+        })
+    });
+    
+    t.end();
+});
+
+test('mug', function (t) {
+    var dir = __dirname + '/resolver';
+    t.equal(
+        resolve.sync('./mug', { basedir : dir }),
+        dir + '/mug.js'
+    );
+    
+    t.equal(
+        resolve.sync('./mug', {
+            basedir : dir,
+            extensions : [ '.coffee', '.js' ]
+        }),
+        dir + '/mug.coffee'
+    );
+    
+    t.equal(
+        resolve.sync('./mug', {
+            basedir : dir,
+            extensions : [ '.js', '.coffee' ]
+        }),
+        dir + '/mug.js'
+    );
+    
+    t.end();
+});
+
+test('other path', function (t) {
+    var resolverDir = __dirname + '/resolver';
+    var dir = resolverDir + '/bar';
+    var otherDir = resolverDir + '/other_path';
+
+    var path = require('path');
+    
+    t.equal(
+        resolve.sync('root', {
+            basedir : dir,
+            paths: [otherDir] }),
+        resolverDir + '/other_path/root.js'
+    );
+    
+    t.equal(
+        resolve.sync('lib/other-lib', {
+            basedir : dir,
+            paths: [otherDir] }),
+        resolverDir + '/other_path/lib/other-lib.js'
+    );
+
+    t.throws(function () {
+        resolve.sync('root', { basedir : dir, });
+    });
+    
+    t.throws(function () {
+        resolve.sync('zzz', {
+            basedir : dir,
+            paths: [otherDir] });
+    });
+    
+    t.end();
+});

-- 
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