1 var path = require('path');
2 var fs = require('fs');
3 var test = require('tape');
4 var map = require('array.prototype.map');
5 var resolve = require('../');
7 var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink');
8 var packageDir = path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'package');
9 var modADir = path.join(__dirname, 'symlinks', 'source', 'node_modules', 'mod-a');
10 var symlinkModADir = path.join(__dirname, 'symlinks', 'dest', 'node_modules', 'mod-a');
12 fs.unlinkSync(symlinkDir);
15 fs.unlinkSync(packageDir);
18 fs.unlinkSync(modADir);
21 fs.unlinkSync(symlinkModADir);
25 fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir');
27 // if fails then it is probably on Windows and lets try to create a junction
28 fs.symlinkSync(path.join(__dirname, 'resolver', 'symlinked', '_', 'symlink_target') + '\\', symlinkDir, 'junction');
31 fs.symlinkSync('../../package', packageDir, 'dir');
33 // if fails then it is probably on Windows and lets try to create a junction
34 fs.symlinkSync(path.join(__dirname, '..', '..', 'package') + '\\', packageDir, 'junction');
37 fs.symlinkSync('../../source/node_modules/mod-a', symlinkModADir, 'dir');
39 // if fails then it is probably on Windows and lets try to create a junction
40 fs.symlinkSync(path.join(__dirname, '..', '..', 'source', 'node_modules', 'mod-a') + '\\', symlinkModADir, 'junction');
43 test('symlink', function (t) {
46 resolve('foo', { basedir: symlinkDir, preserveSymlinks: false }, function (err, res, pkg) {
48 t.equal(res, path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
52 test('sync symlink when preserveSymlinks = true', function (t) {
55 resolve('foo', { basedir: symlinkDir }, function (err, res, pkg) {
56 t.ok(err, 'there is an error');
57 t.notOk(res, 'no result');
59 t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
62 'Cannot find module \'foo\' from \'' + symlinkDir + '\'',
63 'can not find nonexistent module'
68 test('sync symlink', function (t) {
69 var start = new Date();
70 t.doesNotThrow(function () {
71 t.equal(resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }), path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
73 t.ok(new Date() - start < 50, 'resolve.sync timedout');
77 test('sync symlink when preserveSymlinks = true', function (t) {
78 t.throws(function () {
79 resolve.sync('foo', { basedir: symlinkDir });
80 }, /Cannot find module 'foo'/);
84 test('sync symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
85 var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
86 var fn = resolve.sync('package', { basedir: basedir, preserveSymlinks: false });
88 t.equal(fn, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
92 test('async symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
94 var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
95 resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) {
96 t.notOk(err, 'no error');
97 t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
101 test('packageFilter', function (t) {
102 function relative(x) {
103 return path.relative(__dirname, x);
106 function testPackageFilter(preserveSymlinks) {
107 return function (st) {
108 st.plan('is 1.x' ? 3 : 5); // eslint-disable-line no-constant-condition
110 var destMain = 'symlinks/dest/node_modules/mod-a/index.js';
111 var destPkg = 'symlinks/dest/node_modules/mod-a/package.json';
112 var sourceMain = 'symlinks/source/node_modules/mod-a/index.js';
113 var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json';
114 var destDir = path.join(__dirname, 'symlinks', 'dest');
116 /* eslint multiline-comment-style: 0 */
117 /* v2.x will restore these tests
118 var packageFilterPath = [];
119 var actualPath = resolve.sync('mod-a', {
121 preserveSymlinks: preserveSymlinks,
122 packageFilter: function (pkg, pkgfile, dir) {
123 packageFilterPath.push(pkgfile);
127 relative(actualPath),
128 path.normalize(preserveSymlinks ? destMain : sourceMain),
129 'sync: actual path is correct'
132 map(packageFilterPath, relative),
133 map(preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], path.normalize),
134 'sync: packageFilter pkgfile arg is correct'
138 var asyncPackageFilterPath = [];
143 preserveSymlinks: preserveSymlinks,
144 packageFilter: function (pkg, pkgfile) {
145 asyncPackageFilterPath.push(pkgfile);
148 function (err, actualPath) {
149 st.error(err, 'no error');
151 relative(actualPath),
152 path.normalize(preserveSymlinks ? destMain : sourceMain),
153 'async: actual path is correct'
156 map(asyncPackageFilterPath, relative),
158 preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg],
161 'async: packageFilter pkgfile arg is correct'
168 t.test('preserveSymlinks: false', testPackageFilter(false));
170 t.test('preserveSymlinks: true', testPackageFilter(true));