Skip to content

Commit 4f2e372

Browse files
committed
test: add common.noop, default for common.mustCall()
Export a new common.noop no-operation function for general use. Allow using common.mustCall() without a fn argument to simplify test cases. Replace various non-op functions throughout tests with common.noop PR-URL: #12027 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Teddy Katz <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Franziska Hinkelmann <[email protected]>
1 parent d13bd4a commit 4f2e372

File tree

184 files changed

+492
-490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

184 files changed

+492
-490
lines changed

test/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ Gets IP of localhost
324324

325325
Array of IPV6 hosts.
326326

327-
### mustCall(fn[, expected])
327+
### mustCall([fn][, expected])
328328
* fn [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
329329
* expected [&lt;Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1
330330
* return [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)
@@ -333,13 +333,27 @@ Returns a function that calls `fn`. If the returned function has not been called
333333
exactly `expected` number of times when the test is complete, then the test will
334334
fail.
335335

336+
If `fn` is not provided, `common.noop` will be used.
337+
336338
### nodeProcessAborted(exitCode, signal)
337339
* `exitCode` [&lt;Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)
338340
* `signal` [&lt;String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)
339341
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
340342

341343
Returns `true` if the exit code `exitCode` and/or signal name `signal` represent the exit code and/or signal name of a node process that aborted, `false` otherwise.
342344

345+
### noop
346+
347+
A non-op `Function` that can be used for a variety of scenarios.
348+
349+
For instance,
350+
351+
```js
352+
const common = require('../common');
353+
354+
someAsyncAPI('foo', common.mustCall(common.noop));
355+
```
356+
343357
### opensslCli
344358
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
345359

test/addons/async-hello-world/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ const binding = require(`./build/${common.buildType}/binding`);
66
binding(5, common.mustCall(function(err, val) {
77
assert.strictEqual(err, null);
88
assert.strictEqual(val, 10);
9-
process.nextTick(common.mustCall(function() {}));
9+
process.nextTick(common.mustCall());
1010
}));

test/addons/heap-profiler/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const common = require('../../common');
55
const binding = require(`./build/${common.buildType}/binding`);
66

77
// Create an AsyncWrap object.
8-
const timer = setTimeout(function() {}, 1);
8+
const timer = setTimeout(common.noop, 1);
99
timer.unref();
1010

1111
// Stress-test the heap profiler.

test/common.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const execSync = require('child_process').execSync;
3535
const testRoot = process.env.NODE_TEST_DIR ?
3636
fs.realpathSync(process.env.NODE_TEST_DIR) : __dirname;
3737

38+
const noop = () => {};
39+
40+
exports.noop = noop;
3841
exports.fixturesDir = path.join(__dirname, 'fixtures');
3942
exports.tmpDirName = 'tmp';
4043
// PORT should match the definition in test/testpy/__init__.py.
@@ -429,6 +432,13 @@ function runCallChecks(exitCode) {
429432

430433

431434
exports.mustCall = function(fn, expected) {
435+
if (typeof fn === 'number') {
436+
expected = fn;
437+
fn = noop;
438+
} else if (fn === undefined) {
439+
fn = noop;
440+
}
441+
432442
if (expected === undefined)
433443
expected = 1;
434444
else if (typeof expected !== 'number')
@@ -525,9 +535,9 @@ util.inherits(ArrayStream, stream.Stream);
525535
exports.ArrayStream = ArrayStream;
526536
ArrayStream.prototype.readable = true;
527537
ArrayStream.prototype.writable = true;
528-
ArrayStream.prototype.pause = function() {};
529-
ArrayStream.prototype.resume = function() {};
530-
ArrayStream.prototype.write = function() {};
538+
ArrayStream.prototype.pause = noop;
539+
ArrayStream.prototype.resume = noop;
540+
ArrayStream.prototype.write = noop;
531541

532542
// Returns true if the exit code "exitCode" and/or signal name "signal"
533543
// represent the exit code and/or signal name of a node process that aborted,

test/debugger/test-debugger-client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ addTest(function(client, done) {
150150

151151
let connectCount = 0;
152152
const script = 'setTimeout(function() { console.log("blah"); });' +
153-
'setInterval(function() {}, 1000000);';
153+
'setInterval(common.noop, 1000000);';
154154

155155
let nodeProcess;
156156

@@ -193,7 +193,7 @@ function doTest(cb, done) {
193193
console.error('>>> connecting...');
194194
c.connect(debug.port);
195195
c.on('break', function() {
196-
c.reqContinue(function() {});
196+
c.reqContinue(common.noop);
197197
});
198198
c.on('ready', function() {
199199
connectCount++;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Flags: --trace-warnings
22
'use strict';
3-
require('../common');
3+
const common = require('../common');
44
const p = Promise.reject(new Error('This was rejected'));
5-
setImmediate(() => p.catch(() => {}));
5+
setImmediate(() => p.catch(common.noop));

test/parallel/test-assert.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const assert = require('assert');
2525
const a = require('assert');
2626

@@ -505,22 +505,22 @@ a.throws(makeBlock(a.deepEqual, args, []));
505505
// check messages from assert.throws()
506506
{
507507
assert.throws(
508-
() => { a.throws(() => {}); },
508+
() => { a.throws(common.noop); },
509509
/^AssertionError: Missing expected exception\.$/
510510
);
511511

512512
assert.throws(
513-
() => { a.throws(() => {}, TypeError); },
513+
() => { a.throws(common.noop, TypeError); },
514514
/^AssertionError: Missing expected exception \(TypeError\)\.$/
515515
);
516516

517517
assert.throws(
518-
() => { a.throws(() => {}, 'fhqwhgads'); },
518+
() => { a.throws(common.noop, 'fhqwhgads'); },
519519
/^AssertionError: Missing expected exception: fhqwhgads$/
520520
);
521521

522522
assert.throws(
523-
() => { a.throws(() => {}, TypeError, 'fhqwhgads'); },
523+
() => { a.throws(common.noop, TypeError, 'fhqwhgads'); },
524524
/^AssertionError: Missing expected exception \(TypeError\): fhqwhgads$/
525525
);
526526
}

test/parallel/test-async-wrap-check-providers.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -39,50 +39,48 @@ function init(id, provider) {
3939
keyList = keyList.filter((e) => e !== pkeys[provider]);
4040
}
4141

42-
function noop() { }
43-
4442
async_wrap.setupHooks({ init });
4543

4644
async_wrap.enable();
4745

4846

49-
setTimeout(function() { }, 1);
47+
setTimeout(common.noop, 1);
5048

51-
fs.stat(__filename, noop);
49+
fs.stat(__filename, common.noop);
5250

5351
if (!common.isAix) {
5452
// fs-watch currently needs special configuration on AIX and we
5553
// want to improve under https://github.com/nodejs/node/issues/5085.
5654
// strip out fs watch related parts for now
57-
fs.watchFile(__filename, noop);
55+
fs.watchFile(__filename, common.noop);
5856
fs.unwatchFile(__filename);
5957
fs.watch(__filename).close();
6058
}
6159

62-
dns.lookup('localhost', noop);
63-
dns.lookupService('::', 0, noop);
64-
dns.resolve('localhost', noop);
60+
dns.lookup('localhost', common.noop);
61+
dns.lookupService('::', 0, common.noop);
62+
dns.resolve('localhost', common.noop);
6563

6664
new StreamWrap(new net.Socket());
6765

6866
new (process.binding('tty_wrap').TTY)();
6967

70-
crypto.randomBytes(1, noop);
68+
crypto.randomBytes(1, common.noop);
7169

7270
common.refreshTmpDir();
7371

7472
net.createServer(function(c) {
7573
c.end();
7674
this.close();
7775
}).listen(common.PIPE, function() {
78-
net.connect(common.PIPE, noop);
76+
net.connect(common.PIPE, common.noop);
7977
});
8078

8179
net.createServer(function(c) {
8280
c.end();
8381
this.close(checkTLS);
8482
}).listen(0, function() {
85-
net.connect(this.address().port, noop);
83+
net.connect(this.address().port, common.noop);
8684
});
8785

8886
dgram.createSocket('udp4').bind(0, function() {
@@ -99,7 +97,7 @@ function checkTLS() {
9997
key: fs.readFileSync(common.fixturesDir + '/keys/ec-key.pem'),
10098
cert: fs.readFileSync(common.fixturesDir + '/keys/ec-cert.pem')
10199
};
102-
const server = tls.createServer(options, noop)
100+
const server = tls.createServer(options, common.noop)
103101
.listen(0, function() {
104102
const connectOpts = { rejectUnauthorized: false };
105103
tls.connect(this.address().port, connectOpts, function() {

test/parallel/test-async-wrap-disabled-propagate-parent.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const net = require('net');
66
const async_wrap = process.binding('async_wrap');
@@ -28,8 +28,6 @@ function init(uid, type, parentUid, parentHandle) {
2828
}
2929
}
3030

31-
function noop() { }
32-
3331
async_wrap.setupHooks({ init });
3432
async_wrap.enable();
3533

@@ -41,7 +39,7 @@ const server = net.createServer(function(c) {
4139
this.close();
4240
});
4341
}).listen(0, function() {
44-
net.connect(this.address().port, noop);
42+
net.connect(this.address().port, common.noop);
4543
});
4644

4745
async_wrap.disable();

test/parallel/test-async-wrap-propagate-parent.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const net = require('net');
66
const async_wrap = process.binding('async_wrap');
@@ -28,8 +28,6 @@ function init(uid, type, parentUid, parentHandle) {
2828
}
2929
}
3030

31-
function noop() { }
32-
3331
async_wrap.setupHooks({ init });
3432
async_wrap.enable();
3533

@@ -41,7 +39,7 @@ const server = net.createServer(function(c) {
4139
this.close();
4240
});
4341
}).listen(0, function() {
44-
net.connect(this.address().port, noop);
42+
net.connect(this.address().port, common.noop);
4543
});
4644

4745

test/parallel/test-async-wrap-throw-from-callback.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ if (typeof process.argv[2] === 'string') {
4343
d.on('error', common.mustNotCall());
4444
d.run(() => {
4545
// Using randomBytes because timers are not yet supported.
46-
crypto.randomBytes(0, () => { });
46+
crypto.randomBytes(0, common.noop);
4747
});
4848

4949
} else {

test/parallel/test-async-wrap-throw-no-init.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const async_wrap = process.binding('async_wrap');
66

@@ -17,9 +17,9 @@ assert.throws(function() {
1717
}, /init callback is not assigned to a function/);
1818

1919
// Should not throw
20-
async_wrap.setupHooks({ init: () => {} });
20+
async_wrap.setupHooks({ init: common.noop });
2121
async_wrap.enable();
2222

2323
assert.throws(function() {
24-
async_wrap.setupHooks(() => {});
24+
async_wrap.setupHooks(common.noop);
2525
}, /hooks should not be set while also enabled/);

test/parallel/test-async-wrap-uid.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const fs = require('fs');
55
const assert = require('assert');
66
const async_wrap = process.binding('async_wrap');
77

88
// Give the event loop time to clear out the final uv_close().
99
let si_cntr = 3;
1010
process.on('beforeExit', () => {
11-
if (--si_cntr > 0) setImmediate(() => {});
11+
if (--si_cntr > 0) setImmediate(common.noop);
1212
});
1313

1414
const storage = new Map();

test/parallel/test-buffer-includes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44

55
const Buffer = require('buffer').Buffer;
@@ -278,7 +278,7 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
278278
const expectedError =
279279
/^TypeError: "val" argument must be string, number, Buffer or Uint8Array$/;
280280
assert.throws(() => {
281-
b.includes(() => {});
281+
b.includes(common.noop);
282282
}, expectedError);
283283
assert.throws(() => {
284284
b.includes({});

test/parallel/test-child-process-bad-stdio.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const assert = require('assert');
55
const cp = require('child_process');
66

77
if (process.argv[2] === 'child') {
8-
setTimeout(() => {}, common.platformTimeout(100));
8+
setTimeout(common.noop, common.platformTimeout(100));
99
return;
1010
}
1111

test/parallel/test-child-process-disconnect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ if (process.argv[2] === 'child') {
7878
}));
7979

8080
// the process should also self terminate without using signals
81-
child.on('exit', common.mustCall(function() {}));
81+
child.on('exit', common.mustCall());
8282

8383
// when child is listening
8484
child.on('message', function(obj) {

test/parallel/test-child-process-fork-ref2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
'use strict';
23-
require('../common');
23+
const common = require('../common');
2424
const fork = require('child_process').fork;
2525

2626
if (process.argv[2] === 'child') {
@@ -29,7 +29,7 @@ if (process.argv[2] === 'child') {
2929

3030
setTimeout(function() {
3131
console.log('child -> will this keep it alive?');
32-
process.on('message', function() { });
32+
process.on('message', common.noop);
3333
}, 400);
3434

3535
} else {

test/parallel/test-child-process-kill.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ const assert = require('assert');
2525
const spawn = require('child_process').spawn;
2626
const cat = spawn(common.isWindows ? 'cmd' : 'cat');
2727

28-
cat.stdout.on('end', common.mustCall(function() {}));
28+
cat.stdout.on('end', common.mustCall());
2929
cat.stderr.on('data', common.mustNotCall());
30-
cat.stderr.on('end', common.mustCall(function() {}));
30+
cat.stderr.on('end', common.mustCall());
3131

3232
cat.on('exit', common.mustCall(function(code, signal) {
3333
assert.strictEqual(code, null);

0 commit comments

Comments
 (0)