resolver.js (8754B)
1 var path = require('path'); 2 var test = require('tape'); 3 var resolve = require('../'); 4 5 test('async foo', function (t) { 6 t.plan(9); 7 var dir = __dirname + '/resolver'; 8 9 resolve('./foo', { basedir : dir }, function (err, res, pkg) { 10 if (err) t.fail(err); 11 t.equal(res, dir + '/foo.js'); 12 t.equal(pkg.name, 'resolve'); 13 }); 14 15 resolve('./foo.js', { basedir : dir }, function (err, res, pkg) { 16 if (err) t.fail(err); 17 t.equal(res, dir + '/foo.js'); 18 t.equal(pkg.name, 'resolve'); 19 }); 20 21 resolve('./foo', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) { 22 if (err) t.fail(err); 23 t.equal(res, dir + '/foo.js'); 24 t.equal(pkg.main, 'resolver'); 25 }); 26 27 resolve('./foo.js', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) { 28 if (err) t.fail(err); 29 t.equal(res, dir + '/foo.js'); 30 t.equal(pkg.main, 'resolver'); 31 }); 32 33 resolve('foo', { basedir : dir }, function (err) { 34 t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'"); 35 }); 36 }); 37 38 test('bar', function (t) { 39 t.plan(6); 40 var dir = __dirname + '/resolver'; 41 42 resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) { 43 if (err) t.fail(err); 44 t.equal(res, dir + '/bar/node_modules/foo/index.js'); 45 t.equal(pkg, undefined); 46 }); 47 48 resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) { 49 if (err) t.fail(err); 50 t.equal(res, dir + '/bar/node_modules/foo/index.js'); 51 t.equal(pkg, undefined); 52 }); 53 54 resolve('foo', { basedir : dir + '/bar', package: { main: 'bar' } }, function (err, res, pkg) { 55 if (err) t.fail(err); 56 t.equal(res, dir + '/bar/node_modules/foo/index.js'); 57 t.equal(pkg, undefined); 58 }); 59 }); 60 61 test('baz', function (t) { 62 t.plan(4); 63 var dir = __dirname + '/resolver'; 64 65 resolve('./baz', { basedir : dir }, function (err, res, pkg) { 66 if (err) t.fail(err); 67 t.equal(res, dir + '/baz/quux.js'); 68 t.equal(pkg.main, 'quux.js'); 69 }); 70 71 resolve('./baz', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) { 72 if (err) t.fail(err); 73 t.equal(res, dir + '/baz/quux.js'); 74 t.equal(pkg.main, 'quux.js'); 75 }); 76 }); 77 78 test('biz', function (t) { 79 t.plan(24); 80 var dir = __dirname + '/resolver/biz/node_modules'; 81 82 resolve('./grux', { basedir : dir }, function (err, res, pkg) { 83 if (err) t.fail(err); 84 t.equal(res, dir + '/grux/index.js'); 85 t.equal(pkg, undefined); 86 }); 87 88 resolve('./grux', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) { 89 if (err) t.fail(err); 90 t.equal(res, dir + '/grux/index.js'); 91 t.equal(pkg.main, 'biz'); 92 }); 93 94 resolve('./garply', { basedir : dir }, function (err, res, pkg) { 95 if (err) t.fail(err); 96 t.equal(res, dir + '/garply/lib/index.js'); 97 t.equal(pkg.main, './lib'); 98 }); 99 100 resolve('./garply', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) { 101 if (err) t.fail(err); 102 t.equal(res, dir + '/garply/lib/index.js'); 103 t.equal(pkg.main, './lib'); 104 }); 105 106 resolve('tiv', { basedir : dir + '/grux' }, function (err, res, pkg) { 107 if (err) t.fail(err); 108 t.equal(res, dir + '/tiv/index.js'); 109 t.equal(pkg, undefined); 110 }); 111 112 resolve('tiv', { basedir : dir + '/grux', package: { main: 'grux' } }, function (err, res, pkg) { 113 if (err) t.fail(err); 114 t.equal(res, dir + '/tiv/index.js'); 115 t.equal(pkg, undefined); 116 }); 117 118 resolve('tiv', { basedir : dir + '/garply' }, function (err, res, pkg) { 119 if (err) t.fail(err); 120 t.equal(res, dir + '/tiv/index.js'); 121 t.equal(pkg, undefined); 122 }); 123 124 resolve('tiv', { basedir : dir + '/garply', package: { main: './lib' } }, function (err, res, pkg) { 125 if (err) t.fail(err); 126 t.equal(res, dir + '/tiv/index.js'); 127 t.equal(pkg, undefined); 128 }); 129 130 resolve('grux', { basedir : dir + '/tiv' }, function (err, res, pkg) { 131 if (err) t.fail(err); 132 t.equal(res, dir + '/grux/index.js'); 133 t.equal(pkg, undefined); 134 }); 135 136 resolve('grux', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) { 137 if (err) t.fail(err); 138 t.equal(res, dir + '/grux/index.js'); 139 t.equal(pkg, undefined); 140 }); 141 142 resolve('garply', { basedir : dir + '/tiv' }, function (err, res, pkg) { 143 if (err) t.fail(err); 144 t.equal(res, dir + '/garply/lib/index.js'); 145 t.equal(pkg.main, './lib'); 146 }); 147 148 resolve('garply', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) { 149 if (err) t.fail(err); 150 t.equal(res, dir + '/garply/lib/index.js'); 151 t.equal(pkg.main, './lib'); 152 }); 153 }); 154 155 test('quux', function (t) { 156 t.plan(2); 157 var dir = __dirname + '/resolver/quux'; 158 159 resolve('./foo', { basedir : dir, package: { main: 'quux' } }, function (err, res, pkg) { 160 if (err) t.fail(err); 161 t.equal(res, dir + '/foo/index.js'); 162 t.equal(pkg.main, 'quux'); 163 }); 164 }); 165 166 test('normalize', function (t) { 167 t.plan(2); 168 var dir = __dirname + '/resolver/biz/node_modules/grux'; 169 170 resolve('../grux', { basedir : dir }, function (err, res, pkg) { 171 if (err) t.fail(err); 172 t.equal(res, dir + '/index.js'); 173 t.equal(pkg, undefined); 174 }); 175 }); 176 177 test('cup', function (t) { 178 t.plan(3); 179 var dir = __dirname + '/resolver'; 180 181 resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] }, 182 function (err, res) { 183 if (err) t.fail(err); 184 t.equal(res, dir + '/cup.coffee'); 185 }); 186 187 resolve('./cup.coffee', { basedir : dir }, function (err, res) { 188 if (err) t.fail(err); 189 t.equal(res, dir + '/cup.coffee'); 190 }); 191 192 resolve('./cup', { basedir : dir, extensions : [ '.js' ] }, 193 function (err, res) { 194 t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'"); 195 }); 196 }); 197 198 test('mug', function (t) { 199 t.plan(3); 200 var dir = __dirname + '/resolver'; 201 202 resolve('./mug', { basedir : dir }, function (err, res) { 203 if (err) t.fail(err); 204 t.equal(res, dir + '/mug.js'); 205 }); 206 207 resolve('./mug', { basedir : dir, extensions : [ '.coffee', '.js' ] }, 208 function (err, res) { 209 if (err) t.fail(err); 210 t.equal(res, dir + '/mug.coffee'); 211 }); 212 213 resolve('./mug', { basedir : dir, extensions : [ '.js', '.coffee' ] }, 214 function (err, res) { 215 t.equal(res, dir + '/mug.js'); 216 }); 217 }); 218 219 test('other path', function (t) { 220 t.plan(4); 221 var resolverDir = __dirname + '/resolver'; 222 var dir = resolverDir + '/bar'; 223 var otherDir = resolverDir + '/other_path'; 224 225 resolve('root', { basedir : dir, paths: [otherDir] }, function (err, res) { 226 if (err) t.fail(err); 227 t.equal(res, resolverDir + '/other_path/root.js'); 228 }); 229 230 resolve('lib/other-lib', { basedir : dir, paths: [otherDir] }, 231 function (err, res) { 232 if (err) t.fail(err); 233 t.equal(res, resolverDir + '/other_path/lib/other-lib.js'); 234 }); 235 236 resolve('root', { basedir : dir, }, function (err, res) { 237 t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'"); 238 }); 239 240 resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) { 241 t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'"); 242 }); 243 }); 244 245 test('incorrect main', function (t) { 246 t.plan(1) 247 248 var resolverDir = __dirname + '/resolver'; 249 var dir = resolverDir + '/incorrect_main'; 250 251 resolve('./incorrect_main', { basedir : resolverDir }, function (err, res, pkg) { 252 if (err) t.fail(err); 253 t.equal(res, dir + '/index.js'); 254 }); 255 }); 256 257 test('without basedir', function (t) { 258 t.plan(1); 259 260 var dir = __dirname + '/resolver/without_basedir'; 261 var tester = require(dir + '/main.js'); 262 263 tester(t, function (err, res, pkg){ 264 if (err) { 265 t.fail(err); 266 } else { 267 t.equal(res, dir + '/node_modules/mymodule.js'); 268 } 269 }); 270 }); 271 272 test('#25: node modules with the same name as node stdlib modules', function (t) { 273 t.plan(1); 274 275 var resolverDir = __dirname + '/resolver/punycode'; 276 277 resolve('punycode', { basedir : resolverDir }, function (err, res, pkg) { 278 if (err) t.fail(err); 279 t.equal(res, resolverDir + '/node_modules/punycode/index.js'); 280 }); 281 });