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