Skip to content

Commit 1478960

Browse files
committed
test: improve coverage of lib/fs.js
1 parent 910efc2 commit 1478960

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

test/parallel/test-fs-write-file.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,15 @@ fs.open(filename4, 'w+', common.mustSucceed((fd) => {
9595

9696
process.nextTick(() => controller.abort());
9797
}
98+
99+
{
100+
// Test read-only mode
101+
const filename = join(tmpdir.path, 'test6.txt');
102+
fs.writeFileSync(filename, '');
103+
104+
// TODO: Correct the error type
105+
fs.writeFile(filename, s, { flag: 'r' }, common.expectsError({
106+
code: 'EBADF',
107+
message: 'EBADF: bad file descriptor, write'
108+
}));
109+
}

test/parallel/test-fs-writefile-with-fd.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,35 @@ tmpdir.refresh();
5555
}));
5656
}));
5757
}
58+
59+
60+
// Test read-only file descriptor
61+
{
62+
// TODO(pd4d10): https://github.com/nodejs/node/issues/38607
63+
const isWindows = process.platform === 'win32';
64+
const expectedCode = isWindows ? 'EPERM' : 'EBADF';
65+
66+
const file = join(tmpdir.path, 'test.txt');
67+
68+
fs.open(file, 'r', common.mustSucceed((fd) => {
69+
fs.writeFile(fd, 'World', common.expectsError({
70+
code: expectedCode,
71+
message: expectedCode + ': bad file descriptor, write'
72+
}));
73+
}));
74+
}
75+
76+
// Test with an AbortSignal
77+
{
78+
const controller = new AbortController();
79+
const signal = controller.signal;
80+
const file = join(tmpdir.path, 'test.txt');
81+
82+
fs.open(file, 'w', common.mustSucceed((fd) => {
83+
fs.writeFile(fd, 'World', { signal }, common.expectsError({
84+
name: 'AbortError'
85+
}));
86+
}));
87+
88+
controller.abort();
89+
}

0 commit comments

Comments
 (0)