From 78051636a0c9966ea3a2ae6c4187be392fb0f8f5 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang <pd4d10@gmail.com> Date: Sun, 9 May 2021 02:35:53 +0800 Subject: [PATCH] test: improve coverage of lib/fs.js --- test/parallel/test-fs-write-file.js | 10 +++++++++ test/parallel/test-fs-writefile-with-fd.js | 26 ++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/test/parallel/test-fs-write-file.js b/test/parallel/test-fs-write-file.js index 3d0fd48f05f361..43aa619b59b5c2 100644 --- a/test/parallel/test-fs-write-file.js +++ b/test/parallel/test-fs-write-file.js @@ -95,3 +95,13 @@ fs.open(filename4, 'w+', common.mustSucceed((fd) => { process.nextTick(() => controller.abort()); } + +{ + // Test read-only mode + const filename3 = join(tmpdir.path, 'test3.txt'); + const readOnlyOption = { mode: fs.constants.O_RDONLY, flag: 'r' }; + fs.writeFile(filename3, s, readOnlyOption, common.expectsError({ + code: 'EBADF', + message: 'EBADF: bad file descriptor, write' + })); +} diff --git a/test/parallel/test-fs-writefile-with-fd.js b/test/parallel/test-fs-writefile-with-fd.js index 1effc519627b50..f6137e1d26377a 100644 --- a/test/parallel/test-fs-writefile-with-fd.js +++ b/test/parallel/test-fs-writefile-with-fd.js @@ -55,3 +55,29 @@ tmpdir.refresh(); })); })); } + +// Test read-only file descriptor +{ + const file = join(tmpdir.path, 'test.txt'); + fs.open(file, 'r', common.mustSucceed((fd) => { + fs.writeFile(fd, 'World', common.expectsError({ + code: 'EBADF', + message: 'EBADF: bad file descriptor, write' + })); + })); +} + +// Test with an AbortSignal +{ + const controller = new AbortController(); + const signal = controller.signal; + const file = join(tmpdir.path, 'test.txt'); + + fs.open(file, 'w', common.mustSucceed((fd) => { + fs.writeFile(fd, 'World', { signal }, common.expectsError({ + name: 'AbortError' + })); + })); + + controller.abort(); +}