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 MylesBorins committed Aug 16, 2017
1 parent 5c3fd35 commit 3bd0c7c
Show file tree
Hide file tree
Showing 62 changed files with 153 additions and 159 deletions.
21 changes: 4 additions & 17 deletions test/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,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 @@ -217,19 +217,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 @@ -14,7 +14,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/debugger/test-debugger-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ addTest(function(client, done) {

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

let nodeProcess;

Expand Down Expand Up @@ -172,7 +172,7 @@ function doTest(cb, done) {
console.error('>>> connecting...');
c.connect(debug.port);
c.on('break', function() {
c.reqContinue(common.noop);
c.reqContinue(() => {});
});
c.on('ready', function() {
connectCount++;
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-assert.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 a = require('assert');

Expand Down Expand Up @@ -515,7 +515,7 @@ testAssertionMessage(/a/, '/a/');
testAssertionMessage(/abc/gim, '/abc/gim');
testAssertionMessage(function f() {}, '[Function: f]');
testAssertionMessage(function() {}, '[Function]');
testAssertionMessage(common.noop, '[Function: noop]');
testAssertionMessage(() => {}, '[Function]');
testAssertionMessage({}, '{}');
testAssertionMessage(circular, '{ y: 1, x: [Circular] }');
testAssertionMessage({a: undefined, b: null}, '{ a: undefined, b: null }');
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-cluster-rr-domain-listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const domain = require('domain');

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

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

} else if (cluster.isMaster) {
let worker;
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', function discard() {});
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
4 changes: 2 additions & 2 deletions test/parallel/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ global.gc = 42; // Not a valid global unless --expose_gc is set.
assert.deepStrictEqual(common.leakedGlobals(), ['gc']);

assert.throws(function() {
common.mustCall(common.noop, 'foo');
common.mustCall(() => {}, 'foo');
}, /^TypeError: Invalid exact value: foo$/);

assert.throws(function() {
common.mustCall(common.noop, /foo/);
common.mustCall(() => {}, /foo/);
}, /^TypeError: Invalid exact value: \/foo\/$/);

const fnOnce = common.mustCall(() => {});
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) {
[-1, undefined, null, false, true, {}, []].forEach(function(value) {
assert.throws(function() { f(value); }, expectedErrorRegexp);
assert.throws(function() { f(value, common.noop); }, expectedErrorRegexp);
assert.throws(function() { f(value, () => {}); }, expectedErrorRegexp);
});

[0, 1, 2, 4, 16, 256, 1024].forEach(function(len) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-env-var-no-warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if (process.argv[2] === 'child') {
test({ NODE_NO_WARNINGS: false });
test({ NODE_NO_WARNINGS: {} });
test({ NODE_NO_WARNINGS: [] });
test({ NODE_NO_WARNINGS: common.noop });
test({ NODE_NO_WARNINGS: () => {} });
test({ NODE_NO_WARNINGS: 0 });
test({ NODE_NO_WARNINGS: -1 });
test({ NODE_NO_WARNINGS: '0' });
Expand Down
32 changes: 16 additions & 16 deletions test/parallel/test-event-emitter-check-listener-leaks.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');
const events = require('events');

let e = new events.EventEmitter();

// default
for (let i = 0; i < 10; i++) {
e.on('default', common.noop);
e.on('default', () => {});
}
assert.ok(!e._events['default'].hasOwnProperty('warned'));
e.on('default', common.noop);
e.on('default', () => {});
assert.ok(e._events['default'].warned);

// symbol
const symbol = Symbol('symbol');
e.setMaxListeners(1);
e.on(symbol, common.noop);
e.on(symbol, () => {});
assert.ok(!e._events[symbol].hasOwnProperty('warned'));
e.on(symbol, common.noop);
e.on(symbol, () => {});
assert.ok(e._events[symbol].hasOwnProperty('warned'));

// specific
e.setMaxListeners(5);
for (let i = 0; i < 5; i++) {
e.on('specific', common.noop);
e.on('specific', () => {});
}
assert.ok(!e._events['specific'].hasOwnProperty('warned'));
e.on('specific', common.noop);
e.on('specific', () => {});
assert.ok(e._events['specific'].warned);

// only one
e.setMaxListeners(1);
e.on('only one', common.noop);
e.on('only one', () => {});
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
e.on('only one', common.noop);
e.on('only one', () => {});
assert.ok(e._events['only one'].hasOwnProperty('warned'));

// unlimited
e.setMaxListeners(0);
for (let i = 0; i < 1000; i++) {
e.on('unlimited', common.noop);
e.on('unlimited', () => {});
}
assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));

Expand All @@ -49,26 +49,26 @@ events.EventEmitter.defaultMaxListeners = 42;
e = new events.EventEmitter();

for (let i = 0; i < 42; ++i) {
e.on('fortytwo', common.noop);
e.on('fortytwo', () => {});
}
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
e.on('fortytwo', common.noop);
e.on('fortytwo', () => {});
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));
delete e._events['fortytwo'].warned;

events.EventEmitter.defaultMaxListeners = 44;
e.on('fortytwo', common.noop);
e.on('fortytwo', () => {});
assert.ok(!e._events['fortytwo'].hasOwnProperty('warned'));
e.on('fortytwo', common.noop);
e.on('fortytwo', () => {});
assert.ok(e._events['fortytwo'].hasOwnProperty('warned'));

// but _maxListeners still has precedence over defaultMaxListeners
events.EventEmitter.defaultMaxListeners = 42;
e = new events.EventEmitter();
e.setMaxListeners(1);
e.on('uno', common.noop);
e.on('uno', () => {});
assert.ok(!e._events['uno'].hasOwnProperty('warned'));
e.on('uno', common.noop);
e.on('uno', () => {});
assert.ok(e._events['uno'].hasOwnProperty('warned'));

// chainable
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.
6 changes: 3 additions & 3 deletions test/parallel/test-event-emitter-remove-all-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ function listener() {}
ee.on('removeListener', function(name, listener) {
assert.strictEqual(expectLength--, this.listeners('baz').length);
});
ee.on('baz', common.noop);
ee.on('baz', common.noop);
ee.on('baz', common.noop);
ee.on('baz', () => {});
ee.on('baz', () => {});
ee.on('baz', () => {});
assert.strictEqual(ee.listeners('baz').length, expectLength + 1);
ee.removeAllListeners('baz');
assert.strictEqual(ee.listeners('baz').length, 0);
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 @@ -41,6 +41,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);
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 @@ -56,4 +56,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(() => {});
6 changes: 3 additions & 3 deletions test/parallel/test-fs-read-stream-inherit.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ let paused = false;
let file7 =
fs.createReadStream(rangeFile, Object.create({autoClose: false }));
assert.strictEqual(file7.autoClose, false);
file7.on('data', common.noop);
file7.on('data', () => {});
file7.on('end', common.mustCall(function() {
process.nextTick(common.mustCall(function() {
assert(!file7.closed);
Expand Down Expand Up @@ -169,7 +169,7 @@ let paused = false;
{
const options = Object.create({fd: 13337, autoClose: false});
const file8 = fs.createReadStream(null, options);
file8.on('data', common.noop);
file8.on('data', () => {});
file8.on('error', common.mustCall());
process.on('exit', function() {
assert(!file8.closed);
Expand All @@ -181,7 +181,7 @@ let paused = false;
// Make sure stream is destroyed when file does not exist.
{
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
file9.on('data', common.noop);
file9.on('data', () => {});
file9.on('error', common.mustCall());

process.on('exit', function() {
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-fs-read-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pauseRes.pause();
pauseRes.resume();

let file7 = fs.createReadStream(rangeFile, {autoClose: false });
file7.on('data', common.noop);
file7.on('data', () => {});
file7.on('end', function() {
process.nextTick(function() {
assert(!file7.closed);
Expand All @@ -161,12 +161,12 @@ function file7Next() {

// Just to make sure autoClose won't close the stream because of error.
const file8 = fs.createReadStream(null, {fd: 13337, autoClose: false });
file8.on('data', common.noop);
file8.on('data', () => {});
file8.on('error', common.mustCall());

// Make sure stream is destroyed when file does not exist.
const file9 = fs.createReadStream('/path/to/file/that/does/not/exist');
file9.on('data', common.noop);
file9.on('data', () => {});
file9.on('error', common.mustCall());

process.on('exit', function() {
Expand Down
Loading

0 comments on commit 3bd0c7c

Please sign in to comment.