Skip to content

Commit

Permalink
fix: readFile on dir should fail EISDIR not EBADF
Browse files Browse the repository at this point in the history
closes #285
  • Loading branch information
3cp committed Apr 20, 2020
1 parent 83216f3 commit b6f6361
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/binding.js
Expand Up @@ -645,6 +645,9 @@ Binding.prototype.read = function(
throw new FSError('EBADF');
}
const file = descriptor.getItem();
if (file instanceof Directory) {
throw new FSError('EISDIR');
}
if (!(file instanceof File)) {
// deleted or not a regular file
throw new FSError('EBADF');
Expand Down
12 changes: 8 additions & 4 deletions test/lib/fs.readFile.spec.js
Expand Up @@ -42,7 +42,7 @@ describe('fs.readFile(filename, [options], callback)', function() {
it('fails for directory', function(done) {
fs.readFile('path/to', function(err, data) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EBADF');
assert.equal(err.code, 'EISDIR');
done();
});
});
Expand All @@ -55,7 +55,7 @@ describe('fs.readFile(filename, [options], callback)', function() {
},
function(err) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EBADF');
assert.equal(err.code, 'EISDIR');
done();
}
);
Expand Down Expand Up @@ -104,9 +104,13 @@ describe('fs.readFileSync(filename, [options])', function() {
});

it('fails for directory', function() {
assert.throws(function() {
try {
fs.readFileSync('path/to');
});
assert.fail('should not succeed.');
} catch (err) {
assert.instanceOf(err, Error);
assert.equal(err.code, 'EISDIR');
}
});

it('fails for bad path', function() {
Expand Down

0 comments on commit b6f6361

Please sign in to comment.