forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: add support for async iterators to
fs.writeFile
Fixes: nodejs#38075
- Loading branch information
HiroyukiYagihashi
committed
May 8, 2021
1 parent
52e4fb5
commit 785d76d
Showing
5 changed files
with
157 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const join = require('path').join; | ||
const { Readable } = require('stream'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
|
||
{ | ||
const filenameIterable = join(tmpdir.path, 'testIterable.txt'); | ||
const iterable = { | ||
expected: 'abc', | ||
*[Symbol.iterator]() { | ||
yield 'a'; | ||
yield 'b'; | ||
yield 'c'; | ||
} | ||
}; | ||
|
||
fs.writeFile(filenameIterable, iterable, common.mustSucceed(() => { | ||
const data = fs.readFileSync(filenameIterable, 'utf-8'); | ||
assert.strictEqual(iterable.expected, data); | ||
})); | ||
} | ||
|
||
{ | ||
const filenameBufferIterable = join(tmpdir.path, 'testBufferIterable.txt'); | ||
const bufferIterable = { | ||
expected: 'abc', | ||
*[Symbol.iterator]() { | ||
yield Buffer.from('a'); | ||
yield Buffer.from('b'); | ||
yield Buffer.from('c'); | ||
} | ||
}; | ||
|
||
fs.writeFile( | ||
filenameBufferIterable, bufferIterable, common.mustSucceed(() => { | ||
const data = fs.readFileSync(filenameBufferIterable, 'utf-8'); | ||
assert.strictEqual(bufferIterable.expected, data); | ||
}) | ||
); | ||
} | ||
|
||
|
||
{ | ||
const filenameAsyncIterable = join(tmpdir.path, 'testAsyncIterable.txt'); | ||
const asyncIterable = { | ||
expected: 'abc', | ||
*[Symbol.asyncIterator]() { | ||
yield 'a'; | ||
yield 'b'; | ||
yield 'c'; | ||
} | ||
}; | ||
|
||
fs.writeFile(filenameAsyncIterable, asyncIterable, common.mustSucceed(() => { | ||
const data = fs.readFileSync(filenameAsyncIterable, 'utf-8'); | ||
assert.strictEqual(asyncIterable.expected, data); | ||
})); | ||
} | ||
|
||
{ | ||
const filenameStream = join(tmpdir.path, 'testStream.txt'); | ||
const stream = Readable.from(['a', 'b', 'c']); | ||
const expected = 'abc'; | ||
|
||
fs.writeFile(filenameStream, stream, common.mustSucceed(() => { | ||
const data = fs.readFileSync(filenameStream, 'utf-8'); | ||
assert.strictEqual(expected, data); | ||
})); | ||
} | ||
|
||
{ | ||
const filenameStreamWithEncoding = | ||
join(tmpdir.path, 'testStreamWithEncoding.txt'); | ||
const stream = Readable.from(['ümlaut', ' ', 'sechzig']); | ||
const expected = 'ümlaut sechzig'; | ||
|
||
fs.writeFile( | ||
filenameStreamWithEncoding, stream, 'latin1', common.mustSucceed(() => { | ||
const data = fs.readFileSync(filenameStreamWithEncoding, 'latin1'); | ||
assert.strictEqual(expected, data); | ||
}) | ||
); | ||
} |