From 14789601f8b5171758638009d035012eaa93e340 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sun, 9 May 2021 02:35:53 +0800 Subject: [PATCH 1/2] test: improve coverage of lib/fs.js --- test/parallel/test-fs-write-file.js | 12 ++++++++ test/parallel/test-fs-writefile-with-fd.js | 32 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/test/parallel/test-fs-write-file.js b/test/parallel/test-fs-write-file.js index 3d0fd48f05f361..98a033dce9ca00 100644 --- a/test/parallel/test-fs-write-file.js +++ b/test/parallel/test-fs-write-file.js @@ -95,3 +95,15 @@ fs.open(filename4, 'w+', common.mustSucceed((fd) => { process.nextTick(() => controller.abort()); } + +{ + // Test read-only mode + const filename = join(tmpdir.path, 'test6.txt'); + fs.writeFileSync(filename, ''); + + // TODO: Correct the error type + fs.writeFile(filename, s, { flag: 'r' }, 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..4cef96f3f29a08 100644 --- a/test/parallel/test-fs-writefile-with-fd.js +++ b/test/parallel/test-fs-writefile-with-fd.js @@ -55,3 +55,35 @@ tmpdir.refresh(); })); })); } + + +// Test read-only file descriptor +{ + // TODO(pd4d10): https://github.com/nodejs/node/issues/38607 + const isWindows = process.platform === 'win32'; + const expectedCode = isWindows ? 'EPERM' : 'EBADF'; + + const file = join(tmpdir.path, 'test.txt'); + + fs.open(file, 'r', common.mustSucceed((fd) => { + fs.writeFile(fd, 'World', common.expectsError({ + code: expectedCode, + message: expectedCode + ': 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(); +} From a82fb753df365288eca1781e9bd932e3f0502d28 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Wed, 19 May 2021 22:49:45 +0800 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Darshan Sen --- test/parallel/test-fs-write-file.js | 6 ++---- test/parallel/test-fs-writefile-with-fd.js | 8 ++------ 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/test/parallel/test-fs-write-file.js b/test/parallel/test-fs-write-file.js index 98a033dce9ca00..45cbde4a90eb88 100644 --- a/test/parallel/test-fs-write-file.js +++ b/test/parallel/test-fs-write-file.js @@ -102,8 +102,6 @@ fs.open(filename4, 'w+', common.mustSucceed((fd) => { fs.writeFileSync(filename, ''); // TODO: Correct the error type - fs.writeFile(filename, s, { flag: 'r' }, common.expectsError({ - code: 'EBADF', - message: 'EBADF: bad file descriptor, write' - })); + const expectedError = common.isWindows ? /EPERM/ : /EBADF/; + fs.writeFile(filename, s, { flag: 'r' }, common.expectsError(expectedError)); } diff --git a/test/parallel/test-fs-writefile-with-fd.js b/test/parallel/test-fs-writefile-with-fd.js index 4cef96f3f29a08..2af5b39adbc14a 100644 --- a/test/parallel/test-fs-writefile-with-fd.js +++ b/test/parallel/test-fs-writefile-with-fd.js @@ -60,16 +60,12 @@ tmpdir.refresh(); // Test read-only file descriptor { // TODO(pd4d10): https://github.com/nodejs/node/issues/38607 - const isWindows = process.platform === 'win32'; - const expectedCode = isWindows ? 'EPERM' : 'EBADF'; + const expectedError = common.isWindows ? /EPERM/ : /EBADF/; const file = join(tmpdir.path, 'test.txt'); fs.open(file, 'r', common.mustSucceed((fd) => { - fs.writeFile(fd, 'World', common.expectsError({ - code: expectedCode, - message: expectedCode + ': bad file descriptor, write' - })); + fs.writeFile(fd, 'World', common.expectsError(expectedError)); })); }