summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-stream-construct-compat-error-write.js
blob: b47632c2c95e2f552053b014bfd64a15344b66b2 (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
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';

const common = require('../common');
const fs = require('fs');
const assert = require('assert');

const debuglog = (arg) => {
  console.log(new Date().toLocaleString(), arg);
};

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

{
  // Compat error.
  debuglog('start test');

  function WriteStream(...args) {
    debuglog('WriteStream constructor');
    fs.WriteStream.call(this, ...args);
  }
  Object.setPrototypeOf(WriteStream.prototype, fs.WriteStream.prototype);
  Object.setPrototypeOf(WriteStream, fs.WriteStream);

  WriteStream.prototype.open = common.mustCall(function WriteStream$open() {
    debuglog('WriteStream open() callback');
    const that = this;
    fs.open(that.path, that.flags, that.mode, (err, fd) => {
      debuglog('inner fs open() callback');
      that.emit('error', err);
    });
  });

  fs.open(`${tmpdir.path}/dummy`, 'wx+', common.mustCall((err, fd) => {
    debuglog('fs open() callback');
    assert.ifError(err);
    fs.close(fd, () => { debuglog(`closed ${fd}`); });
    const w = new WriteStream(`${tmpdir.path}/dummy`,
                              { flags: 'wx+', emitClose: true })
      .on('error', common.mustCall((err) => {
        debuglog('error event callback');
        assert.strictEqual(err.code, 'EEXIST');
        w.destroy();
        w.on('close', common.mustCall(() => {
          debuglog('close event callback');
        }));
      }));
  }));
  debuglog('waiting for callbacks');
}