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 () {
72 resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }),
73 path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js')
76 t.ok(new Date() - start < 50, 'resolve.sync timedout');
80 test('sync symlink when preserveSymlinks = true', function (t) {
81 t.throws(function () {
82 resolve.sync('foo', { basedir: symlinkDir });
83 }, /Cannot find module 'foo'/);
87 test('sync symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
88 var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
89 var fn = resolve.sync('package', { basedir: basedir, preserveSymlinks: false });
91 t.equal(fn, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
95 test('async symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
97 var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
98 resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) {
99 t.notOk(err, 'no error');
100 t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
104 test('packageFilter', function (t) {
105 function relative(x) {
106 return path.relative(__dirname, x);
109 function testPackageFilter(preserveSymlinks) {
110 return function (st) {
111 st.plan('is 1.x' ? 3 : 5); // eslint-disable-line no-constant-condition
113 var destMain = 'symlinks/dest/node_modules/mod-a/index.js';
114 var destPkg = 'symlinks/dest/node_modules/mod-a/package.json';
115 var sourceMain = 'symlinks/source/node_modules/mod-a/index.js';
116 var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json';
117 var destDir = path.join(__dirname, 'symlinks', 'dest');
119 /* eslint multiline-comment-style: 0 */
120 /* v2.x will restore these tests
121 var packageFilterPath = [];
122 var actualPath = resolve.sync('mod-a', {
124 preserveSymlinks: preserveSymlinks,
125 packageFilter: function (pkg, pkgfile, dir) {
126 packageFilterPath.push(pkgfile);
130 relative(actualPath),
131 path.normalize(preserveSymlinks ? destMain : sourceMain),
132 'sync: actual path is correct'
135 map(packageFilterPath, relative),
136 map(preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], path.normalize),
137 'sync: packageFilter pkgfile arg is correct'
141 var asyncPackageFilterPath = [];
146 preserveSymlinks: preserveSymlinks,
147 packageFilter: function (pkg, pkgfile) {
148 asyncPackageFilterPath.push(pkgfile);
151 function (err, actualPath) {
152 st.error(err, 'no error');
154 relative(actualPath),
155 path.normalize(preserveSymlinks ? destMain : sourceMain),
156 'async: actual path is correct'
159 map(asyncPackageFilterPath, relative),
161 preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg],
164 'async: packageFilter pkgfile arg is correct'
171 t.test('preserveSymlinks: false', testPackageFilter(false));
173 t.test('preserveSymlinks: true', testPackageFilter(true));