Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/node-core-library",
"comment": "Fix a bug in `FileSystem.isErrnoException` that failed to identify errors if the underlying method was invoked using only a file descriptor, e.g. for `fs.readSync`.",
"type": "patch"
}
],
"packageName": "@rushstack/node-core-library"
}
3 changes: 2 additions & 1 deletion libraries/node-core-library/src/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1508,10 +1508,11 @@ export class FileSystem {
*/
public static isErrnoException(error: Error): error is NodeJS.ErrnoException {
const typedError: NodeJS.ErrnoException = error;
// Don't check for `path` because the syscall may not have a path.
// For example, when invoked with a file descriptor.
return (
typeof typedError.code === 'string' &&
typeof typedError.errno === 'number' &&
typeof typedError.path === 'string' &&
typeof typedError.syscall === 'string'
);
}
Expand Down
56 changes: 43 additions & 13 deletions libraries/node-core-library/src/test/FileSystem.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import fs from 'node:fs';

import { FileSystem } from '../FileSystem';
import { PosixModeBits } from '../PosixModeBits';

// The PosixModeBits are intended to be used with bitwise operations.
/* eslint-disable no-bitwise */
describe(FileSystem.name, () => {
test(FileSystem.formatPosixModeBits.name, () => {
// The PosixModeBits are intended to be used with bitwise operations.
/* eslint-disable no-bitwise */
let modeBits: number = PosixModeBits.AllRead | PosixModeBits.AllWrite;

expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-rw-rw-rw-');

modeBits |= PosixModeBits.GroupExecute;
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-rw-rwxrw-');

test('PosixModeBits tests', () => {
let modeBits: number = PosixModeBits.AllRead | PosixModeBits.AllWrite;
// Add the group execute bit
modeBits |= PosixModeBits.OthersExecute;
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-rw-rwxrwx');

expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-rw-rw-rw-');
// Add the group execute bit
modeBits &= ~PosixModeBits.AllWrite;
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-r--r-xr-x');
/* eslint-enable no-bitwise */
});

modeBits |= PosixModeBits.GroupExecute;
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-rw-rwxrw-');
describe(FileSystem.isErrnoException.name, () => {
test('Should return false for a non-ErrnoException', () => {
const error: Error = new Error('Test error');
expect(FileSystem.isErrnoException(error)).toBe(false);
});

// Add the group execute bit
modeBits |= PosixModeBits.OthersExecute;
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-rw-rwxrwx');
test('Should return true for an error on a path call', () => {
expect.assertions(1);
try {
fs.openSync(`${__dirname}/nonexistent.txt`, 'r');
} catch (error) {
expect(FileSystem.isErrnoException(error)).toBe(true);
}
});

// Add the group execute bit
modeBits &= ~PosixModeBits.AllWrite;
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-r--r-xr-x');
test('Should return true for an error on a file descriptor call', () => {
const buffer: Buffer = Buffer.allocUnsafeSlow(1024);
expect.assertions(1);
try {
fs.readSync(11, buffer, 0, buffer.length, -1);
} catch (error) {
expect(FileSystem.isErrnoException(error)).toBe(true);
}
});
});
});
Loading