1'use strict'; 2 3// This test makes sure that `readFile()` always reads from the current 4// position of the file, instead of reading from the beginning of the file. 5 6const common = require('../common'); 7const assert = require('assert'); 8const path = require('path'); 9const { writeFileSync } = require('fs'); 10const { open } = require('fs').promises; 11 12const tmpdir = require('../common/tmpdir'); 13tmpdir.refresh(); 14 15const fn = path.join(tmpdir.path, 'test.txt'); 16writeFileSync(fn, 'Hello World'); 17 18async function readFileTest() { 19 const handle = await open(fn, 'r'); 20 21 /* Read only five bytes, so that the position moves to five. */ 22 const buf = Buffer.alloc(5); 23 const { bytesRead } = await handle.read(buf, 0, 5, null); 24 assert.strictEqual(bytesRead, 5); 25 assert.strictEqual(buf.toString(), 'Hello'); 26 27 /* readFile() should read from position five, instead of zero. */ 28 assert.strictEqual((await handle.readFile()).toString(), ' World'); 29 30 await handle.close(); 31} 32 33 34readFileTest() 35 .then(common.mustCall()); 36