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 d7237ca
Show file tree
Hide file tree
Showing 2 changed files with 27 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
26 changes: 26 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,29 @@ 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;
}
});

fs.readFile('unknown-size.txt', 'utf-8', function(err, data) {
assert.isNull(err);
assert.equal(data, 'unknown size');
done();
});
});
});

0 comments on commit d7237ca

Please sign in to comment.