2010-07-15 20:47:25 +02:00
|
|
|
common = require("../common");
|
|
|
|
assert = common.assert
|
2010-09-20 03:50:26 +02:00
|
|
|
var path = require('path'),
|
|
|
|
fs = require('fs');
|
2009-09-28 12:06:30 +02:00
|
|
|
|
2010-07-15 20:47:25 +02:00
|
|
|
common.debug("load test-module-loading.js");
|
2009-09-28 12:06:30 +02:00
|
|
|
|
2010-02-25 07:36:17 +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-02-25 07:36:17 +01:00
|
|
|
var d3 = require(path.join(__dirname, "../fixtures/b/d"));
|
2009-11-02 21:21:02 +01:00
|
|
|
// Relative
|
2010-02-25 07:36:17 +01:00
|
|
|
var d4 = require("../fixtures/b/d");
|
2009-06-08 15:30:10 +02:00
|
|
|
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +01:00
|
|
|
assert.equal("D", d4.D());
|
2009-11-02 21:21:02 +01:00
|
|
|
|
2010-02-11 12:09:46 +01:00
|
|
|
assert.ok((new a.SomeClass) instanceof c.SomeClass);
|
|
|
|
|
2010-07-15 20:47:25 +02:00
|
|
|
common.debug("test index.js modules ids and relative loading")
|
2010-02-25 07:36:17 +01:00
|
|
|
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);
|
|
|
|
|
2010-07-15 20:47:25 +02:00
|
|
|
common.debug("test cycles containing a .. path");
|
2010-02-25 07:36:17 +01:00
|
|
|
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);
|
|
|
|
|
2010-07-15 20:47:25 +02:00
|
|
|
common.debug("test name clashes");
|
2010-07-01 20:19:54 +02:00
|
|
|
// this one exists and should import the local module
|
|
|
|
var my_path = require("./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
|
|
|
|
assert.throws(function() { require("./utils")});
|
|
|
|
|
2009-12-18 23:58:04 +01:00
|
|
|
var errorThrown = false;
|
|
|
|
try {
|
2010-02-25 07:36:17 +01:00
|
|
|
require("../fixtures/throws_error");
|
2009-12-18 23:58:04 +01:00
|
|
|
} catch (e) {
|
|
|
|
errorThrown = true;
|
|
|
|
assert.equal("blah", e.message);
|
|
|
|
}
|
|
|
|
|
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');
|
|
|
|
require.extensions['.test'] = function (module, filename) {
|
|
|
|
var content = fs.readFileSync(filename).toString();
|
2010-03-08 05:35:39 +01:00
|
|
|
assert.equal("this is custom source\n", content);
|
2010-09-20 03:50:26 +02:00
|
|
|
content = content.replace("this is custom source", "exports.test = 'passed'");
|
|
|
|
module._compile(content, filename);
|
|
|
|
};
|
2010-03-08 05:35:39 +01:00
|
|
|
|
|
|
|
assert.equal(require('../fixtures/registerExt').test, "passed");
|
2010-09-22 01:51:55 +02:00
|
|
|
// unknown extension, load as .js
|
|
|
|
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-09-20 03:50:26 +02:00
|
|
|
require.extensions['.test'] = function (module, filename) {
|
|
|
|
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-08-17 17:21:43 +02:00
|
|
|
common.debug("load modules by absolute id, then change require.paths, and load another module with the same absolute id.");
|
Cache modules based on filename rather than ID
This is ever so slightly less efficient than caching based on ID, since the
filename has to be looked up before we can check the cache. However, it's
the most minimal approach possible to get this change in place. Since
require() is a blocking startup-time operation anyway, a bit of slowness is
not a huge problem.
A test involving require.paths modification and absolute loading. Here's the
gist of it.
Files: /p1/foo.js /p2/foo.js
1. Add "/p1" to require.paths.
2. foo1 = require("foo")
3. assert foo1 === require("/p1/foo") (fail)
4. Remove /p1 from require.paths.
5. Add /p2 to require.paths.
6. foo2 = require("foo")
7. assert foo1 !== foo2 (fail)
8. assert foo2 === require("/p2/foo") (fail)
It's an edge case, but it affects how dependencies are mapped by npm.
If your module requires foo-1.2.3, and my module requires foo-2.3.4,
then you should expect to have require("foo") give you foo-1.2.3, and
I should expect require("foo") to give me foo-2.3.4. However, with
module ID based caching, if your code loads *first*, then your "foo"
is THE "foo", so I'll get your version instead of mine.
It hasn't yet been a problem, but only because there are so few
modules, and everyone pretty much uses the latest version all the
time. But as things start to get to the 1.x and 2.x versions, it'll
be an issue, I'm sure. Dependency hell isn't fun, so this is a way to
avoid it before it strikes.
2010-07-15 19:41:40 +02:00
|
|
|
// this will throw if it fails.
|
|
|
|
var foo = require("../fixtures/require-path/p1/foo");
|
|
|
|
process.assert(foo.bar.expect === foo.bar.actual);
|
2010-03-08 05:35:39 +01:00
|
|
|
|
2010-07-20 02:54:49 +02:00
|
|
|
assert.equal(require('../fixtures/foo').foo, 'ok',
|
|
|
|
'require module with no extension');
|
|
|
|
|
2010-08-04 22:45:52 +02:00
|
|
|
// Should not attempt to load a directory
|
|
|
|
try {
|
|
|
|
require("../fixtures/empty");
|
|
|
|
} catch(err) {
|
|
|
|
assert.equal(err.message, "Cannot find module '../fixtures/empty'");
|
|
|
|
}
|
|
|
|
|
2010-08-04 23:55:24 +02:00
|
|
|
// Check load order is as expected
|
|
|
|
common.debug('load order');
|
|
|
|
|
|
|
|
var loadOrder = '../fixtures/module-load-order/',
|
|
|
|
msg = "Load order incorrect.";
|
|
|
|
|
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
|
|
|
|
|
|
|
assert.equal(require(loadOrder + 'file1').file1, 'file1', msg);
|
|
|
|
assert.equal(require(loadOrder + 'file2').file2, 'file2.js', msg);
|
|
|
|
try {
|
|
|
|
require(loadOrder + 'file3');
|
|
|
|
} catch (e) {
|
|
|
|
// Not a real .node module, but we know we require'd the right thing.
|
|
|
|
assert.ok(e.message.match(/file3\.node/));
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
try {
|
|
|
|
require(loadOrder + 'file7');
|
|
|
|
} catch (e) {
|
|
|
|
assert.ok(e.message.match(/file7\/index\.node/));
|
|
|
|
}
|
|
|
|
assert.equal(require(loadOrder + 'file8').file8, 'file8/index.reg', msg);
|
|
|
|
assert.equal(require(loadOrder + 'file9').file9, 'file9/index.reg2', msg);
|
|
|
|
|
2009-08-26 18:51:04 +02:00
|
|
|
process.addListener("exit", function () {
|
2010-08-17 22:19:19 +02:00
|
|
|
assert.ok(common.indirectInstanceOf(a.A, Function));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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));
|
2009-11-28 18:26:59 +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-06-24 02:40:51 +02:00
|
|
|
console.log("exit");
|
2009-08-26 18:51:04 +02:00
|
|
|
});
|