From 7fe378dfd20451e012d0369c47cc8b0b926394dc Mon Sep 17 00:00:00 2001 From: Chunpeng Huo Date: Fri, 3 Jan 2020 23:06:13 +1100 Subject: [PATCH] fix: readFile on dir should fail EISDIR not EBADF closes #285 --- lib/binding.js | 3 +++ test/lib/fs.readFile.spec.js | 12 ++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/binding.js b/lib/binding.js index bc4c6cb3..e9fbb231 100644 --- a/lib/binding.js +++ b/lib/binding.js @@ -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'); diff --git a/test/lib/fs.readFile.spec.js b/test/lib/fs.readFile.spec.js index 233f2063..a7935f4b 100644 --- a/test/lib/fs.readFile.spec.js +++ b/test/lib/fs.readFile.spec.js @@ -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(); }); }); @@ -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(); } ); @@ -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() {