Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix api differences #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = function resolve(x, options, callback) {
var res;
function validBasedir(basedir) {
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
res = path.resolve(basedir, x);
res = path.normalize(path.join(basedir, x));
if (x === '..' || x.slice(-1) === '/') res += '/';
if ((/\/$/).test(x) && res === basedir) {
loadAsDirectory(res, opts.package, onfile);
Expand Down
2 changes: 1 addition & 1 deletion lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = function (x, options) {
}

if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
var res = path.resolve(absoluteStart, x);
var res = path.normalize(path.join(basedir, x));
if (x === '..' || x.slice(-1) === '/') res += '/';
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
if (m) return m;
Expand Down
17 changes: 17 additions & 0 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ test('async: #121 - treating an existing file as a dir when no basedir', functio
});
});

t.test('with a trailing slash', function (st) {
st.plan(4);

resolve('./' + testFile + '/', function (err, res, pkg) {
st.ok(err, 'there is an error');
st.notOk(res, 'no result');

st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
st.equal(
err && err.message,
'Cannot find module \'./' + testFile + '/\' from \'' + __dirname + '\'',
'can not find nonexistent module'
);
st.end();
});
});

t.test('with a fake directory', function (st) {
st.plan(4);

Expand Down
22 changes: 22 additions & 0 deletions test/resolver_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,28 @@ test('sync: #121 - treating an existing file as a dir when no basedir', function
st.end();
});

t.test('with a trailing slash', function (st) {
var path = './' + testFile + '/';
function run() { return resolve.sync(path); }

var result, error;
try {
result = run();
} catch (e) {
error = e;
}
st.ok(error, 'there is an error');
st.notOk(result, 'no result');
st.equal(error && error.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve');
st.equal(
error && error.message,
'Cannot find module \'' + path + '\' from \'' + __dirname + '\'',
'can not find nonexistent module'
);

st.end();
});

t.test('with a fake directory', function (st) {
function run() { return resolve.sync('./' + testFile + '/blah'); }

Expand Down