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:
+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);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user