Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v8.x] test: bump test/common to master #14459

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ function innerThrows(shouldThrow, block, expected, message) {
if (typeof block !== 'function') {
const errors = lazyErrors();
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'block', 'function',
typeof block);
block);
}

if (typeof expected === 'string') {
Expand Down
192 changes: 112 additions & 80 deletions test/common/README.md

Large diffs are not rendered by default.

80 changes: 50 additions & 30 deletions test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ const path = require('path');
const fs = require('fs');
const assert = require('assert');
const os = require('os');
const child_process = require('child_process');
const { exec, execSync, spawn, spawnSync } = require('child_process');
const stream = require('stream');
const buffer = require('buffer');
const util = require('util');
const Timer = process.binding('timer_wrap').Timer;
const execSync = require('child_process').execSync;

const testRoot = process.env.NODE_TEST_DIR ?
fs.realpathSync(process.env.NODE_TEST_DIR) : path.resolve(__dirname, '..');
Expand All @@ -54,8 +52,6 @@ exports.isLinux = process.platform === 'linux';
exports.isOSX = process.platform === 'darwin';

exports.enoughTestMem = os.totalmem() > 0x40000000; /* 1 Gb */
exports.bufferMaxSizeMsg = new RegExp(
`^RangeError: "size" argument must not be larger than ${buffer.kMaxLength}$`);
const cpus = os.cpus();
exports.enoughTestCpu = Array.isArray(cpus) &&
(cpus.length > 1 || cpus[0].speed > 999);
Expand Down Expand Up @@ -189,7 +185,7 @@ Object.defineProperty(exports, 'inFreeBSDJail', {
if (inFreeBSDJail !== null) return inFreeBSDJail;

if (exports.isFreeBSD &&
child_process.execSync('sysctl -n security.jail.jailed').toString() ===
execSync('sysctl -n security.jail.jailed').toString() ===
'1\n') {
inFreeBSDJail = true;
} else {
Expand Down Expand Up @@ -223,7 +219,7 @@ Object.defineProperty(exports, 'localhostIPv4', {
});

// opensslCli defined lazily to reduce overhead of spawnSync
Object.defineProperty(exports, 'opensslCli', {get: function() {
Object.defineProperty(exports, 'opensslCli', { get: function() {
if (opensslCli !== null) return opensslCli;

if (process.config.variables.node_shared_openssl) {
Expand All @@ -236,13 +232,13 @@ Object.defineProperty(exports, 'opensslCli', {get: function() {

if (exports.isWindows) opensslCli += '.exe';

const opensslCmd = child_process.spawnSync(opensslCli, ['version']);
const opensslCmd = spawnSync(opensslCli, ['version']);
if (opensslCmd.status !== 0 || opensslCmd.error !== undefined) {
// openssl command cannot be executed
opensslCli = false;
}
return opensslCli;
}, enumerable: true});
}, enumerable: true });

Object.defineProperty(exports, 'hasCrypto', {
get: function() {
Expand Down Expand Up @@ -286,7 +282,7 @@ exports.childShouldThrowAndAbort = function() {
}
testCmd += `"${process.argv[0]}" --abort-on-uncaught-exception `;
testCmd += `"${process.argv[1]}" child`;
const child = child_process.exec(testCmd);
const child = exec(testCmd);
child.on('exit', function onExit(exitCode, signal) {
const errMsg = 'Test should have aborted ' +
`but instead exited with exit code ${exitCode}` +
Expand All @@ -306,8 +302,6 @@ exports.ddCommand = function(filename, kilobytes) {


exports.spawnPwd = function(options) {
const spawn = require('child_process').spawn;

if (exports.isWindows) {
return spawn('cmd.exe', ['/d', '/c', 'cd'], options);
} else {
Expand All @@ -317,8 +311,6 @@ exports.spawnPwd = function(options) {


exports.spawnSyncPwd = function(options) {
const spawnSync = require('child_process').spawnSync;

if (exports.isWindows) {
return spawnSync('cmd.exe', ['/d', '/c', 'cd'], options);
} else {
Expand Down Expand Up @@ -487,17 +479,15 @@ exports.mustCallAtLeast = function(fn, minimum) {
return _mustCallInner(fn, minimum, 'minimum');
};

function _mustCallInner(fn, criteria, field) {
function _mustCallInner(fn, criteria = 1, field) {
if (typeof fn === 'number') {
criteria = fn;
fn = noop;
} else if (fn === undefined) {
fn = noop;
}

if (criteria === undefined)
criteria = 1;
else if (typeof criteria !== 'number')
if (typeof criteria !== 'number')
throw new TypeError(`Invalid ${field} value: ${criteria}`);

const context = {
Expand Down Expand Up @@ -701,21 +691,51 @@ Object.defineProperty(exports, 'hasSmallICU', {
});

// Useful for testing expected internal/error objects
exports.expectsError = function expectsError({code, type, message}) {
return function(error) {
assert.strictEqual(error.code, code);
if (type !== undefined) {
exports.expectsError = function expectsError(fn, settings, exact) {
if (typeof fn !== 'function') {
exact = settings;
settings = fn;
fn = undefined;
}
const innerFn = exports.mustCall(function(error) {
assert.strictEqual(error.code, settings.code);
if ('type' in settings) {
const type = settings.type;
if (type !== Error && !Error.isPrototypeOf(type)) {
throw new TypeError('`settings.type` must inherit from `Error`');
}
assert(error instanceof type,
`${error} is not the expected type ${type}`);
`${error.name} is not instance of ${type.name}`);
}
if ('message' in settings) {
const message = settings.message;
if (typeof message === 'string') {
assert.strictEqual(error.message, message);
} else {
assert(message.test(error.message),
`${error.message} does not match ${message}`);
}
}
if ('name' in settings) {
assert.strictEqual(error.name, settings.name);
}
if (message instanceof RegExp) {
assert(message.test(error.message),
`${error.message} does not match ${message}`);
} else if (typeof message === 'string') {
assert.strictEqual(error.message, message);
if (error.constructor.name === 'AssertionError') {
['generatedMessage', 'actual', 'expected', 'operator'].forEach((key) => {
if (key in settings) {
const actual = error[key];
const expected = settings[key];
assert.strictEqual(actual, expected,
`${key}: expected ${expected}, not ${actual}`);
}
});
}
return true;
};
}, exact);
if (fn) {
assert.throws(fn, innerFn);
return;
}
return innerFn;
};

exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() {
Expand Down Expand Up @@ -764,7 +784,7 @@ exports.getTTYfd = function getTTYfd() {
else if (!tty.isatty(tty_fd)) tty_fd++;
else {
try {
tty_fd = require('fs').openSync('/dev/tty');
tty_fd = fs.openSync('/dev/tty');
} catch (e) {
// There aren't any tty fd's available to use.
return -1;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-assert-fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ assert.throws(
code: 'ERR_ASSERTION',
type: assert.AssertionError,
operator: undefined,
actual: undefined,
actual: 'custom message',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this semver-major?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not accompanied by any changes to lib or src, so it really can’t be. I have to admit I didn’t really figure out why this is required, if you like, feel free to do the digging.

Copy link
Contributor

@refack refack Jul 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember now. Before this change expectsError only checked ['message', 'type','code'] now it compares name if provided. And for AssertionError also ['generatedMessage', 'actual', 'expected', 'operator']

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so it sounds like everything is working as expected and better than before? :)

expected: undefined,
message: 'custom message'
})
Expand Down
20 changes: 10 additions & 10 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,7 @@ assert.throws(makeBlock(a.deepEqual, /a/igm, /a/im),
{
const re1 = /a/g;
re1.lastIndex = 3;
assert.doesNotThrow(makeBlock(a.deepEqual, re1, /a/g),
common.expectsError({
code: 'ERR_ASSERTION',
message: /^\/a\/g deepEqual \/a\/g$/
}));
assert.doesNotThrow(makeBlock(a.deepEqual, re1, /a/g));
}

assert.doesNotThrow(makeBlock(a.deepEqual, 4, '4'), 'deepEqual(4, \'4\')');
Expand Down Expand Up @@ -670,10 +666,9 @@ try {

{
// Verify that throws() and doesNotThrow() throw on non-function block
const validationFunction = common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError
});
function typeName(value) {
return value === null ? 'null' : typeof value;
}

const testBlockTypeError = (method, block) => {
let threw = true;
Expand All @@ -682,7 +677,12 @@ try {
method(block);
threw = false;
} catch (e) {
validationFunction(e);
common.expectsError({
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "block" argument must be of type function. Received ' +
'type ' + typeName(block)
})(e);
}

assert.ok(threw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ if (!common.isWindows) {
// Validate the killSignal option
const typeErr = /^TypeError: "killSignal" must be a string or number$/;
const unknownSignalErr =
common.expectsError({ code: 'ERR_UNKNOWN_SIGNAL' });
common.expectsError({ code: 'ERR_UNKNOWN_SIGNAL' }, 17);

pass('killSignal', undefined);
pass('killSignal', null);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-child-process-validate-stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const assert = require('assert');
const _validateStdio = require('internal/child_process')._validateStdio;

const expectedError =
common.expectsError({code: 'ERR_INVALID_OPT_VALUE', type: TypeError});
common.expectsError({ code: 'ERR_INVALID_OPT_VALUE', type: TypeError }, 2);

// should throw if string and not ignore, pipe, or inherit
assert.throws(() => _validateStdio('foo'), expectedError);
Expand All @@ -27,7 +27,7 @@ assert.throws(() => _validateStdio(600), expectedError);
// should throw if stdio has ipc and sync is true
const stdio2 = ['ipc', 'ipc', 'ipc'];
assert.throws(() => _validateStdio(stdio2, true),
common.expectsError({code: 'ERR_IPC_SYNC_FORK', type: Error}));
common.expectsError({ code: 'ERR_IPC_SYNC_FORK', type: Error }));

{
const stdio3 = [process.stdin, process.stdout, process.stderr];
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-internal-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ assert.throws(() => {
}, common.expectsError({ code: 'TEST_ERROR_1', type: RangeError }));
}, common.expectsError({
code: 'ERR_ASSERTION',
message: /^.+ is not the expected type \S/
message: /^.+ is not instance of \S/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not semver-major since it's a change in expectsError

}));

assert.throws(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-process-emitwarning.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ warningThrowToString.toString = function() {
process.emitWarning(warningThrowToString);

const expectedError =
common.expectsError({code: 'ERR_INVALID_ARG_TYPE', type: TypeError});
common.expectsError({ code: 'ERR_INVALID_ARG_TYPE', type: TypeError }, 11);

// TypeError is thrown on invalid input
assert.throws(() => process.emitWarning(1), expectedError);
Expand Down
32 changes: 16 additions & 16 deletions test/parallel/test-url-format-whatwg.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ assert.strictEqual(
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "options" argument must be of type object'
});
}, 4);
assert.throws(() => url.format(myURL, true), expectedErr);
assert.throws(() => url.format(myURL, 1), expectedErr);
assert.throws(() => url.format(myURL, 'test'), expectedErr);
Expand All @@ -36,76 +36,76 @@ assert.strictEqual(
// Any truthy value will be treated as true.

assert.strictEqual(
url.format(myURL, {fragment: false}),
url.format(myURL, { fragment: false }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b'
);

assert.strictEqual(
url.format(myURL, {fragment: ''}),
url.format(myURL, { fragment: '' }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b'
);

assert.strictEqual(
url.format(myURL, {fragment: 0}),
url.format(myURL, { fragment: 0 }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b'
);

assert.strictEqual(
url.format(myURL, {fragment: 1}),
url.format(myURL, { fragment: 1 }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {fragment: {}}),
url.format(myURL, { fragment: {} }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {search: false}),
url.format(myURL, { search: false }),
'http://xn--lck1c3crb1723bpq4a.com/a#c'
);

assert.strictEqual(
url.format(myURL, {search: ''}),
url.format(myURL, { search: '' }),
'http://xn--lck1c3crb1723bpq4a.com/a#c'
);

assert.strictEqual(
url.format(myURL, {search: 0}),
url.format(myURL, { search: 0 }),
'http://xn--lck1c3crb1723bpq4a.com/a#c'
);

assert.strictEqual(
url.format(myURL, {search: 1}),
url.format(myURL, { search: 1 }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {search: {}}),
url.format(myURL, { search: {} }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {unicode: true}),
url.format(myURL, { unicode: true }),
'http://理容ナカムラ.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {unicode: 1}),
url.format(myURL, { unicode: 1 }),
'http://理容ナカムラ.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {unicode: {}}),
url.format(myURL, { unicode: {} }),
'http://理容ナカムラ.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {unicode: false}),
url.format(myURL, { unicode: false }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);

assert.strictEqual(
url.format(myURL, {unicode: 0}),
url.format(myURL, { unicode: 0 }),
'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'
);
2 changes: 1 addition & 1 deletion test/parallel/test-whatwg-url-domainto.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const wptToASCIITests = require('../fixtures/url-toascii.js');

{
const expectedError = common.expectsError(
{ code: 'ERR_MISSING_ARGS', type: TypeError });
{ code: 'ERR_MISSING_ARGS', type: TypeError }, 2);
assert.throws(() => domainToASCII(), expectedError);
assert.throws(() => domainToUnicode(), expectedError);
assert.strictEqual(domainToASCII(undefined), 'undefined');
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-whatwg-url-parsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const failureTests = tests.filter((test) => test.failure).concat([
]);

const expectedError = common.expectsError(
{ code: 'ERR_INVALID_URL', type: TypeError });
{ code: 'ERR_INVALID_URL', type: TypeError }, 102);

for (const test of failureTests) {
assert.throws(
Expand Down
Loading