Skip to content

Commit 87301de

Browse files
committed
- Update eslint and related devDependencies to latest version
- Tests no longer run tap from the command line - Cluster test should no longer intermittenly hang - Reduce the opportunities for test.end to be called multiple times - Split tests into multiple files - Add a noColor option to ensure consistent behavior - Move minimum node version from 8 to 10 - Update tap to latest version - Drop node 8 from travis and add node 14
1 parent ce3321e commit 87301de

17 files changed

Lines changed: 302 additions & 274 deletions

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"vars-on-top": 0,
1515
"no-console": 0,
1616
"no-param-reassign": 0,
17-
"no-use-before-define": 0
17+
"no-use-before-define": 0,
18+
"strict": 0
1819
}
1920
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
script: npm run lint
55
language: node_js
66
node_js:
7+
- 14
78
- 12
89
- 10
9-
- 8
1010
sudo: false

lib/log.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ var colors = {
77
warn: '33'
88
};
99

10+
function noop(s) { return s; }
11+
12+
function colorFactory(enableColor) {
13+
return enableColor ? function (s, c) {
14+
return '\x1B[' + c + 'm' + s + '\x1B[0m';
15+
} : noop;
16+
}
17+
1018
/**
1119
* Logs a message to the console. The level is displayed in ANSI colors,
1220
* either bright red in case of an error or green otherwise.
1321
*/
1422
module.exports = function (cfg) {
15-
function color(s, c) {
16-
if (process.stdout.isTTY) {
17-
return '\x1B[' + c + 'm' + s + '\x1B[0m';
18-
}
19-
return s;
20-
}
23+
var enableColor = !(cfg.noColor || !process.stdout.isTTY);
24+
var color = colorFactory(enableColor);
2125

2226
function log(msg, level) {
23-
if (cfg.timestamp) msg = color(fmt(new Date(), cfg.timestamp), '39') + ' ' + msg;
27+
var timestamp = cfg.timestamp ? color(fmt(new Date(), cfg.timestamp), '39') + ' ' : '';
2428
var c = colors[level.toLowerCase()] || '32';
25-
var output = '[' + color(level.toUpperCase(), c) + '] ' + msg;
29+
var output = '[' + color(level.toUpperCase(), c) + '] ' + timestamp + msg;
2630
console.log(output);
2731
return output;
2832
}

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"main": "./lib",
2525
"preferGlobal": true,
2626
"engines": {
27-
"node": ">=8"
27+
"node": ">=10"
2828
},
2929
"scripts": {
3030
"lint": "eslint lib test",
31-
"test": "tap test/*.js"
31+
"test": "node test"
3232
},
3333
"dependencies": {
3434
"dateformat": "^3.0.3",
@@ -40,10 +40,11 @@
4040
},
4141
"devDependencies": {
4242
"coffeescript": "^2.4.1",
43-
"eslint": "^5.16.0",
44-
"eslint-config-airbnb-base": "^13.1.0",
45-
"eslint-plugin-import": "^2.17.2",
46-
"tap": "^13.1.2",
43+
"eslint": "^7.3.1",
44+
"eslint-config-airbnb-base": "^14.2.0",
45+
"eslint-plugin-import": "^2.22.0",
46+
"nyc": "^15.1.0",
47+
"tap": "^14.10.7",
4748
"touch": "^3.1.0"
4849
}
4950
}

test/fixture/catchNoSuchModule.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
try {
2-
/* eslint-disable import/no-unresolved */
32
require('some_module_that_does_not_exist');
43
} catch (err) {
54
console.log('Caught', err);

test/fixture/cluster.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
var cluster = require('cluster');
22

3-
function createWorker(i) {
4-
var worker = cluster.fork();
5-
worker.on('message', function (msg) {
6-
console.log('Message from worker', i, ':', msg);
7-
});
8-
worker.on('disconnect', function () {
9-
console.log('Worker disconnected', i);
10-
});
11-
return worker;
12-
}
13-
14-
var workers = [];
15-
163
if (cluster.isMaster) {
4+
function createWorker(i) {
5+
var worker = cluster.fork();
6+
worker.on('message', function (msg) {
7+
console.log('Message from worker' + i + ':', msg);
8+
});
9+
worker.on('exit', function (code) {
10+
console.log(`Worker #${i} exited with code: ${code}`);
11+
});
12+
return worker;
13+
}
14+
1715
for (var i = 0; i < 2; i += 1) {
1816
console.log('Forking worker', i);
19-
workers.push(createWorker(i));
17+
createWorker(i);
2018
}
21-
process.on('SIGTERM', function () {
19+
process.once('SIGTERM', function () {
2220
console.log('Master received SIGTERM');
23-
workers.forEach(function (worker) {
24-
worker.disconnect();
21+
cluster.disconnect(function () {
22+
console.log('All workers disconnected.');
2523
});
2624
});
2725
} else {
26+
var server = require('./server');
27+
process.on('disconnect', function () {
28+
console.log(process.pid, 'disconnect received, shutting down');
29+
if (server.lisening) {
30+
server.close();
31+
}
32+
});
2833
process.send('Hello');
29-
require('./server');
3034
}

test/fixture/ipc-server.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
var http = require('http');
32
var message = require('./message');
43

test/fixture/noSuchModule.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
/* eslint-disable import/no-unresolved */
21
require('some_module_that_does_not_exist');

test/fixture/server.coffee

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ server = http.createServer (req, res) ->
88
res.write message
99
res.end '\n'
1010

11-
server.listen 8080
11+
server.once 'listening', ->
12+
addr = this.address()
13+
console.log 'Server listening on %s:%s', addr.address, addr.port
14+
console.log message
15+
.listen 0
1216

13-
console.log 'Server running at http://localhost:8080/'
14-
console.log message
15-
16-
process.on 'SIGTERM', -> server.close()
17+
process.once 'SIGTERM', -> server.close()
18+
process.once 'exit', -> console.log 'exit'

test/fixture/server.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
var http = require('http');
32
var message = require('./message');
43

@@ -11,19 +10,20 @@ var server = http.createServer(function (req, res) {
1110
res.end('\n');
1211
});
1312

14-
server.on('listening', function () {
13+
server.once('listening', function () {
1514
var addr = this.address();
1615
console.log('Server listening on %s:%s', addr.address, addr.port);
1716
console.log(message);
1817
}).listen(0);
1918

20-
process.on('SIGTERM', function () {
21-
server.close();
22-
setTimeout(function () { console.log('3'); }, 200);
23-
setTimeout(function () { console.log('2'); }, 400);
24-
setTimeout(function () { console.log('1'); }, 600);
19+
process.once('SIGTERM', function () {
20+
if (server.listening) {
21+
server.close();
22+
}
2523
});
2624

27-
process.on('exit', function () {
25+
process.once('exit', function() {
2826
console.log('exit');
2927
});
28+
29+
module.exports = server;

0 commit comments

Comments
 (0)