summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-write-stream-file-handle-2.js
blob: 25f68b14da70b39ddd0ae58c7de10d1fe456452d (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
'use strict';
const common = require('../common');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const tmpdir = require('../common/tmpdir');
const file = path.join(tmpdir.path, 'write_stream_filehandle_test.txt');
const input = 'hello world';

tmpdir.refresh();

fs.promises.open(file, 'w+').then((handle) => {
  let calls = 0;
  const {
    write: originalWriteFunction,
    writev: originalWritevFunction
  } = handle;
  handle.write = function write() {
    calls++;
    return Reflect.apply(originalWriteFunction, this, arguments);
  };
  handle.writev = function writev() {
    calls++;
    return Reflect.apply(originalWritevFunction, this, arguments);
  };
  const stream = fs.createWriteStream(null, { fd: handle });

  stream.end(input);
  stream.on('close', common.mustCall(() => {
    assert(calls > 0, 'expected at least one call to fileHandle.write or ' +
    'fileHandle.writev, got 0');
  }));
}).then(common.mustCall());