2017-04-11 08:24:42 +08:00
|
|
|
// Flags: --expose-internals
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
2018-05-15 12:34:49 -07:00
|
|
|
const SyncWriteStream = require('internal/fs/sync_write_stream');
|
2017-04-11 08:24:42 +08:00
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
2017-04-11 08:24:42 +08:00
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const filename = path.join(tmpdir.path, 'sync-write-stream.txt');
|
2017-04-11 08:24:42 +08:00
|
|
|
|
2017-06-16 17:49:20 +02:00
|
|
|
// Verify constructing the instance with default options.
|
2017-04-11 08:24:42 +08:00
|
|
|
{
|
|
|
|
const stream = new SyncWriteStream(1);
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, 1);
|
|
|
|
assert.strictEqual(stream.readable, false);
|
|
|
|
assert.strictEqual(stream.autoClose, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify constructing the instance with specified options.
|
|
|
|
{
|
|
|
|
const stream = new SyncWriteStream(1, { autoClose: false });
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, 1);
|
|
|
|
assert.strictEqual(stream.readable, false);
|
|
|
|
assert.strictEqual(stream.autoClose, false);
|
|
|
|
}
|
|
|
|
|
2018-01-06 19:34:27 +01:00
|
|
|
// Verify that the file will be written synchronously.
|
2017-04-11 08:24:42 +08:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
const chunk = Buffer.from('foo');
|
|
|
|
|
|
|
|
assert.strictEqual(stream._write(chunk, null, common.mustCall(1)), true);
|
|
|
|
assert.strictEqual(fs.readFileSync(filename).equals(chunk), true);
|
2019-07-30 07:50:21 +01:00
|
|
|
|
|
|
|
fs.closeSync(fd);
|
2017-04-11 08:24:42 +08:00
|
|
|
}
|
|
|
|
|
2017-06-16 17:49:20 +02:00
|
|
|
// Verify that the stream will unset the fd after destroy().
|
2017-04-11 08:24:42 +08:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
|
2019-03-15 19:58:42 +01:00
|
|
|
stream.on('close', common.mustCall());
|
|
|
|
assert.strictEqual(stream.destroy(), stream);
|
|
|
|
assert.strictEqual(stream.fd, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the stream will unset the fd after destroySoon().
|
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
2017-04-11 08:24:42 +08:00
|
|
|
|
2019-03-15 19:58:42 +01:00
|
|
|
stream.on('close', common.mustCall());
|
|
|
|
assert.strictEqual(stream.destroySoon(), stream);
|
2017-04-11 08:24:42 +08:00
|
|
|
assert.strictEqual(stream.fd, null);
|
|
|
|
}
|
|
|
|
|
2019-03-15 19:58:42 +01:00
|
|
|
// Verify that calling end() will also destroy the stream.
|
2017-04-11 08:24:42 +08:00
|
|
|
{
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
const stream = new SyncWriteStream(fd);
|
|
|
|
|
|
|
|
assert.strictEqual(stream.fd, fd);
|
|
|
|
|
2019-03-15 19:58:42 +01:00
|
|
|
stream.end();
|
2019-11-30 15:39:44 +01:00
|
|
|
stream.on('close', common.mustCall(() => {
|
|
|
|
assert.strictEqual(stream.fd, null);
|
|
|
|
}));
|
2017-04-11 08:24:42 +08:00
|
|
|
}
|