Fix #325. Add test and check for zero-length file contents in fs.readFileSync
authorisaacs <i@izs.me>
Mon, 4 Oct 2010 19:22:59 +0000 (12:22 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Mon, 4 Oct 2010 19:50:58 +0000 (12:50 -0700)
lib/fs.js
test/simple/test-fs-readfile-empty.js

index 0bbb14b6d4bec15c4a9e6ee1995f5cded967e6bb..b5d1ae0aec800ebdf849d7fe7bea5b163b537143 100644 (file)
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -112,9 +112,11 @@ fs.readFileSync = function (path, encoding) {
       i.copy(buffer,offset,0,i._bytesRead);
       offset += i._bytesRead;
     })
-  } else {
+  } else if (buffers.length) {
     //buffers has exactly 1 (possibly zero length) buffer, so this should be a shortcut
     buffer = buffers[0].slice(0, buffers[0]._bytesRead);
+  } else {
+    buffer = new Buffer(0);
   }
 
   if (encoding) buffer = buffer.toString(encoding);
index 7b3ab1f2ddff0bbbe1893155e35fa99a173076c9..090f668e1fdeb2f7d565b9c055040c1fcde66999 100644 (file)
@@ -12,4 +12,7 @@ fs.readFile(fn, function(err, data) {
 
 fs.readFile(fn, 'utf8', function(err, data) {
   assert.strictEqual('', data);
-});
\ No newline at end of file
+});
+
+assert.ok(fs.readFileSync(fn));
+assert.strictEqual('', fs.readFileSync(fn, 'utf8'));