• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const path = require('path');
5const fs = require('fs');
6
7const tmpdir = require('../common/tmpdir');
8
9const file = path.join(tmpdir.path, 'write-autoclose-opt1.txt');
10tmpdir.refresh();
11let stream = fs.createWriteStream(file, { flags: 'w+', autoClose: false });
12stream.write('Test1');
13stream.end();
14stream.on('finish', common.mustCall(function() {
15  stream.on('close', common.mustNotCall());
16  process.nextTick(common.mustCall(function() {
17    assert.strictEqual(stream.closed, false);
18    assert.notStrictEqual(stream.fd, null);
19    next();
20  }));
21}));
22
23function next() {
24  // This will tell us if the fd is usable again or not
25  stream = fs.createWriteStream(null, { fd: stream.fd, start: 0 });
26  stream.write('Test2');
27  stream.end();
28  stream.on('finish', common.mustCall(function() {
29    assert.strictEqual(stream.closed, false);
30    stream.on('close', common.mustCall(function() {
31      assert.strictEqual(stream.fd, null);
32      assert.strictEqual(stream.closed, true);
33      process.nextTick(next2);
34    }));
35  }));
36}
37
38function next2() {
39  // This will test if after reusing the fd data is written properly
40  fs.readFile(file, function(err, data) {
41    assert.ifError(err);
42    assert.strictEqual(data.toString(), 'Test2');
43    process.nextTick(common.mustCall(next3));
44  });
45}
46
47function next3() {
48  // This is to test success scenario where autoClose is true
49  const stream = fs.createWriteStream(file, { autoClose: true });
50  stream.write('Test3');
51  stream.end();
52  stream.on('finish', common.mustCall(function() {
53    assert.strictEqual(stream.closed, false);
54    stream.on('close', common.mustCall(function() {
55      assert.strictEqual(stream.fd, null);
56      assert.strictEqual(stream.closed, true);
57    }));
58  }));
59}
60