|
| 1 | +var test = require('tape'); |
| 2 | +var path = require('path'); |
| 3 | + |
| 4 | +var getAllNodeModulePathsUpwards = function (start, moduleDirectory) { |
| 5 | + if (!moduleDirectory) { moduleDirectory = 'node_modules'; } |
| 6 | + var currentPath = start; |
| 7 | + var expectedDirs = ['/' + path.join(currentPath, moduleDirectory)]; |
| 8 | + while (currentPath && currentPath !== '/') { |
| 9 | + currentPath = path.join(currentPath, '../'); |
| 10 | + expectedDirs.push('/' + path.join(currentPath, moduleDirectory)); |
| 11 | + } |
| 12 | + return expectedDirs; |
| 13 | +} |
| 14 | + |
| 15 | +var nodeModulesPaths = require('../lib/node-modules-paths'); |
| 16 | + |
| 17 | +test('node-modules-paths', function (t) { |
| 18 | + t.test('no options', function (t) { |
| 19 | + var start = path.join(__dirname, 'resolver'); |
| 20 | + var dirs = nodeModulesPaths(start); |
| 21 | + |
| 22 | + var expectedDirs = getAllNodeModulePathsUpwards(start); |
| 23 | + |
| 24 | + t.deepEqual(dirs, expectedDirs); |
| 25 | + |
| 26 | + t.end(); |
| 27 | + }); |
| 28 | + |
| 29 | + t.test('empty options', function (t) { |
| 30 | + var start = path.join(__dirname, 'resolver'); |
| 31 | + var dirs = nodeModulesPaths(start, {}); |
| 32 | + |
| 33 | + var expectedDirs = getAllNodeModulePathsUpwards(start); |
| 34 | + |
| 35 | + t.deepEqual(dirs, expectedDirs); |
| 36 | + |
| 37 | + t.end(); |
| 38 | + }); |
| 39 | + |
| 40 | + t.test('with paths option', function (t) { |
| 41 | + var start = path.join(__dirname, 'resolver'); |
| 42 | + var paths = ['a', 'b']; |
| 43 | + var dirs = nodeModulesPaths(start, { paths: paths }); |
| 44 | + |
| 45 | + var expectedDirs = getAllNodeModulePathsUpwards(start); |
| 46 | + |
| 47 | + t.deepEqual(dirs, expectedDirs.concat(paths)); |
| 48 | + |
| 49 | + t.end(); |
| 50 | + }); |
| 51 | + |
| 52 | + t.test('with moduleDirectory option', function (t) { |
| 53 | + var start = path.join(__dirname, 'resolver'); |
| 54 | + var paths = ['a', 'b']; |
| 55 | + var dirs = nodeModulesPaths(start, { paths: paths }); |
| 56 | + |
| 57 | + var expectedDirs = getAllNodeModulePathsUpwards(start); |
| 58 | + |
| 59 | + t.deepEqual(dirs, expectedDirs.concat(paths)); |
| 60 | + |
| 61 | + t.end(); |
| 62 | + }); |
| 63 | + |
| 64 | + t.test('with moduleDirectory and paths options', function (t) { |
| 65 | + var start = path.join(__dirname, 'resolver'); |
| 66 | + var paths = ['a', 'b']; |
| 67 | + var moduleDirectory = 'not node modules'; |
| 68 | + var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectory }); |
| 69 | + |
| 70 | + var expectedDirs = getAllNodeModulePathsUpwards(start, moduleDirectory); |
| 71 | + |
| 72 | + t.deepEqual(dirs, expectedDirs.concat(paths)); |
| 73 | + |
| 74 | + t.end(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments