.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / resolve / test / resolver_sync.js
1 var path = require('path');
2 var test = require('tape');
3 var resolve = require('../');
4
5 test('foo', function (t) {
6     var dir = path.join(__dirname, 'resolver');
7
8     t.equal(
9         resolve.sync('./foo', { basedir: dir }),
10         path.join(dir, 'foo.js')
11     );
12
13     t.equal(
14         resolve.sync('./foo.js', { basedir: dir }),
15         path.join(dir, 'foo.js')
16     );
17
18     t.equal(
19         resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
20         path.join(dir, 'foo.js')
21     );
22
23     t.throws(function () {
24         resolve.sync('foo', { basedir: dir });
25     });
26
27     // Test that filename is reported as the "from" value when passed.
28     t.throws(
29         function () {
30             resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
31         },
32         {
33             name: 'Error',
34             message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
35         }
36     );
37
38     t.end();
39 });
40
41 test('bar', function (t) {
42     var dir = path.join(__dirname, 'resolver');
43
44     t.equal(
45         resolve.sync('foo', { basedir: path.join(dir, 'bar') }),
46         path.join(dir, 'bar/node_modules/foo/index.js')
47     );
48     t.end();
49 });
50
51 test('baz', function (t) {
52     var dir = path.join(__dirname, 'resolver');
53
54     t.equal(
55         resolve.sync('./baz', { basedir: dir }),
56         path.join(dir, 'baz/quux.js')
57     );
58     t.end();
59 });
60
61 test('biz', function (t) {
62     var dir = path.join(__dirname, 'resolver/biz/node_modules');
63     t.equal(
64         resolve.sync('./grux', { basedir: dir }),
65         path.join(dir, 'grux/index.js')
66     );
67
68     t.equal(
69         resolve.sync('tiv', { basedir: path.join(dir, 'grux') }),
70         path.join(dir, 'tiv/index.js')
71     );
72
73     t.equal(
74         resolve.sync('grux', { basedir: path.join(dir, 'tiv') }),
75         path.join(dir, 'grux/index.js')
76     );
77     t.end();
78 });
79
80 test('normalize', function (t) {
81     var dir = path.join(__dirname, 'resolver/biz/node_modules/grux');
82     t.equal(
83         resolve.sync('../grux', { basedir: dir }),
84         path.join(dir, 'index.js')
85     );
86     t.end();
87 });
88
89 test('cup', function (t) {
90     var dir = path.join(__dirname, 'resolver');
91     t.equal(
92         resolve.sync('./cup', {
93             basedir: dir,
94             extensions: ['.js', '.coffee']
95         }),
96         path.join(dir, 'cup.coffee')
97     );
98
99     t.equal(
100         resolve.sync('./cup.coffee', { basedir: dir }),
101         path.join(dir, 'cup.coffee')
102     );
103
104     t.throws(function () {
105         resolve.sync('./cup', {
106             basedir: dir,
107             extensions: ['.js']
108         });
109     });
110
111     t.end();
112 });
113
114 test('mug', function (t) {
115     var dir = path.join(__dirname, 'resolver');
116     t.equal(
117         resolve.sync('./mug', { basedir: dir }),
118         path.join(dir, 'mug.js')
119     );
120
121     t.equal(
122         resolve.sync('./mug', {
123             basedir: dir,
124             extensions: ['.coffee', '.js']
125         }),
126         path.join(dir, 'mug.coffee')
127     );
128
129     t.equal(
130         resolve.sync('./mug', {
131             basedir: dir,
132             extensions: ['.js', '.coffee']
133         }),
134         path.join(dir, 'mug.js')
135     );
136
137     t.end();
138 });
139
140 test('other path', function (t) {
141     var resolverDir = path.join(__dirname, 'resolver');
142     var dir = path.join(resolverDir, 'bar');
143     var otherDir = path.join(resolverDir, 'other_path');
144
145     t.equal(
146         resolve.sync('root', {
147             basedir: dir,
148             paths: [otherDir]
149         }),
150         path.join(resolverDir, 'other_path/root.js')
151     );
152
153     t.equal(
154         resolve.sync('lib/other-lib', {
155             basedir: dir,
156             paths: [otherDir]
157         }),
158         path.join(resolverDir, 'other_path/lib/other-lib.js')
159     );
160
161     t.throws(function () {
162         resolve.sync('root', { basedir: dir });
163     });
164
165     t.throws(function () {
166         resolve.sync('zzz', {
167             basedir: dir,
168             paths: [otherDir]
169         });
170     });
171
172     t.end();
173 });
174
175 test('path iterator', function (t) {
176     var resolverDir = path.join(__dirname, 'resolver');
177
178     var exactIterator = function (x, start, getPackageCandidates, opts) {
179         return [path.join(resolverDir, x)];
180     };
181
182     t.equal(
183         resolve.sync('baz', { packageIterator: exactIterator }),
184         path.join(resolverDir, 'baz/quux.js')
185     );
186
187     t.end();
188 });
189
190 test('incorrect main', function (t) {
191     var resolverDir = path.join(__dirname, 'resolver');
192     var dir = path.join(resolverDir, 'incorrect_main');
193
194     t.equal(
195         resolve.sync('./incorrect_main', { basedir: resolverDir }),
196         path.join(dir, 'index.js')
197     );
198
199     t.end();
200 });
201
202 var stubStatSync = function stubStatSync(fn) {
203     var fs = require('fs');
204     var statSync = fs.statSync;
205     try {
206         fs.statSync = function () {
207             throw new EvalError('Unknown Error');
208         };
209         return fn();
210     } finally {
211         fs.statSync = statSync;
212     }
213 };
214
215 test('#79 - re-throw non ENOENT errors from stat', function (t) {
216     var dir = path.join(__dirname, 'resolver');
217
218     stubStatSync(function () {
219         t.throws(function () {
220             resolve.sync('foo', { basedir: dir });
221         }, /Unknown Error/);
222     });
223
224     t.end();
225 });
226
227 test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
228     var dir = path.join(__dirname, 'resolver');
229
230     t.equal(
231         resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }),
232         path.join(dir, 'same_names/foo.js')
233     );
234     t.equal(
235         resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }),
236         path.join(dir, 'same_names/foo/index.js')
237     );
238     t.end();
239 });
240
241 test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
242     var dir = path.join(__dirname, 'resolver');
243
244     t.equal(
245         resolve.sync('./', { basedir: path.join(dir, 'same_names/foo') }),
246         path.join(dir, 'same_names/foo/index.js')
247     );
248     t.equal(
249         resolve.sync('.', { basedir: path.join(dir, 'same_names/foo') }),
250         path.join(dir, 'same_names/foo/index.js')
251     );
252     t.end();
253 });
254
255 test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
256     var testFile = path.basename(__filename);
257
258     t.test('sanity check', function (st) {
259         st.equal(
260             resolve.sync('./' + testFile),
261             __filename,
262             'sanity check'
263         );
264         st.end();
265     });
266
267     t.test('with a fake directory', function (st) {
268         function run() { return resolve.sync('./' + testFile + '/blah'); }
269
270         st.throws(run, 'throws an error');
271
272         try {
273             run();
274         } catch (e) {
275             st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
276             st.equal(
277                 e.message,
278                 'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'',
279                 'can not find nonexistent module'
280             );
281         }
282
283         st.end();
284     });
285
286     t.end();
287 });
288
289 test('sync dot main', function (t) {
290     var start = new Date();
291     t.equal(resolve.sync('./resolver/dot_main'), path.join(__dirname, 'resolver/dot_main/index.js'));
292     t.ok(new Date() - start < 50, 'resolve.sync timedout');
293     t.end();
294 });
295
296 test('sync dot slash main', function (t) {
297     var start = new Date();
298     t.equal(resolve.sync('./resolver/dot_slash_main'), path.join(__dirname, 'resolver/dot_slash_main/index.js'));
299     t.ok(new Date() - start < 50, 'resolve.sync timedout');
300     t.end();
301 });
302
303 test('not a directory', function (t) {
304     var path = './foo';
305     try {
306         resolve.sync(path, { basedir: __filename });
307         t.fail();
308     } catch (err) {
309         t.ok(err, 'a non-directory errors');
310         t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
311         t.equal(err && err.code, 'MODULE_NOT_FOUND');
312     }
313     t.end();
314 });
315
316 test('non-string "main" field in package.json', function (t) {
317     var dir = path.join(__dirname, 'resolver');
318     try {
319         var result = resolve.sync('./invalid_main', { basedir: dir });
320         t.equal(result, undefined, 'result should not exist');
321         t.fail('should not get here');
322     } catch (err) {
323         t.ok(err, 'errors on non-string main');
324         t.equal(err.message, 'package “invalid main” `main` must be a string');
325         t.equal(err.code, 'INVALID_PACKAGE_MAIN');
326     }
327     t.end();
328 });
329
330 test('non-string "main" field in package.json', function (t) {
331     var dir = path.join(__dirname, 'resolver');
332     try {
333         var result = resolve.sync('./invalid_main', { basedir: dir });
334         t.equal(result, undefined, 'result should not exist');
335         t.fail('should not get here');
336     } catch (err) {
337         t.ok(err, 'errors on non-string main');
338         t.equal(err.message, 'package “invalid main” `main` must be a string');
339         t.equal(err.code, 'INVALID_PACKAGE_MAIN');
340     }
341     t.end();
342 });
343
344 test('browser field in package.json', function (t) {
345     var dir = path.join(__dirname, 'resolver');
346     var res = resolve.sync('./browser_field', {
347         basedir: dir,
348         packageFilter: function packageFilter(pkg) {
349             if (pkg.browser) {
350                 pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
351                 delete pkg.browser; // eslint-disable-line no-param-reassign
352             }
353             return pkg;
354         }
355     });
356     t.equal(res, path.join(dir, 'browser_field', 'b.js'));
357     t.end();
358 });