-
-
Notifications
You must be signed in to change notification settings - Fork 36.2k
doc,test: widen fsPromises.appendFile()'s data type, add missing tests #64279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hkleungai
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
hkleungai:update-fsPromises-appendFile-data-type-and-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,191 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const fs = require('fs'); | ||
| const fsPromises = fs.promises; | ||
| const path = require('path'); | ||
| const tmpdir = require('../common/tmpdir'); | ||
| const assert = require('assert'); | ||
| const tmpDir = tmpdir.path; | ||
| const { Readable } = require('stream'); | ||
|
|
||
| tmpdir.refresh(); | ||
|
|
||
| const buffer = Buffer.from('abc'.repeat(1000)); | ||
| const stream = Readable.from(['a', 'b', 'c']); | ||
| const stream2 = Readable.from(['ümlaut', ' ', 'sechzig']); | ||
| const iterable = { | ||
| expected: 'abc', | ||
| *[Symbol.iterator]() { | ||
| yield 'a'; | ||
| yield 'b'; | ||
| yield 'c'; | ||
| } | ||
| }; | ||
|
|
||
| const veryLargeIterable = { | ||
| expected: 'dogs running'.repeat(512 * 1024), | ||
| *[Symbol.iterator]() { | ||
| yield Buffer.from('dogs running'.repeat(512 * 1024), 'utf8'); | ||
| } | ||
| }; | ||
|
|
||
| function iterableWith(value) { | ||
| return { | ||
| *[Symbol.iterator]() { | ||
| yield value; | ||
| } | ||
| }; | ||
| } | ||
| const bufferIterable = { | ||
| expected: 'abc', | ||
| *[Symbol.iterator]() { | ||
| yield Buffer.from('a'); | ||
| yield Buffer.from('b'); | ||
| yield Buffer.from('c'); | ||
| } | ||
| }; | ||
| const asyncIterable = { | ||
| expected: 'abc', | ||
| async* [Symbol.asyncIterator]() { | ||
| yield 'a'; | ||
| yield 'b'; | ||
| yield 'c'; | ||
| } | ||
| }; | ||
|
|
||
| let counter = 0; | ||
|
|
||
| async function doAppendString() { | ||
| const string = 'x~yz'.repeat(100); | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await fsPromises.appendFile(dest, string); | ||
| const data = fs.readFileSync(dest); | ||
| const stringAsBuffer = Buffer.from(string, 'utf8'); | ||
| assert.deepStrictEqual(stringAsBuffer, data); | ||
| } | ||
|
|
||
| async function doAppendBuffer() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await fsPromises.appendFile(dest, buffer); | ||
| const data = fs.readFileSync(dest); | ||
| assert.deepStrictEqual(data, buffer); | ||
| } | ||
|
|
||
| async function doAppendStream() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await fsPromises.appendFile(dest, stream); | ||
| const expected = 'abc'; | ||
| const data = fs.readFileSync(dest, 'utf-8'); | ||
| assert.deepStrictEqual(data, expected); | ||
| } | ||
|
|
||
| async function doAppendStreamWithCancel() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| const controller = new AbortController(); | ||
| const { signal } = controller; | ||
| process.nextTick(() => controller.abort()); | ||
| await assert.rejects( | ||
| fsPromises.appendFile(dest, stream, { signal }), | ||
| { name: 'AbortError' } | ||
| ); | ||
| } | ||
|
|
||
| async function doAppendIterable() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await fsPromises.appendFile(dest, iterable); | ||
| const data = fs.readFileSync(dest, 'utf-8'); | ||
| assert.deepStrictEqual(data, iterable.expected); | ||
| } | ||
|
|
||
| async function doAppendInvalidIterable() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await Promise.all( | ||
| [42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) => | ||
| assert.rejects(fsPromises.appendFile(dest, iterableWith(value)), { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| }) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| async function doAppendIterableWithEncoding() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await fsPromises.appendFile(dest, stream2, 'latin1'); | ||
| const expected = 'ümlaut sechzig'; | ||
| const data = fs.readFileSync(dest, 'latin1'); | ||
| assert.deepStrictEqual(data, expected); | ||
| } | ||
|
|
||
| async function doAppendBufferIterable() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await fsPromises.appendFile(dest, bufferIterable); | ||
| const data = fs.readFileSync(dest, 'utf-8'); | ||
| assert.deepStrictEqual(data, bufferIterable.expected); | ||
| } | ||
|
|
||
| async function doAppendAsyncIterable() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await fsPromises.appendFile(dest, asyncIterable); | ||
| const data = fs.readFileSync(dest, 'utf-8'); | ||
| assert.deepStrictEqual(data, asyncIterable.expected); | ||
| } | ||
|
|
||
| async function doAppendLargeIterable() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await fsPromises.appendFile(dest, veryLargeIterable); | ||
| const data = fs.readFileSync(dest, 'utf-8'); | ||
| assert.deepStrictEqual(data, veryLargeIterable.expected); | ||
| } | ||
|
|
||
| async function doAppendInvalidValues() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| await Promise.all( | ||
| [42, 42n, {}, Symbol('42'), true, undefined, null, NaN].map((value) => | ||
| assert.rejects(fsPromises.appendFile(dest, value), { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| }) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| async function doAppendBufferAndCancel() { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
| const controller = new AbortController(); | ||
| const { signal } = controller; | ||
| process.nextTick(() => controller.abort()); | ||
| await assert.rejects( | ||
| fsPromises.appendFile(dest, buffer, { signal }), | ||
| { name: 'AbortError' } | ||
| ); | ||
| } | ||
|
|
||
| async function doAppendTypedArrays() { | ||
| for (const Constructor of [Uint8Array, Uint16Array, Uint32Array]) { | ||
| const dest = path.resolve(tmpDir, `tmp-${counter++}.txt`); | ||
|
|
||
| // Use a file size larger than `kReadFileMaxChunkSize`. | ||
| const buffer = Buffer.from('012'.repeat(2 ** 14)); | ||
|
|
||
| const array = new Constructor(buffer.buffer); | ||
| await fsPromises.appendFile(dest, array); | ||
| const data = await fsPromises.readFile(dest); | ||
| assert.deepStrictEqual(data, buffer); | ||
| } | ||
| } | ||
|
|
||
| (async () => { | ||
| await doAppendBuffer(); | ||
| await doAppendBufferAndCancel(); | ||
| await doAppendString(); | ||
| await doAppendStream(); | ||
| await doAppendStreamWithCancel(); | ||
| await doAppendIterable(); | ||
| await doAppendInvalidIterable(); | ||
| await doAppendIterableWithEncoding(); | ||
| await doAppendBufferIterable(); | ||
| await doAppendAsyncIterable(); | ||
| await doAppendLargeIterable(); | ||
| await doAppendInvalidValues(); | ||
| await doAppendTypedArrays(); | ||
| })().then(common.mustCall()); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine, but for running a batch of async tests, you may wish to consider utilising the
node:testrunner which has intrinsic async test support (example: https://github.com/nodejs/node/blob/main/test/parallel/test-aborted-util.js)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not read too deep on how nodejs worked with async test batch in the latest practice.
I suppose I may come back and open another PR to address things in later free time :)