1'use strict'; 2 3// This test makes sure that `writeFile()` always writes from the current 4// position of the file, instead of truncating the file. 5 6const common = require('../common'); 7const assert = require('assert'); 8const path = require('path'); 9const { readFileSync } = require('fs'); 10const { open } = require('fs').promises; 11 12const tmpdir = require('../common/tmpdir'); 13tmpdir.refresh(); 14 15const fn = path.join(tmpdir.path, 'test.txt'); 16 17async function writeFileTest() { 18 const handle = await open(fn, 'w'); 19 20 /* Write only five bytes, so that the position moves to five. */ 21 const buf = Buffer.from('Hello'); 22 const { bytesWritten } = await handle.write(buf, 0, 5, null); 23 assert.strictEqual(bytesWritten, 5); 24 25 /* Write some more with writeFile(). */ 26 await handle.writeFile('World'); 27 28 /* New content should be written at position five, instead of zero. */ 29 assert.strictEqual(readFileSync(fn).toString(), 'HelloWorld'); 30 31 await handle.close(); 32} 33 34 35writeFileTest() 36 .then(common.mustCall()); 37