aboutsummaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-read-stream-inherit.js
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2016-12-12 21:35:17 -0800
committerRich Trott <rtrott@gmail.com>2016-12-15 21:42:52 -0800
commit44e704f1f840e30208399ef07ccb7a28236b907c (patch)
tree8180ac4e40f9f79dc498f19106560475868d98f6 /test/parallel/test-fs-read-stream-inherit.js
parent3daa4b1f76ed413fe984c514c30e4f7bd5abb95e (diff)
downloadandroid-node-v8-44e704f1f840e30208399ef07ccb7a28236b907c.tar.gz
android-node-v8-44e704f1f840e30208399ef07ccb7a28236b907c.tar.bz2
android-node-v8-44e704f1f840e30208399ef07ccb7a28236b907c.zip
test: refactor test-fs-read-stream-inherit
Refactor to take advantage of block scoping to isolate tests. Checks in exit handlers now reside with the relevant test block. Where test cases start and end is more clear. Also: Some use of `common.mustCall()` and improved wrapping/indentation. PR-URL: https://github.com/nodejs/node/pull/10246 Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Diffstat (limited to 'test/parallel/test-fs-read-stream-inherit.js')
-rw-r--r--test/parallel/test-fs-read-stream-inherit.js307
1 files changed, 159 insertions, 148 deletions
diff --git a/test/parallel/test-fs-read-stream-inherit.js b/test/parallel/test-fs-read-stream-inherit.js
index 40ec5ed883..d71dc3d438 100644
--- a/test/parallel/test-fs-read-stream-inherit.js
+++ b/test/parallel/test-fs-read-stream-inherit.js
@@ -7,174 +7,185 @@ const fs = require('fs');
const fn = path.join(common.fixturesDir, 'elipses.txt');
const rangeFile = path.join(common.fixturesDir, 'x.txt');
-const callbacks = { open: 0, end: 0, close: 0 };
-
let paused = false;
-const file = fs.ReadStream(fn);
-
-file.on('open', function(fd) {
- file.length = 0;
- callbacks.open++;
- assert.strictEqual(typeof fd, 'number');
- assert.ok(file.readable);
+{
+ const file = fs.ReadStream(fn);
- // GH-535
- file.pause();
- file.resume();
- file.pause();
- file.resume();
-});
+ file.on('open', common.mustCall(function(fd) {
+ file.length = 0;
+ assert.strictEqual(typeof fd, 'number');
+ assert.ok(file.readable);
-file.on('data', function(data) {
- assert.ok(data instanceof Buffer);
- assert.ok(!paused);
- file.length += data.length;
+ // GH-535
+ file.pause();
+ file.resume();
+ file.pause();
+ file.resume();
+ }));
- paused = true;
- file.pause();
+ file.on('data', function(data) {
+ assert.ok(data instanceof Buffer);
+ assert.ok(!paused);
+ file.length += data.length;
- setTimeout(function() {
- paused = false;
- file.resume();
- }, 10);
-});
+ paused = true;
+ file.pause();
+ setTimeout(function() {
+ paused = false;
+ file.resume();
+ }, 10);
+ });
-file.on('end', function(chunk) {
- callbacks.end++;
-});
+ file.on('end', common.mustCall(function() {}));
-file.on('close', function() {
- callbacks.close++;
-});
-const file3 = fs.createReadStream(fn, Object.create({encoding: 'utf8'}));
-file3.length = 0;
-file3.on('data', function(data) {
- assert.strictEqual(typeof data, 'string');
- file3.length += data.length;
+ file.on('close', common.mustCall(function() {
+ assert.strictEqual(file.length, 30000);
+ }));
+}
- for (let i = 0; i < data.length; i++) {
- // http://www.fileformat.info/info/unicode/char/2026/index.htm
- assert.strictEqual(data[i], '\u2026');
- }
-});
-
-file3.on('close', function() {
- callbacks.close++;
-});
-
-process.on('exit', function() {
- assert.strictEqual(callbacks.open, 1);
- assert.strictEqual(callbacks.end, 1);
- assert.strictEqual(callbacks.close, 2);
- assert.strictEqual(file.length, 30000);
- assert.strictEqual(file3.length, 10000);
- console.error('ok');
-});
-
-const file4 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1,
- start: 1, end: 2}));
-assert.strictEqual(file4.start, 1);
-assert.strictEqual(file4.end, 2);
-let contentRead = '';
-file4.on('data', function(data) {
- contentRead += data.toString('utf-8');
-});
-file4.on('end', function(data) {
- assert.strictEqual(contentRead, 'yz');
-});
-
-const file5 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1,
- start: 1}));
-assert.strictEqual(file5.start, 1);
-file5.data = '';
-file5.on('data', function(data) {
- file5.data += data.toString('utf-8');
-});
-file5.on('end', function() {
- assert.strictEqual(file5.data, 'yz\n');
-});
+{
+ const file3 = fs.createReadStream(fn, Object.create({encoding: 'utf8'}));
+ file3.length = 0;
+ file3.on('data', function(data) {
+ assert.strictEqual(typeof data, 'string');
+ file3.length += data.length;
+
+ for (let i = 0; i < data.length; i++) {
+ // http://www.fileformat.info/info/unicode/char/2026/index.htm
+ assert.strictEqual(data[i], '\u2026');
+ }
+ });
-// https://github.com/joyent/node/issues/2320
-const file6 = fs.createReadStream(rangeFile, Object.create({bufferSize: 1.23,
- start: 1}));
-assert.strictEqual(file6.start, 1);
-file6.data = '';
-file6.on('data', function(data) {
- file6.data += data.toString('utf-8');
-});
-file6.on('end', function() {
- assert.strictEqual(file6.data, 'yz\n');
-});
-
-assert.throws(function() {
- fs.createReadStream(rangeFile, Object.create({start: 10, end: 2}));
-}, /"start" option must be <= "end" option/);
-
-const stream = fs.createReadStream(rangeFile, Object.create({ start: 0,
- end: 0 }));
-assert.strictEqual(stream.start, 0);
-assert.strictEqual(stream.end, 0);
-stream.data = '';
-
-stream.on('data', function(chunk) {
- stream.data += chunk;
-});
-
-stream.on('end', function() {
- assert.strictEqual(stream.data, 'x');
-});
+ file3.on('close', common.mustCall(function() {
+ assert.strictEqual(file3.length, 10000);
+ }));
+}
-// pause and then resume immediately.
-const pauseRes = fs.createReadStream(rangeFile);
-pauseRes.pause();
-pauseRes.resume();
-
-let file7 = fs.createReadStream(rangeFile, Object.create({autoClose: false }));
-assert.strictEqual(file7.autoClose, false);
-file7.on('data', function() {});
-file7.on('end', function() {
- process.nextTick(function() {
- assert(!file7.closed);
- assert(!file7.destroyed);
- file7Next();
+{
+ const options = Object.create({bufferSize: 1, start: 1, end: 2});
+ const file4 = fs.createReadStream(rangeFile, options);
+ assert.strictEqual(file4.start, 1);
+ assert.strictEqual(file4.end, 2);
+ let contentRead = '';
+ file4.on('data', function(data) {
+ contentRead += data.toString('utf-8');
});
-});
-
-function file7Next() {
- // This will tell us if the fd is usable again or not.
- file7 = fs.createReadStream(null, Object.create({fd: file7.fd, start: 0 }));
- file7.data = '';
- file7.on('data', function(data) {
- file7.data += data;
+ file4.on('end', common.mustCall(function() {
+ assert.strictEqual(contentRead, 'yz');
+ }));
+}
+
+{
+ const options = Object.create({bufferSize: 1, start: 1});
+ const file5 = fs.createReadStream(rangeFile, options);
+ assert.strictEqual(file5.start, 1);
+ file5.data = '';
+ file5.on('data', function(data) {
+ file5.data += data.toString('utf-8');
});
- file7.on('end', function(err) {
- assert.strictEqual(file7.data, 'xyz\n');
+ file5.on('end', common.mustCall(function() {
+ assert.strictEqual(file5.data, 'yz\n');
+ }));
+}
+
+// https://github.com/joyent/node/issues/2320
+{
+ const options = Object.create({bufferSize: 1.23, start: 1});
+ const file6 = fs.createReadStream(rangeFile, options);
+ assert.strictEqual(file6.start, 1);
+ file6.data = '';
+ file6.on('data', function(data) {
+ file6.data += data.toString('utf-8');
});
+ file6.on('end', common.mustCall(function() {
+ assert.strictEqual(file6.data, 'yz\n');
+ }));
}
-// Just to make sure autoClose won't close the stream because of error.
-const file8 = fs.createReadStream(null, Object.create({fd: 13337,
- autoClose: false }));
-file8.on('data', function() {});
-file8.on('error', common.mustCall(function() {}));
+{
+ assert.throws(function() {
+ fs.createReadStream(rangeFile, Object.create({start: 10, end: 2}));
+ }, /"start" option must be <= "end" option/);
+}
-// Make sure stream is destroyed when file does not exist.
-const file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
-file9.on('data', function() {});
-file9.on('error', common.mustCall(function() {}));
+{
+ const options = Object.create({start: 0, end: 0});
+ const stream = fs.createReadStream(rangeFile, options);
+ assert.strictEqual(stream.start, 0);
+ assert.strictEqual(stream.end, 0);
+ stream.data = '';
+
+ stream.on('data', function(chunk) {
+ stream.data += chunk;
+ });
+
+ stream.on('end', common.mustCall(function() {
+ assert.strictEqual(stream.data, 'x');
+ }));
+}
+
+// pause and then resume immediately.
+{
+ const pauseRes = fs.createReadStream(rangeFile);
+ pauseRes.pause();
+ pauseRes.resume();
+}
-process.on('exit', function() {
- assert(file7.closed);
- assert(file7.destroyed);
+{
+ let file7 =
+ fs.createReadStream(rangeFile, Object.create({autoClose: false }));
+ assert.strictEqual(file7.autoClose, false);
+ file7.on('data', function() {});
+ file7.on('end', common.mustCall(function() {
+ process.nextTick(common.mustCall(function() {
+ assert(!file7.closed);
+ assert(!file7.destroyed);
+ file7Next();
+ }));
+ }));
+
+ function file7Next() {
+ // This will tell us if the fd is usable again or not.
+ file7 = fs.createReadStream(null, Object.create({fd: file7.fd, start: 0 }));
+ file7.data = '';
+ file7.on('data', function(data) {
+ file7.data += data;
+ });
+ file7.on('end', common.mustCall(function() {
+ assert.strictEqual(file7.data, 'xyz\n');
+ }));
+ }
+ process.on('exit', function() {
+ assert(file7.closed);
+ assert(file7.destroyed);
+ });
+}
- assert(!file8.closed);
- assert(!file8.destroyed);
- assert(file8.fd);
+// Just to make sure autoClose won't close the stream because of error.
+{
+ const options = Object.create({fd: 13337, autoClose: false});
+ const file8 = fs.createReadStream(null, options);
+ file8.on('data', function() {});
+ file8.on('error', common.mustCall(function() {}));
+ process.on('exit', function() {
+ assert(!file8.closed);
+ assert(!file8.destroyed);
+ assert(file8.fd);
+ });
+}
- assert(!file9.closed);
- assert(file9.destroyed);
-});
+// Make sure stream is destroyed when file does not exist.
+{
+ const file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
+ file9.on('data', function() {});
+ file9.on('error', common.mustCall(function() {}));
+
+ process.on('exit', function() {
+ assert(!file9.closed);
+ assert(file9.destroyed);
+ });
+}