Browse Source

fix corner-case bug in Module

and added a test to expose it
v0.7.4-release
David Siegel 15 years ago
committed by Ryan Dahl
parent
commit
2085909aeb
  1. 7
      lib/module.js
  2. 5
      test/simple/path.js
  3. 7
      test/simple/test-module-loading.js

7
lib/module.js

@ -179,7 +179,6 @@ function resolveModulePath(request, parent) {
var id, paths; var id, paths;
if (request.charAt(0) == "." && (request.charAt(1) == "/" || request.charAt(1) == ".")) { if (request.charAt(0) == "." && (request.charAt(1) == "/" || request.charAt(1) == ".")) {
// Relative request // Relative request
debug("RELATIVE: requested:" + request + " set ID to: "+id+" from "+parent.id);
var exts = ['js', 'node'], ext; var exts = ['js', 'node'], ext;
var extensions = Object.keys(extensionCache); var extensions = Object.keys(extensionCache);
@ -191,6 +190,12 @@ function resolveModulePath(request, parent) {
var parentIdPath = path.dirname(parent.id + var parentIdPath = path.dirname(parent.id +
(path.basename(parent.filename).match(new RegExp('^index\\.(' + exts.join('|') + ')$')) ? "/" : "")); (path.basename(parent.filename).match(new RegExp('^index\\.(' + exts.join('|') + ')$')) ? "/" : ""));
id = path.join(parentIdPath, request); id = path.join(parentIdPath, request);
// make sure require('./path') and require('path') get distinct ids, even
// when called from the toplevel js file
if (parentIdPath == '.' && ! id.match(new RegExp('/'))) {
id = './' + id;
}
debug("RELATIVE: requested:" + request + " set ID to: "+id+" from "+parent.id);
paths = [path.dirname(parent.filename)]; paths = [path.dirname(parent.filename)];
} else { } else {
id = request; id = request;

5
test/simple/path.js

@ -0,0 +1,5 @@
// This is actually more a fixture than a test. It is used to make
// sure that require('./path') and require('path') do different things.
// It has to be in the same directory as the test 'test-module-loading.js'
// and it has to have the same name as an internal module.
exports.path_func = function() { return "path_func"}

7
test/simple/test-module-loading.js

@ -48,6 +48,13 @@ var root = require("../fixtures/cycles/root"),
assert.equal(root.foo, foo); assert.equal(root.foo, foo);
assert.equal(root.sayHello(), root.hello); assert.equal(root.sayHello(), root.hello);
debug("test name clashes");
// this one exists and should import the local module
var my_path = require("./path");
assert.equal(true, my_path.path_func instanceof Function);
// this one does not exist and should throw
assert.throws(function() { require("./utils")});
var errorThrown = false; var errorThrown = false;
try { try {
require("../fixtures/throws_error"); require("../fixtures/throws_error");

Loading…
Cancel
Save