Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cd02228

Browse filesBrowse files
authored
[node-core-library] Fix FileSystem.isErrnoException (#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
Copy full SHA for cd02228

File tree

Expand file treeCollapse file tree

3 files changed

+55
-14
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+55
-14
lines changed
+10Lines 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

Copy file name to clipboardExpand all lines: libraries/node-core-library/src/FileSystem.ts
+2-1Lines 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
}
+43-13Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.