Skip to content

Commit

Permalink
test: use runWithInvalidFD() in tests expecting EBADF
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18864
Fixes: nodejs#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>
  • Loading branch information
joyeecheung authored and MayaLekova committed May 8, 2018
1 parent 61620a1 commit fbd0b59
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 86 deletions.
40 changes: 0 additions & 40 deletions test/parallel/test-fs-close-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
// include the desired properties

const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const uv = process.binding('uv');

['', false, null, undefined, {}, []].forEach((i) => {
common.expectsError(
Expand All @@ -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);
}));
}));
}
83 changes: 50 additions & 33 deletions test/parallel/test-fs-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,14 @@ function re(literals, ...values) {
return true;
};

const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);

fs.fstat(fd, common.mustCall(validateError));

assert.throws(
() => fs.fstatSync(fd),
validateError
);
common.runWithInvalidFD((fd) => {
fs.fstat(fd, common.mustCall(validateError));

assert.throws(
() => fs.fstatSync(fd),
validateError
);
});
}

// realpath
Expand Down Expand Up @@ -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
{
const validateError = (err) => {
Expand Down Expand Up @@ -472,15 +492,14 @@ function re(literals, ...values) {
return true;
};

const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);
common.runWithInvalidFD((fd) => {
fs.ftruncate(fd, 4, common.mustCall(validateError));

fs.ftruncate(fd, 4, common.mustCall(validateError));

assert.throws(
() => fs.ftruncateSync(fd, 4),
validateError
);
assert.throws(
() => fs.ftruncateSync(fd, 4),
validateError
);
});
}

// fdatasync
Expand All @@ -493,15 +512,14 @@ function re(literals, ...values) {
return true;
};

const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);

fs.fdatasync(fd, common.mustCall(validateError));
common.runWithInvalidFD((fd) => {
fs.fdatasync(fd, common.mustCall(validateError));

assert.throws(
() => fs.fdatasyncSync(fd),
validateError
);
assert.throws(
() => fs.fdatasyncSync(fd),
validateError
);
});
}

// fsync
Expand All @@ -514,13 +532,12 @@ function re(literals, ...values) {
return true;
};

const fd = fs.openSync(existingFile, 'r');
fs.closeSync(fd);

fs.fsync(fd, common.mustCall(validateError));
common.runWithInvalidFD((fd) => {
fs.fsync(fd, common.mustCall(validateError));

assert.throws(
() => fs.fsyncSync(fd),
validateError
);
assert.throws(
() => fs.fsyncSync(fd),
validateError
);
});
}
19 changes: 6 additions & 13 deletions test/parallel/test-ttywrap-invalid-fd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Flags: --expose-internals

const common = require('../common');
const fs = require('fs');
const tty = require('tty');
const { SystemError } = require('internal/errors');

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

common.expectsError(
() => {
let fd = 2;
// Get first known bad file descriptor.
try {
while (fs.fstatSync(++fd));
} catch (e) { }
new tty.WriteStream(fd);
common.runWithInvalidFD((fd) => {
new tty.WriteStream(fd);
});
}, {
code: 'ERR_SYSTEM_ERROR',
type: SystemError,
Expand All @@ -37,12 +33,9 @@ common.expectsError(

common.expectsError(
() => {
let fd = 2;
// Get first known bad file descriptor.
try {
while (fs.fstatSync(++fd));
} catch (e) { }
new tty.ReadStream(fd);
common.runWithInvalidFD((fd) => {
new tty.ReadStream(fd);
});
}, {
code: 'ERR_SYSTEM_ERROR',
type: SystemError,
Expand Down

0 comments on commit fbd0b59

Please sign in to comment.