1 var path = require('path');
2 var test = require('tape');
3 var resolve = require('../');
5 test('mock', function (t) {
9 files[path.resolve('/foo/bar/baz.js')] = 'beep';
12 dirs[path.resolve('/foo/bar')] = true;
14 function opts(basedir) {
16 basedir: path.resolve(basedir),
17 isFile: function (file, cb) {
18 cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
20 isDirectory: function (dir, cb) {
21 cb(null, !!dirs[path.resolve(dir)]);
23 readFile: function (file, cb) {
24 cb(null, files[path.resolve(file)]);
26 realpath: function (file, cb) {
32 resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
33 if (err) return t.fail(err);
34 t.equal(res, path.resolve('/foo/bar/baz.js'));
35 t.equal(pkg, undefined);
38 resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
39 if (err) return t.fail(err);
40 t.equal(res, path.resolve('/foo/bar/baz.js'));
41 t.equal(pkg, undefined);
44 resolve('baz', opts('/foo/bar'), function (err, res) {
45 t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
46 t.equal(err.code, 'MODULE_NOT_FOUND');
49 resolve('../baz', opts('/foo/bar'), function (err, res) {
50 t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
51 t.equal(err.code, 'MODULE_NOT_FOUND');
55 test('mock from package', function (t) {
59 files[path.resolve('/foo/bar/baz.js')] = 'beep';
62 dirs[path.resolve('/foo/bar')] = true;
64 function opts(basedir) {
66 basedir: path.resolve(basedir),
67 isFile: function (file, cb) {
68 cb(null, Object.prototype.hasOwnProperty.call(files, file));
70 isDirectory: function (dir, cb) {
71 cb(null, !!dirs[path.resolve(dir)]);
73 'package': { main: 'bar' },
74 readFile: function (file, cb) {
75 cb(null, files[file]);
77 realpath: function (file, cb) {
83 resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
84 if (err) return t.fail(err);
85 t.equal(res, path.resolve('/foo/bar/baz.js'));
86 t.equal(pkg && pkg.main, 'bar');
89 resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
90 if (err) return t.fail(err);
91 t.equal(res, path.resolve('/foo/bar/baz.js'));
92 t.equal(pkg && pkg.main, 'bar');
95 resolve('baz', opts('/foo/bar'), function (err, res) {
96 t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
97 t.equal(err.code, 'MODULE_NOT_FOUND');
100 resolve('../baz', opts('/foo/bar'), function (err, res) {
101 t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
102 t.equal(err.code, 'MODULE_NOT_FOUND');
106 test('mock package', function (t) {
110 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
111 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
116 dirs[path.resolve('/foo')] = true;
117 dirs[path.resolve('/foo/node_modules')] = true;
119 function opts(basedir) {
121 basedir: path.resolve(basedir),
122 isFile: function (file, cb) {
123 cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
125 isDirectory: function (dir, cb) {
126 cb(null, !!dirs[path.resolve(dir)]);
128 readFile: function (file, cb) {
129 cb(null, files[path.resolve(file)]);
131 realpath: function (file, cb) {
137 resolve('bar', opts('/foo'), function (err, res, pkg) {
138 if (err) return t.fail(err);
139 t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
140 t.equal(pkg && pkg.main, './baz.js');
144 test('mock package from package', function (t) {
148 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
149 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
154 dirs[path.resolve('/foo')] = true;
155 dirs[path.resolve('/foo/node_modules')] = true;
157 function opts(basedir) {
159 basedir: path.resolve(basedir),
160 isFile: function (file, cb) {
161 cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
163 isDirectory: function (dir, cb) {
164 cb(null, !!dirs[path.resolve(dir)]);
166 'package': { main: 'bar' },
167 readFile: function (file, cb) {
168 cb(null, files[path.resolve(file)]);
170 realpath: function (file, cb) {
176 resolve('bar', opts('/foo'), function (err, res, pkg) {
177 if (err) return t.fail(err);
178 t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
179 t.equal(pkg && pkg.main, './baz.js');
183 test('symlinked', function (t) {
187 files[path.resolve('/foo/bar/baz.js')] = 'beep';
188 files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
191 dirs[path.resolve('/foo/bar')] = true;
192 dirs[path.resolve('/foo/bar/symlinked')] = true;
194 function opts(basedir) {
196 preserveSymlinks: false,
197 basedir: path.resolve(basedir),
198 isFile: function (file, cb) {
199 cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
201 isDirectory: function (dir, cb) {
202 cb(null, !!dirs[path.resolve(dir)]);
204 readFile: function (file, cb) {
205 cb(null, files[path.resolve(file)]);
207 realpath: function (file, cb) {
208 var resolved = path.resolve(file);
210 if (resolved.indexOf('symlinked') >= 0) {
215 var ext = path.extname(resolved);
218 var dir = path.dirname(resolved);
219 var base = path.basename(resolved);
220 cb(null, path.join(dir, 'symlinked', base));
222 cb(null, path.join(resolved, 'symlinked'));
228 resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
229 if (err) return t.fail(err);
230 t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
231 t.equal(pkg, undefined);
234 resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
235 if (err) return t.fail(err);
236 t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
237 t.equal(pkg, undefined);
241 test('readPackage', function (t) {
245 files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
246 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
249 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
252 dirs[path.resolve('/foo')] = true;
253 dirs[path.resolve('/foo/node_modules')] = true;
255 function opts(basedir) {
257 basedir: path.resolve(basedir),
258 isFile: function (file, cb) {
259 cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
261 isDirectory: function (dir, cb) {
262 cb(null, !!dirs[path.resolve(dir)]);
264 'package': { main: 'bar' },
265 readFile: function (file, cb) {
266 cb(null, files[path.resolve(file)]);
268 realpath: function (file, cb) {
274 t.test('with readFile', function (st) {
277 resolve('bar', opts('/foo'), function (err, res, pkg) {
279 st.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
280 st.equal(pkg && pkg.main, './baz.js');
284 var readPackage = function (readFile, file, cb) {
285 var barPackage = path.join('bar', 'package.json');
286 if (file.slice(-barPackage.length) === barPackage) {
287 cb(null, { main: './something-else.js' });
289 cb(null, JSON.parse(files[path.resolve(file)]));
293 t.test('with readPackage', function (st) {
296 var options = opts('/foo');
297 delete options.readFile;
298 options.readPackage = readPackage;
299 resolve('bar', options, function (err, res, pkg) {
301 st.equal(res, path.resolve('/foo/node_modules/bar/something-else.js'));
302 st.equal(pkg && pkg.main, './something-else.js');
306 t.test('with readFile and readPackage', function (st) {
309 var options = opts('/foo');
310 options.readPackage = readPackage;
311 resolve('bar', options, function (err) {
312 st.throws(function () { throw err; }, TypeError, 'errors when both readFile and readPackage are provided');