summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-promises-writefile-with-fd.js
blob: 446f9b6d8a11942a782c769e24420cc5aa68ff73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';

/*
 * This test makes sure that `writeFile()` always writes from the current
 * position of the file, instead of truncating the file.
 */

const common = require('../common');
const assert = require('assert');
const path = require('path');
const { readFileSync } = require('fs');
const { open } = require('fs').promises;

const tmpdir = require('../common/tmpdir');
tmpdir.refresh();

const fn = path.join(tmpdir.path, 'test.txt');

async function writeFileTest() {
  const handle = await open(fn, 'w');

  /* Write only five bytes, so that the position moves to five. */
  const buf = Buffer.from('Hello');
  const { bytesWritten } = await handle.write(buf, 0, 5, null);
  assert.strictEqual(bytesWritten, 5);

  /* Write some more with writeFile(). */
  await handle.writeFile('World');

  /* New content should be written at position five, instead of zero. */
  assert.deepStrictEqual(readFileSync(fn).toString(), 'HelloWorld');

  await handle.close();
}


writeFileTest()
  .then(common.mustCall());