Skip to content

Commit 5055c29

Browse files
committed
test: use runWithInvalidFD() in tests expecting EBADF
PR-URL: #18864 Fixes: #18820 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent acf2fd3 commit 5055c29

File tree

3 files changed

+56
-86
lines changed

3 files changed

+56
-86
lines changed

test/parallel/test-fs-close-errors.js

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
// include the desired properties
55

66
const common = require('../common');
7-
const assert = require('assert');
87
const fs = require('fs');
9-
const uv = process.binding('uv');
108

119
['', false, null, undefined, {}, []].forEach((i) => {
1210
common.expectsError(
@@ -26,41 +24,3 @@ const uv = process.binding('uv');
2624
}
2725
);
2826
});
29-
30-
{
31-
assert.throws(
32-
() => {
33-
const fd = fs.openSync(__filename, 'r');
34-
fs.closeSync(fd);
35-
fs.closeSync(fd);
36-
},
37-
(err) => {
38-
assert.strictEqual(err.code, 'EBADF');
39-
assert.strictEqual(
40-
err.message,
41-
'EBADF: bad file descriptor, close'
42-
);
43-
assert.strictEqual(err.constructor, Error);
44-
assert.strictEqual(err.syscall, 'close');
45-
assert.strictEqual(err.errno, uv.UV_EBADF);
46-
return true;
47-
}
48-
);
49-
}
50-
51-
{
52-
const fd = fs.openSync(__filename, 'r');
53-
fs.close(fd, common.mustCall((err) => {
54-
assert.ifError(err);
55-
fs.close(fd, common.mustCall((err) => {
56-
assert.strictEqual(err.code, 'EBADF');
57-
assert.strictEqual(
58-
err.message,
59-
'EBADF: bad file descriptor, close'
60-
);
61-
assert.strictEqual(err.constructor, Error);
62-
assert.strictEqual(err.syscall, 'close');
63-
assert.strictEqual(err.errno, uv.UV_EBADF);
64-
}));
65-
}));
66-
}

test/parallel/test-fs-error-messages.js

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,14 @@ function re(literals, ...values) {
9494
return true;
9595
};
9696

97-
const fd = fs.openSync(existingFile, 'r');
98-
fs.closeSync(fd);
99-
100-
fs.fstat(fd, common.mustCall(validateError));
101-
102-
assert.throws(
103-
() => fs.fstatSync(fd),
104-
validateError
105-
);
97+
common.runWithInvalidFD((fd) => {
98+
fs.fstat(fd, common.mustCall(validateError));
99+
100+
assert.throws(
101+
() => fs.fstatSync(fd),
102+
validateError
103+
);
104+
});
106105
}
107106

108107
// realpath
@@ -414,6 +413,27 @@ function re(literals, ...values) {
414413
);
415414
}
416415

416+
417+
// close
418+
{
419+
const validateError = (err) => {
420+
assert.strictEqual(err.message, 'EBADF: bad file descriptor, close');
421+
assert.strictEqual(err.errno, uv.UV_EBADF);
422+
assert.strictEqual(err.code, 'EBADF');
423+
assert.strictEqual(err.syscall, 'close');
424+
return true;
425+
};
426+
427+
common.runWithInvalidFD((fd) => {
428+
fs.close(fd, common.mustCall(validateError));
429+
430+
assert.throws(
431+
() => fs.closeSync(fd),
432+
validateError
433+
);
434+
});
435+
}
436+
417437
// readFile
418438
{
419439
const validateError = (err) => {
@@ -472,15 +492,14 @@ function re(literals, ...values) {
472492
return true;
473493
};
474494

475-
const fd = fs.openSync(existingFile, 'r');
476-
fs.closeSync(fd);
495+
common.runWithInvalidFD((fd) => {
496+
fs.ftruncate(fd, 4, common.mustCall(validateError));
477497

478-
fs.ftruncate(fd, 4, common.mustCall(validateError));
479-
480-
assert.throws(
481-
() => fs.ftruncateSync(fd, 4),
482-
validateError
483-
);
498+
assert.throws(
499+
() => fs.ftruncateSync(fd, 4),
500+
validateError
501+
);
502+
});
484503
}
485504

486505
// fdatasync
@@ -493,15 +512,14 @@ function re(literals, ...values) {
493512
return true;
494513
};
495514

496-
const fd = fs.openSync(existingFile, 'r');
497-
fs.closeSync(fd);
498-
499-
fs.fdatasync(fd, common.mustCall(validateError));
515+
common.runWithInvalidFD((fd) => {
516+
fs.fdatasync(fd, common.mustCall(validateError));
500517

501-
assert.throws(
502-
() => fs.fdatasyncSync(fd),
503-
validateError
504-
);
518+
assert.throws(
519+
() => fs.fdatasyncSync(fd),
520+
validateError
521+
);
522+
});
505523
}
506524

507525
// fsync
@@ -514,13 +532,12 @@ function re(literals, ...values) {
514532
return true;
515533
};
516534

517-
const fd = fs.openSync(existingFile, 'r');
518-
fs.closeSync(fd);
519-
520-
fs.fsync(fd, common.mustCall(validateError));
535+
common.runWithInvalidFD((fd) => {
536+
fs.fsync(fd, common.mustCall(validateError));
521537

522-
assert.throws(
523-
() => fs.fsyncSync(fd),
524-
validateError
525-
);
538+
assert.throws(
539+
() => fs.fsyncSync(fd),
540+
validateError
541+
);
542+
});
526543
}

test/parallel/test-ttywrap-invalid-fd.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Flags: --expose-internals
33

44
const common = require('../common');
5-
const fs = require('fs');
65
const tty = require('tty');
76
const { SystemError } = require('internal/errors');
87

@@ -22,12 +21,9 @@ common.expectsError(
2221

2322
common.expectsError(
2423
() => {
25-
let fd = 2;
26-
// Get first known bad file descriptor.
27-
try {
28-
while (fs.fstatSync(++fd));
29-
} catch (e) { }
30-
new tty.WriteStream(fd);
24+
common.runWithInvalidFD((fd) => {
25+
new tty.WriteStream(fd);
26+
});
3127
}, {
3228
code: 'ERR_SYSTEM_ERROR',
3329
type: SystemError,
@@ -37,12 +33,9 @@ common.expectsError(
3733

3834
common.expectsError(
3935
() => {
40-
let fd = 2;
41-
// Get first known bad file descriptor.
42-
try {
43-
while (fs.fstatSync(++fd));
44-
} catch (e) { }
45-
new tty.ReadStream(fd);
36+
common.runWithInvalidFD((fd) => {
37+
new tty.ReadStream(fd);
38+
});
4639
}, {
4740
code: 'ERR_SYSTEM_ERROR',
4841
type: SystemError,

0 commit comments

Comments
 (0)