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');
133 resolve.sync('./baz', opts('/foo/bar')),
134 path.resolve('/foo/bar/symlinked/baz.js')
138 resolve.sync('./baz.js', opts('/foo/bar')),
139 path.resolve('/foo/bar/symlinked/baz.js')
143 test('readPackageSync', function (t) {
147 files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
148 files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
151 files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
154 dirs[path.resolve('/foo')] = true;
155 dirs[path.resolve('/foo/node_modules')] = true;
157 function opts(basedir, useReadPackage) {
159 basedir: path.resolve(basedir),
160 isFile: function (file) {
161 return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
163 isDirectory: function (dir) {
164 return !!dirs[path.resolve(dir)];
166 readFileSync: useReadPackage ? null : function (file) {
167 return files[path.resolve(file)];
169 realpathSync: function (file) {
174 t.test('with readFile', function (st) {
178 resolve.sync('bar', opts('/foo')),
179 path.resolve('/foo/node_modules/bar/baz.js')
183 var readPackageSync = function (readFileSync, file) {
184 if (file.indexOf(path.join('bar', 'package.json')) >= 0) {
185 return { main: './something-else.js' };
187 return JSON.parse(files[path.resolve(file)]);
191 t.test('with readPackage', function (st) {
194 var options = opts('/foo');
195 delete options.readFileSync;
196 options.readPackageSync = readPackageSync;
199 resolve.sync('bar', options),
200 path.resolve('/foo/node_modules/bar/something-else.js')
204 t.test('with readFile and readPackage', function (st) {
207 var options = opts('/foo');
208 options.readPackageSync = readPackageSync;
210 function () { resolve.sync('bar', options); },
212 'errors when both readFile and readPackage are provided'