Skip to content

Commit 5793586

Browse files
tniessenMylesBorins
authored andcommitted
test: use ES6 classes instead of util.inherits
Backport-PR-URL: #17068 PR-URL: #16938 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
1 parent 9d986d4 commit 5793586

24 files changed

+373
-428
lines changed

test/parallel/test-crypto-lazy-transform-writable.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ if (!common.hasCrypto)
77
const assert = require('assert');
88
const crypto = require('crypto');
99
const Stream = require('stream');
10-
const util = require('util');
1110

1211
const hasher1 = crypto.createHash('sha256');
1312
const hasher2 = crypto.createHash('sha256');
@@ -18,12 +17,12 @@ hasher1.end();
1817

1918
const expected = hasher1.read().toString('hex');
2019

21-
function OldStream() {
22-
Stream.call(this);
23-
24-
this.readable = true;
20+
class OldStream extends Stream {
21+
constructor() {
22+
super();
23+
this.readable = true;
24+
}
2525
}
26-
util.inherits(OldStream, Stream);
2726

2827
const stream = new OldStream();
2928

test/parallel/test-crypto-stream.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ if (!common.hasCrypto)
55

66
const assert = require('assert');
77
const stream = require('stream');
8-
const util = require('util');
98
const crypto = require('crypto');
109

11-
// Small stream to buffer converter
12-
function Stream2buffer(callback) {
13-
stream.Writable.call(this);
14-
15-
this._buffers = [];
16-
this.once('finish', function() {
17-
callback(null, Buffer.concat(this._buffers));
18-
});
19-
}
20-
util.inherits(Stream2buffer, stream.Writable);
21-
22-
Stream2buffer.prototype._write = function(data, encodeing, done) {
23-
this._buffers.push(data);
24-
return done(null);
25-
};
26-
2710
if (!common.hasFipsCrypto) {
11+
// Small stream to buffer converter
12+
class Stream2buffer extends stream.Writable {
13+
constructor(callback) {
14+
super();
15+
16+
this._buffers = [];
17+
this.once('finish', function() {
18+
callback(null, Buffer.concat(this._buffers));
19+
});
20+
}
21+
22+
_write(data, encodeing, done) {
23+
this._buffers.push(data);
24+
return done(null);
25+
}
26+
}
27+
2828
// Create an md5 hash of "Hallo world"
2929
const hasher1 = crypto.createHash('md5');
3030
hasher1.pipe(new Stream2buffer(common.mustCall(function end(err, hash) {

test/parallel/test-event-emitter-listeners.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
require('../common');
44
const assert = require('assert');
55
const events = require('events');
6-
const util = require('util');
76

87
function listener() {}
98
function listener2() {}
10-
class TestStream { constructor() { } }
11-
util.inherits(TestStream, events.EventEmitter);
129

1310
{
1411
const ee = new events.EventEmitter();
@@ -47,6 +44,7 @@ util.inherits(TestStream, events.EventEmitter);
4744
}
4845

4946
{
47+
class TestStream extends events.EventEmitter {}
5048
const s = new TestStream();
5149
assert.deepStrictEqual(s.listeners('foo'), []);
5250
}

test/parallel/test-http-client-read-in-error.js

+21-25
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,34 @@
22
require('../common');
33
const net = require('net');
44
const http = require('http');
5-
const util = require('util');
65

7-
function Agent() {
8-
http.Agent.call(this);
9-
}
10-
util.inherits(Agent, http.Agent);
11-
12-
Agent.prototype.createConnection = function() {
13-
const self = this;
14-
const socket = new net.Socket();
6+
class Agent extends http.Agent {
7+
createConnection() {
8+
const socket = new net.Socket();
159

16-
socket.on('error', function() {
17-
socket.push('HTTP/1.1 200\r\n\r\n');
18-
});
10+
socket.on('error', function() {
11+
socket.push('HTTP/1.1 200\r\n\r\n');
12+
});
1913

20-
socket.on('newListener', function onNewListener(name) {
21-
if (name !== 'error')
22-
return;
23-
socket.removeListener('newListener', onNewListener);
14+
let onNewListener;
15+
socket.on('newListener', onNewListener = (name) => {
16+
if (name !== 'error')
17+
return;
18+
socket.removeListener('newListener', onNewListener);
2419

25-
// Let other listeners to be set up too
26-
process.nextTick(function() {
27-
self.breakSocket(socket);
20+
// Let other listeners to be set up too
21+
process.nextTick(() => {
22+
this.breakSocket(socket);
23+
});
2824
});
29-
});
3025

31-
return socket;
32-
};
26+
return socket;
27+
}
3328

34-
Agent.prototype.breakSocket = function breakSocket(socket) {
35-
socket.emit('error', new Error('Intentional error'));
36-
};
29+
breakSocket(socket) {
30+
socket.emit('error', new Error('Intentional error'));
31+
}
32+
}
3733

3834
const agent = new Agent();
3935

test/parallel/test-http-client-readable.js

+27-31
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,37 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const http = require('http');
5-
const util = require('util');
65

76
const Duplex = require('stream').Duplex;
87

9-
function FakeAgent() {
10-
http.Agent.call(this);
8+
class FakeAgent extends http.Agent {
9+
createConnection() {
10+
const s = new Duplex();
11+
let once = false;
12+
13+
s._read = function() {
14+
if (once)
15+
return this.push(null);
16+
once = true;
17+
18+
this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
19+
this.push('b\r\nhello world\r\n');
20+
this.readable = false;
21+
this.push('0\r\n\r\n');
22+
};
23+
24+
// Blackhole
25+
s._write = function(data, enc, cb) {
26+
cb();
27+
};
28+
29+
s.destroy = s.destroySoon = function() {
30+
this.writable = false;
31+
};
32+
33+
return s;
34+
}
1135
}
12-
util.inherits(FakeAgent, http.Agent);
13-
14-
FakeAgent.prototype.createConnection = function() {
15-
const s = new Duplex();
16-
let once = false;
17-
18-
s._read = function() {
19-
if (once)
20-
return this.push(null);
21-
once = true;
22-
23-
this.push('HTTP/1.1 200 Ok\r\nTransfer-Encoding: chunked\r\n\r\n');
24-
this.push('b\r\nhello world\r\n');
25-
this.readable = false;
26-
this.push('0\r\n\r\n');
27-
};
28-
29-
// Blackhole
30-
s._write = function(data, enc, cb) {
31-
cb();
32-
};
33-
34-
s.destroy = s.destroySoon = function() {
35-
this.writable = false;
36-
};
37-
38-
return s;
39-
};
4036

4137
let received = '';
4238

test/parallel/test-readline-interface.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ const common = require('../common');
33
const assert = require('assert');
44
const readline = require('readline');
55
const EventEmitter = require('events').EventEmitter;
6-
const inherits = require('util').inherits;
76
const { Writable, Readable } = require('stream');
87

9-
function FakeInput() {
10-
EventEmitter.call(this);
8+
class FakeInput extends EventEmitter {
9+
resume() {}
10+
pause() {}
11+
write() {}
12+
end() {}
1113
}
12-
inherits(FakeInput, EventEmitter);
13-
FakeInput.prototype.resume = () => {};
14-
FakeInput.prototype.pause = () => {};
15-
FakeInput.prototype.write = () => {};
16-
FakeInput.prototype.end = () => {};
1714

1815
function isWarned(emitter) {
1916
for (const name in emitter) {

test/parallel/test-readline-keys.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
const common = require('../common');
33
const PassThrough = require('stream').PassThrough;
44
const assert = require('assert');
5-
const inherits = require('util').inherits;
65
const Interface = require('readline').Interface;
76

8-
9-
function FakeInput() {
10-
PassThrough.call(this);
11-
}
12-
inherits(FakeInput, PassThrough);
7+
class FakeInput extends PassThrough {}
138

149
function extend(k) {
1510
return Object.assign({ ctrl: false, meta: false, shift: false }, k);

test/parallel/test-stream-big-packet.js

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
'use strict';
22
require('../common');
33
const assert = require('assert');
4-
const util = require('util');
54
const stream = require('stream');
65

76
let passed = false;
87

9-
function PassThrough() {
10-
stream.Transform.call(this);
11-
}
12-
util.inherits(PassThrough, stream.Transform);
13-
PassThrough.prototype._transform = function(chunk, encoding, done) {
14-
this.push(chunk);
15-
done();
16-
};
17-
18-
function TestStream() {
19-
stream.Transform.call(this);
8+
class PassThrough extends stream.Transform {
9+
_transform(chunk, encoding, done) {
10+
this.push(chunk);
11+
done();
12+
}
2013
}
21-
util.inherits(TestStream, stream.Transform);
22-
TestStream.prototype._transform = function(chunk, encoding, done) {
23-
if (!passed) {
24-
// Char 'a' only exists in the last write
25-
passed = chunk.toString().includes('a');
14+
15+
class TestStream extends stream.Transform {
16+
_transform(chunk, encoding, done) {
17+
if (!passed) {
18+
// Char 'a' only exists in the last write
19+
passed = chunk.toString().includes('a');
20+
}
21+
done();
2622
}
27-
done();
28-
};
23+
}
2924

3025
const s1 = new PassThrough();
3126
const s2 = new PassThrough();

test/parallel/test-stream-events-prepend.js

+13-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
'use strict';
22
const common = require('../common');
33
const stream = require('stream');
4-
const util = require('util');
54

6-
function Writable() {
7-
this.writable = true;
8-
stream.Writable.call(this);
9-
this.prependListener = undefined;
5+
class Writable extends stream.Writable {
6+
constructor() {
7+
super();
8+
this.prependListener = undefined;
9+
}
10+
11+
_write(chunk, end, cb) {
12+
cb();
13+
}
1014
}
11-
util.inherits(Writable, stream.Writable);
12-
Writable.prototype._write = function(chunk, end, cb) {
13-
cb();
14-
};
1515

16-
function Readable() {
17-
this.readable = true;
18-
stream.Readable.call(this);
16+
class Readable extends stream.Readable {
17+
_read() {
18+
this.push(null);
19+
}
1920
}
20-
util.inherits(Readable, stream.Readable);
21-
Readable.prototype._read = function() {
22-
this.push(null);
23-
};
2421

2522
const w = new Writable();
2623
w.on('pipe', common.mustCall());

0 commit comments

Comments
 (0)