11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fs = require('fs');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst debuglog = (arg) => {
81cb0ef41Sopenharmony_ci  console.log(new Date().toLocaleString(), arg);
91cb0ef41Sopenharmony_ci};
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
121cb0ef41Sopenharmony_citmpdir.refresh();
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci{
151cb0ef41Sopenharmony_ci  // Compat error.
161cb0ef41Sopenharmony_ci  debuglog('start test');
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  function WriteStream(...args) {
191cb0ef41Sopenharmony_ci    debuglog('WriteStream constructor');
201cb0ef41Sopenharmony_ci    fs.WriteStream.call(this, ...args);
211cb0ef41Sopenharmony_ci  }
221cb0ef41Sopenharmony_ci  Object.setPrototypeOf(WriteStream.prototype, fs.WriteStream.prototype);
231cb0ef41Sopenharmony_ci  Object.setPrototypeOf(WriteStream, fs.WriteStream);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  WriteStream.prototype.open = common.mustCall(function WriteStream$open() {
261cb0ef41Sopenharmony_ci    debuglog('WriteStream open() callback');
271cb0ef41Sopenharmony_ci    const that = this;
281cb0ef41Sopenharmony_ci    fs.open(that.path, that.flags, that.mode, (err, fd) => {
291cb0ef41Sopenharmony_ci      debuglog('inner fs open() callback');
301cb0ef41Sopenharmony_ci      that.emit('error', err);
311cb0ef41Sopenharmony_ci    });
321cb0ef41Sopenharmony_ci  });
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  fs.open(`${tmpdir.path}/dummy`, 'wx+', common.mustCall((err, fd) => {
351cb0ef41Sopenharmony_ci    debuglog('fs open() callback');
361cb0ef41Sopenharmony_ci    assert.ifError(err);
371cb0ef41Sopenharmony_ci    fs.close(fd, () => { debuglog(`closed ${fd}`); });
381cb0ef41Sopenharmony_ci    const w = new WriteStream(`${tmpdir.path}/dummy`,
391cb0ef41Sopenharmony_ci                              { flags: 'wx+', emitClose: true })
401cb0ef41Sopenharmony_ci      .on('error', common.mustCall((err) => {
411cb0ef41Sopenharmony_ci        debuglog('error event callback');
421cb0ef41Sopenharmony_ci        assert.strictEqual(err.code, 'EEXIST');
431cb0ef41Sopenharmony_ci        w.destroy();
441cb0ef41Sopenharmony_ci        w.on('close', common.mustCall(() => {
451cb0ef41Sopenharmony_ci          debuglog('close event callback');
461cb0ef41Sopenharmony_ci        }));
471cb0ef41Sopenharmony_ci      }));
481cb0ef41Sopenharmony_ci  }));
491cb0ef41Sopenharmony_ci  debuglog('waiting for callbacks');
501cb0ef41Sopenharmony_ci}
51