wallet-core/node_modules/resolve/test/mock.js

144 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-05-28 00:38:50 +02:00
var path = require('path');
2016-10-10 03:43:44 +02:00
var test = require('tape');
var resolve = require('../');
test('mock', function (t) {
2017-05-28 00:38:50 +02:00
t.plan(8);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
function opts(basedir) {
2016-10-10 03:43:44 +02:00
return {
2017-05-28 00:38:50 +02:00
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
2016-10-10 03:43:44 +02:00
},
2017-05-28 00:38:50 +02:00
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
};
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
2017-05-28 00:38:50 +02:00
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
2017-05-28 00:38:50 +02:00
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
2016-10-10 03:43:44 +02:00
t.equal(pkg, undefined);
});
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('baz', opts('/foo/bar'), function (err, res) {
2017-05-28 00:38:50 +02:00
t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('../baz', opts('/foo/bar'), function (err, res) {
2017-05-28 00:38:50 +02:00
t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
2016-10-10 03:43:44 +02:00
});
});
test('mock from package', function (t) {
2017-05-28 00:38:50 +02:00
t.plan(8);
var files = {};
files[path.resolve('/foo/bar/baz.js')] = 'beep';
function opts(basedir) {
2016-10-10 03:43:44 +02:00
return {
2017-05-28 00:38:50 +02:00
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, file));
2016-10-10 03:43:44 +02:00
},
2017-05-28 00:38:50 +02:00
'package': { main: 'bar' },
readFile: function (file, cb) {
2016-10-10 03:43:44 +02:00
cb(null, files[file]);
}
2017-05-28 00:38:50 +02:00
};
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
2017-05-28 00:38:50 +02:00
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg && pkg.main, 'bar');
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
2017-05-28 00:38:50 +02:00
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/bar/baz.js'));
t.equal(pkg && pkg.main, 'bar');
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('baz', opts('/foo/bar'), function (err, res) {
2017-05-28 00:38:50 +02:00
t.equal(err.message, "Cannot find module 'baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
2016-10-10 03:43:44 +02:00
});
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('../baz', opts('/foo/bar'), function (err, res) {
2017-05-28 00:38:50 +02:00
t.equal(err.message, "Cannot find module '../baz' from '" + path.resolve('/foo/bar') + "'");
t.equal(err.code, 'MODULE_NOT_FOUND');
2016-10-10 03:43:44 +02:00
});
});
test('mock package', function (t) {
t.plan(2);
2017-05-28 00:38:50 +02:00
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
function opts(basedir) {
2016-10-10 03:43:44 +02:00
return {
2017-05-28 00:38:50 +02:00
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
2016-10-10 03:43:44 +02:00
},
2017-05-28 00:38:50 +02:00
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
};
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('bar', opts('/foo'), function (err, res, pkg) {
2017-05-28 00:38:50 +02:00
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
t.equal(pkg && pkg.main, './baz.js');
2016-10-10 03:43:44 +02:00
});
});
test('mock package from package', function (t) {
t.plan(2);
2017-05-28 00:38:50 +02:00
var files = {};
files[path.resolve('/foo/node_modules/bar/baz.js')] = 'beep';
files[path.resolve('/foo/node_modules/bar/package.json')] = JSON.stringify({
main: './baz.js'
});
function opts(basedir) {
2016-10-10 03:43:44 +02:00
return {
2017-05-28 00:38:50 +02:00
basedir: path.resolve(basedir),
isFile: function (file, cb) {
cb(null, Object.prototype.hasOwnProperty.call(files, path.resolve(file)));
2016-10-10 03:43:44 +02:00
},
2017-05-28 00:38:50 +02:00
'package': { main: 'bar' },
readFile: function (file, cb) {
cb(null, files[path.resolve(file)]);
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
};
2016-10-10 03:43:44 +02:00
}
2017-05-28 00:38:50 +02:00
2016-10-10 03:43:44 +02:00
resolve('bar', opts('/foo'), function (err, res, pkg) {
2017-05-28 00:38:50 +02:00
if (err) return t.fail(err);
t.equal(res, path.resolve('/foo/node_modules/bar/baz.js'));
t.equal(pkg && pkg.main, './baz.js');
2016-10-10 03:43:44 +02:00
});
});