From c175af659ceb16234d6a9130d5ec8c135332ed01 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 17 Sep 2021 13:19:24 +0200 Subject: [PATCH] readfilecontext: added test for reading file with unknown length --- lib/readfilecontext.js | 2 +- test/lib/readfilecontext.js | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/readfilecontext.js b/lib/readfilecontext.js index 699b6cb..4bd66ee 100644 --- a/lib/readfilecontext.js +++ b/lib/readfilecontext.js @@ -62,7 +62,7 @@ exports.patchReadFileContext = function(prototype) { bytesRead === kReadFileUnknownBufferLength ? context.buffer : context.buffer.slice(0, bytesRead); - Array.prototype.push.apply(context.buffers, buffer); + context.buffers.push(buffer); } context.read(); } diff --git a/test/lib/readfilecontext.js b/test/lib/readfilecontext.js index f82475d..4e1f4a1 100644 --- a/test/lib/readfilecontext.js +++ b/test/lib/readfilecontext.js @@ -1,5 +1,6 @@ 'use strict'; +const constants = require('constants'); const helper = require('../helper'); const fs = require('fs'); const mock = require('../../lib/index'); @@ -145,4 +146,30 @@ describe('fs.readFile() with ReadFileContext', function() { done(); }); }); + + it('allows file reads with unknown size', function(done) { + mock({ + 'unknown-size.txt': function() { + const file = mock.file({ + content: Buffer.from('unknown size') + })(); + + // Override getStats to drop the S_IFREG flag + const origGetStats = file.getStats; + file.getStats = function() { + const stats = origGetStats.apply(this, arguments); + stats[1] ^= constants.S_IFREG; + return stats; + }; + return file; + } + }); + // This isn't actually supported by mock-fs, but let's make sure the call goes through + // It also covers the case of reading an empty file and reading with encoding + fs.readFile('unknown-size.txt', 'utf-8', function(err, data) { + assert.isNull(err); + assert.equal(data, 'unknown size'); + done(); + }); + }); });