mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-07-12 03:06:05 +09:00
Clean up the oopsie. Rebuild the blank exercises.
This commit is contained in:
+50
-5
@@ -10,6 +10,9 @@ test('core modules', function (t) {
|
||||
|
||||
st.ok(!resolve.isCore('seq'));
|
||||
st.ok(!resolve.isCore('../'));
|
||||
|
||||
st.ok(!resolve.isCore('toString'));
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
@@ -19,18 +22,60 @@ test('core modules', function (t) {
|
||||
|
||||
for (var i = 0; i < cores.length; ++i) {
|
||||
var mod = cores[i];
|
||||
var requireFunc = function () { require(mod); }; // eslint-disable-line no-loop-func
|
||||
console.log(mod, resolve.core, resolve.core[mod]);
|
||||
if (resolve.core[mod]) {
|
||||
st.doesNotThrow(
|
||||
function () { require(mod); }, // eslint-disable-line no-loop-func
|
||||
'requiring ' + mod + ' does not throw'
|
||||
);
|
||||
st.doesNotThrow(requireFunc, mod + ' supported; requiring does not throw');
|
||||
} else {
|
||||
st.skip(mod + ' not supported');
|
||||
st.throws(requireFunc, mod + ' not supported; requiring throws');
|
||||
}
|
||||
}
|
||||
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('core via repl module', { skip: !resolve.core.repl }, function (st) {
|
||||
var libs = require('repl')._builtinLibs; // eslint-disable-line no-underscore-dangle
|
||||
if (!libs) {
|
||||
st.skip('module.builtinModules does not exist');
|
||||
return st.end();
|
||||
}
|
||||
for (var i = 0; i < libs.length; ++i) {
|
||||
var mod = libs[i];
|
||||
st.ok(resolve.core[mod], mod + ' is a core module');
|
||||
st.doesNotThrow(
|
||||
function () { require(mod); }, // eslint-disable-line no-loop-func
|
||||
'requiring ' + mod + ' does not throw'
|
||||
);
|
||||
}
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.test('core via builtinModules list', { skip: !resolve.core.module }, function (st) {
|
||||
var libs = require('module').builtinModules;
|
||||
if (!libs) {
|
||||
st.skip('module.builtinModules does not exist');
|
||||
return st.end();
|
||||
}
|
||||
var blacklist = [
|
||||
'_debug_agent',
|
||||
'v8/tools/tickprocessor-driver',
|
||||
'v8/tools/SourceMap',
|
||||
'v8/tools/tickprocessor',
|
||||
'v8/tools/profile'
|
||||
];
|
||||
for (var i = 0; i < libs.length; ++i) {
|
||||
var mod = libs[i];
|
||||
if (blacklist.indexOf(mod) === -1) {
|
||||
st.ok(resolve.core[mod], mod + ' is a core module');
|
||||
st.doesNotThrow(
|
||||
function () { require(mod); }, // eslint-disable-line no-loop-func
|
||||
'requiring ' + mod + ' does not throw'
|
||||
);
|
||||
}
|
||||
}
|
||||
st.end();
|
||||
});
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
+18
-2
@@ -1,4 +1,5 @@
|
||||
var test = require('tape');
|
||||
var path = require('path');
|
||||
var resolve = require('../');
|
||||
|
||||
test('faulty basedir must produce error in windows', { skip: process.platform !== 'win32' }, function (t) {
|
||||
@@ -7,7 +8,22 @@ test('faulty basedir must produce error in windows', { skip: process.platform !=
|
||||
var resolverDir = 'C:\\a\\b\\c\\d';
|
||||
|
||||
resolve('tape/lib/test.js', { basedir: resolverDir }, function (err, res, pkg) {
|
||||
t.equal(true, !!err);
|
||||
t.equal(!!err, true);
|
||||
});
|
||||
});
|
||||
|
||||
test('non-existent basedir should not throw when preserveSymlinks is false', function (t) {
|
||||
t.plan(2);
|
||||
|
||||
var opts = {
|
||||
basedir: path.join(path.sep, 'unreal', 'path', 'that', 'does', 'not', 'exist'),
|
||||
preserveSymlinks: false
|
||||
};
|
||||
|
||||
var module = './dotdot/abc';
|
||||
|
||||
resolve(module, opts, function (err, res) {
|
||||
t.equal(err.code, 'MODULE_NOT_FOUND');
|
||||
t.equal(res, undefined);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
+20
-5
@@ -3,17 +3,32 @@ var test = require('tape');
|
||||
var resolve = require('../');
|
||||
|
||||
test('filter', function (t) {
|
||||
t.plan(2);
|
||||
t.plan(4);
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
var packageFilterArgs;
|
||||
resolve('./baz', {
|
||||
basedir: dir,
|
||||
packageFilter: function (pkg) {
|
||||
pkg.main = 'doom';
|
||||
packageFilter: function (pkg, pkgfile) {
|
||||
pkg.main = 'doom'; // eslint-disable-line no-param-reassign
|
||||
packageFilterArgs = [pkg, pkgfile];
|
||||
return pkg;
|
||||
}
|
||||
}, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'baz/doom.js'));
|
||||
t.equal(pkg.main, 'doom');
|
||||
|
||||
t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');
|
||||
|
||||
var packageData = packageFilterArgs[0];
|
||||
t.equal(pkg, packageData, 'first packageFilter argument is "pkg"');
|
||||
t.equal(packageData.main, 'doom', 'package "main" was altered');
|
||||
|
||||
var packageFile = packageFilterArgs[1];
|
||||
t.equal(
|
||||
packageFile,
|
||||
path.join(dir, 'baz/package.json'),
|
||||
'second packageFilter argument is "pkgfile"'
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
+20
-3
@@ -4,13 +4,30 @@ var resolve = require('../');
|
||||
|
||||
test('filter', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
var packageFilterArgs;
|
||||
var res = resolve.sync('./baz', {
|
||||
basedir: dir,
|
||||
packageFilter: function (pkg) {
|
||||
pkg.main = 'doom';
|
||||
// NOTE: in v2.x, this will be `pkg, pkgfile, dir`, but must remain "broken" here in v1.x for compatibility
|
||||
packageFilter: function (pkg, /*pkgfile,*/ dir) { // eslint-disable-line spaced-comment
|
||||
pkg.main = 'doom'; // eslint-disable-line no-param-reassign
|
||||
packageFilterArgs = 'is 1.x' ? [pkg, dir] : [pkg, pkgfile, dir]; // eslint-disable-line no-constant-condition, no-undef
|
||||
return pkg;
|
||||
}
|
||||
});
|
||||
t.equal(res, path.join(dir, 'baz/doom.js'));
|
||||
|
||||
t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');
|
||||
|
||||
var packageData = packageFilterArgs[0];
|
||||
t.equal(packageData.main, 'doom', 'package "main" was altered');
|
||||
|
||||
if (!'is 1.x') { // eslint-disable-line no-constant-condition
|
||||
var packageFile = packageFilterArgs[1];
|
||||
t.equal(packageFile, path.join(dir, 'baz', 'package.json'), 'package.json path is correct');
|
||||
}
|
||||
|
||||
var packageDir = packageFilterArgs['is 1.x' ? 1 : 2]; // eslint-disable-line no-constant-condition
|
||||
// eslint-disable-next-line no-constant-condition
|
||||
t.equal(packageDir, path.join(dir, 'baz'), ('is 1.x' ? 'second' : 'third') + ' packageFilter argument is "dir"');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
+172
@@ -8,14 +8,23 @@ test('mock', function (t) {
|
||||
var files = {};
|
||||
files[path.resolve('/foo/bar/baz.js')] = 'beep';
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo/bar')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file, cb) {
|
||||
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
|
||||
},
|
||||
isDirectory: function (dir, cb) {
|
||||
cb(null, !!dirs[path.resolve(dir)]);
|
||||
},
|
||||
readFile: function (file, cb) {
|
||||
cb(null, files[path.resolve(file)]);
|
||||
},
|
||||
realpath: function (file, cb) {
|
||||
cb(null, file);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -49,15 +58,24 @@ test('mock from package', function (t) {
|
||||
var files = {};
|
||||
files[path.resolve('/foo/bar/baz.js')] = 'beep';
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo/bar')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file, cb) {
|
||||
cb(null, Object.prototype.hasOwnProperty.call(files, file));
|
||||
},
|
||||
isDirectory: function (dir, cb) {
|
||||
cb(null, !!dirs[path.resolve(dir)]);
|
||||
},
|
||||
'package': { main: 'bar' },
|
||||
readFile: function (file, cb) {
|
||||
cb(null, files[file]);
|
||||
},
|
||||
realpath: function (file, cb) {
|
||||
cb(null, file);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -94,14 +112,24 @@ test('mock package', function (t) {
|
||||
main: './baz.js'
|
||||
});
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo')] = true;
|
||||
dirs[path.resolve('/foo/node_modules')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file, cb) {
|
||||
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
|
||||
},
|
||||
isDirectory: function (dir, cb) {
|
||||
cb(null, !!dirs[path.resolve(dir)]);
|
||||
},
|
||||
readFile: function (file, cb) {
|
||||
cb(null, files[path.resolve(file)]);
|
||||
},
|
||||
realpath: function (file, cb) {
|
||||
cb(null, file);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -122,15 +150,25 @@ test('mock package from package', function (t) {
|
||||
main: './baz.js'
|
||||
});
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo')] = true;
|
||||
dirs[path.resolve('/foo/node_modules')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file, cb) {
|
||||
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
|
||||
},
|
||||
isDirectory: function (dir, cb) {
|
||||
cb(null, !!dirs[path.resolve(dir)]);
|
||||
},
|
||||
'package': { main: 'bar' },
|
||||
readFile: function (file, cb) {
|
||||
cb(null, files[path.resolve(file)]);
|
||||
},
|
||||
realpath: function (file, cb) {
|
||||
cb(null, file);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -141,3 +179,137 @@ test('mock package from package', function (t) {
|
||||
t.equal(pkg && pkg.main, './baz.js');
|
||||
});
|
||||
});
|
||||
|
||||
test('symlinked', function (t) {
|
||||
t.plan(4);
|
||||
|
||||
var files = {};
|
||||
files[path.resolve('/foo/bar/baz.js')] = 'beep';
|
||||
files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo/bar')] = true;
|
||||
dirs[path.resolve('/foo/bar/symlinked')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
preserveSymlinks: false,
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file, cb) {
|
||||
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
|
||||
},
|
||||
isDirectory: function (dir, cb) {
|
||||
cb(null, !!dirs[path.resolve(dir)]);
|
||||
},
|
||||
readFile: function (file, cb) {
|
||||
cb(null, files[path.resolve(file)]);
|
||||
},
|
||||
realpath: function (file, cb) {
|
||||
var resolved = path.resolve(file);
|
||||
|
||||
if (resolved.indexOf('symlinked') >= 0) {
|
||||
cb(null, resolved);
|
||||
return;
|
||||
}
|
||||
|
||||
var ext = path.extname(resolved);
|
||||
|
||||
if (ext) {
|
||||
var dir = path.dirname(resolved);
|
||||
var base = path.basename(resolved);
|
||||
cb(null, path.join(dir, 'symlinked', base));
|
||||
} else {
|
||||
cb(null, path.join(resolved, 'symlinked'));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
|
||||
if (err) return t.fail(err);
|
||||
t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
|
||||
t.equal(pkg, undefined);
|
||||
});
|
||||
|
||||
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
|
||||
if (err) return t.fail(err);
|
||||
t.equal(res, path.resolve('/foo/bar/symlinked/baz.js'));
|
||||
t.equal(pkg, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
test('readPackage', function (t) {
|
||||
t.plan(3);
|
||||
|
||||
var files = {};
|
||||
files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
|
||||
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
|
||||
main: './baz.js'
|
||||
});
|
||||
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo')] = true;
|
||||
dirs[path.resolve('/foo/node_modules')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file, cb) {
|
||||
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
|
||||
},
|
||||
isDirectory: function (dir, cb) {
|
||||
cb(null, !!dirs[path.resolve(dir)]);
|
||||
},
|
||||
'package': { main: 'bar' },
|
||||
readFile: function (file, cb) {
|
||||
cb(null, files[path.resolve(file)]);
|
||||
},
|
||||
realpath: function (file, cb) {
|
||||
cb(null, file);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
t.test('with readFile', function (st) {
|
||||
st.plan(3);
|
||||
|
||||
resolve('bar', opts('/foo'), function (err, res, pkg) {
|
||||
st.error(err);
|
||||
st.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
|
||||
st.equal(pkg && pkg.main, './baz.js');
|
||||
});
|
||||
});
|
||||
|
||||
var readPackage = function (readFile, file, cb) {
|
||||
var barPackage = path.join('bar', 'package.json');
|
||||
if (file.slice(-barPackage.length) === barPackage) {
|
||||
cb(null, { main: './something-else.js' });
|
||||
} else {
|
||||
cb(null, JSON.parse(files[path.resolve(file)]));
|
||||
}
|
||||
};
|
||||
|
||||
t.test('with readPackage', function (st) {
|
||||
st.plan(3);
|
||||
|
||||
var options = opts('/foo');
|
||||
delete options.readFile;
|
||||
options.readPackage = readPackage;
|
||||
resolve('bar', options, function (err, res, pkg) {
|
||||
st.error(err);
|
||||
st.equal(res, path.resolve('/foo/node_modules/bar/something-else.js'));
|
||||
st.equal(pkg && pkg.main, './something-else.js');
|
||||
});
|
||||
});
|
||||
|
||||
t.test('with readFile and readPackage', function (st) {
|
||||
st.plan(1);
|
||||
|
||||
var options = opts('/foo');
|
||||
options.readPackage = readPackage;
|
||||
resolve('bar', options, function (err) {
|
||||
st.throws(function () { throw err; }, TypeError, 'errors when both readFile and readPackage are provided');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+153
-4
@@ -8,14 +8,23 @@ test('mock', function (t) {
|
||||
var files = {};
|
||||
files[path.resolve('/foo/bar/baz.js')] = 'beep';
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo/bar')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file) {
|
||||
return Object.prototype.hasOwnProperty.call(files, file);
|
||||
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
||||
},
|
||||
isDirectory: function (dir) {
|
||||
return !!dirs[path.resolve(dir)];
|
||||
},
|
||||
readFileSync: function (file) {
|
||||
return files[file];
|
||||
return files[path.resolve(file)];
|
||||
},
|
||||
realpathSync: function (file) {
|
||||
return file;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -48,14 +57,24 @@ test('mock package', function (t) {
|
||||
main: './baz.js'
|
||||
});
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo')] = true;
|
||||
dirs[path.resolve('/foo/node_modules')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file) {
|
||||
return Object.prototype.hasOwnProperty.call(files, file);
|
||||
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
||||
},
|
||||
isDirectory: function (dir) {
|
||||
return !!dirs[path.resolve(dir)];
|
||||
},
|
||||
readFileSync: function (file) {
|
||||
return files[file];
|
||||
return files[path.resolve(file)];
|
||||
},
|
||||
realpathSync: function (file) {
|
||||
return file;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -65,3 +84,133 @@ test('mock package', function (t) {
|
||||
path.resolve('/foo/node_modules/bar/baz.js')
|
||||
);
|
||||
});
|
||||
|
||||
test('symlinked', function (t) {
|
||||
t.plan(2);
|
||||
|
||||
var files = {};
|
||||
files[path.resolve('/foo/bar/baz.js')] = 'beep';
|
||||
files[path.resolve('/foo/bar/symlinked/baz.js')] = 'beep';
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo/bar')] = true;
|
||||
dirs[path.resolve('/foo/bar/symlinked')] = true;
|
||||
|
||||
function opts(basedir) {
|
||||
return {
|
||||
preserveSymlinks: false,
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file) {
|
||||
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
||||
},
|
||||
isDirectory: function (dir) {
|
||||
return !!dirs[path.resolve(dir)];
|
||||
},
|
||||
readFileSync: function (file) {
|
||||
return files[path.resolve(file)];
|
||||
},
|
||||
realpathSync: function (file) {
|
||||
var resolved = path.resolve(file);
|
||||
|
||||
if (resolved.indexOf('symlinked') >= 0) {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
var ext = path.extname(resolved);
|
||||
|
||||
if (ext) {
|
||||
var dir = path.dirname(resolved);
|
||||
var base = path.basename(resolved);
|
||||
return path.join(dir, 'symlinked', base);
|
||||
} else {
|
||||
return path.join(resolved, 'symlinked');
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
t.equal(
|
||||
resolve.sync('./baz', opts('/foo/bar')),
|
||||
path.resolve('/foo/bar/symlinked/baz.js')
|
||||
);
|
||||
|
||||
t.equal(
|
||||
resolve.sync('./baz.js', opts('/foo/bar')),
|
||||
path.resolve('/foo/bar/symlinked/baz.js')
|
||||
);
|
||||
});
|
||||
|
||||
test('readPackageSync', function (t) {
|
||||
t.plan(3);
|
||||
|
||||
var files = {};
|
||||
files[path.resolve('/foo/node_modules/bar/something-else.js')] = 'beep';
|
||||
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
|
||||
main: './baz.js'
|
||||
});
|
||||
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'boop';
|
||||
|
||||
var dirs = {};
|
||||
dirs[path.resolve('/foo')] = true;
|
||||
dirs[path.resolve('/foo/node_modules')] = true;
|
||||
|
||||
function opts(basedir, useReadPackage) {
|
||||
return {
|
||||
basedir: path.resolve(basedir),
|
||||
isFile: function (file) {
|
||||
return Object.prototype.hasOwnProperty.call(files, path.resolve(file));
|
||||
},
|
||||
isDirectory: function (dir) {
|
||||
return !!dirs[path.resolve(dir)];
|
||||
},
|
||||
readFileSync: useReadPackage ? null : function (file) {
|
||||
return files[path.resolve(file)];
|
||||
},
|
||||
realpathSync: function (file) {
|
||||
return file;
|
||||
}
|
||||
};
|
||||
}
|
||||
t.test('with readFile', function (st) {
|
||||
st.plan(1);
|
||||
|
||||
st.equal(
|
||||
resolve.sync('bar', opts('/foo')),
|
||||
path.resolve('/foo/node_modules/bar/baz.js')
|
||||
);
|
||||
});
|
||||
|
||||
var readPackageSync = function (readFileSync, file) {
|
||||
if (file.indexOf(path.join('bar', 'package.json')) >= 0) {
|
||||
return { main: './something-else.js' };
|
||||
} else {
|
||||
return JSON.parse(files[path.resolve(file)]);
|
||||
}
|
||||
};
|
||||
|
||||
t.test('with readPackage', function (st) {
|
||||
st.plan(1);
|
||||
|
||||
var options = opts('/foo');
|
||||
delete options.readFileSync;
|
||||
options.readPackageSync = readPackageSync;
|
||||
|
||||
st.equal(
|
||||
resolve.sync('bar', options),
|
||||
path.resolve('/foo/node_modules/bar/something-else.js')
|
||||
);
|
||||
});
|
||||
|
||||
t.test('with readFile and readPackage', function (st) {
|
||||
st.plan(1);
|
||||
|
||||
var options = opts('/foo');
|
||||
options.readPackageSync = readPackageSync;
|
||||
st.throws(
|
||||
function () { resolve.sync('bar', options); },
|
||||
TypeError,
|
||||
'errors when both readFile and readPackage are provided'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+52
-2
@@ -7,6 +7,11 @@ var nodeModulesPaths = require('../lib/node-modules-paths');
|
||||
|
||||
var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) {
|
||||
var moduleDirs = [].concat(moduleDirectories || 'node_modules');
|
||||
if (paths) {
|
||||
for (var k = 0; k < paths.length; ++k) {
|
||||
moduleDirs.push(path.basename(paths[k]));
|
||||
}
|
||||
}
|
||||
|
||||
var foundModuleDirs = {};
|
||||
var uniqueDirs = {};
|
||||
@@ -20,7 +25,7 @@ var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) {
|
||||
}
|
||||
t.equal(keys(parsedDirs).length >= start.split(path.sep).length, true, 'there are >= dirs than "start" has');
|
||||
var foundModuleDirNames = keys(foundModuleDirs);
|
||||
t.deepEqual(foundModuleDirNames, moduleDirs.concat(paths || []), 'all desired module dirs were found');
|
||||
t.deepEqual(foundModuleDirNames, moduleDirs, 'all desired module dirs were found');
|
||||
t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique');
|
||||
|
||||
var counts = {};
|
||||
@@ -49,7 +54,7 @@ test('node-modules-paths', function (t) {
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test('with paths option', function (t) {
|
||||
t.test('with paths=array option', function (t) {
|
||||
var start = path.join(__dirname, 'resolver');
|
||||
var paths = ['a', 'b'];
|
||||
var dirs = nodeModulesPaths(start, { paths: paths });
|
||||
@@ -59,6 +64,29 @@ test('node-modules-paths', function (t) {
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test('with paths=function option', function (t) {
|
||||
var paths = function paths(request, absoluteStart, getNodeModulesDirs, opts) {
|
||||
return getNodeModulesDirs().concat(path.join(absoluteStart, 'not node modules', request));
|
||||
};
|
||||
|
||||
var start = path.join(__dirname, 'resolver');
|
||||
var dirs = nodeModulesPaths(start, { paths: paths }, 'pkg');
|
||||
|
||||
verifyDirs(t, start, dirs, null, [path.join(start, 'not node modules', 'pkg')]);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test('with paths=function skipping node modules resolution', function (t) {
|
||||
var paths = function paths(request, absoluteStart, getNodeModulesDirs, opts) {
|
||||
return [];
|
||||
};
|
||||
var start = path.join(__dirname, 'resolver');
|
||||
var dirs = nodeModulesPaths(start, { paths: paths });
|
||||
t.deepEqual(dirs, [], 'no node_modules was computed');
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test('with moduleDirectory option', function (t) {
|
||||
var start = path.join(__dirname, 'resolver');
|
||||
var moduleDirectory = 'not node modules';
|
||||
@@ -90,4 +118,26 @@ test('node-modules-paths', function (t) {
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test('combine paths correctly on Windows', function (t) {
|
||||
var start = 'C:\\Users\\username\\myProject\\src';
|
||||
var paths = [];
|
||||
var moduleDirectories = ['node_modules', start];
|
||||
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
|
||||
|
||||
t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
t.test('combine paths correctly on non-Windows', { skip: process.platform === 'win32' }, function (t) {
|
||||
var start = '/Users/username/git/myProject/src';
|
||||
var paths = [];
|
||||
var moduleDirectories = ['node_modules', '/Users/username/git/myProject/src'];
|
||||
var dirs = nodeModulesPaths(start, { paths: paths, moduleDirectory: moduleDirectories });
|
||||
|
||||
t.equal(dirs.indexOf(path.resolve(start)) > -1, true, 'should contain start dir');
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
+32
-11
@@ -1,18 +1,34 @@
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var test = require('tape');
|
||||
var resolve = require('../');
|
||||
|
||||
test('$NODE_PATH', function (t) {
|
||||
t.plan(4);
|
||||
t.plan(8);
|
||||
|
||||
var isDir = function (dir, cb) {
|
||||
if (dir === '/node_path' || dir === 'node_path/x') {
|
||||
return cb(null, true);
|
||||
}
|
||||
fs.stat(dir, function (err, stat) {
|
||||
if (!err) {
|
||||
return cb(null, stat.isDirectory());
|
||||
}
|
||||
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
|
||||
resolve('aaa', {
|
||||
paths: [
|
||||
path.join(__dirname, '/node_path/x'),
|
||||
path.join(__dirname, '/node_path/y')
|
||||
],
|
||||
basedir: __dirname
|
||||
basedir: __dirname,
|
||||
isDirectory: isDir
|
||||
}, function (err, res) {
|
||||
t.equal(res, path.join(__dirname, '/node_path/x/aaa/index.js'));
|
||||
t.error(err);
|
||||
t.equal(res, path.join(__dirname, '/node_path/x/aaa/index.js'), 'aaa resolves');
|
||||
});
|
||||
|
||||
resolve('bbb', {
|
||||
@@ -20,9 +36,11 @@ test('$NODE_PATH', function (t) {
|
||||
path.join(__dirname, '/node_path/x'),
|
||||
path.join(__dirname, '/node_path/y')
|
||||
],
|
||||
basedir: __dirname
|
||||
basedir: __dirname,
|
||||
isDirectory: isDir
|
||||
}, function (err, res) {
|
||||
t.equal(res, path.join(__dirname, '/node_path/y/bbb/index.js'));
|
||||
t.error(err);
|
||||
t.equal(res, path.join(__dirname, '/node_path/y/bbb/index.js'), 'bbb resolves');
|
||||
});
|
||||
|
||||
resolve('ccc', {
|
||||
@@ -30,20 +48,23 @@ test('$NODE_PATH', function (t) {
|
||||
path.join(__dirname, '/node_path/x'),
|
||||
path.join(__dirname, '/node_path/y')
|
||||
],
|
||||
basedir: __dirname
|
||||
basedir: __dirname,
|
||||
isDirectory: isDir
|
||||
}, function (err, res) {
|
||||
t.equal(res, path.join(__dirname, '/node_path/x/ccc/index.js'));
|
||||
t.error(err);
|
||||
t.equal(res, path.join(__dirname, '/node_path/x/ccc/index.js'), 'ccc resolves');
|
||||
});
|
||||
|
||||
// ensure that relative paths still resolve against the
|
||||
// regular `node_modules` correctly
|
||||
// ensure that relative paths still resolve against the regular `node_modules` correctly
|
||||
resolve('tap', {
|
||||
paths: [
|
||||
'node_path'
|
||||
],
|
||||
basedir: 'node_path/x'
|
||||
basedir: path.join(__dirname, 'node_path/x'),
|
||||
isDirectory: isDir
|
||||
}, function (err, res) {
|
||||
var root = require('tap/package.json').main;
|
||||
t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap', root));
|
||||
t.error(err);
|
||||
t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap', root), 'tap resolves');
|
||||
});
|
||||
});
|
||||
|
||||
+59
-26
@@ -2,41 +2,74 @@ var path = require('path');
|
||||
var test = require('tape');
|
||||
var resolve = require('../');
|
||||
|
||||
test('#62: deep module references and the pathFilter', function (t) {
|
||||
t.plan(9);
|
||||
var resolverDir = path.join(__dirname, '/pathfilter/deep_ref');
|
||||
|
||||
var resolverDir = path.join(__dirname, '/pathfilter/deep_ref');
|
||||
var pathFilter = function (pkg, x, remainder) {
|
||||
var pathFilterFactory = function (t) {
|
||||
return function (pkg, x, remainder) {
|
||||
t.equal(pkg.version, '1.2.3');
|
||||
t.equal(x, path.join(resolverDir, 'node_modules/deep/ref'));
|
||||
t.equal(remainder, 'ref');
|
||||
return 'alt';
|
||||
};
|
||||
};
|
||||
|
||||
resolve('deep/ref', { basedir: resolverDir }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
test('#62: deep module references and the pathFilter', function (t) {
|
||||
t.test('deep/ref.js', function (st) {
|
||||
st.plan(3);
|
||||
|
||||
t.equal(pkg.version, '1.2.3');
|
||||
t.equal(res, path.join(resolverDir, 'node_modules/deep/ref.js'));
|
||||
resolve('deep/ref', { basedir: resolverDir }, function (err, res, pkg) {
|
||||
if (err) st.fail(err);
|
||||
|
||||
st.equal(pkg.version, '1.2.3');
|
||||
st.equal(res, path.join(resolverDir, 'node_modules/deep/ref.js'));
|
||||
});
|
||||
|
||||
var res = resolve.sync('deep/ref', { basedir: resolverDir });
|
||||
st.equal(res, path.join(resolverDir, 'node_modules/deep/ref.js'));
|
||||
});
|
||||
|
||||
resolve(
|
||||
'deep/deeper/ref',
|
||||
{ basedir: resolverDir },
|
||||
function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.notEqual(pkg, undefined);
|
||||
t.equal(pkg.version, '1.2.3');
|
||||
t.equal(res, path.join(resolverDir, 'node_modules/deep/deeper/ref.js'));
|
||||
}
|
||||
);
|
||||
t.test('deep/deeper/ref', function (st) {
|
||||
st.plan(4);
|
||||
|
||||
resolve(
|
||||
'deep/ref',
|
||||
{ basedir: resolverDir, pathFilter: pathFilter },
|
||||
function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
|
||||
}
|
||||
);
|
||||
resolve(
|
||||
'deep/deeper/ref',
|
||||
{ basedir: resolverDir },
|
||||
function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
st.notEqual(pkg, undefined);
|
||||
st.equal(pkg.version, '1.2.3');
|
||||
st.equal(res, path.join(resolverDir, 'node_modules/deep/deeper/ref.js'));
|
||||
}
|
||||
);
|
||||
|
||||
var res = resolve.sync(
|
||||
'deep/deeper/ref',
|
||||
{ basedir: resolverDir }
|
||||
);
|
||||
st.equal(res, path.join(resolverDir, 'node_modules/deep/deeper/ref.js'));
|
||||
});
|
||||
|
||||
t.test('deep/ref alt', function (st) {
|
||||
st.plan(8);
|
||||
|
||||
var pathFilter = pathFilterFactory(st);
|
||||
|
||||
var res = resolve.sync(
|
||||
'deep/ref',
|
||||
{ basedir: resolverDir, pathFilter: pathFilter }
|
||||
);
|
||||
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
|
||||
|
||||
resolve(
|
||||
'deep/ref',
|
||||
{ basedir: resolverDir, pathFilter: pathFilter },
|
||||
function (err, res, pkg) {
|
||||
if (err) st.fail(err);
|
||||
st.equal(res, path.join(resolverDir, 'node_modules/deep/alt.js'));
|
||||
st.end();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
+118
-17
@@ -3,7 +3,7 @@ var test = require('tape');
|
||||
var resolve = require('../');
|
||||
|
||||
test('async foo', function (t) {
|
||||
t.plan(10);
|
||||
t.plan(12);
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
|
||||
resolve('./foo', { basedir: dir }, function (err, res, pkg) {
|
||||
@@ -30,10 +30,20 @@ test('async foo', function (t) {
|
||||
t.equal(pkg.main, 'resolver');
|
||||
});
|
||||
|
||||
resolve('./foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err, res) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'foo.js'));
|
||||
});
|
||||
|
||||
resolve('foo', { basedir: dir }, function (err) {
|
||||
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
|
||||
t.equal(err.code, 'MODULE_NOT_FOUND');
|
||||
});
|
||||
|
||||
// Test that filename is reported as the "from" value when passed.
|
||||
resolve('foo', { basedir: dir, filename: path.join(dir, 'baz.js') }, function (err) {
|
||||
t.equal(err.message, "Cannot find module 'foo' from '" + path.join(dir, 'baz.js') + "'");
|
||||
});
|
||||
});
|
||||
|
||||
test('bar', function (t) {
|
||||
@@ -55,7 +65,7 @@ test('bar', function (t) {
|
||||
resolve('foo', { basedir: dir + '/bar', 'package': { main: 'bar' } }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'bar/node_modules/foo/index.js'));
|
||||
t.equal(pkg, undefined);
|
||||
t.equal(pkg.main, 'bar');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -113,7 +123,7 @@ test('biz', function (t) {
|
||||
resolve('tiv', { basedir: dir + '/grux', 'package': { main: 'grux' } }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'tiv/index.js'));
|
||||
t.equal(pkg, undefined);
|
||||
t.equal(pkg.main, 'grux');
|
||||
});
|
||||
|
||||
resolve('tiv', { basedir: dir + '/garply' }, function (err, res, pkg) {
|
||||
@@ -125,7 +135,7 @@ test('biz', function (t) {
|
||||
resolve('tiv', { basedir: dir + '/garply', 'package': { main: './lib' } }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'tiv/index.js'));
|
||||
t.equal(pkg, undefined);
|
||||
t.equal(pkg.main, './lib');
|
||||
});
|
||||
|
||||
resolve('grux', { basedir: dir + '/tiv' }, function (err, res, pkg) {
|
||||
@@ -137,7 +147,7 @@ test('biz', function (t) {
|
||||
resolve('grux', { basedir: dir + '/tiv', 'package': { main: 'tiv' } }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'grux/index.js'));
|
||||
t.equal(pkg, undefined);
|
||||
t.equal(pkg.main, 'tiv');
|
||||
});
|
||||
|
||||
resolve('garply', { basedir: dir + '/tiv' }, function (err, res, pkg) {
|
||||
@@ -176,7 +186,7 @@ test('normalize', function (t) {
|
||||
});
|
||||
|
||||
test('cup', function (t) {
|
||||
t.plan(4);
|
||||
t.plan(5);
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
|
||||
resolve('./cup', { basedir: dir, extensions: ['.js', '.coffee'] }, function (err, res) {
|
||||
@@ -193,6 +203,11 @@ test('cup', function (t) {
|
||||
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
|
||||
t.equal(err.code, 'MODULE_NOT_FOUND');
|
||||
});
|
||||
|
||||
// Test that filename is reported as the "from" value when passed.
|
||||
resolve('./cup', { basedir: dir, extensions: ['.js'], filename: path.join(dir, 'cupboard.js') }, function (err, res) {
|
||||
t.equal(err.message, "Cannot find module './cup' from '" + path.join(dir, 'cupboard.js') + "'");
|
||||
});
|
||||
});
|
||||
|
||||
test('mug', function (t) {
|
||||
@@ -241,6 +256,22 @@ test('other path', function (t) {
|
||||
});
|
||||
});
|
||||
|
||||
test('path iterator', function (t) {
|
||||
t.plan(2);
|
||||
|
||||
var resolverDir = path.join(__dirname, 'resolver');
|
||||
|
||||
var exactIterator = function (x, start, getPackageCandidates, opts) {
|
||||
return [path.join(resolverDir, x)];
|
||||
};
|
||||
|
||||
resolve('baz', { packageIterator: exactIterator }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(resolverDir, 'baz/quux.js'));
|
||||
t.equal(pkg && pkg.name, 'baz');
|
||||
});
|
||||
});
|
||||
|
||||
test('incorrect main', function (t) {
|
||||
t.plan(1);
|
||||
|
||||
@@ -268,17 +299,6 @@ test('without basedir', function (t) {
|
||||
});
|
||||
});
|
||||
|
||||
test('#25: node modules with the same name as node stdlib modules', function (t) {
|
||||
t.plan(1);
|
||||
|
||||
var resolverDir = path.join(__dirname, 'resolver/punycode');
|
||||
|
||||
resolve('punycode', { basedir: resolverDir }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(resolverDir, 'node_modules/punycode/index.js'));
|
||||
});
|
||||
});
|
||||
|
||||
test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) {
|
||||
t.plan(2);
|
||||
|
||||
@@ -295,6 +315,22 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
|
||||
});
|
||||
});
|
||||
|
||||
test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
|
||||
t.plan(2);
|
||||
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
|
||||
resolve('./', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
|
||||
});
|
||||
|
||||
resolve('.', { basedir: path.join(dir, 'same_names/foo') }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'same_names/foo/index.js'));
|
||||
});
|
||||
});
|
||||
|
||||
test('async: #121 - treating an existing file as a dir when no basedir', function (t) {
|
||||
var testFile = path.basename(__filename);
|
||||
|
||||
@@ -347,3 +383,68 @@ test('async dot slash main', function (t) {
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('not a directory', function (t) {
|
||||
t.plan(6);
|
||||
var path = './foo';
|
||||
resolve(path, { basedir: __filename }, function (err, res, pkg) {
|
||||
t.ok(err, 'a non-directory errors');
|
||||
t.equal(arguments.length, 1);
|
||||
t.equal(res, undefined);
|
||||
t.equal(pkg, undefined);
|
||||
|
||||
t.equal(err && err.message, 'Cannot find module \'' + path + '\' from \'' + __filename + '\'');
|
||||
t.equal(err && err.code, 'MODULE_NOT_FOUND');
|
||||
});
|
||||
});
|
||||
|
||||
test('non-string "main" field in package.json', function (t) {
|
||||
t.plan(5);
|
||||
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
|
||||
t.ok(err, 'errors on non-string main');
|
||||
t.equal(err.message, 'package “invalid main” `main` must be a string');
|
||||
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
|
||||
t.equal(res, undefined, 'res is undefined');
|
||||
t.equal(pkg, undefined, 'pkg is undefined');
|
||||
});
|
||||
});
|
||||
|
||||
test('non-string "main" field in package.json', function (t) {
|
||||
t.plan(5);
|
||||
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
resolve('./invalid_main', { basedir: dir }, function (err, res, pkg) {
|
||||
t.ok(err, 'errors on non-string main');
|
||||
t.equal(err.message, 'package “invalid main” `main` must be a string');
|
||||
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
|
||||
t.equal(res, undefined, 'res is undefined');
|
||||
t.equal(pkg, undefined, 'pkg is undefined');
|
||||
});
|
||||
});
|
||||
|
||||
test('browser field in package.json', function (t) {
|
||||
t.plan(3);
|
||||
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
resolve(
|
||||
'./browser_field',
|
||||
{
|
||||
basedir: dir,
|
||||
packageFilter: function packageFilter(pkg) {
|
||||
if (pkg.browser) {
|
||||
pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
|
||||
delete pkg.browser; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
return pkg;
|
||||
}
|
||||
},
|
||||
function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.equal(res, path.join(dir, 'browser_field', 'b.js'));
|
||||
t.equal(pkg && pkg.main, 'b');
|
||||
t.equal(pkg && pkg.browser, undefined);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
+1
@@ -1,3 +1,4 @@
|
||||
{
|
||||
"name": "baz",
|
||||
"main": "quux.js"
|
||||
}
|
||||
|
||||
+102
-11
@@ -15,10 +15,26 @@ test('foo', function (t) {
|
||||
path.join(dir, 'foo.js')
|
||||
);
|
||||
|
||||
t.equal(
|
||||
resolve.sync('./foo.js', { basedir: dir, filename: path.join(dir, 'bar.js') }),
|
||||
path.join(dir, 'foo.js')
|
||||
);
|
||||
|
||||
t.throws(function () {
|
||||
resolve.sync('foo', { basedir: dir });
|
||||
});
|
||||
|
||||
// Test that filename is reported as the "from" value when passed.
|
||||
t.throws(
|
||||
function () {
|
||||
resolve.sync('foo', { basedir: dir, filename: path.join(dir, 'bar.js') });
|
||||
},
|
||||
{
|
||||
name: 'Error',
|
||||
message: "Cannot find module 'foo' from '" + path.join(dir, 'bar.js') + "'"
|
||||
}
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
@@ -156,6 +172,21 @@ test('other path', function (t) {
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('path iterator', function (t) {
|
||||
var resolverDir = path.join(__dirname, 'resolver');
|
||||
|
||||
var exactIterator = function (x, start, getPackageCandidates, opts) {
|
||||
return [path.join(resolverDir, x)];
|
||||
};
|
||||
|
||||
t.equal(
|
||||
resolve.sync('baz', { packageIterator: exactIterator }),
|
||||
path.join(resolverDir, 'baz/quux.js')
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('incorrect main', function (t) {
|
||||
var resolverDir = path.join(__dirname, 'resolver');
|
||||
var dir = path.join(resolverDir, 'incorrect_main');
|
||||
@@ -168,17 +199,6 @@ test('incorrect main', function (t) {
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('#25: node modules with the same name as node stdlib modules', function (t) {
|
||||
var resolverDir = path.join(__dirname, 'resolver/punycode');
|
||||
|
||||
t.equal(
|
||||
resolve.sync('punycode', { basedir: resolverDir }),
|
||||
path.join(resolverDir, 'node_modules/punycode/index.js')
|
||||
);
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
var stubStatSync = function stubStatSync(fn) {
|
||||
var fs = require('fs');
|
||||
var statSync = fs.statSync;
|
||||
@@ -218,6 +238,20 @@ test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('#211 - incorrectly resolves module-paths like "." when from inside a folder with a sibling file of the same name', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
|
||||
t.equal(
|
||||
resolve.sync('./', { basedir: path.join(dir, 'same_names/foo') }),
|
||||
path.join(dir, 'same_names/foo/index.js')
|
||||
);
|
||||
t.equal(
|
||||
resolve.sync('.', { basedir: path.join(dir, 'same_names/foo') }),
|
||||
path.join(dir, 'same_names/foo/index.js')
|
||||
);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('sync: #121 - treating an existing file as a dir when no basedir', function (t) {
|
||||
var testFile = path.basename(__filename);
|
||||
|
||||
@@ -265,3 +299,60 @@ test('sync dot slash main', function (t) {
|
||||
t.ok(new Date() - start < 50, 'resolve.sync timedout');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('not a directory', function (t) {
|
||||
var path = './foo';
|
||||
try {
|
||||
resolve.sync(path, { basedir: __filename });
|
||||
t.fail();
|
||||
} catch (err) {
|
||||
t.ok(err, 'a non-directory errors');
|
||||
t.equal(err && err.message, 'Cannot find module \'' + path + "' from '" + __filename + "'");
|
||||
t.equal(err && err.code, 'MODULE_NOT_FOUND');
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('non-string "main" field in package.json', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
try {
|
||||
var result = resolve.sync('./invalid_main', { basedir: dir });
|
||||
t.equal(result, undefined, 'result should not exist');
|
||||
t.fail('should not get here');
|
||||
} catch (err) {
|
||||
t.ok(err, 'errors on non-string main');
|
||||
t.equal(err.message, 'package “invalid main” `main` must be a string');
|
||||
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('non-string "main" field in package.json', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
try {
|
||||
var result = resolve.sync('./invalid_main', { basedir: dir });
|
||||
t.equal(result, undefined, 'result should not exist');
|
||||
t.fail('should not get here');
|
||||
} catch (err) {
|
||||
t.ok(err, 'errors on non-string main');
|
||||
t.equal(err.message, 'package “invalid main” `main` must be a string');
|
||||
t.equal(err.code, 'INVALID_PACKAGE_MAIN');
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('browser field in package.json', function (t) {
|
||||
var dir = path.join(__dirname, 'resolver');
|
||||
var res = resolve.sync('./browser_field', {
|
||||
basedir: dir,
|
||||
packageFilter: function packageFilter(pkg) {
|
||||
if (pkg.browser) {
|
||||
pkg.main = pkg.browser; // eslint-disable-line no-param-reassign
|
||||
delete pkg.browser; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
return pkg;
|
||||
}
|
||||
});
|
||||
t.equal(res, path.join(dir, 'browser_field', 'b.js'));
|
||||
t.end();
|
||||
});
|
||||
|
||||
+125
-3
@@ -1,24 +1,50 @@
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
var test = require('tape');
|
||||
var map = require('array.prototype.map');
|
||||
var resolve = require('../');
|
||||
|
||||
var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink');
|
||||
var packageDir = path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'package');
|
||||
var modADir = path.join(__dirname, 'symlinks', 'source', 'node_modules', 'mod-a');
|
||||
var symlinkModADir = path.join(__dirname, 'symlinks', 'dest', 'node_modules', 'mod-a');
|
||||
try {
|
||||
fs.unlinkSync(symlinkDir);
|
||||
} catch (err) {}
|
||||
try {
|
||||
fs.unlinkSync(packageDir);
|
||||
} catch (err) {}
|
||||
try {
|
||||
fs.unlinkSync(modADir);
|
||||
} catch (err) {}
|
||||
try {
|
||||
fs.unlinkSync(symlinkModADir);
|
||||
} catch (err) {}
|
||||
|
||||
try {
|
||||
fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir');
|
||||
} catch (err) {
|
||||
// if fails then it is probably on Windows and lets try to create a junction
|
||||
fs.symlinkSync(path.join(__dirname, 'resolver', 'symlinked', '_', 'symlink_target') + '\\', symlinkDir, 'junction');
|
||||
}
|
||||
try {
|
||||
fs.symlinkSync('../../package', packageDir, 'dir');
|
||||
} catch (err) {
|
||||
// if fails then it is probably on Windows and lets try to create a junction
|
||||
fs.symlinkSync(path.join(__dirname, '..', '..', 'package') + '\\', packageDir, 'junction');
|
||||
}
|
||||
try {
|
||||
fs.symlinkSync('../../source/node_modules/mod-a', symlinkModADir, 'dir');
|
||||
} catch (err) {
|
||||
// if fails then it is probably on Windows and lets try to create a junction
|
||||
fs.symlinkSync(path.join(__dirname, '..', '..', 'source', 'node_modules', 'mod-a') + '\\', symlinkModADir, 'junction');
|
||||
}
|
||||
|
||||
test('symlink', function (t) {
|
||||
t.plan(1);
|
||||
t.plan(2);
|
||||
|
||||
resolve('foo', { basedir: symlinkDir, preserveSymlinks: false }, function (err, res, pkg) {
|
||||
if (err) t.fail(err);
|
||||
t.error(err);
|
||||
t.equal(res, path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
|
||||
});
|
||||
});
|
||||
@@ -41,7 +67,12 @@ test('sync symlink when preserveSymlinks = true', function (t) {
|
||||
|
||||
test('sync symlink', function (t) {
|
||||
var start = new Date();
|
||||
t.equal(resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }), path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js'));
|
||||
t.doesNotThrow(function () {
|
||||
t.equal(
|
||||
resolve.sync('foo', { basedir: symlinkDir, preserveSymlinks: false }),
|
||||
path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'foo.js')
|
||||
);
|
||||
});
|
||||
t.ok(new Date() - start < 50, 'resolve.sync timedout');
|
||||
t.end();
|
||||
});
|
||||
@@ -52,3 +83,94 @@ test('sync symlink when preserveSymlinks = true', function (t) {
|
||||
}, /Cannot find module 'foo'/);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('sync symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
|
||||
var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
|
||||
var fn = resolve.sync('package', { basedir: basedir, preserveSymlinks: false });
|
||||
|
||||
t.equal(fn, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('async symlink from node_modules to other dir when preserveSymlinks = false', function (t) {
|
||||
t.plan(2);
|
||||
var basedir = path.join(__dirname, 'resolver', 'symlinked', '_');
|
||||
resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) {
|
||||
t.notOk(err, 'no error');
|
||||
t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
|
||||
});
|
||||
});
|
||||
|
||||
test('packageFilter', function (t) {
|
||||
function relative(x) {
|
||||
return path.relative(__dirname, x);
|
||||
}
|
||||
|
||||
function testPackageFilter(preserveSymlinks) {
|
||||
return function (st) {
|
||||
st.plan('is 1.x' ? 3 : 5); // eslint-disable-line no-constant-condition
|
||||
|
||||
var destMain = 'symlinks/dest/node_modules/mod-a/index.js';
|
||||
var destPkg = 'symlinks/dest/node_modules/mod-a/package.json';
|
||||
var sourceMain = 'symlinks/source/node_modules/mod-a/index.js';
|
||||
var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json';
|
||||
var destDir = path.join(__dirname, 'symlinks', 'dest');
|
||||
|
||||
/* eslint multiline-comment-style: 0 */
|
||||
/* v2.x will restore these tests
|
||||
var packageFilterPath = [];
|
||||
var actualPath = resolve.sync('mod-a', {
|
||||
basedir: destDir,
|
||||
preserveSymlinks: preserveSymlinks,
|
||||
packageFilter: function (pkg, pkgfile, dir) {
|
||||
packageFilterPath.push(pkgfile);
|
||||
}
|
||||
});
|
||||
st.equal(
|
||||
relative(actualPath),
|
||||
path.normalize(preserveSymlinks ? destMain : sourceMain),
|
||||
'sync: actual path is correct'
|
||||
);
|
||||
st.deepEqual(
|
||||
map(packageFilterPath, relative),
|
||||
map(preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], path.normalize),
|
||||
'sync: packageFilter pkgfile arg is correct'
|
||||
);
|
||||
*/
|
||||
|
||||
var asyncPackageFilterPath = [];
|
||||
resolve(
|
||||
'mod-a',
|
||||
{
|
||||
basedir: destDir,
|
||||
preserveSymlinks: preserveSymlinks,
|
||||
packageFilter: function (pkg, pkgfile) {
|
||||
asyncPackageFilterPath.push(pkgfile);
|
||||
}
|
||||
},
|
||||
function (err, actualPath) {
|
||||
st.error(err, 'no error');
|
||||
st.equal(
|
||||
relative(actualPath),
|
||||
path.normalize(preserveSymlinks ? destMain : sourceMain),
|
||||
'async: actual path is correct'
|
||||
);
|
||||
st.deepEqual(
|
||||
map(asyncPackageFilterPath, relative),
|
||||
map(
|
||||
preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg],
|
||||
path.normalize
|
||||
),
|
||||
'async: packageFilter pkgfile arg is correct'
|
||||
);
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
t.test('preserveSymlinks: false', testPackageFilter(false));
|
||||
|
||||
t.test('preserveSymlinks: true', testPackageFilter(true));
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user