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) {
18 return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
20 isDirectory: function (dir) {
21 return !!dirs[path.resolve(dir)];
23 readFileSync: function (file) {
24 return files[path.resolve(file)];
26 realpathSync: function (file) {
33 resolve.sync('./baz', opts('/foo/bar')),
34 path.resolve('/foo/bar/baz.js')
38 resolve.sync('./baz.js', opts('/foo/bar')),
39 path.resolve('/foo/bar/baz.js')
42 t.throws(function () {
43 resolve.sync('baz', opts('/foo/bar'));
46 t.throws(function () {
47 resolve.sync('../baz', opts('/foo/bar'));
51 test('mock package', function (t) {
55 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
56 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
61 dirs[path.resolve('/foo')] = true;
62 dirs[path.resolve('/foo/node_modules')] = true;
64 function opts(basedir) {
66 basedir: path.resolve(basedir),
67 isFile: function (file) {
68 return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
70 isDirectory: function (dir) {
71 return !!dirs[path.resolve(dir)];
73 readFileSync: function (file) {
74 return files[path.resolve(file)];
76 realpathSync: function (file) {
83 resolve.sync('bar', opts('/foo')),
84 path.resolve('/foo/node_modules/bar/baz.js')
88 test('symlinked', function (t) {
92 files[path.resolve('/foo/bar/baz.js')] = 'beep';
93 files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
96 dirs[path.resolve('/foo/bar')] = true;
97 dirs[path.resolve('/foo/bar/symlinked')] = true;
99 function opts(basedir) {
101 preserveSymlinks: false,
102 basedir: path.resolve(basedir),
103 isFile: function (file) {
104 return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
106 isDirectory: function (dir) {
107 return !!dirs[path.resolve(dir)];
109 readFileSync: function (file) {
110 return files[path.resolve(file)];
112 realpathSync: function (file) {
113 var resolved = path.resolve(file);
115 if (resolved.indexOf('symlinked') >= 0) {
119 var ext = path.extname(resolved);
122 var dir = path.dirname(resolved);
123 var base = path.basename(resolved);
124 return path.join(dir, 'symlinked', base);
126 return path.join(resolved, 'symlinked');
132 resolve.sync('./baz', opts('/foo/bar')),
133 path.resolve('/foo/bar/symlinked/baz.js')
137 resolve.sync('./baz.js', opts('/foo/bar')),
138 path.resolve('/foo/bar/symlinked/baz.js')
142 test('readPackageSync', function (t) {
146 files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
147 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
150 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
153 dirs[path.resolve('/foo')] = true;
154 dirs[path.resolve('/foo/node_modules')] = true;
156 function opts(basedir, useReadPackage) {
158 basedir: path.resolve(basedir),
159 isFile: function (file) {
160 return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
162 isDirectory: function (dir) {
163 return !!dirs[path.resolve(dir)];
165 readFileSync: useReadPackage ? null : function (file) {
166 return files[path.resolve(file)];
168 realpathSync: function (file) {
173 t.test('with readFile', function (st) {
177 resolve.sync('bar', opts('/foo')),
178 path.resolve('/foo/node_modules/bar/baz.js')
182 var readPackageSync = function (readFileSync, file) {
183 if (file.indexOf(path.join('bar', 'package.json')) >= 0) {
184 return { main: './something-else.js' };
186 return JSON.parse(files[path.resolve(file)]);
189 t.test('with readPackage', function (st) {
192 var options = opts('/foo');
193 delete options.readFileSync;
194 options.readPackageSync = readPackageSync;
197 resolve.sync('bar', options),
198 path.resolve('/foo/node_modules/bar/something-else.js')
202 t.test('with readFile and readPackage', function (st) {
205 var options = opts('/foo');
206 options.readPackageSync = readPackageSync;
208 function () { resolve.sync('bar', options); },
210 'errors when both readFile and readPackage are provided'