2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2016-12-30 23:38:06 +00:00
|
|
|
const common = require('../common');
|
2017-10-06 17:22:24 +00:00
|
|
|
const fixtures = require('../common/fixtures');
|
2016-12-30 23:38:06 +00:00
|
|
|
const assert = require('assert');
|
2017-07-11 00:55:21 +00:00
|
|
|
const { execFile } = require('child_process');
|
2017-02-16 23:52:47 +00:00
|
|
|
|
|
|
|
// test for leaked global detection
|
2014-01-30 12:02:58 +00:00
|
|
|
global.gc = 42; // Not a valid global unless --expose_gc is set.
|
2016-04-19 22:37:45 +00:00
|
|
|
assert.deepStrictEqual(common.leakedGlobals(), ['gc']);
|
2017-02-16 23:52:47 +00:00
|
|
|
delete global.gc;
|
|
|
|
|
2016-10-11 19:43:36 +00:00
|
|
|
|
2017-02-16 23:52:47 +00:00
|
|
|
// common.mustCall() tests
|
2016-10-11 19:43:36 +00:00
|
|
|
assert.throws(function() {
|
|
|
|
common.mustCall(function() {}, 'foo');
|
2017-05-09 21:16:52 +00:00
|
|
|
}, /^TypeError: Invalid exact value: foo$/);
|
2016-10-11 19:43:36 +00:00
|
|
|
|
|
|
|
assert.throws(function() {
|
|
|
|
common.mustCall(function() {}, /foo/);
|
2017-05-09 21:16:52 +00:00
|
|
|
}, /^TypeError: Invalid exact value: \/foo\/$/);
|
2017-02-16 23:52:47 +00:00
|
|
|
|
2017-05-09 21:16:52 +00:00
|
|
|
assert.throws(function() {
|
|
|
|
common.mustCallAtLeast(function() {}, /foo/);
|
|
|
|
}, /^TypeError: Invalid minimum value: \/foo\/$/);
|
2017-02-16 23:52:47 +00:00
|
|
|
|
2017-04-09 17:53:30 +00:00
|
|
|
// assert.fail() tests
|
2017-02-16 23:52:47 +00:00
|
|
|
assert.throws(
|
2017-04-09 17:53:30 +00:00
|
|
|
() => { assert.fail('fhqwhgads'); },
|
2017-04-25 20:18:25 +00:00
|
|
|
common.expectsError({
|
|
|
|
code: 'ERR_ASSERTION',
|
|
|
|
message: /^fhqwhgads$/
|
|
|
|
}));
|
2017-05-09 21:16:52 +00:00
|
|
|
|
|
|
|
const fnOnce = common.mustCall(() => {});
|
|
|
|
fnOnce();
|
|
|
|
const fnTwice = common.mustCall(() => {}, 2);
|
|
|
|
fnTwice();
|
|
|
|
fnTwice();
|
|
|
|
const fnAtLeast1Called1 = common.mustCallAtLeast(() => {}, 1);
|
|
|
|
fnAtLeast1Called1();
|
|
|
|
const fnAtLeast1Called2 = common.mustCallAtLeast(() => {}, 1);
|
|
|
|
fnAtLeast1Called2();
|
|
|
|
fnAtLeast1Called2();
|
|
|
|
const fnAtLeast2Called2 = common.mustCallAtLeast(() => {}, 2);
|
|
|
|
fnAtLeast2Called2();
|
|
|
|
fnAtLeast2Called2();
|
|
|
|
const fnAtLeast2Called3 = common.mustCallAtLeast(() => {}, 2);
|
|
|
|
fnAtLeast2Called3();
|
|
|
|
fnAtLeast2Called3();
|
|
|
|
fnAtLeast2Called3();
|
|
|
|
|
|
|
|
const failFixtures = [
|
|
|
|
[
|
2017-10-06 17:22:24 +00:00
|
|
|
fixtures.path('failmustcall1.js'),
|
2017-05-09 21:16:52 +00:00
|
|
|
'Mismatched <anonymous> function calls. Expected exactly 2, actual 1.'
|
|
|
|
], [
|
2017-10-06 17:22:24 +00:00
|
|
|
fixtures.path('failmustcall2.js'),
|
2017-05-09 21:16:52 +00:00
|
|
|
'Mismatched <anonymous> function calls. Expected at least 2, actual 1.'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
for (const p of failFixtures) {
|
|
|
|
const [file, expected] = p;
|
|
|
|
execFile(process.argv[0], [file], common.mustCall((ex, stdout, stderr) => {
|
|
|
|
assert.ok(ex);
|
|
|
|
assert.strictEqual(stderr, '');
|
|
|
|
const firstLine = stdout.split('\n').shift();
|
|
|
|
assert.strictEqual(firstLine, expected);
|
|
|
|
}));
|
|
|
|
}
|
2017-06-03 17:24:05 +00:00
|
|
|
|
|
|
|
// hijackStderr and hijackStdout
|
|
|
|
const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ];
|
|
|
|
[ 'err', 'out' ].forEach((txt) => {
|
|
|
|
const stream = process[`std${txt}`];
|
|
|
|
const originalWrite = stream.write;
|
|
|
|
|
|
|
|
common[`hijackStd${txt}`](common.mustCall(function(data) {
|
|
|
|
assert.strictEqual(data, HIJACK_TEST_ARRAY[stream.writeTimes]);
|
|
|
|
}, HIJACK_TEST_ARRAY.length));
|
|
|
|
assert.notStrictEqual(originalWrite, stream.write);
|
|
|
|
|
|
|
|
HIJACK_TEST_ARRAY.forEach((val) => {
|
2017-05-04 04:07:54 +00:00
|
|
|
stream.write(val, common.mustCall());
|
2017-06-03 17:24:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.strictEqual(HIJACK_TEST_ARRAY.length, stream.writeTimes);
|
|
|
|
common[`restoreStd${txt}`]();
|
|
|
|
assert.strictEqual(originalWrite, stream.write);
|
|
|
|
});
|
2017-08-06 06:16:05 +00:00
|
|
|
|
|
|
|
// hijackStderr and hijackStdout again
|
|
|
|
// for console
|
|
|
|
[[ 'err', 'error' ], [ 'out', 'log' ]].forEach(([ type, method ]) => {
|
|
|
|
common[`hijackStd${type}`](common.mustCall(function(data) {
|
|
|
|
assert.strictEqual(data, 'test\n');
|
|
|
|
|
|
|
|
// throw an error
|
|
|
|
throw new Error(`console ${type} error`);
|
|
|
|
}));
|
|
|
|
|
|
|
|
console[method]('test');
|
|
|
|
common[`restoreStd${type}`]();
|
|
|
|
});
|
|
|
|
|
|
|
|
let uncaughtTimes = 0;
|
|
|
|
process.on('uncaughtException', common.mustCallAtLeast(function(e) {
|
|
|
|
assert.strictEqual(uncaughtTimes < 2, true);
|
|
|
|
assert.strictEqual(e instanceof Error, true);
|
|
|
|
assert.strictEqual(
|
|
|
|
e.message,
|
|
|
|
`console ${([ 'err', 'out' ])[uncaughtTimes++]} error`);
|
|
|
|
}, 2));
|