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 Jan 3, 2020
1 parent ad93ac5 commit 7fe378d
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
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,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
Original file line number Diff line number Diff line change
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 7fe378d

Please sign in to comment.