10000 [node-core-library] Fix FileSystem.isErrnoException (#5213) · lwsinclair/rushstack@cd02228 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd02228

Browse files
authored
[node-core-library] Fix FileSystem.isErrnoException (microsoft#5213)
* [node-core-library] Fix FileSystem.isErrnoException * Add unit tests --------- Co-authored-by: David Michon <dmichon-msft@users.noreply.github.com>
1 parent 66196ff commit cd02228

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/node-core-library",
5+
"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`.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@rushstack/node-core-library"
10+
}

libraries/node-core-library/src/FileSystem.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,10 +1508,11 @@ export class FileSystem {
15081508
*/
15091509
public static isErrnoException(error: Error): error is NodeJS.ErrnoException {
15101510
const typedError: NodeJS.ErrnoException = error;
1511+
// Don't check for `path` because the syscall may not have a path.
1512+
// For example, when invoked with a file descriptor.
15111513
return (
15121514
typeof typedError.code === 'string' &&
15131515
typeof typedError.errno === 'number' &&
1514-
typeof typedError.path === 'string' &&
15151516
typeof typedError.syscall === 'string'
15161517
);
15171518
}
Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
import fs from 'node:fs';
5+
46
import { FileSystem } from '../FileSystem';
57
import { PosixModeBits } from '../PosixModeBits';
68

7-
// The PosixModeBits are intended to be used with bitwise operations.
8-
/* eslint-disable no-bitwise */
9+
describe(FileSystem.name, () => {
10+
test(FileSystem.formatPosixModeBits.name, () => {
11+
// The PosixModeBits are intended to be used with bitwise operations.
12+
/* eslint-disable no-bitwise */
13+
let modeBits: number = PosixModeBits.AllRead | PosixModeBits.AllWrite;
14+
15+
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-rw-rw-rw-');
16+
17+
modeBits |= PosixModeBits.GroupExecute;
18+
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-rw-rwxrw-');
919

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

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

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

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

22-
// Add the group execute bit
23-
modeBits &= ~PosixModeBits.AllWrite;
24-
expect(FileSystem.formatPosixModeBits(modeBits)).toEqual('-r--r-xr-x');
45+
test('Should return true for an error on a file descriptor call', () => {
46+
const buffer: Buffer = Buffer.allocUnsafeSlow(1024);
47+
expect.assertions(1);
48+
try {
49+
fs.readSync(11, buffer, 0, buffer.length, -1);
50+
} catch (error) {
51+
expect(FileSystem.isErrnoException(error)).toBe(true);
52+
}
53+
});
54+
});
2555
});

0 commit comments

Comments
 (0)
0