Skip to content

Commit

Permalink
readfilecontext: added test for reading file with unknown length
Browse files Browse the repository at this point in the history
  • Loading branch information
Rugvip committed Sep 17, 2021
1 parent bef0a6c commit c175af6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/readfilecontext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
27 changes: 27 additions & 0 deletions test/lib/readfilecontext.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const constants = require('constants');
const helper = require('../helper');
const fs = require('fs');
const mock = require('../../lib/index');
Expand Down Expand Up @@ -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();
});
});
});

0 comments on commit c175af6

Please # to comment.