Skip to content

Commit

Permalink
test: improve coverage of lib/fs.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pd4d10 committed May 18, 2021
1 parent 910efc2 commit 1478960
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions test/parallel/test-fs-write-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}));
}
32 changes: 32 additions & 0 deletions test/parallel/test-fs-writefile-with-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 1478960

Please sign in to comment.