source: imaps-frontend/node_modules/resolve/test/mock_sync.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 6.0 KB
RevLine 
[d565449]1var path = require('path');
2var test = require('tape');
3var resolve = require('../');
4
5test('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 dirs[path.resolve('/foo/node_modules')] = true;
14
15 function opts(basedir) {
16 return {
17 basedir: path.resolve(basedir),
18 isFile: function (file) {
19 return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
20 },
21 isDirectory: function (dir) {
22 return !!dirs[path.resolve(dir)];
23 },
24 readFileSync: function (file) {
25 return files[path.resolve(file)];
26 },
27 realpathSync: function (file) {
28 return file;
29 }
30 };
31 }
32
33 t.equal(
34 resolve.sync('./baz', opts('/foo/bar')),
35 path.resolve('/foo/bar/baz.js')
36 );
37
38 t.equal(
39 resolve.sync('./baz.js', opts('/foo/bar')),
40 path.resolve('/foo/bar/baz.js')
41 );
42
43 t.throws(function () {
44 resolve.sync('baz', opts('/foo/bar'));
45 });
46
47 t.throws(function () {
48 resolve.sync('../baz', opts('/foo/bar'));
49 });
50});
51
52test('mock package', function (t) {
53 t.plan(1);
54
55 var files = {};
56 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
57 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
58 main: './baz.js'
59 });
60
61 var dirs = {};
62 dirs[path.resolve('/foo')] = true;
63 dirs[path.resolve('/foo/node_modules')] = true;
64
65 function opts(basedir) {
66 return {
67 basedir: path.resolve(basedir),
68 isFile: function (file) {
69 return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
70 },
71 isDirectory: function (dir) {
72 return !!dirs[path.resolve(dir)];
73 },
74 readFileSync: function (file) {
75 return files[path.resolve(file)];
76 },
77 realpathSync: function (file) {
78 return file;
79 }
80 };
81 }
82
83 t.equal(
84 resolve.sync('bar', opts('/foo')),
85 path.resolve('/foo/node_modules/bar/baz.js')
86 );
87});
88
89test('symlinked', function (t) {
90 t.plan(2);
91
92 var files = {};
93 files[path.resolve('/foo/bar/baz.js')] = 'beep';
94 files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
95
96 var dirs = {};
97 dirs[path.resolve('/foo/bar')] = true;
98 dirs[path.resolve('/foo/bar/symlinked')] = true;
99
100 function opts(basedir) {
101 return {
102 preserveSymlinks: false,
103 basedir: path.resolve(basedir),
104 isFile: function (file) {
105 return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
106 },
107 isDirectory: function (dir) {
108 return !!dirs[path.resolve(dir)];
109 },
110 readFileSync: function (file) {
111 return files[path.resolve(file)];
112 },
113 realpathSync: function (file) {
114 var resolved = path.resolve(file);
115
116 if (resolved.indexOf('symlinked') >= 0) {
117 return resolved;
118 }
119
120 var ext = path.extname(resolved);
121
122 if (ext) {
123 var dir = path.dirname(resolved);
124 var base = path.basename(resolved);
125 return path.join(dir, 'symlinked', base);
126 }
127 return path.join(resolved, 'symlinked');
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
143test('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 }
187 return JSON.parse(files[path.resolve(file)]);
188 };
189
190 t.test('with readPackage', function (st) {
191 st.plan(1);
192
193 var options = opts('/foo');
194 delete options.readFileSync;
195 options.readPackageSync = readPackageSync;
196
197 st.equal(
198 resolve.sync('bar', options),
199 path.resolve('/foo/node_modules/bar/something-else.js')
200 );
201 });
202
203 t.test('with readFile and readPackage', function (st) {
204 st.plan(1);
205
206 var options = opts('/foo');
207 options.readPackageSync = readPackageSync;
208 st.throws(
209 function () { resolve.sync('bar', options); },
210 TypeError,
211 'errors when both readFile and readPackage are provided'
212 );
213 });
214});
215
Note: See TracBrowser for help on using the repository browser.