Browse Source

test: refactor test-fs-read-stream-inherit

* block scope `paused`
* change name of block-scoped `file3` etc. to `file`
* alphabetize modules
* confirm contents provided in `data` callback
* confirm `data` callbacks will not fire on tests for errors

PR-URL: https://github.com/nodejs/node/pull/13618
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v6
Rich Trott 8 years ago
parent
commit
3c506af78b
  1. 115
      test/parallel/test-fs-read-stream-inherit.js

115
test/parallel/test-fs-read-stream-inherit.js

@ -1,15 +1,16 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const path = require('path'); const assert = require('assert');
const fs = require('fs'); const fs = require('fs');
const path = require('path');
const fn = path.join(common.fixturesDir, 'elipses.txt'); const fn = path.join(common.fixturesDir, 'elipses.txt');
const rangeFile = path.join(common.fixturesDir, 'x.txt'); const rangeFile = path.join(common.fixturesDir, 'x.txt');
let paused = false;
{ {
let paused = false;
const file = fs.ReadStream(fn); const file = fs.ReadStream(fn);
file.on('open', common.mustCall(function(fd) { file.on('open', common.mustCall(function(fd) {
@ -48,11 +49,11 @@ let paused = false;
} }
{ {
const file3 = fs.createReadStream(fn, Object.create({encoding: 'utf8'})); const file = fs.createReadStream(fn, Object.create({encoding: 'utf8'}));
file3.length = 0; file.length = 0;
file3.on('data', function(data) { file.on('data', function(data) {
assert.strictEqual(typeof data, 'string'); assert.strictEqual(typeof data, 'string');
file3.length += data.length; file.length += data.length;
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
// http://www.fileformat.info/info/unicode/char/2026/index.htm // http://www.fileformat.info/info/unicode/char/2026/index.htm
@ -60,49 +61,49 @@ let paused = false;
} }
}); });
file3.on('close', common.mustCall(function() { file.on('close', common.mustCall(function() {
assert.strictEqual(file3.length, 10000); assert.strictEqual(file.length, 10000);
})); }));
} }
{ {
const options = Object.create({bufferSize: 1, start: 1, end: 2}); const options = Object.create({bufferSize: 1, start: 1, end: 2});
const file4 = fs.createReadStream(rangeFile, options); const file = fs.createReadStream(rangeFile, options);
assert.strictEqual(file4.start, 1); assert.strictEqual(file.start, 1);
assert.strictEqual(file4.end, 2); assert.strictEqual(file.end, 2);
let contentRead = ''; let contentRead = '';
file4.on('data', function(data) { file.on('data', function(data) {
contentRead += data.toString('utf-8'); contentRead += data.toString('utf-8');
}); });
file4.on('end', common.mustCall(function() { file.on('end', common.mustCall(function() {
assert.strictEqual(contentRead, 'yz'); assert.strictEqual(contentRead, 'yz');
})); }));
} }
{ {
const options = Object.create({bufferSize: 1, start: 1}); const options = Object.create({bufferSize: 1, start: 1});
const file5 = fs.createReadStream(rangeFile, options); const file = fs.createReadStream(rangeFile, options);
assert.strictEqual(file5.start, 1); assert.strictEqual(file.start, 1);
file5.data = ''; file.data = '';
file5.on('data', function(data) { file.on('data', function(data) {
file5.data += data.toString('utf-8'); file.data += data.toString('utf-8');
}); });
file5.on('end', common.mustCall(function() { file.on('end', common.mustCall(function() {
assert.strictEqual(file5.data, 'yz\n'); assert.strictEqual(file.data, 'yz\n');
})); }));
} }
// https://github.com/joyent/node/issues/2320 // https://github.com/joyent/node/issues/2320
{ {
const options = Object.create({bufferSize: 1.23, start: 1}); const options = Object.create({bufferSize: 1.23, start: 1});
const file6 = fs.createReadStream(rangeFile, options); const file = fs.createReadStream(rangeFile, options);
assert.strictEqual(file6.start, 1); assert.strictEqual(file.start, 1);
file6.data = ''; file.data = '';
file6.on('data', function(data) { file.on('data', function(data) {
file6.data += data.toString('utf-8'); file.data += data.toString('utf-8');
}); });
file6.on('end', common.mustCall(function() { file.on('end', common.mustCall(function() {
assert.strictEqual(file6.data, 'yz\n'); assert.strictEqual(file.data, 'yz\n');
})); }));
} }
@ -136,56 +137,58 @@ let paused = false;
} }
{ {
let file7 = let data = '';
let file =
fs.createReadStream(rangeFile, Object.create({autoClose: false })); fs.createReadStream(rangeFile, Object.create({autoClose: false }));
assert.strictEqual(file7.autoClose, false); assert.strictEqual(file.autoClose, false);
file7.on('data', common.noop); file.on('data', (chunk) => { data += chunk; });
file7.on('end', common.mustCall(function() { file.on('end', common.mustCall(function() {
process.nextTick(common.mustCall(function() { process.nextTick(common.mustCall(function() {
assert(!file7.closed); assert(!file.closed);
assert(!file7.destroyed); assert(!file.destroyed);
file7Next(); assert.strictEqual(data, 'xyz\n');
fileNext();
})); }));
})); }));
function file7Next() { function fileNext() {
// This will tell us if the fd is usable again or not. // This will tell us if the fd is usable again or not.
file7 = fs.createReadStream(null, Object.create({fd: file7.fd, start: 0 })); file = fs.createReadStream(null, Object.create({fd: file.fd, start: 0 }));
file7.data = ''; file.data = '';
file7.on('data', function(data) { file.on('data', function(data) {
file7.data += data; file.data += data;
}); });
file7.on('end', common.mustCall(function() { file.on('end', common.mustCall(function() {
assert.strictEqual(file7.data, 'xyz\n'); assert.strictEqual(file.data, 'xyz\n');
})); }));
} }
process.on('exit', function() { process.on('exit', function() {
assert(file7.closed); assert(file.closed);
assert(file7.destroyed); assert(file.destroyed);
}); });
} }
// Just to make sure autoClose won't close the stream because of error. // Just to make sure autoClose won't close the stream because of error.
{ {
const options = Object.create({fd: 13337, autoClose: false}); const options = Object.create({fd: 13337, autoClose: false});
const file8 = fs.createReadStream(null, options); const file = fs.createReadStream(null, options);
file8.on('data', common.noop); file.on('data', common.mustNotCall());
file8.on('error', common.mustCall()); file.on('error', common.mustCall());
process.on('exit', function() { process.on('exit', function() {
assert(!file8.closed); assert(!file.closed);
assert(!file8.destroyed); assert(!file.destroyed);
assert(file8.fd); assert(file.fd);
}); });
} }
// Make sure stream is destroyed when file does not exist. // Make sure stream is destroyed when file does not exist.
{ {
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist'); const file = fs.createReadStream('/path/to/file/that/does/not/exist');
file9.on('data', common.noop); file.on('data', common.mustNotCall());
file9.on('error', common.mustCall()); file.on('error', common.mustCall());
process.on('exit', function() { process.on('exit', function() {
assert(!file9.closed); assert(!file.closed);
assert(file9.destroyed); assert(file.destroyed);
}); });
} }

Loading…
Cancel
Save