11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// This test makes sure that `writeFile()` always writes from the current
41cb0ef41Sopenharmony_ci// position of the file, instead of truncating the file.
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst common = require('../common');
71cb0ef41Sopenharmony_ciconst assert = require('assert');
81cb0ef41Sopenharmony_ciconst path = require('path');
91cb0ef41Sopenharmony_ciconst { readFileSync } = require('fs');
101cb0ef41Sopenharmony_ciconst { open } = require('fs').promises;
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
131cb0ef41Sopenharmony_citmpdir.refresh();
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst fn = path.join(tmpdir.path, 'test.txt');
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciasync function writeFileTest() {
181cb0ef41Sopenharmony_ci  const handle = await open(fn, 'w');
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  /* Write only five bytes, so that the position moves to five. */
211cb0ef41Sopenharmony_ci  const buf = Buffer.from('Hello');
221cb0ef41Sopenharmony_ci  const { bytesWritten } = await handle.write(buf, 0, 5, null);
231cb0ef41Sopenharmony_ci  assert.strictEqual(bytesWritten, 5);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  /* Write some more with writeFile(). */
261cb0ef41Sopenharmony_ci  await handle.writeFile('World');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  /* New content should be written at position five, instead of zero. */
291cb0ef41Sopenharmony_ci  assert.strictEqual(readFileSync(fn).toString(), 'HelloWorld');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  await handle.close();
321cb0ef41Sopenharmony_ci}
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ciwriteFileTest()
361cb0ef41Sopenharmony_ci  .then(common.mustCall());
37