Skip to content

Commit 0d87b31

Browse files
Trottaddaleax
authored andcommitted
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: #13618 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 58e177c commit 0d87b31

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

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

+59-56
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
'use strict';
22
const common = require('../common');
3-
const assert = require('assert');
43

5-
const path = require('path');
4+
const assert = require('assert');
65
const fs = require('fs');
6+
const path = require('path');
7+
78
const fn = path.join(common.fixturesDir, 'elipses.txt');
89
const rangeFile = path.join(common.fixturesDir, 'x.txt');
910

10-
let paused = false;
11-
1211
{
12+
let paused = false;
13+
1314
const file = fs.ReadStream(fn);
1415

1516
file.on('open', common.mustCall(function(fd) {
@@ -48,61 +49,61 @@ let paused = false;
4849
}
4950

5051
{
51-
const file3 = fs.createReadStream(fn, Object.create({encoding: 'utf8'}));
52-
file3.length = 0;
53-
file3.on('data', function(data) {
52+
const file = fs.createReadStream(fn, Object.create({encoding: 'utf8'}));
53+
file.length = 0;
54+
file.on('data', function(data) {
5455
assert.strictEqual(typeof data, 'string');
55-
file3.length += data.length;
56+
file.length += data.length;
5657

5758
for (let i = 0; i < data.length; i++) {
5859
// http://www.fileformat.info/info/unicode/char/2026/index.htm
5960
assert.strictEqual(data[i], '\u2026');
6061
}
6162
});
6263

63-
file3.on('close', common.mustCall(function() {
64-
assert.strictEqual(file3.length, 10000);
64+
file.on('close', common.mustCall(function() {
65+
assert.strictEqual(file.length, 10000);
6566
}));
6667
}
6768

6869
{
6970
const options = Object.create({bufferSize: 1, start: 1, end: 2});
70-
const file4 = fs.createReadStream(rangeFile, options);
71-
assert.strictEqual(file4.start, 1);
72-
assert.strictEqual(file4.end, 2);
71+
const file = fs.createReadStream(rangeFile, options);
72+
assert.strictEqual(file.start, 1);
73+
assert.strictEqual(file.end, 2);
7374
let contentRead = '';
74-
file4.on('data', function(data) {
75+
file.on('data', function(data) {
7576
contentRead += data.toString('utf-8');
7677
});
77-
file4.on('end', common.mustCall(function() {
78+
file.on('end', common.mustCall(function() {
7879
assert.strictEqual(contentRead, 'yz');
7980
}));
8081
}
8182

8283
{
8384
const options = Object.create({bufferSize: 1, start: 1});
84-
const file5 = fs.createReadStream(rangeFile, options);
85-
assert.strictEqual(file5.start, 1);
86-
file5.data = '';
87-
file5.on('data', function(data) {
88-
file5.data += data.toString('utf-8');
85+
const file = fs.createReadStream(rangeFile, options);
86+
assert.strictEqual(file.start, 1);
87+
file.data = '';
88+
file.on('data', function(data) {
89+
file.data += data.toString('utf-8');
8990
});
90-
file5.on('end', common.mustCall(function() {
91-
assert.strictEqual(file5.data, 'yz\n');
91+
file.on('end', common.mustCall(function() {
92+
assert.strictEqual(file.data, 'yz\n');
9293
}));
9394
}
9495

9596
// https://github.com/joyent/node/issues/2320
9697
{
9798
const options = Object.create({bufferSize: 1.23, start: 1});
98-
const file6 = fs.createReadStream(rangeFile, options);
99-
assert.strictEqual(file6.start, 1);
100-
file6.data = '';
101-
file6.on('data', function(data) {
102-
file6.data += data.toString('utf-8');
99+
const file = fs.createReadStream(rangeFile, options);
100+
assert.strictEqual(file.start, 1);
101+
file.data = '';
102+
file.on('data', function(data) {
103+
file.data += data.toString('utf-8');
103104
});
104-
file6.on('end', common.mustCall(function() {
105-
assert.strictEqual(file6.data, 'yz\n');
105+
file.on('end', common.mustCall(function() {
106+
assert.strictEqual(file.data, 'yz\n');
106107
}));
107108
}
108109

@@ -136,56 +137,58 @@ let paused = false;
136137
}
137138

138139
{
139-
let file7 =
140+
let data = '';
141+
let file =
140142
fs.createReadStream(rangeFile, Object.create({autoClose: false }));
141-
assert.strictEqual(file7.autoClose, false);
142-
file7.on('data', common.noop);
143-
file7.on('end', common.mustCall(function() {
143+
assert.strictEqual(file.autoClose, false);
144+
file.on('data', (chunk) => { data += chunk; });
145+
file.on('end', common.mustCall(function() {
144146
process.nextTick(common.mustCall(function() {
145-
assert(!file7.closed);
146-
assert(!file7.destroyed);
147-
file7Next();
147+
assert(!file.closed);
148+
assert(!file.destroyed);
149+
assert.strictEqual(data, 'xyz\n');
150+
fileNext();
148151
}));
149152
}));
150153

151-
function file7Next() {
154+
function fileNext() {
152155
// This will tell us if the fd is usable again or not.
153-
file7 = fs.createReadStream(null, Object.create({fd: file7.fd, start: 0 }));
154-
file7.data = '';
155-
file7.on('data', function(data) {
156-
file7.data += data;
156+
file = fs.createReadStream(null, Object.create({fd: file.fd, start: 0 }));
157+
file.data = '';
158+
file.on('data', function(data) {
159+
file.data += data;
157160
});
158-
file7.on('end', common.mustCall(function() {
159-
assert.strictEqual(file7.data, 'xyz\n');
161+
file.on('end', common.mustCall(function() {
162+
assert.strictEqual(file.data, 'xyz\n');
160163
}));
161164
}
162165
process.on('exit', function() {
163-
assert(file7.closed);
164-
assert(file7.destroyed);
166+
assert(file.closed);
167+
assert(file.destroyed);
165168
});
166169
}
167170

168171
// Just to make sure autoClose won't close the stream because of error.
169172
{
170173
const options = Object.create({fd: 13337, autoClose: false});
171-
const file8 = fs.createReadStream(null, options);
172-
file8.on('data', common.noop);
173-
file8.on('error', common.mustCall());
174+
const file = fs.createReadStream(null, options);
175+
file.on('data', common.mustNotCall());
176+
file.on('error', common.mustCall());
174177
process.on('exit', function() {
175-
assert(!file8.closed);
176-
assert(!file8.destroyed);
177-
assert(file8.fd);
178+
assert(!file.closed);
179+
assert(!file.destroyed);
180+
assert(file.fd);
178181
});
179182
}
180183

181184
// Make sure stream is destroyed when file does not exist.
182185
{
183-
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
184-
file9.on('data', common.noop);
185-
file9.on('error', common.mustCall());
186+
const file = fs.createReadStream('/path/to/file/that/does/not/exist');
187+
file.on('data', common.mustNotCall());
188+
file.on('error', common.mustCall());
186189

187190
process.on('exit', function() {
188-
assert(!file9.closed);
189-
assert(file9.destroyed);
191+
assert(!file.closed);
192+
assert(file.destroyed);
190193
});
191194
}

0 commit comments

Comments
 (0)