1'use strict'; 2 3const common = require('../common'); 4const fixtures = require('../common/fixtures'); 5const fs = require('fs'); 6const assert = require('assert'); 7const filepath = fixtures.path('x.txt'); 8 9const expected = Buffer.from('xyz\n'); 10const defaultBufferAsync = Buffer.alloc(16384); 11const bufferAsOption = Buffer.allocUnsafe(expected.byteLength); 12 13function testValid(message, ...options) { 14 const paramsMsg = `${message} (as params)`; 15 const paramsFilehandle = fs.openSync(filepath, 'r'); 16 fs.read(paramsFilehandle, ...options, common.mustSucceed((bytesRead, buffer) => { 17 assert.strictEqual(bytesRead, expected.byteLength, paramsMsg); 18 assert.deepStrictEqual(defaultBufferAsync.byteLength, buffer.byteLength, paramsMsg); 19 fs.closeSync(paramsFilehandle); 20 })); 21 22 const optionsMsg = `${message} (as options)`; 23 const optionsFilehandle = fs.openSync(filepath, 'r'); 24 fs.read(optionsFilehandle, bufferAsOption, ...options, common.mustSucceed((bytesRead, buffer) => { 25 assert.strictEqual(bytesRead, expected.byteLength, optionsMsg); 26 assert.deepStrictEqual(bufferAsOption.byteLength, buffer.byteLength, optionsMsg); 27 fs.closeSync(optionsFilehandle); 28 })); 29} 30 31testValid('Not passing in any object'); 32testValid('Passing in a null', null); 33testValid('Passing in an empty object', common.mustNotMutateObjectDeep({})); 34testValid('Passing in an object', common.mustNotMutateObjectDeep({ 35 offset: 0, 36 length: bufferAsOption.byteLength, 37 position: 0, 38})); 39