source: imaps-frontend/node_modules/resolve/test/symlinks.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[d565449]1var path = require('path');
2var fs = require('fs');
3var test = require('tape');
4var map = require('array.prototype.map');
5var resolve = require('../');
6
7var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink');
8var packageDir = path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'package');
9var modADir = path.join(__dirname, 'symlinks', 'source', 'node_modules', 'mod-a');
10var symlinkModADir = path.join(__dirname, 'symlinks', 'dest', 'node_modules', 'mod-a');
11try {
12 fs.unlinkSync(symlinkDir);
13} catch (err) {}
14try {
15 fs.unlinkSync(packageDir);
16} catch (err) {}
17try {
18 fs.unlinkSync(modADir);
19} catch (err) {}
20try {
21 fs.unlinkSync(symlinkModADir);
22} catch (err) {}
23
24try {
25 fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir');
26} catch (err) {
27 if (err.code !== 'EEXIST') {
28 // if fails then it is probably on Windows and lets try to create a junction
29 fs.symlinkSync(path.join(__dirname, 'resolver', 'symlinked', '_', 'symlink_target') + '\\', symlinkDir, 'junction');
30 }
31}
32try {
33 fs.symlinkSync('../../package', packageDir, 'dir');
34} catch (err) {
35 // if fails then it is probably on Windows and lets try to create a junction
36 fs.symlinkSync(path.join(__dirname, '..', '..', 'package') + '\\', packageDir, 'junction');
37}
38try {
39 fs.symlinkSync('../../source/node_modules/mod-a', symlinkModADir, 'dir');
40} catch (err) {
41 // if fails then it is probably on Windows and lets try to create a junction
42 fs.symlinkSync(path.join(__dirname, '..', '..', 'source', 'node_modules', 'mod-a') + '\\', symlinkModADir, 'junction');
43}
44
45test('symlink', function (t) {
46 t.plan(2);
47
48 resolve('foo', { basedir: symlinkDir }, function (err, res, pkg) {
49 t.error(err);
50 t.equal(res, path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
51 });
52});
53
54test('sync symlink when preserveSymlinks = true', function (t) {
55 t.plan(4);
56
57 resolve('foo', { basedir: symlinkDir, preserveSymlinks: true }, function (err, res, pkg) {
58 t.ok(err, 'there is an error');
59 t.notOk(res, 'no result');
60
61 t.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
62 t.equal(
63 err && err.message,
64 'Cannot find module \'foo\' from \'' + symlinkDir + '\'',
65 'can not find nonexistent module'
66 );
67 });
68});
69
70test('sync symlink', function (t) {
71 var start = new Date();
72 t.doesNotThrow(function () {
73 t.equal(
74 resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }),
75 path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js')
76 );
77 });
78 t.ok(new Date() - start < 50, 'resolve.sync timedout');
79 t.end();
80});
81
82test('sync symlink when preserveSymlinks = true', function (t) {
83 t.throws(function () {
84 resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: true });
85 }, /Cannot find module 'foo'/);
86 t.end();
87});
88
89test('sync symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
90 var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
91 var fn = resolve.sync('package', { basedir: basedir, preserveSymlinks: false });
92
93 t.equal(fn, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
94 t.end();
95});
96
97test('async symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
98 t.plan(2);
99 var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
100 resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) {
101 t.notOk(err, 'no error');
102 t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
103 });
104});
105
106test('packageFilter', function (t) {
107 function relative(x) {
108 return path.relative(__dirname, x);
109 }
110
111 function testPackageFilter(preserveSymlinks) {
112 return function (st) {
113 st.plan(5);
114
115 var destMain = 'symlinks/dest/node_modules/mod-a/index.js';
116 var destPkg = 'symlinks/dest/node_modules/mod-a/package.json';
117 var sourceMain = 'symlinks/source/node_modules/mod-a/index.js';
118 var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json';
119 var destDir = path.join(__dirname, 'symlinks', 'dest');
120
121 var packageFilterPath = [];
122 var actualPath = resolve.sync('mod-a', {
123 basedir: destDir,
124 preserveSymlinks: preserveSymlinks,
125 packageFilter: function (pkg, pkgfile, dir) {
126 packageFilterPath.push(pkgfile);
127 }
128 });
129 st.equal(
130 relative(actualPath),
131 path.normalize(preserveSymlinks ? destMain : sourceMain),
132 'sync: actual path is correct'
133 );
134 st.deepEqual(
135 map(packageFilterPath, relative),
136 map(preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], path.normalize),
137 'sync: packageFilter pkgfile arg is correct'
138 );
139
140 var asyncPackageFilterPath = [];
141 resolve(
142 'mod-a',
143 {
144 basedir: destDir,
145 preserveSymlinks: preserveSymlinks,
146 packageFilter: function (pkg, pkgfile) {
147 asyncPackageFilterPath.push(pkgfile);
148 }
149 },
150 function (err, actualPath) {
151 st.error(err, 'no error');
152 st.equal(
153 relative(actualPath),
154 path.normalize(preserveSymlinks ? destMain : sourceMain),
155 'async: actual path is correct'
156 );
157 st.deepEqual(
158 map(asyncPackageFilterPath, relative),
159 map(
160 preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg],
161 path.normalize
162 ),
163 'async: packageFilter pkgfile arg is correct'
164 );
165 }
166 );
167 };
168 }
169
170 t.test('preserveSymlinks: false', testPackageFilter(false));
171
172 t.test('preserveSymlinks: true', testPackageFilter(true));
173
174 t.end();
175});
Note: See TracBrowser for help on using the repository browser.