Skip to content

Commit e4b29a5

Browse files
TrottItalo A. Casas
authored andcommitted
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: #10246 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 7e8c5e3 commit e4b29a5

File tree

1 file changed

+159
-148
lines changed

1 file changed

+159
-148
lines changed

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

Lines changed: 159 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -7,174 +7,185 @@ const fs = require('fs');
77
constfn=path.join(common.fixturesDir,'elipses.txt');
88
constrangeFile=path.join(common.fixturesDir,'x.txt');
99

10-
constcallbacks={open: 0,end: 0,close: 0};
11-
1210
letpaused=false;
1311

14-
constfile=fs.ReadStream(fn);
15-
16-
file.on('open',function(fd){
17-
file.length=0;
18-
callbacks.open++;
19-
assert.strictEqual(typeoffd,'number');
20-
assert.ok(file.readable);
12+
{
13+
constfile=fs.ReadStream(fn);
2114

22-
// GH-535
23-
file.pause();
24-
file.resume();
25-
file.pause();
26-
file.resume();
27-
});
15+
file.on('open',common.mustCall(function(fd){
16+
file.length=0;
17+
assert.strictEqual(typeoffd,'number');
18+
assert.ok(file.readable);
2819

29-
file.on('data',function(data){
30-
assert.ok(datainstanceofBuffer);
31-
assert.ok(!paused);
32-
file.length+=data.length;
20+
// GH-535
21+
file.pause();
22+
file.resume();
23+
file.pause();
24+
file.resume();
25+
}));
3326

34-
paused=true;
35-
file.pause();
27+
file.on('data',function(data){
28+
assert.ok(datainstanceofBuffer);
29+
assert.ok(!paused);
30+
file.length+=data.length;
3631

37-
setTimeout(function(){
38-
paused=false;
39-
file.resume();
40-
},10);
41-
});
32+
paused=true;
33+
file.pause();
4234

35+
setTimeout(function(){
36+
paused=false;
37+
file.resume();
38+
},10);
39+
});
4340

44-
file.on('end',function(chunk){
45-
callbacks.end++;
46-
});
4741

42+
file.on('end',common.mustCall(function(){}));
4843

49-
file.on('close',function(){
50-
callbacks.close++;
51-
});
5244

53-
constfile3=fs.createReadStream(fn,Object.create({encoding: 'utf8'}));
54-
file3.length=0;
55-
file3.on('data',function(data){
56-
assert.strictEqual(typeofdata,'string');
57-
file3.length+=data.length;
45+
file.on('close',common.mustCall(function(){
46+
assert.strictEqual(file.length,30000);
47+
}));
48+
}
5849

59-
for(leti=0;i<data.length;i++){
60-
// http://www.fileformat.info/info/unicode/char/2026/index.htm
61-
assert.strictEqual(data[i],'\u2026');
62-
}
63-
});
64-
65-
file3.on('close',function(){
66-
callbacks.close++;
67-
});
68-
69-
process.on('exit',function(){
70-
assert.strictEqual(callbacks.open,1);
71-
assert.strictEqual(callbacks.end,1);
72-
assert.strictEqual(callbacks.close,2);
73-
assert.strictEqual(file.length,30000);
74-
assert.strictEqual(file3.length,10000);
75-
console.error('ok');
76-
});
77-
78-
constfile4=fs.createReadStream(rangeFile,Object.create({bufferSize: 1,
79-
start: 1,end: 2}));
80-
assert.strictEqual(file4.start,1);
81-
assert.strictEqual(file4.end,2);
82-
letcontentRead='';
83-
file4.on('data',function(data){
84-
contentRead+=data.toString('utf-8');
85-
});
86-
file4.on('end',function(data){
87-
assert.strictEqual(contentRead,'yz');
88-
});
89-
90-
constfile5=fs.createReadStream(rangeFile,Object.create({bufferSize: 1,
91-
start: 1}));
92-
assert.strictEqual(file5.start,1);
93-
file5.data='';
94-
file5.on('data',function(data){
95-
file5.data+=data.toString('utf-8');
96-
});
97-
file5.on('end',function(){
98-
assert.strictEqual(file5.data,'yz\n');
99-
});
50+
{
51+
constfile3=fs.createReadStream(fn,Object.create({encoding: 'utf8'}));
52+
file3.length=0;
53+
file3.on('data',function(data){
54+
assert.strictEqual(typeofdata,'string');
55+
file3.length+=data.length;
56+
57+
for(leti=0;i<data.length;i++){
58+
// http://www.fileformat.info/info/unicode/char/2026/index.htm
59+
assert.strictEqual(data[i],'\u2026');
60+
}
61+
});
10062

101-
// https://github.com/joyent/node/issues/2320
102-
constfile6=fs.createReadStream(rangeFile,Object.create({bufferSize: 1.23,
103-
start: 1}));
104-
assert.strictEqual(file6.start,1);
105-
file6.data='';
106-
file6.on('data',function(data){
107-
file6.data+=data.toString('utf-8');
108-
});
109-
file6.on('end',function(){
110-
assert.strictEqual(file6.data,'yz\n');
111-
});
112-
113-
assert.throws(function(){
114-
fs.createReadStream(rangeFile,Object.create({start: 10,end: 2}));
115-
},/"start"optionmustbe<="end"option/);
116-
117-
conststream=fs.createReadStream(rangeFile,Object.create({start: 0,
118-
end: 0}));
119-
assert.strictEqual(stream.start,0);
120-
assert.strictEqual(stream.end,0);
121-
stream.data='';
122-
123-
stream.on('data',function(chunk){
124-
stream.data+=chunk;
125-
});
126-
127-
stream.on('end',function(){
128-
assert.strictEqual(stream.data,'x');
129-
});
63+
file3.on('close',common.mustCall(function(){
64+
assert.strictEqual(file3.length,10000);
65+
}));
66+
}
13067

131-
// pause and then resume immediately.
132-
constpauseRes=fs.createReadStream(rangeFile);
133-
pauseRes.pause();
134-
pauseRes.resume();
135-
136-
letfile7=fs.createReadStream(rangeFile,Object.create({autoClose: false}));
137-
assert.strictEqual(file7.autoClose,false);
138-
file7.on('data',function(){});
139-
file7.on('end',function(){
140-
process.nextTick(function(){
141-
assert(!file7.closed);
142-
assert(!file7.destroyed);
143-
file7Next();
68+
{
69+
constoptions=Object.create({bufferSize: 1,start: 1,end: 2});
70+
constfile4=fs.createReadStream(rangeFile,options);
71+
assert.strictEqual(file4.start,1);
72+
assert.strictEqual(file4.end,2);
73+
letcontentRead='';
74+
file4.on('data',function(data){
75+
contentRead+=data.toString('utf-8');
14476
});
145-
});
146-
147-
functionfile7Next(){
148-
// This will tell us if the fd is usable again or not.
149-
file7=fs.createReadStream(null,Object.create({fd: file7.fd,start: 0}));
150-
file7.data='';
151-
file7.on('data',function(data){
152-
file7.data+=data;
77+
file4.on('end',common.mustCall(function(){
78+
assert.strictEqual(contentRead,'yz');
79+
}));
80+
}
81+
82+
{
83+
constoptions=Object.create({bufferSize: 1,start: 1});
84+
constfile5=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');
15389
});
154-
file7.on('end',function(err){
155-
assert.strictEqual(file7.data,'xyz\n');
90+
file5.on('end',common.mustCall(function(){
91+
assert.strictEqual(file5.data,'yz\n');
92+
}));
93+
}
94+
95+
// https://github.com/joyent/node/issues/2320
96+
{
97+
constoptions=Object.create({bufferSize: 1.23,start: 1});
98+
constfile6=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');
156103
});
104+
file6.on('end',common.mustCall(function(){
105+
assert.strictEqual(file6.data,'yz\n');
106+
}));
157107
}
158108

159-
// Just to make sure autoClose won't close the stream because of error.
160-
constfile8=fs.createReadStream(null,Object.create({fd: 13337,
161-
autoClose: false}));
162-
file8.on('data',function(){});
163-
file8.on('error',common.mustCall(function(){}));
109+
{
110+
assert.throws(function(){
111+
fs.createReadStream(rangeFile,Object.create({start: 10,end: 2}));
112+
},/"start"optionmustbe<="end"option/);
113+
}
164114

165-
// Make sure stream is destroyed when file does not exist.
166-
constfile9=fs.createReadStream('/path/to/file/that/does/not/exist');
167-
file9.on('data',function(){});
168-
file9.on('error',common.mustCall(function(){}));
115+
{
116+
constoptions=Object.create({start: 0,end: 0});
117+
conststream=fs.createReadStream(rangeFile,options);
118+
assert.strictEqual(stream.start,0);
119+
assert.strictEqual(stream.end,0);
120+
stream.data='';
121+
122+
stream.on('data',function(chunk){
123+
stream.data+=chunk;
124+
});
125+
126+
stream.on('end',common.mustCall(function(){
127+
assert.strictEqual(stream.data,'x');
128+
}));
129+
}
130+
131+
// pause and then resume immediately.
132+
{
133+
constpauseRes=fs.createReadStream(rangeFile);
134+
pauseRes.pause();
135+
pauseRes.resume();
136+
}
169137

170-
process.on('exit',function(){
171-
assert(file7.closed);
172-
assert(file7.destroyed);
138+
{
139+
letfile7=
140+
fs.createReadStream(rangeFile,Object.create({autoClose: false}));
141+
assert.strictEqual(file7.autoClose,false);
142+
file7.on('data',function(){});
143+
file7.on('end',common.mustCall(function(){
144+
process.nextTick(common.mustCall(function(){
145+
assert(!file7.closed);
146+
assert(!file7.destroyed);
147+
file7Next();
148+
}));
149+
}));
150+
151+
functionfile7Next(){
152+
// 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;
157+
});
158+
file7.on('end',common.mustCall(function(){
159+
assert.strictEqual(file7.data,'xyz\n');
160+
}));
161+
}
162+
process.on('exit',function(){
163+
assert(file7.closed);
164+
assert(file7.destroyed);
165+
});
166+
}
173167

174-
assert(!file8.closed);
175-
assert(!file8.destroyed);
176-
assert(file8.fd);
168+
// Just to make sure autoClose won't close the stream because of error.
169+
{
170+
constoptions=Object.create({fd: 13337,autoClose: false});
171+
constfile8=fs.createReadStream(null,options);
172+
file8.on('data',function(){});
173+
file8.on('error',common.mustCall(function(){}));
174+
process.on('exit',function(){
175+
assert(!file8.closed);
176+
assert(!file8.destroyed);
177+
assert(file8.fd);
178+
});
179+
}
177180

178-
assert(!file9.closed);
179-
assert(file9.destroyed);
180-
});
181+
// Make sure stream is destroyed when file does not exist.
182+
{
183+
constfile9=fs.createReadStream('/path/to/file/that/does/not/exist');
184+
file9.on('data',function(){});
185+
file9.on('error',common.mustCall(function(){}));
186+
187+
process.on('exit',function(){
188+
assert(!file9.closed);
189+
assert(file9.destroyed);
190+
});
191+
}

0 commit comments

Comments
(0)