1 | var path = require('path');
|
2 | var test = require('tape');
|
3 | var resolve = require('../');
|
4 |
|
5 | test('mock', function (t) {
|
6 | t.plan(4);
|
7 |
|
8 | var files = {};
|
9 | files[path.resolve('/foo/bar/baz.js')] = 'beep';
|
10 |
|
11 | var dirs = {};
|
12 | dirs[path.resolve('/foo/bar')] = true;
|
13 |
|
14 | function opts(basedir) {
|
15 | return {
|
16 | basedir: path.resolve(basedir),
|
17 | isFile: function (file) {
|
18 | return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
19 | },
|
20 | isDirectory: function (dir) {
|
21 | return !!dirs[path.resolve(dir)];
|
22 | },
|
23 | readFileSync: function (file) {
|
24 | return files[path.resolve(file)];
|
25 | },
|
26 | realpathSync: function (file) {
|
27 | return file;
|
28 | }
|
29 | };
|
30 | }
|
31 |
|
32 | t.equal(
|
33 | resolve.sync('./baz', opts('/foo/bar')),
|
34 | path.resolve('/foo/bar/baz.js')
|
35 | );
|
36 |
|
37 | t.equal(
|
38 | resolve.sync('./baz.js', opts('/foo/bar')),
|
39 | path.resolve('/foo/bar/baz.js')
|
40 | );
|
41 |
|
42 | t.throws(function () {
|
43 | resolve.sync('baz', opts('/foo/bar'));
|
44 | });
|
45 |
|
46 | t.throws(function () {
|
47 | resolve.sync('../baz', opts('/foo/bar'));
|
48 | });
|
49 | });
|
50 |
|
51 | test('mock package', function (t) {
|
52 | t.plan(1);
|
53 |
|
54 | var files = {};
|
55 | files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
|
56 | files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
|
57 | main: './baz.js'
|
58 | });
|
59 |
|
60 | var dirs = {};
|
61 | dirs[path.resolve('/foo')] = true;
|
62 | dirs[path.resolve('/foo/node_modules')] = true;
|
63 |
|
64 | function opts(basedir) {
|
65 | return {
|
66 | basedir: path.resolve(basedir),
|
67 | isFile: function (file) {
|
68 | return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
69 | },
|
70 | isDirectory: function (dir) {
|
71 | return !!dirs[path.resolve(dir)];
|
72 | },
|
73 | readFileSync: function (file) {
|
74 | return files[path.resolve(file)];
|
75 | },
|
76 | realpathSync: function (file) {
|
77 | return file;
|
78 | }
|
79 | };
|
80 | }
|
81 |
|
82 | t.equal(
|
83 | resolve.sync('bar', opts('/foo')),
|
84 | path.resolve('/foo/node_modules/bar/baz.js')
|
85 | );
|
86 | });
|
87 |
|
88 | test('symlinked', function (t) {
|
89 | t.plan(2);
|
90 |
|
91 | var files = {};
|
92 | files[path.resolve('/foo/bar/baz.js')] = 'beep';
|
93 | files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
|
94 |
|
95 | var dirs = {};
|
96 | dirs[path.resolve('/foo/bar')] = true;
|
97 | dirs[path.resolve('/foo/bar/symlinked')] = true;
|
98 |
|
99 | function opts(basedir) {
|
100 | return {
|
101 | preserveSymlinks: false,
|
102 | basedir: path.resolve(basedir),
|
103 | isFile: function (file) {
|
104 | return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
105 | },
|
106 | isDirectory: function (dir) {
|
107 | return !!dirs[path.resolve(dir)];
|
108 | },
|
109 | readFileSync: function (file) {
|
110 | return files[path.resolve(file)];
|
111 | },
|
112 | realpathSync: function (file) {
|
113 | var resolved = path.resolve(file);
|
114 |
|
115 | if (resolved.indexOf('symlinked') >= 0) {
|
116 | return resolved;
|
117 | }
|
118 |
|
119 | var ext = path.extname(resolved);
|
120 |
|
121 | if (ext) {
|
122 | var dir = path.dirname(resolved);
|
123 | var base = path.basename(resolved);
|
124 | return path.join(dir, 'symlinked', base);
|
125 | } else {
|
126 | return path.join(resolved, 'symlinked');
|
127 | }
|
128 | }
|
129 | };
|
130 | }
|
131 |
|
132 | t.equal(
|
133 | resolve.sync('./baz', opts('/foo/bar')),
|
134 | path.resolve('/foo/bar/symlinked/baz.js')
|
135 | );
|
136 |
|
137 | t.equal(
|
138 | resolve.sync('./baz.js', opts('/foo/bar')),
|
139 | path.resolve('/foo/bar/symlinked/baz.js')
|
140 | );
|
141 | });
|
142 |
|
143 | test('readPackageSync', function (t) {
|
144 | t.plan(3);
|
145 |
|
146 | var files = {};
|
147 | files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
|
148 | files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
|
149 | main: './baz.js'
|
150 | });
|
151 | files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
|
152 |
|
153 | var dirs = {};
|
154 | dirs[path.resolve('/foo')] = true;
|
155 | dirs[path.resolve('/foo/node_modules')] = true;
|
156 |
|
157 | function opts(basedir, useReadPackage) {
|
158 | return {
|
159 | basedir: path.resolve(basedir),
|
160 | isFile: function (file) {
|
161 | return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
162 | },
|
163 | isDirectory: function (dir) {
|
164 | return !!dirs[path.resolve(dir)];
|
165 | },
|
166 | readFileSync: useReadPackage ? null : function (file) {
|
167 | return files[path.resolve(file)];
|
168 | },
|
169 | realpathSync: function (file) {
|
170 | return file;
|
171 | }
|
172 | };
|
173 | }
|
174 | t.test('with readFile', function (st) {
|
175 | st.plan(1);
|
176 |
|
177 | st.equal(
|
178 | resolve.sync('bar', opts('/foo')),
|
179 | path.resolve('/foo/node_modules/bar/baz.js')
|
180 | );
|
181 | });
|
182 |
|
183 | var readPackageSync = function (readFileSync, file) {
|
184 | if (file.indexOf(path.join('bar', 'package.json')) >= 0) {
|
185 | return { main: './something-else.js' };
|
186 | } else {
|
187 | return JSON.parse(files[path.resolve(file)]);
|
188 | }
|
189 | };
|
190 |
|
191 | t.test('with readPackage', function (st) {
|
192 | st.plan(1);
|
193 |
|
194 | var options = opts('/foo');
|
195 | delete options.readFileSync;
|
196 | options.readPackageSync = readPackageSync;
|
197 |
|
198 | st.equal(
|
199 | resolve.sync('bar', options),
|
200 | path.resolve('/foo/node_modules/bar/something-else.js')
|
201 | );
|
202 | });
|
203 |
|
204 | t.test('with readFile and readPackage', function (st) {
|
205 | st.plan(1);
|
206 |
|
207 | var options = opts('/foo');
|
208 | options.readPackageSync = readPackageSync;
|
209 | st.throws(
|
210 | function () { resolve.sync('bar', options); },
|
211 | TypeError,
|
212 | 'errors when both readFile and readPackage are provided'
|
213 | );
|
214 | });
|
215 | });
|
216 |
|