| 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 | }
|
|---|
| 126 | return path.join(resolved, 'symlinked');
|
|---|
| 127 | }
|
|---|
| 128 | };
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | t.equal(
|
|---|
| 132 | resolve.sync('./baz', opts('/foo/bar')),
|
|---|
| 133 | path.resolve('/foo/bar/symlinked/baz.js')
|
|---|
| 134 | );
|
|---|
| 135 |
|
|---|
| 136 | t.equal(
|
|---|
| 137 | resolve.sync('./baz.js', opts('/foo/bar')),
|
|---|
| 138 | path.resolve('/foo/bar/symlinked/baz.js')
|
|---|
| 139 | );
|
|---|
| 140 | });
|
|---|
| 141 |
|
|---|
| 142 | test('readPackageSync', function (t) {
|
|---|
| 143 | t.plan(4);
|
|---|
| 144 |
|
|---|
| 145 | var files = {};
|
|---|
| 146 | files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
|
|---|
| 147 | files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
|
|---|
| 148 | main: './baz.js'
|
|---|
| 149 | });
|
|---|
| 150 | files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
|
|---|
| 151 |
|
|---|
| 152 | var dirs = {};
|
|---|
| 153 | dirs[path.resolve('/foo')] = true;
|
|---|
| 154 | dirs[path.resolve('/foo/node_modules')] = true;
|
|---|
| 155 |
|
|---|
| 156 | function opts(basedir, useReadPackage) {
|
|---|
| 157 | return {
|
|---|
| 158 | basedir: path.resolve(basedir),
|
|---|
| 159 | isFile: function (file) {
|
|---|
| 160 | return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
|---|
| 161 | },
|
|---|
| 162 | isDirectory: function (dir) {
|
|---|
| 163 | return !!dirs[path.resolve(dir)];
|
|---|
| 164 | },
|
|---|
| 165 | readFileSync: useReadPackage ? null : function (file) {
|
|---|
| 166 | return files[path.resolve(file)];
|
|---|
| 167 | },
|
|---|
| 168 | realpathSync: function (file) {
|
|---|
| 169 | return file;
|
|---|
| 170 | }
|
|---|
| 171 | };
|
|---|
| 172 | }
|
|---|
| 173 | t.test('with readFile', function (st) {
|
|---|
| 174 | st.plan(1);
|
|---|
| 175 |
|
|---|
| 176 | st.equal(
|
|---|
| 177 | resolve.sync('bar', opts('/foo')),
|
|---|
| 178 | path.resolve('/foo/node_modules/bar/baz.js')
|
|---|
| 179 | );
|
|---|
| 180 | });
|
|---|
| 181 |
|
|---|
| 182 | function readPackageSync(readFileSync, file) {
|
|---|
| 183 | if (file.indexOf(path.join('bar', 'package.json')) >= 0) {
|
|---|
| 184 | return { main: './something-else.js' };
|
|---|
| 185 | }
|
|---|
| 186 | return JSON.parse(files[path.resolve(file)]);
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | t.test('with readPackage', function (st) {
|
|---|
| 190 | st.plan(1);
|
|---|
| 191 |
|
|---|
| 192 | var options = opts('/foo');
|
|---|
| 193 | delete options.readFileSync;
|
|---|
| 194 | options.readPackageSync = readPackageSync;
|
|---|
| 195 |
|
|---|
| 196 | st.equal(
|
|---|
| 197 | resolve.sync('bar', options),
|
|---|
| 198 | path.resolve('/foo/node_modules/bar/something-else.js')
|
|---|
| 199 | );
|
|---|
| 200 | });
|
|---|
| 201 |
|
|---|
| 202 | t.test('with readFile and readPackage', function (st) {
|
|---|
| 203 | st.plan(1);
|
|---|
| 204 |
|
|---|
| 205 | var options = opts('/foo');
|
|---|
| 206 | options.readPackageSync = readPackageSync;
|
|---|
| 207 | st.throws(
|
|---|
| 208 | function () { resolve.sync('bar', options); },
|
|---|
| 209 | TypeError,
|
|---|
| 210 | 'errors when both readFile and readPackage are provided'
|
|---|
| 211 | );
|
|---|
| 212 | });
|
|---|
| 213 |
|
|---|
| 214 | t.test('readPackageSync error propagates correctly', function (st) {
|
|---|
| 215 | st.plan(1);
|
|---|
| 216 |
|
|---|
| 217 | var readPackageError = new Error('read package error');
|
|---|
| 218 |
|
|---|
| 219 | function failReadPackageSync() {
|
|---|
| 220 | throw readPackageError;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | var options = opts('/foo');
|
|---|
| 224 | delete options.readFileSync;
|
|---|
| 225 | options.readPackageSync = failReadPackageSync;
|
|---|
| 226 |
|
|---|
| 227 | st.throws(
|
|---|
| 228 | function () { resolve.sync('bar', options); },
|
|---|
| 229 | readPackageError,
|
|---|
| 230 | 'readPackageSync error is thrown'
|
|---|
| 231 | );
|
|---|
| 232 | });
|
|---|
| 233 | });
|
|---|
| 234 |
|
|---|