@@ -38,8 +38,6 @@ const s = '南越国是前203年至前111年存在于岭南地区的一个国家 |
38 | 38 | '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' + |
39 | 39 | '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'; |
40 | 40 |
|
41 | | -let ncallbacks = 0; |
42 | | - |
43 | 41 | tmpdir.refresh(); |
44 | 42 |
|
45 | 43 | const throwNextTick = (e) => { process.nextTick(() => { throw e; }); }; |
@@ -178,42 +176,50 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); }; |
178 | 176 | .catch(throwNextTick); |
179 | 177 | } |
180 | 178 |
|
181 | | -// test that appendFile accepts file descriptors |
182 | | -const filename5 = join(tmpdir.path, 'append5.txt'); |
183 | | -fs.writeFileSync(filename5, currentFileData); |
184 | | - |
185 | | -fs.open(filename5, 'a+', function(e, fd) { |
186 | | - assert.ifError(e); |
187 | | - |
188 | | - ncallbacks++; |
| 179 | +// test that appendFile accepts file descriptors (callback API) |
| 180 | +{ |
| 181 | + const filename = join(tmpdir.path, 'append-descriptors.txt'); |
| 182 | + fs.writeFileSync(filename, currentFileData); |
189 | 183 |
|
190 | | - fs.appendFile(fd, s, function(e) { |
| 184 | + fs.open(filename, 'a+', common.mustCall((e, fd) => { |
191 | 185 | assert.ifError(e); |
192 | 186 |
|
193 | | - ncallbacks++; |
194 | | - |
195 | | - fs.close(fd, function(e) { |
| 187 | + fs.appendFile(fd, s, common.mustCall((e) => { |
196 | 188 | assert.ifError(e); |
197 | 189 |
|
198 | | - ncallbacks++; |
199 | | - |
200 | | - fs.readFile(filename5, function(e, buffer) { |
| 190 | + fs.close(fd, common.mustCall((e) => { |
201 | 191 | assert.ifError(e); |
202 | 192 |
|
203 | | - ncallbacks++; |
204 | | - assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, |
205 | | - buffer.length); |
206 | | - }); |
207 | | - }); |
208 | | - }); |
209 | | -}); |
| 193 | + fs.readFile(filename, common.mustCall((e, buffer) => { |
| 194 | + assert.ifError(e); |
| 195 | + assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, |
| 196 | + buffer.length); |
| 197 | + })); |
| 198 | + })); |
| 199 | + })); |
| 200 | + })); |
| 201 | +} |
| 202 | + |
| 203 | +// test that appendFile accepts file descriptors (promises API) |
| 204 | +{ |
| 205 | + const filename = join(tmpdir.path, 'append-descriptors-promises.txt'); |
| 206 | + fs.writeFileSync(filename, currentFileData); |
| 207 | + |
| 208 | + let fd; |
| 209 | + fs.promises.open(filename, 'a+') |
| 210 | + .then(common.mustCall((fileDescriptor) => { |
| 211 | + fd = fileDescriptor; |
| 212 | + return fs.promises.appendFile(fd, s); |
| 213 | + })) |
| 214 | + .then(common.mustCall(() => fd.close())) |
| 215 | + .then(common.mustCall(() => fs.promises.readFile(filename))) |
| 216 | + .then(common.mustCall((buffer) => { |
| 217 | + assert.strictEqual(Buffer.byteLength(s) + currentFileData.length, |
| 218 | + buffer.length); |
| 219 | + })) |
| 220 | + .catch(throwNextTick); |
| 221 | +} |
210 | 222 |
|
211 | 223 | assert.throws( |
212 | 224 | () => fs.appendFile(join(tmpdir.path, 'append6.txt'), console.log), |
213 | 225 | { code: 'ERR_INVALID_CALLBACK' }); |
214 | | - |
215 | | -process.on('exit', function() { |
216 | | - assert.strictEqual(ncallbacks, 4); |
217 | | - |
218 | | - fs.unlinkSync(filename5); |
219 | | -}); |
|