diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2024-09-11 18:08:40 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-11 11:08:40 +0200 |
commit | 8bdd364dd568f93097ecee41e66c74d16d75c015 (patch) | |
tree | ae124042299ed91ee81c0a9b455e1f972be1a9e9 /tests/node_compat | |
parent | 87bc47b3bf9afe26e33450757b13c3b14b8703d8 (diff) |
fix(ext/node): add `FileHandle#writeFile` (#25555)
This PR adds `writeFile` methods of `FileHandle` class
https://nodejs.org/api/fs.html#filehandlewritefiledata-options
Diffstat (limited to 'tests/node_compat')
-rw-r--r-- | tests/node_compat/config.jsonc | 1 | ||||
-rw-r--r-- | tests/node_compat/runner/TODO.md | 1 | ||||
-rw-r--r-- | tests/node_compat/test/parallel/test-fs-promises-writefile-with-fd.js | 42 |
3 files changed, 43 insertions, 1 deletions
diff --git a/tests/node_compat/config.jsonc b/tests/node_compat/config.jsonc index ed63b5950..8c8d503d5 100644 --- a/tests/node_compat/config.jsonc +++ b/tests/node_compat/config.jsonc @@ -335,6 +335,7 @@ "test-fs-open-numeric-flags.js", "test-fs-open.js", "test-fs-opendir.js", + "test-fs-promises-writefile-with-fd.js", "test-fs-read-stream-autoClose.js", "test-fs-read-stream-concurrent-reads.js", "test-fs-read-stream-double-close.js", diff --git a/tests/node_compat/runner/TODO.md b/tests/node_compat/runner/TODO.md index 1656729e2..24e827182 100644 --- a/tests/node_compat/runner/TODO.md +++ b/tests/node_compat/runner/TODO.md @@ -829,7 +829,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co - [parallel/test-fs-promises-watch.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-fs-promises-watch.js) - [parallel/test-fs-promises-write-optional-params.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-fs-promises-write-optional-params.js) - [parallel/test-fs-promises-writefile-typedarray.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-fs-promises-writefile-typedarray.js) -- [parallel/test-fs-promises-writefile-with-fd.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-fs-promises-writefile-with-fd.js) - [parallel/test-fs-promises-writefile.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-fs-promises-writefile.js) - [parallel/test-fs-promises.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-fs-promises.js) - [parallel/test-fs-promisified.js](https://github.com/nodejs/node/tree/v18.12.1/test/parallel/test-fs-promisified.js) diff --git a/tests/node_compat/test/parallel/test-fs-promises-writefile-with-fd.js b/tests/node_compat/test/parallel/test-fs-promises-writefile-with-fd.js new file mode 100644 index 000000000..f750c28be --- /dev/null +++ b/tests/node_compat/test/parallel/test-fs-promises-writefile-with-fd.js @@ -0,0 +1,42 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +// Copyright Joyent and Node contributors. All rights reserved. MIT license. +// Taken from Node 18.12.1 +// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually. + +'use strict'; + +// This test makes sure that `writeFile()` always writes from the current +// position of the file, instead of truncating the file. + +const common = require('../common'); +const assert = require('assert'); +const { readFileSync } = require('fs'); +const { open } = require('fs').promises; + +const tmpdir = require('../common/tmpdir'); +tmpdir.refresh(); + +const fn = tmpdir.resolve('test.txt'); + +async function writeFileTest() { + const handle = await open(fn, 'w'); + + /* Write only five bytes, so that the position moves to five. */ + const buf = Buffer.from('Hello'); + const { bytesWritten } = await handle.write(buf, 0, 5, null); + assert.strictEqual(bytesWritten, 5); + + /* Write some more with writeFile(). */ + await handle.writeFile('World'); + + /* New content should be written at position five, instead of zero. */ + assert.strictEqual(readFileSync(fn).toString(), 'HelloWorld'); + + await handle.close(); +} + + +writeFileTest() + .then(common.mustCall()); |