|
1 | 1 | 'use strict';
|
2 | 2 | const common = require('../common');
|
3 |
| -const assert = require('assert'); |
4 | 3 |
|
5 |
| -const path = require('path'); |
| 4 | +const assert = require('assert'); |
6 | 5 | const fs = require('fs');
|
| 6 | +const path = require('path'); |
| 7 | + |
7 | 8 | const fn = path.join(common.fixturesDir, 'elipses.txt');
|
8 | 9 | const rangeFile = path.join(common.fixturesDir, 'x.txt');
|
9 | 10 |
|
10 |
| -let paused = false; |
11 |
| - |
12 | 11 | {
|
| 12 | + let paused = false; |
| 13 | + |
13 | 14 | const file = fs.ReadStream(fn);
|
14 | 15 |
|
15 | 16 | file.on('open', common.mustCall(function(fd) {
|
@@ -48,61 +49,61 @@ let paused = false;
|
48 | 49 | }
|
49 | 50 |
|
50 | 51 | {
|
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) { |
54 | 55 | assert.strictEqual(typeof data, 'string');
|
55 |
| - file3.length += data.length; |
| 56 | + file.length += data.length; |
56 | 57 |
|
57 | 58 | for (let i = 0; i < data.length; i++) {
|
58 | 59 | // http://www.fileformat.info/info/unicode/char/2026/index.htm
|
59 | 60 | assert.strictEqual(data[i], '\u2026');
|
60 | 61 | }
|
61 | 62 | });
|
62 | 63 |
|
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); |
65 | 66 | }));
|
66 | 67 | }
|
67 | 68 |
|
68 | 69 | {
|
69 | 70 | 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); |
73 | 74 | let contentRead = '';
|
74 |
| - file4.on('data', function(data) { |
| 75 | + file.on('data', function(data) { |
75 | 76 | contentRead += data.toString('utf-8');
|
76 | 77 | });
|
77 |
| - file4.on('end', common.mustCall(function() { |
| 78 | + file.on('end', common.mustCall(function() { |
78 | 79 | assert.strictEqual(contentRead, 'yz');
|
79 | 80 | }));
|
80 | 81 | }
|
81 | 82 |
|
82 | 83 | {
|
83 | 84 | 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'); |
89 | 90 | });
|
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'); |
92 | 93 | }));
|
93 | 94 | }
|
94 | 95 |
|
95 | 96 | // https://github.com/joyent/node/issues/2320
|
96 | 97 | {
|
97 | 98 | 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'); |
103 | 104 | });
|
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'); |
106 | 107 | }));
|
107 | 108 | }
|
108 | 109 |
|
@@ -136,56 +137,58 @@ let paused = false;
|
136 | 137 | }
|
137 | 138 |
|
138 | 139 | {
|
139 |
| - let file7 = |
| 140 | + let data = ''; |
| 141 | + let file = |
140 | 142 | 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() { |
144 | 146 | 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(); |
148 | 151 | }));
|
149 | 152 | }));
|
150 | 153 |
|
151 |
| - function file7Next() { |
| 154 | + function fileNext() { |
152 | 155 | // 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; |
157 | 160 | });
|
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'); |
160 | 163 | }));
|
161 | 164 | }
|
162 | 165 | process.on('exit', function() {
|
163 |
| - assert(file7.closed); |
164 |
| - assert(file7.destroyed); |
| 166 | + assert(file.closed); |
| 167 | + assert(file.destroyed); |
165 | 168 | });
|
166 | 169 | }
|
167 | 170 |
|
168 | 171 | // Just to make sure autoClose won't close the stream because of error.
|
169 | 172 | {
|
170 | 173 | 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()); |
174 | 177 | 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); |
178 | 181 | });
|
179 | 182 | }
|
180 | 183 |
|
181 | 184 | // Make sure stream is destroyed when file does not exist.
|
182 | 185 | {
|
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()); |
186 | 189 |
|
187 | 190 | process.on('exit', function() {
|
188 |
| - assert(!file9.closed); |
189 |
| - assert(file9.destroyed); |
| 191 | + assert(!file.closed); |
| 192 | + assert(file.destroyed); |
190 | 193 | });
|
191 | 194 | }
|
0 commit comments