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 43// Verify that the stream will unset the fd after destroy(). 44{ 45 const fd = fs.openSync(filename, 'w'); 46 const stream = new SyncWriteStream(fd); 47 48 stream.on('close', common.mustCall()); 49 assert.strictEqual(stream.destroy(), stream); 50 assert.strictEqual(stream.fd, null); 51} 52 53// Verify that the stream will unset the fd after destroySoon(). 54{ 55 const fd = fs.openSync(filename, 'w'); 56 const stream = new SyncWriteStream(fd); 57 58 stream.on('close', common.mustCall()); 59 assert.strictEqual(stream.destroySoon(), stream); 60 assert.strictEqual(stream.fd, null); 61} 62 63// Verify that calling end() will also destroy the stream. 64{ 65 const fd = fs.openSync(filename, 'w'); 66 const stream = new SyncWriteStream(fd); 67 68 assert.strictEqual(stream.fd, fd); 69 70 stream.end(); 71 assert.strictEqual(stream.fd, null); 72} 73