| 1 |
var fs = require('fs'); |
| 2 |
var path = require('path'); |
| 3 |
var test = require('tape'); |
| 4 |
var resolve = require('../'); |
| 5 |
|
| 6 |
test('$NODE_PATH', function (t) { |
| 7 |
t.plan(8); |
| 8 |
|
| 9 |
function isDir(dir, cb) { |
| 10 |
if (dir === '/node_path' || dir === 'node_path/x') { |
| 11 |
return cb(null, true); |
| 12 |
} |
| 13 |
fs.stat(dir, function (err, stat) { |
| 14 |
if (!err) { |
| 15 |
return cb(null, stat.isDirectory()); |
| 16 |
} |
| 17 |
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false); |
| 18 |
return cb(err); |
| 19 |
}); |
| 20 |
} |
| 21 |
|
| 22 |
resolve('aaa', { |
| 23 |
paths: [ |
| 24 |
path.join(__dirname, '/node_path/x'), |
| 25 |
path.join(__dirname, '/node_path/y') |
| 26 |
], |
| 27 |
basedir: __dirname, |
| 28 |
isDirectory: isDir |
| 29 |
}, function (err, res) { |
| 30 |
t.error(err); |
| 31 |
t.equal(res, path.join(__dirname, '/node_path/x/aaa/index.js'), 'aaa resolves'); |
| 32 |
}); |
| 33 |
|
| 34 |
resolve('bbb', { |
| 35 |
paths: [ |
| 36 |
path.join(__dirname, '/node_path/x'), |
| 37 |
path.join(__dirname, '/node_path/y') |
| 38 |
], |
| 39 |
basedir: __dirname, |
| 40 |
isDirectory: isDir |
| 41 |
}, function (err, res) { |
| 42 |
t.error(err); |
| 43 |
t.equal(res, path.join(__dirname, '/node_path/y/bbb/index.js'), 'bbb resolves'); |
| 44 |
}); |
| 45 |
|
| 46 |
resolve('ccc', { |
| 47 |
paths: [ |
| 48 |
path.join(__dirname, '/node_path/x'), |
| 49 |
path.join(__dirname, '/node_path/y') |
| 50 |
], |
| 51 |
basedir: __dirname, |
| 52 |
isDirectory: isDir |
| 53 |
}, function (err, res) { |
| 54 |
t.error(err); |
| 55 |
t.equal(res, path.join(__dirname, '/node_path/x/ccc/index.js'), 'ccc resolves'); |
| 56 |
}); |
| 57 |
|
| 58 |
// ensure that relative paths still resolve against the regular `node_modules` correctly |
| 59 |
resolve('tap', { |
| 60 |
paths: [ |
| 61 |
'node_path' |
| 62 |
], |
| 63 |
basedir: path.join(__dirname, 'node_path/x'), |
| 64 |
isDirectory: isDir |
| 65 |
}, function (err, res) { |
| 66 |
var root = require('tap/package.json').main; // eslint-disable-line global-require |
| 67 |
t.error(err); |
| 68 |
t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap', root), 'tap resolves'); |
| 69 |
}); |
| 70 |
}); |