test: use runWithInvalidFD() in tests expecting EBADF

PR-URL: https://github.com/nodejs/node/pull/18864
Fixes: https://github.com/nodejs/node/issues/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>
This commit is contained in:
Joyee Cheung 2018-02-19 17:38:11 +08:00
parent acf2fd39f7
commit 5055c29e82
No known key found for this signature in database
GPG Key ID: F586868AAD831D0C
3 changed files with 55 additions and 85 deletions

View File

@ -4,9 +4,7 @@
// include the desired properties // include the desired properties
const common = require('../common'); const common = require('../common');
const assert = require('assert');
const fs = require('fs'); const fs = require('fs');
const uv = process.binding('uv');
['', false, null, undefined, {}, []].forEach((i) => { ['', false, null, undefined, {}, []].forEach((i) => {
common.expectsError( common.expectsError(
@ -26,41 +24,3 @@ const uv = process.binding('uv');
} }
); );
}); });
{
assert.throws(
() => {
const fd = fs.openSync(__filename, 'r');
fs.closeSync(fd);
fs.closeSync(fd);
},
(err) => {
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(
err.message,
'EBADF: bad file descriptor, close'
);
assert.strictEqual(err.constructor, Error);
assert.strictEqual(err.syscall, 'close');
assert.strictEqual(err.errno, uv.UV_EBADF);
return true;
}
);
}
{
const fd = fs.openSync(__filename, 'r');
fs.close(fd, common.mustCall((err) => {
assert.ifError(err);
fs.close(fd, common.mustCall((err) => {
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(
err.message,
'EBADF: bad file descriptor, close'
);
assert.strictEqual(err.constructor, Error);
assert.strictEqual(err.syscall, 'close');
assert.strictEqual(err.errno, uv.UV_EBADF);
}));
}));
}

View File

@ -94,15 +94,14 @@ function re(literals, ...values) {
return true; return true;
}; };
const fd = fs.openSync(existingFile, 'r'); common.runWithInvalidFD((fd) => {
fs.closeSync(fd); fs.fstat(fd, common.mustCall(validateError));
fs.fstat(fd, common.mustCall(validateError)); assert.throws(
() => fs.fstatSync(fd),
assert.throws( validateError
() => fs.fstatSync(fd), );
validateError });
);
} }
// realpath // realpath
@ -414,6 +413,27 @@ function re(literals, ...values) {
); );
} }
// close
{
const validateError = (err) => {
assert.strictEqual(err.message, 'EBADF: bad file descriptor, close');
assert.strictEqual(err.errno, uv.UV_EBADF);
assert.strictEqual(err.code, 'EBADF');
assert.strictEqual(err.syscall, 'close');
return true;
};
common.runWithInvalidFD((fd) => {
fs.close(fd, common.mustCall(validateError));
assert.throws(
() => fs.closeSync(fd),
validateError
);
});
}
// readFile // readFile
{ {
const validateError = (err) => { const validateError = (err) => {
@ -472,15 +492,14 @@ function re(literals, ...values) {
return true; return true;
}; };
const fd = fs.openSync(existingFile, 'r'); common.runWithInvalidFD((fd) => {
fs.closeSync(fd); fs.ftruncate(fd, 4, common.mustCall(validateError));
fs.ftruncate(fd, 4, common.mustCall(validateError)); assert.throws(
() => fs.ftruncateSync(fd, 4),
assert.throws( validateError
() => fs.ftruncateSync(fd, 4), );
validateError });
);
} }
// fdatasync // fdatasync
@ -493,15 +512,14 @@ function re(literals, ...values) {
return true; return true;
}; };
const fd = fs.openSync(existingFile, 'r'); common.runWithInvalidFD((fd) => {
fs.closeSync(fd); fs.fdatasync(fd, common.mustCall(validateError));
fs.fdatasync(fd, common.mustCall(validateError)); assert.throws(
() => fs.fdatasyncSync(fd),
assert.throws( validateError
() => fs.fdatasyncSync(fd), );
validateError });
);
} }
// fsync // fsync
@ -514,13 +532,12 @@ function re(literals, ...values) {
return true; return true;
}; };
const fd = fs.openSync(existingFile, 'r'); common.runWithInvalidFD((fd) => {
fs.closeSync(fd); fs.fsync(fd, common.mustCall(validateError));
fs.fsync(fd, common.mustCall(validateError)); assert.throws(
() => fs.fsyncSync(fd),
assert.throws( validateError
() => fs.fsyncSync(fd), );
validateError });
);
} }

View File

@ -2,7 +2,6 @@
// Flags: --expose-internals // Flags: --expose-internals
const common = require('../common'); const common = require('../common');
const fs = require('fs');
const tty = require('tty'); const tty = require('tty');
const { SystemError } = require('internal/errors'); const { SystemError } = require('internal/errors');
@ -22,12 +21,9 @@ common.expectsError(
common.expectsError( common.expectsError(
() => { () => {
let fd = 2; common.runWithInvalidFD((fd) => {
// Get first known bad file descriptor. new tty.WriteStream(fd);
try { });
while (fs.fstatSync(++fd));
} catch (e) { }
new tty.WriteStream(fd);
}, { }, {
code: 'ERR_SYSTEM_ERROR', code: 'ERR_SYSTEM_ERROR',
type: SystemError, type: SystemError,
@ -37,12 +33,9 @@ common.expectsError(
common.expectsError( common.expectsError(
() => { () => {
let fd = 2; common.runWithInvalidFD((fd) => {
// Get first known bad file descriptor. new tty.ReadStream(fd);
try { });
while (fs.fstatSync(++fd));
} catch (e) { }
new tty.ReadStream(fd);
}, { }, {
code: 'ERR_SYSTEM_ERROR', code: 'ERR_SYSTEM_ERROR',
type: SystemError, type: SystemError,