Skip to content

Commit

Permalink
test: remove common.noop
Browse files Browse the repository at this point in the history
This change removes `common.noop` from the Node.js internal testing
common module.

Over the last few weeks, I've grown to dislike the `common.noop`
abstraction.

First, new (and experienced) contributors are unaware of it and so it
results in a large number of low-value nits on PRs. It also increases
the number of things newcomers and infrequent contributors have to be
aware of to be effective on the project.

Second, it is confusing. Is it a singleton/property or a getter? Which
should be expected? This can lead to subtle and hard-to-find bugs. (To
my knowledge, none have landed on master. But I also think it's only a
matter of time.)

Third, the abstraction is low-value in my opinion. What does it really
get us? A case could me made that it is without value at all.

Lastly, and this is minor, but the abstraction is wordier than not using
the abstraction. `common.noop` doesn't save anything over `() => {}`.

So, I propose removing it.

PR-URL: #12822
Backport-PR-URL: #14174
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Refael Ackermann <refack@gmail.com>
  • Loading branch information
Trott authored and addaleax committed Jul 18, 2017
1 parent 76ba1b5 commit e879a56
Show file tree
Hide file tree
Showing 67 changed files with 135 additions and 149 deletions.
21 changes: 4 additions & 17 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,26 @@ Gets IP of localhost
Array of IPV6 hosts.

### mustCall([fn][, exact])
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = `common.noop`
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = () => {}
* `exact` [&lt;Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1
* return [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)

Returns a function that calls `fn`. If the returned function has not been called
exactly `expected` number of times when the test is complete, then the test will
fail.

If `fn` is not provided, `common.noop` will be used.
If `fn` is not provided, an empty function will be used.

### mustCallAtLeast([fn][, minimum])
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = `common.noop`
* `fn` [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) default = () => {}
* `minimum` [&lt;Number>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type) default = 1
* return [&lt;Function>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function)

Returns a function that calls `fn`. If the returned function has not been called
at least `minimum` number of times when the test is complete, then the test will
fail.

If `fn` is not provided, `common.noop` will be used.
If `fn` is not provided, an empty function will be used.

### mustNotCall([msg])
* `msg` [&lt;String>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type) default = 'function should not have been called'
Expand All @@ -243,19 +243,6 @@ Returns a function that triggers an `AssertionError` if it is invoked. `msg` is

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.

### noop

A non-op `Function` that can be used for a variety of scenarios.

For instance,

<!-- eslint-disable strict, no-undef -->
```js
const common = require('../common');

someAsyncAPI('foo', common.mustCall(common.noop));
```

### opensslCli
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)

Expand Down
1 change: 0 additions & 1 deletion test/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const testRoot = process.env.NODE_TEST_DIR ?

const noop = () => {};

exports.noop = noop;
exports.fixturesDir = path.join(__dirname, '..', 'fixtures');
exports.tmpDirName = 'tmp';
// PORT should match the definition in test/testpy/__init__.py.
Expand Down
4 changes: 2 additions & 2 deletions test/message/unhandled_promise_trace_warnings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Flags: --trace-warnings
'use strict';
const common = require('../common');
require('../common');
const p = Promise.reject(new Error('This was rejected'));
setImmediate(() => p.catch(common.noop));
setImmediate(() => p.catch(() => {}));
9 changes: 5 additions & 4 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,29 +571,30 @@ a.throws(makeBlock(a.deepEqual, args, []));

// check messages from assert.throws()
{
const noop = () => {};
assert.throws(
() => { a.throws(common.noop); },
() => { a.throws((noop)); },
common.expectsError({
code: 'ERR_ASSERTION',
message: /^Missing expected exception\.$/
}));

assert.throws(
() => { a.throws(common.noop, TypeError); },
() => { a.throws(noop, TypeError); },
common.expectsError({
code: 'ERR_ASSERTION',
message: /^Missing expected exception \(TypeError\)\.$/
}));

assert.throws(
() => { a.throws(common.noop, 'fhqwhgads'); },
() => { a.throws(noop, 'fhqwhgads'); },
common.expectsError({
code: 'ERR_ASSERTION',
message: /^Missing expected exception: fhqwhgads$/
}));

assert.throws(
() => { a.throws(common.noop, TypeError, 'fhqwhgads'); },
() => { a.throws(noop, TypeError, 'fhqwhgads'); },
common.expectsError({
code: 'ERR_ASSERTION',
message: /^Missing expected exception \(TypeError\): fhqwhgads$/
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-buffer-includes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');

const b = Buffer.from('abcdef');
Expand Down Expand Up @@ -274,7 +274,7 @@ for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
const expectedError =
/^TypeError: "val" argument must be string, number, Buffer or Uint8Array$/;
assert.throws(() => {
b.includes(common.noop);
b.includes(() => {});
}, expectedError);
assert.throws(() => {
b.includes({});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-bad-stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const assert = require('assert');
const cp = require('child_process');

if (process.argv[2] === 'child') {
setTimeout(common.noop, common.platformTimeout(100));
setTimeout(() => {}, common.platformTimeout(100));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-child-process-spawnsync-kill-signal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const assert = require('assert');
const cp = require('child_process');

if (process.argv[2] === 'child') {
setInterval(common.noop, 1000);
setInterval(() => {}, 1000);
} else {
const { SIGKILL } = process.binding('constants').os.signals;

Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-cluster-rr-domain-listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
require('../common');
const cluster = require('cluster');
const domain = require('domain');

Expand All @@ -29,10 +29,10 @@ const domain = require('domain');

if (cluster.isWorker) {
const d = domain.create();
d.run(common.noop);
d.run(() => {});

const http = require('http');
http.Server(common.noop).listen(0, '127.0.0.1');
http.Server(() => {}).listen(0, '127.0.0.1');

} else if (cluster.isMaster) {

Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-cluster-worker-wait-server-close.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (cluster.isWorker) {
const server = net.createServer(function(socket) {
// Wait for any data, then close connection
socket.write('.');
socket.on('data', common.noop);
socket.on('data', () => {});
}).listen(0, common.localhostIPv4);

server.once('close', function() {
Expand All @@ -20,7 +20,7 @@ if (cluster.isWorker) {

// Although not typical, the worker process can exit before the disconnect
// event fires. Use this to keep the process open until the event has fired.
const keepOpen = setInterval(common.noop, 9999);
const keepOpen = setInterval(() => {}, 9999);

// Check worker events and properties
process.once('disconnect', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const HIJACK_TEST_ARRAY = [ 'foo\n', 'bar\n', 'baz\n' ];
assert.notStrictEqual(originalWrite, stream.write);

HIJACK_TEST_ARRAY.forEach((val) => {
stream.write(val, common.mustCall(common.noop));
stream.write(val, common.mustCall());
});

assert.strictEqual(HIJACK_TEST_ARRAY.length, stream.writeTimes);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-console-instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ assert.throws(() => {

// Console constructor should throw if stderr exists but is not writable
assert.throws(() => {
out.write = common.noop;
out.write = () => {};
err.write = undefined;
new Console(out, err);
}, /^TypeError: Console expects writable stream instances$/);
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-event-emitter-get-max-listeners.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');
const EventEmitter = require('events');

Expand All @@ -15,5 +15,5 @@ assert.strictEqual(emitter.getMaxListeners(), 3);

// https://github.com/nodejs/node/issues/523 - second call should not throw.
const recv = {};
EventEmitter.prototype.on.call(recv, 'event', common.noop);
EventEmitter.prototype.on.call(recv, 'event', common.noop);
EventEmitter.prototype.on.call(recv, 'event', () => {});
EventEmitter.prototype.on.call(recv, 'event', () => {});
10 changes: 5 additions & 5 deletions test/parallel/test-event-emitter-listener-count.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');
const EventEmitter = require('events');

const emitter = new EventEmitter();
emitter.on('foo', common.noop);
emitter.on('foo', common.noop);
emitter.on('baz', common.noop);
emitter.on('foo', () => {});
emitter.on('foo', () => {});
emitter.on('baz', () => {});
// Allow any type
emitter.on(123, common.noop);
emitter.on(123, () => {});

assert.strictEqual(EventEmitter.listenerCount(emitter, 'foo'), 2);
assert.strictEqual(emitter.listenerCount('foo'), 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ process.on('warning', common.mustCall((warning) => {
assert.ok(warning.message.includes('2 null listeners added.'));
}));

e.on(null, common.noop);
e.on(null, common.noop);
e.on(null, () => {});
e.on(null, () => {});
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ process.on('warning', common.mustCall((warning) => {
assert.ok(warning.message.includes('2 Symbol(symbol) listeners added.'));
}));

e.on(symbol, common.noop);
e.on(symbol, common.noop);
e.on(symbol, () => {});
e.on(symbol, () => {});
6 changes: 3 additions & 3 deletions test/parallel/test-event-emitter-max-listeners-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ process.on('warning', common.mustCall((warning) => {
assert.ok(warning.message.includes('2 event-type listeners added.'));
}));

e.on('event-type', common.noop);
e.on('event-type', common.noop); // Trigger warning.
e.on('event-type', common.noop); // Verify that warning is emitted only once.
e.on('event-type', () => {});
e.on('event-type', () => {}); // Trigger warning.
e.on('event-type', () => {}); // Verify that warning is emitted only once.
4 changes: 2 additions & 2 deletions test/parallel/test-event-emitter-remove-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ function listener2() {}
{
const ee = new EventEmitter();

assert.deepStrictEqual(ee, ee.removeListener('foo', common.noop));
assert.deepStrictEqual(ee, ee.removeListener('foo', () => {}));
}

// Verify that the removed listener must be a function
Expand All @@ -152,7 +152,7 @@ assert.throws(() => {

{
const ee = new EventEmitter();
const listener = common.noop;
const listener = () => {};
ee._events = undefined;
const e = ee.removeListener('foo', listener);
assert.strictEqual(e, ee);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-event-emitter-special-event-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const EventEmitter = require('events');
const assert = require('assert');

const ee = new EventEmitter();
const handler = common.noop;
const handler = () => {};

assert.deepStrictEqual(ee.eventNames(), []);

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-event-emitter-subclass.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ MyEE2.prototype = new EventEmitter();
const ee1 = new MyEE2();
const ee2 = new MyEE2();

ee1.on('x', common.noop);
ee1.on('x', () => {});

assert.strictEqual(ee2.listenerCount('x'), 0);
1 change: 0 additions & 1 deletion test/parallel/test-events-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const EventEmitter = require('events');
const assert = require('assert');

const EE = new EventEmitter();
// Do not use common.noop here, these need to be separate listener functions
const m = () => {};
EE.on('foo', () => {});
assert.deepStrictEqual(['foo'], EE.eventNames());
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-buffertype-writesync.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const fs = require('fs');
const path = require('path');

const filePath = path.join(common.tmpDir, 'test_buffer_type');
const v = [true, false, 0, 1, Infinity, common.noop, {}, [], undefined, null];
const v = [true, false, 0, 1, Infinity, () => {}, {}, [], undefined, null];

common.refreshTmpDir();

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,4 @@ common.refreshTmpDir();

// Keep the event loop alive so the async mkdir() requests
// have a chance to run (since they don't ref the event loop).
process.nextTick(common.noop);
process.nextTick(() => {});
4 changes: 2 additions & 2 deletions test/parallel/test-handle-wrap-isrefed.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const strictEqual = require('assert').strictEqual;
// tcp
{
const net = require('net');
const server = net.createServer(common.noop).listen(0);
const server = net.createServer(() => {}).listen(0);
strictEqual(Object.getPrototypeOf(server._handle).hasOwnProperty('hasRef'),
true, 'tcp_wrap: hasRef() missing');
strictEqual(server._handle.hasRef(),
Expand All @@ -112,7 +112,7 @@ const strictEqual = require('assert').strictEqual;

// timers
{
const timer = setTimeout(common.noop, 500);
const timer = setTimeout(() => {}, 500);
timer.unref();
strictEqual(Object.getPrototypeOf(timer._handle).hasOwnProperty('hasRef'),
true, 'timer_wrap: hasRef() missing');
Expand Down
8 changes: 4 additions & 4 deletions test/parallel/test-http-client-defaults.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');
const ClientRequest = require('http').ClientRequest;

{
const req = new ClientRequest({ createConnection: common.noop });
const req = new ClientRequest({ createConnection: () => {} });
assert.strictEqual(req.path, '/');
assert.strictEqual(req.method, 'GET');
}

{
const req = new ClientRequest({ method: '', createConnection: common.noop });
const req = new ClientRequest({ method: '', createConnection: () => {} });
assert.strictEqual(req.path, '/');
assert.strictEqual(req.method, 'GET');
}

{
const req = new ClientRequest({ path: '', createConnection: common.noop });
const req = new ClientRequest({ path: '', createConnection: () => {} });
assert.strictEqual(req.path, '/');
assert.strictEqual(req.method, 'GET');
}
4 changes: 2 additions & 2 deletions test/parallel/test-http-parser-bad-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Flags: --expose_gc

const common = require('../common');
require('../common');
const assert = require('assert');
const HTTPParser = process.binding('http_parser').HTTPParser;

Expand Down Expand Up @@ -39,7 +39,7 @@ function demoBug(part1, part2) {
console.log('url', info.url);
};

parser[kOnBody] = common.noop;
parser[kOnBody] = () => {};

parser[kOnMessageComplete] = function() {
messagesComplete++;
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-http-upgrade-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

'use strict';

const common = require('../common');
require('../common');
const assert = require('assert');

const util = require('util');
Expand All @@ -38,7 +38,7 @@ function createTestServer() {
}

function testServer() {
http.Server.call(this, common.noop);
http.Server.call(this, () => {});

this.on('connection', function() {
requests_recv++;
Expand Down
Loading

0 comments on commit e879a56

Please sign in to comment.