• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6const fs = require('fs');
7const path = require('path');
8const SyncWriteStream = require('internal/fs/sync_write_stream');
9
10const tmpdir = require('../common/tmpdir');
11tmpdir.refresh();
12
13const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
14
15// Verify constructing the instance with default options.
16{
17  const stream = new SyncWriteStream(1);
18
19  assert.strictEqual(stream.fd, 1);
20  assert.strictEqual(stream.readable, false);
21  assert.strictEqual(stream.autoClose, true);
22}
23
24// Verify constructing the instance with specified options.
25{
26  const stream = new SyncWriteStream(1, { autoClose: false });
27
28  assert.strictEqual(stream.fd, 1);
29  assert.strictEqual(stream.readable, false);
30  assert.strictEqual(stream.autoClose, false);
31}
32
33// Verify that the file will be written synchronously.
34{
35  const fd = fs.openSync(filename, 'w');
36  const stream = new SyncWriteStream(fd);
37  const chunk = Buffer.from('foo');
38
39  assert.strictEqual(stream._write(chunk, null, common.mustCall(1)), true);
40  assert.strictEqual(fs.readFileSync(filename).equals(chunk), true);
41
42  fs.closeSync(fd);
43}
44
45// Verify that the stream will unset the fd after destroy().
46{
47  const fd = fs.openSync(filename, 'w');
48  const stream = new SyncWriteStream(fd);
49
50  stream.on('close', common.mustCall());
51  assert.strictEqual(stream.destroy(), stream);
52  assert.strictEqual(stream.fd, null);
53}
54
55// Verify that the stream will unset the fd after destroySoon().
56{
57  const fd = fs.openSync(filename, 'w');
58  const stream = new SyncWriteStream(fd);
59
60  stream.on('close', common.mustCall());
61  assert.strictEqual(stream.destroySoon(), stream);
62  assert.strictEqual(stream.fd, null);
63}
64
65// Verify that calling end() will also destroy the stream.
66{
67  const fd = fs.openSync(filename, 'w');
68  const stream = new SyncWriteStream(fd);
69
70  assert.strictEqual(stream.fd, fd);
71
72  stream.end();
73  stream.on('close', common.mustCall(() => {
74    assert.strictEqual(stream.fd, null);
75  }));
76}
77