From af9aa93e0ccb7f91a2aab8950868cf0a62f9bb2e Mon Sep 17 00:00:00 2001 From: Marco Rogers Date: Wed, 25 Aug 2010 00:36:08 -0400 Subject: [PATCH] fix for fs.readFile to return string when encoding specified on zero length read --- lib/fs.js | 2 +- test/simple/test-fs-readfile-empty.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index adaf5a4281c..7c8b38c05a5 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -56,7 +56,7 @@ fs.readFile = function (path, encoding_, callback) { function doRead() { if (size < 1) { binding.close(fd); - callback(null, buffer); + callback(null, encoding ? '' : buffer); return; } // position is offset or null so we can read files on unseekable mediums diff --git a/test/simple/test-fs-readfile-empty.js b/test/simple/test-fs-readfile-empty.js index 770d5e4e673..7b3ab1f2ddf 100644 --- a/test/simple/test-fs-readfile-empty.js +++ b/test/simple/test-fs-readfile-empty.js @@ -9,3 +9,7 @@ var fs.readFile(fn, function(err, data) { assert.ok(data); }); + +fs.readFile(fn, 'utf8', function(err, data) { + assert.strictEqual('', data); +}); \ No newline at end of file