source: trip-planner-front/node_modules/resolve/test/node-modules-paths.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.0 KB
Line 
1var test = require('tape');
2var path = require('path');
3var parse = path.parse || require('path-parse');
4var keys = require('object-keys');
5
6var nodeModulesPaths = require('../lib/node-modules-paths');
7
8var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) {
9 var moduleDirs = [].concat(moduleDirectories || 'node_modules');
10 if (paths) {
11 for (var k = 0; k < paths.length; ++k) {
12 moduleDirs.push(path.basename(paths[k]));
13 }
14 }
15
16 var foundModuleDirs = {};
17 var uniqueDirs = {};
18 var parsedDirs = {};
19 for (var i = 0; i < dirs.length; ++i) {
20 var parsed = parse(dirs[i]);
21 if (!foundModuleDirs[parsed.base]) { foundModuleDirs[parsed.base] = 0; }
22 foundModuleDirs[parsed.base] += 1;
23 parsedDirs[parsed.dir] = true;
24 uniqueDirs[dirs[i]] = true;
25 }
26 t.equal(keys(parsedDirs).length >= start.split(path.sep).length, true, 'there are >= dirs than "start" has');
27 var foundModuleDirNames = keys(foundModuleDirs);
28 t.deepEqual(foundModuleDirNames, moduleDirs, 'all desired module dirs were found');
29 t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique');
30
31 var counts = {};
32 for (var j = 0; j < foundModuleDirNames.length; ++j) {
33 counts[foundModuleDirs[j]] = true;
34 }
35 t.equal(keys(counts).length, 1, 'all found module directories had the same count');
36};
37
38test('node-modules-paths', function (t) {
39 t.test('no options', function (t) {
40 var start = path.join(__dirname, 'resolver');
41 var dirs = nodeModulesPaths(start);
42
43 verifyDirs(t, start, dirs);
44
45 t.end();
46 });
47
48 t.test('empty options', function (t) {
49 var start = path.join(__dirname, 'resolver');
50 var dirs = nodeModulesPaths(start, {});
51
52 verifyDirs(t, start, dirs);
53
54 t.end();
55 });
56
57 t.test('with paths=array option', function (t) {
58 var start = path.join(__dirname, 'resolver');
59 var paths = ['a', 'b'];
60 var dirs = nodeModulesPaths(start, { paths: paths });
61
62 verifyDirs(t, start, dirs, null, paths);
63
64 t.end();
65 });
66
67 t.test('with paths=function option', function (t) {
68 var paths = function paths(request, absoluteStart, getNodeModulesDirs, opts) {
69 return getNodeModulesDirs().concat(path.join(absoluteStart, 'not node modules', request));
70 };
71
72 var start = path.join(__dirname, 'resolver');
73 var dirs = nodeModulesPaths(start, { paths: paths }, 'pkg');
74
75 verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules', 'pkg')]);
76
77 t.end();
78 });
79
80 t.test('with paths=function skipping node modules resolution', function (t) {
81 var paths = function paths(request, absoluteStart, getNodeModulesDirs, opts) {
82 return [];
83 };
84 var start = path.join(__dirname, 'resolver');
85 var dirs = nodeModulesPaths(start, { paths: paths });
86 t.deepEqual(dirs, [], 'no node_modules was computed');
87 t.end();
88 });
89
90 t.test('with moduleDirectory option', function (t) {
91 var start = path.join(__dirname, 'resolver');
92 var moduleDirectory = 'not node modules';
93 var dirs = nodeModulesPaths(start, { moduleDirectory: moduleDirectory });
94
95 verifyDirs(t, start, dirs, moduleDirectory);
96
97 t.end();
98 });
99
100 t.test('with 1 moduleDirectory and paths options', function (t) {
101 var start = path.join(__dirname, 'resolver');
102 var paths = ['a', 'b'];
103 var moduleDirectory = 'not node modules';
104 var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectory });
105
106 verifyDirs(t, start, dirs, moduleDirectory, paths);
107
108 t.end();
109 });
110
111 t.test('with 1+ moduleDirectory and paths options', function (t) {
112 var start = path.join(__dirname, 'resolver');
113 var paths = ['a', 'b'];
114 var moduleDirectories = ['not node modules', 'other modules'];
115 var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
116
117 verifyDirs(t, start, dirs, moduleDirectories, paths);
118
119 t.end();
120 });
121
122 t.test('combine paths correctly on Windows', function (t) {
123 var start = 'C:\\Users\\username\\myProject\\src';
124 var paths = [];
125 var moduleDirectories = ['node_modules', start];
126 var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
127
128 t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
129
130 t.end();
131 });
132
133 t.test('combine paths correctly on non-Windows', { skip: process.platform === 'win32' }, function (t) {
134 var start = '/Users/username/git/myProject/src';
135 var paths = [];
136 var moduleDirectories = ['node_modules', '/Users/username/git/myProject/src'];
137 var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
138
139 t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
140
141 t.end();
142 });
143});
Note: See TracBrowser for help on using the repository browser.