summaryrefslogtreecommitdiff
path: root/tests/node_compat/test/parallel/test-fs-promises-writefile-with-fd.js
diff options
context:
space:
mode:
Diffstat (limited to 'tests/node_compat/test/parallel/test-fs-promises-writefile-with-fd.js')
-rw-r--r--tests/node_compat/test/parallel/test-fs-promises-writefile-with-fd.js42
1 files changed, 42 insertions, 0 deletions
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());