2015-05-19 13:00:06 +02:00
|
|
|
'use strict';
|
2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-12-04 23:45:52 +01:00
|
|
|
var path = require('path');
|
|
|
|
var fs = require('fs');
|
2009-09-28 12:06:30 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
common.debug('load test-module-loading.js');
|
2009-09-28 12:06:30 +02:00
|
|
|
|
2011-02-02 18:56:32 +01:00
|
|
|
// assert that this is the main module.
|
|
|
|
assert.equal(require.main.id, '.', 'main module should have id of \'.\'');
|
|
|
|
assert.equal(require.main, module, 'require.main should === module');
|
|
|
|
assert.equal(process.mainModule, module,
|
|
|
|
'process.mainModule should === module');
|
|
|
|
// assert that it's *not* the main module in the required module.
|
|
|
|
require('../fixtures/not-main-module.js');
|
|
|
|
|
2010-12-01 23:31:20 +01:00
|
|
|
// require a file with a request that includes the extension
|
2010-12-04 23:45:52 +01:00
|
|
|
var a_js = require('../fixtures/a.js');
|
2010-12-01 23:31:20 +01:00
|
|
|
assert.equal(42, a_js.number);
|
|
|
|
|
|
|
|
// require a file without any extensions
|
2010-12-04 23:45:52 +01:00
|
|
|
var foo_no_ext = require('../fixtures/foo');
|
|
|
|
assert.equal('ok', foo_no_ext.foo);
|
2010-12-01 23:31:20 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
var a = require('../fixtures/a');
|
|
|
|
var c = require('../fixtures/b/c');
|
|
|
|
var d = require('../fixtures/b/d');
|
|
|
|
var d2 = require('../fixtures/b/d');
|
2009-11-02 21:21:02 +01:00
|
|
|
// Absolute
|
2010-12-04 23:45:52 +01:00
|
|
|
var d3 = require(path.join(__dirname, '../fixtures/b/d'));
|
2009-11-02 21:21:02 +01:00
|
|
|
// Relative
|
2010-12-04 23:45:52 +01:00
|
|
|
var d4 = require('../fixtures/b/d');
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal(false, false, 'testing the test program.');
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-08-17 17:21:43 +02:00
|
|
|
assert.equal(true, common.indirectInstanceOf(a.A, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('A', a.A());
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-08-17 17:21:43 +02:00
|
|
|
assert.equal(true, common.indirectInstanceOf(a.C, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('C', a.C());
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-08-17 17:21:43 +02:00
|
|
|
assert.equal(true, common.indirectInstanceOf(a.D, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('D', a.D());
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-08-17 17:21:43 +02:00
|
|
|
assert.equal(true, common.indirectInstanceOf(d.D, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('D', d.D());
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2010-08-17 17:21:43 +02:00
|
|
|
assert.equal(true, common.indirectInstanceOf(d2.D, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('D', d2.D());
|
2009-06-08 16:17:33 +02:00
|
|
|
|
2010-08-17 17:21:43 +02:00
|
|
|
assert.equal(true, common.indirectInstanceOf(d3.D, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('D', d3.D());
|
2009-11-02 00:34:16 +01:00
|
|
|
|
2010-08-17 17:21:43 +02:00
|
|
|
assert.equal(true, common.indirectInstanceOf(d4.D, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('D', d4.D());
|
2009-11-02 21:21:02 +01:00
|
|
|
|
2015-05-19 13:00:06 +02:00
|
|
|
assert.ok((new a.SomeClass()) instanceof c.SomeClass);
|
2010-02-11 12:09:46 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
common.debug('test index.js modules ids and relative loading');
|
|
|
|
var one = require('../fixtures/nested-index/one'),
|
|
|
|
two = require('../fixtures/nested-index/two');
|
2009-12-22 16:24:32 +01:00
|
|
|
assert.notEqual(one.hello, two.hello);
|
|
|
|
|
2011-01-27 22:14:47 +01:00
|
|
|
common.debug('test index.js in a folder with a trailing slash');
|
|
|
|
var three = require('../fixtures/nested-index/three'),
|
|
|
|
threeFolder = require('../fixtures/nested-index/three/'),
|
|
|
|
threeIndex = require('../fixtures/nested-index/three/index.js');
|
|
|
|
assert.equal(threeFolder, threeIndex);
|
|
|
|
assert.notEqual(threeFolder, three);
|
|
|
|
|
2011-01-28 02:57:45 +01:00
|
|
|
common.debug('test package.json require() loading');
|
|
|
|
assert.equal(require('../fixtures/packages/main').ok, 'ok',
|
|
|
|
'Failed loading package');
|
2011-02-17 21:00:39 +01:00
|
|
|
assert.equal(require('../fixtures/packages/main-index').ok, 'ok',
|
|
|
|
'Failed loading package with index.js in main subdir');
|
2011-01-28 02:57:45 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
common.debug('test cycles containing a .. path');
|
|
|
|
var root = require('../fixtures/cycles/root'),
|
|
|
|
foo = require('../fixtures/cycles/folder/foo');
|
2010-02-11 12:09:46 +01:00
|
|
|
assert.equal(root.foo, foo);
|
2009-12-22 16:24:32 +01:00
|
|
|
assert.equal(root.sayHello(), root.hello);
|
|
|
|
|
2011-02-09 22:28:30 +01:00
|
|
|
common.debug('test node_modules folders');
|
|
|
|
// asserts are in the fixtures files themselves,
|
|
|
|
// since they depend on the folder structure.
|
|
|
|
require('../fixtures/node_modules/foo');
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
common.debug('test name clashes');
|
2010-07-01 20:19:54 +02:00
|
|
|
// this one exists and should import the local module
|
2014-12-17 14:30:04 +01:00
|
|
|
var my_path = require('../fixtures/path');
|
2010-08-17 17:21:43 +02:00
|
|
|
assert.ok(common.indirectInstanceOf(my_path.path_func, Function));
|
2010-07-01 20:19:54 +02:00
|
|
|
// this one does not exist and should throw
|
2015-05-19 13:00:06 +02:00
|
|
|
assert.throws(function() { require('./utils'); });
|
2010-07-01 20:19:54 +02:00
|
|
|
|
2009-12-18 23:58:04 +01:00
|
|
|
var errorThrown = false;
|
|
|
|
try {
|
2010-12-04 23:45:52 +01:00
|
|
|
require('../fixtures/throws_error');
|
2009-12-18 23:58:04 +01:00
|
|
|
} catch (e) {
|
|
|
|
errorThrown = true;
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('blah', e.message);
|
2009-12-18 23:58:04 +01:00
|
|
|
}
|
|
|
|
|
2010-01-16 13:51:57 +01:00
|
|
|
assert.equal(require('path').dirname(__filename), __dirname);
|
|
|
|
|
2010-09-20 03:50:26 +02:00
|
|
|
common.debug('load custom file types with extensions');
|
2010-12-04 23:45:52 +01:00
|
|
|
require.extensions['.test'] = function(module, filename) {
|
2010-09-20 03:50:26 +02:00
|
|
|
var content = fs.readFileSync(filename).toString();
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('this is custom source\n', content);
|
|
|
|
content = content.replace('this is custom source',
|
|
|
|
'exports.test = \'passed\'');
|
2010-09-20 03:50:26 +02:00
|
|
|
module._compile(content, filename);
|
|
|
|
};
|
2010-03-08 05:35:39 +01:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal(require('../fixtures/registerExt').test, 'passed');
|
2010-09-22 01:51:55 +02:00
|
|
|
// unknown extension, load as .js
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal(require('../fixtures/registerExt.hello.world').test, 'passed');
|
2010-03-08 05:35:39 +01:00
|
|
|
|
2010-07-15 20:47:25 +02:00
|
|
|
common.debug('load custom file types that return non-strings');
|
2010-12-04 23:45:52 +01:00
|
|
|
require.extensions['.test'] = function(module, filename) {
|
2010-09-20 03:50:26 +02:00
|
|
|
module.exports = {
|
2010-03-08 05:35:39 +01:00
|
|
|
custom: 'passed'
|
|
|
|
};
|
2010-09-20 03:50:26 +02:00
|
|
|
};
|
2010-03-08 05:35:39 +01:00
|
|
|
|
|
|
|
assert.equal(require('../fixtures/registerExt2').custom, 'passed');
|
|
|
|
|
2010-07-20 02:54:49 +02:00
|
|
|
assert.equal(require('../fixtures/foo').foo, 'ok',
|
2010-12-04 23:45:52 +01:00
|
|
|
'require module with no extension');
|
2010-07-20 02:54:49 +02:00
|
|
|
|
2011-07-15 23:05:01 +02:00
|
|
|
assert.throws(function() {
|
|
|
|
require.paths;
|
|
|
|
}, /removed/, 'Accessing require.paths should throw.');
|
|
|
|
|
2010-08-04 22:45:52 +02:00
|
|
|
// Should not attempt to load a directory
|
|
|
|
try {
|
2010-12-04 23:45:52 +01:00
|
|
|
require('../fixtures/empty');
|
|
|
|
} catch (err) {
|
|
|
|
assert.equal(err.message, 'Cannot find module \'../fixtures/empty\'');
|
2010-08-04 22:45:52 +02:00
|
|
|
}
|
|
|
|
|
2010-08-04 23:55:24 +02:00
|
|
|
// Check load order is as expected
|
|
|
|
common.debug('load order');
|
|
|
|
|
|
|
|
var loadOrder = '../fixtures/module-load-order/',
|
2010-12-04 23:45:52 +01:00
|
|
|
msg = 'Load order incorrect.';
|
2010-08-04 23:55:24 +02:00
|
|
|
|
2010-09-20 03:50:26 +02:00
|
|
|
require.extensions['.reg'] = require.extensions['.js'];
|
|
|
|
require.extensions['.reg2'] = require.extensions['.js'];
|
2010-08-04 23:55:24 +02:00
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal(require(loadOrder + 'file1').file1, 'file1', msg);
|
|
|
|
assert.equal(require(loadOrder + 'file2').file2, 'file2.js', msg);
|
2010-08-04 23:55:24 +02:00
|
|
|
try {
|
|
|
|
require(loadOrder + 'file3');
|
|
|
|
} catch (e) {
|
|
|
|
// Not a real .node module, but we know we require'd the right thing.
|
2011-11-05 01:48:24 +01:00
|
|
|
assert.ok(e.message.replace(/\\/g, '/').match(/file3\.node/));
|
2010-08-04 23:55:24 +02:00
|
|
|
}
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal(require(loadOrder + 'file4').file4, 'file4.reg', msg);
|
|
|
|
assert.equal(require(loadOrder + 'file5').file5, 'file5.reg2', msg);
|
|
|
|
assert.equal(require(loadOrder + 'file6').file6, 'file6/index.js', msg);
|
2010-08-04 23:55:24 +02:00
|
|
|
try {
|
|
|
|
require(loadOrder + 'file7');
|
|
|
|
} catch (e) {
|
2011-11-05 01:48:24 +01:00
|
|
|
assert.ok(e.message.replace(/\\/g, '/').match(/file7\/index\.node/));
|
2010-08-04 23:55:24 +02:00
|
|
|
}
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal(require(loadOrder + 'file8').file8, 'file8/index.reg', msg);
|
2010-08-04 23:55:24 +02:00
|
|
|
assert.equal(require(loadOrder + 'file9').file9, 'file9/index.reg2', msg);
|
|
|
|
|
2011-06-12 19:36:05 +02:00
|
|
|
|
2011-07-14 22:55:51 +02:00
|
|
|
// make sure that module.require() is the same as
|
|
|
|
// doing require() inside of that module.
|
|
|
|
var parent = require('../fixtures/module-require/parent/');
|
|
|
|
var child = require('../fixtures/module-require/child/');
|
|
|
|
assert.equal(child.loaded, parent.loaded);
|
|
|
|
|
|
|
|
|
2011-07-21 02:39:23 +02:00
|
|
|
// #1357 Loading JSON files with require()
|
|
|
|
var json = require('../fixtures/packages/main/package.json');
|
2011-10-05 00:08:18 +02:00
|
|
|
assert.deepEqual(json, {
|
|
|
|
name: 'package-name',
|
|
|
|
version: '1.2.3',
|
|
|
|
main: 'package-main-module'
|
|
|
|
});
|
2011-07-21 02:39:23 +02:00
|
|
|
|
|
|
|
|
2012-03-12 18:18:29 +01:00
|
|
|
// now verify that module.children contains all the different
|
|
|
|
// modules that we've required, and that all of them contain
|
|
|
|
// the appropriate children, and so on.
|
|
|
|
|
|
|
|
var children = module.children.reduce(function red(set, child) {
|
2015-05-19 13:00:06 +02:00
|
|
|
var id = path.relative(path.dirname(__dirname), child.id);
|
2012-06-12 02:04:44 +02:00
|
|
|
id = id.replace(/\\/g, '/');
|
2012-03-12 18:18:29 +01:00
|
|
|
set[id] = child.children.reduce(red, {});
|
|
|
|
return set;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
assert.deepEqual(children, {
|
|
|
|
'common.js': {},
|
|
|
|
'fixtures/not-main-module.js': {},
|
|
|
|
'fixtures/a.js': {
|
|
|
|
'fixtures/b/c.js': {
|
|
|
|
'fixtures/b/d.js': {},
|
|
|
|
'fixtures/b/package/index.js': {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'fixtures/foo': {},
|
|
|
|
'fixtures/nested-index/one/index.js': {
|
|
|
|
'fixtures/nested-index/one/hello.js': {}
|
|
|
|
},
|
|
|
|
'fixtures/nested-index/two/index.js': {
|
|
|
|
'fixtures/nested-index/two/hello.js': {}
|
|
|
|
},
|
|
|
|
'fixtures/nested-index/three.js': {},
|
|
|
|
'fixtures/nested-index/three/index.js': {},
|
|
|
|
'fixtures/packages/main/package-main-module.js': {},
|
|
|
|
'fixtures/packages/main-index/package-main-module/index.js': {},
|
|
|
|
'fixtures/cycles/root.js': {
|
|
|
|
'fixtures/cycles/folder/foo.js': {}
|
|
|
|
},
|
|
|
|
'fixtures/node_modules/foo.js': {
|
|
|
|
'fixtures/node_modules/baz/index.js': {
|
|
|
|
'fixtures/node_modules/bar.js': {},
|
|
|
|
'fixtures/node_modules/baz/node_modules/asdf.js': {}
|
|
|
|
}
|
|
|
|
},
|
2014-12-17 14:30:04 +01:00
|
|
|
'fixtures/path.js': {},
|
2012-03-12 18:18:29 +01:00
|
|
|
'fixtures/throws_error.js': {},
|
|
|
|
'fixtures/registerExt.test': {},
|
|
|
|
'fixtures/registerExt.hello.world': {},
|
|
|
|
'fixtures/registerExt2.test': {},
|
|
|
|
'fixtures/empty.js': {},
|
|
|
|
'fixtures/module-load-order/file1': {},
|
|
|
|
'fixtures/module-load-order/file2.js': {},
|
|
|
|
'fixtures/module-load-order/file3.node': {},
|
|
|
|
'fixtures/module-load-order/file4.reg': {},
|
|
|
|
'fixtures/module-load-order/file5.reg2': {},
|
|
|
|
'fixtures/module-load-order/file6/index.js': {},
|
|
|
|
'fixtures/module-load-order/file7/index.node': {},
|
|
|
|
'fixtures/module-load-order/file8/index.reg': {},
|
|
|
|
'fixtures/module-load-order/file9/index.reg2': {},
|
|
|
|
'fixtures/module-require/parent/index.js': {
|
|
|
|
'fixtures/module-require/child/index.js': {
|
|
|
|
'fixtures/module-require/child/node_modules/target.js': {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
'fixtures/packages/main/package.json': {}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2013-01-16 21:27:53 +01:00
|
|
|
// require() must take string, and must be truthy
|
|
|
|
assert.throws(function() {
|
|
|
|
console.error('require non-string');
|
|
|
|
require({ foo: 'bar' });
|
|
|
|
}, 'path must be a string');
|
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
console.error('require empty string');
|
|
|
|
require('');
|
|
|
|
}, 'missing path');
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2010-08-17 22:19:19 +02:00
|
|
|
assert.ok(common.indirectInstanceOf(a.A, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('A done', a.A());
|
2009-06-08 16:17:33 +02:00
|
|
|
|
2010-08-17 22:19:19 +02:00
|
|
|
assert.ok(common.indirectInstanceOf(a.C, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('C done', a.C());
|
2009-06-08 16:17:33 +02:00
|
|
|
|
2010-08-17 22:19:19 +02:00
|
|
|
assert.ok(common.indirectInstanceOf(a.D, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('D done', a.D());
|
2009-06-08 16:17:33 +02:00
|
|
|
|
2010-08-17 22:19:19 +02:00
|
|
|
assert.ok(common.indirectInstanceOf(d.D, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('D done', d.D());
|
2009-06-08 16:17:33 +02:00
|
|
|
|
2010-08-17 22:19:19 +02:00
|
|
|
assert.ok(common.indirectInstanceOf(d2.D, Function));
|
2010-12-04 23:45:52 +01:00
|
|
|
assert.equal('D done', d2.D());
|
2009-09-18 15:45:47 +02:00
|
|
|
|
2009-12-18 23:58:04 +01:00
|
|
|
assert.equal(true, errorThrown);
|
|
|
|
|
2010-12-04 23:45:52 +01:00
|
|
|
console.log('exit');
|
2009-08-26 18:51:04 +02:00
|
|
|
});
|
2011-08-02 23:18:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
// #1440 Loading files with a byte order marker.
|
|
|
|
assert.equal(42, require('../fixtures/utf8-bom.js'));
|
|
|
|
assert.equal(42, require('../fixtures/utf8-bom.json'));
|