Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / resolve / test / mock_sync.js
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 });