test: add internal/fs tests
PR-URL: https://github.com/nodejs/node/pull/12306 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
parent
b07c72b8be
commit
f98db78f77
66
test/parallel/test-internal-fs-syncwritestream.js
Normal file
66
test/parallel/test-internal-fs-syncwritestream.js
Normal file
@ -0,0 +1,66 @@
|
||||
// Flags: --expose-internals
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const SyncWriteStream = require('internal/fs').SyncWriteStream;
|
||||
|
||||
common.refreshTmpDir();
|
||||
|
||||
const filename = path.join(common.tmpDir, 'sync-write-stream.txt');
|
||||
|
||||
// Verify constructing the instance with defualt options.
|
||||
{
|
||||
const stream = new SyncWriteStream(1);
|
||||
|
||||
assert.strictEqual(stream.fd, 1);
|
||||
assert.strictEqual(stream.readable, false);
|
||||
assert.strictEqual(stream.autoClose, true);
|
||||
assert.strictEqual(stream.listenerCount('end'), 1);
|
||||
}
|
||||
|
||||
// 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);
|
||||
assert.strictEqual(stream.listenerCount('end'), 1);
|
||||
}
|
||||
|
||||
// Verfiy that the file will be writen synchronously.
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
// Verify that the stream will unset the fd after destory().
|
||||
{
|
||||
const fd = fs.openSync(filename, 'w');
|
||||
const stream = new SyncWriteStream(fd);
|
||||
|
||||
stream.on('close', common.mustCall(3));
|
||||
|
||||
assert.strictEqual(stream.destroy(), true);
|
||||
assert.strictEqual(stream.fd, null);
|
||||
assert.strictEqual(stream.destroy(), true);
|
||||
assert.strictEqual(stream.destroySoon(), true);
|
||||
}
|
||||
|
||||
// Verfit that the 'end' event listener will also destroy the stream.
|
||||
{
|
||||
const fd = fs.openSync(filename, 'w');
|
||||
const stream = new SyncWriteStream(fd);
|
||||
|
||||
assert.strictEqual(stream.fd, fd);
|
||||
|
||||
stream.emit('end');
|
||||
assert.strictEqual(stream.fd, null);
|
||||
}
|
10
test/parallel/test-internal-fs.js
Normal file
10
test/parallel/test-internal-fs.js
Normal file
@ -0,0 +1,10 @@
|
||||
// Flags: --expose-internals
|
||||
'use strict';
|
||||
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
const fs = require('internal/fs');
|
||||
|
||||
assert.doesNotThrow(() => fs.assertEncoding());
|
||||
assert.doesNotThrow(() => fs.assertEncoding('utf8'));
|
||||
assert.throws(() => fs.assertEncoding('foo'), /^Error: Unknown encoding: foo$/);
|
Loading…
x
Reference in New Issue
Block a user