summaryrefslogtreecommitdiff
path: root/tests/node_compat/test/parallel/test-fs-write-file-sync.js
diff options
context:
space:
mode:
Diffstat (limited to 'tests/node_compat/test/parallel/test-fs-write-file-sync.js')
-rw-r--r--tests/node_compat/test/parallel/test-fs-write-file-sync.js53
1 files changed, 34 insertions, 19 deletions
diff --git a/tests/node_compat/test/parallel/test-fs-write-file-sync.js b/tests/node_compat/test/parallel/test-fs-write-file-sync.js
index d23523ad3..e7da85260 100644
--- a/tests/node_compat/test/parallel/test-fs-write-file-sync.js
+++ b/tests/node_compat/test/parallel/test-fs-write-file-sync.js
@@ -33,7 +33,6 @@ if (!common.isMainThread)
common.skip('Setting process.umask is not supported in Workers');
const assert = require('assert');
-const path = require('path');
const fs = require('fs');
// On Windows chmod is only able to manipulate read-only bit. Test if creating
@@ -48,7 +47,7 @@ tmpdir.refresh();
// Test writeFileSync
{
- const file = path.join(tmpdir.path, 'testWriteFileSync.txt');
+ const file = tmpdir.resolve('testWriteFileSync.txt');
fs.writeFileSync(file, '123', { mode });
const content = fs.readFileSync(file, { encoding: 'utf8' });
@@ -58,7 +57,7 @@ tmpdir.refresh();
// Test appendFileSync
{
- const file = path.join(tmpdir.path, 'testAppendFileSync.txt');
+ const file = tmpdir.resolve('testAppendFileSync.txt');
fs.appendFileSync(file, 'abc', { mode });
const content = fs.readFileSync(file, { encoding: 'utf8' });
@@ -84,7 +83,7 @@ tmpdir.refresh();
return _closeSync(...args);
};
- const file = path.join(tmpdir.path, 'testWriteFileSyncFd.txt');
+ const file = tmpdir.resolve('testWriteFileSyncFd.txt');
const fd = fs.openSync(file, 'w+', mode);
fs.writeFileSync(fd, '123');
@@ -101,7 +100,7 @@ tmpdir.refresh();
// Test writeFileSync with flags
{
- const file = path.join(tmpdir.path, 'testWriteFileSyncFlags.txt');
+ const file = tmpdir.resolve('testWriteFileSyncFlags.txt');
fs.writeFileSync(file, 'hello ', { encoding: 'utf8', flag: 'a' });
fs.writeFileSync(file, 'world!', { encoding: 'utf8', flag: 'a' });
@@ -109,20 +108,36 @@ tmpdir.refresh();
assert.strictEqual(content, 'hello world!');
}
-// Test writeFileSync with an object with an own toString function
+// Test writeFileSync with no flags
{
- // Runtime deprecated by DEP0162
- common.expectWarning('DeprecationWarning',
- 'Implicit coercion of objects with own toString property is deprecated.',
- 'DEP0162');
- const file = path.join(tmpdir.path, 'testWriteFileSyncStringify.txt');
- const data = {
- toString() {
- return 'hello world!';
- }
- };
+ const utf8Data = 'hello world!';
+ for (const test of [
+ { data: utf8Data },
+ { data: utf8Data, options: { encoding: 'utf8' } },
+ { data: Buffer.from(utf8Data, 'utf8').toString('hex'), options: { encoding: 'hex' } },
+ ]) {
+ const file = tmpdir.resolve(`testWriteFileSyncNewFile_${Math.random()}.txt`);
+ fs.writeFileSync(file, test.data, test.options);
+
+ const content = fs.readFileSync(file, { encoding: 'utf-8' });
+ assert.strictEqual(content, utf8Data);
+ }
+}
- fs.writeFileSync(file, data, { encoding: 'utf8', flag: 'a' });
- const content = fs.readFileSync(file, { encoding: 'utf8' });
- assert.strictEqual(content, String(data));
+// Test writeFileSync with an invalid input
+{
+ const file = tmpdir.resolve('testWriteFileSyncInvalid.txt');
+ for (const data of [
+ false, 5, {}, [], null, undefined, true, 5n, () => {}, Symbol(), new Map(),
+ new String('notPrimitive'),
+ { [Symbol.toPrimitive]: (hint) => 'amObject' },
+ { toString() { return 'amObject'; } },
+ Promise.resolve('amPromise'),
+ common.mustNotCall(),
+ ]) {
+ assert.throws(
+ () => fs.writeFileSync(file, data, { encoding: 'utf8', flag: 'a' }),
+ { code: 'ERR_INVALID_ARG_TYPE' }
+ );
+ }
}