33const common = require ( '../common' ) ;
44const stream = require ( 'stream' ) ;
55const {
6- Readable,
7- Writable,
8- promises,
6+ Readable, Writable, promises,
97} = stream ;
108const {
11- finished,
12- pipeline,
9+ finished, pipeline,
1310} = require ( 'stream/promises' ) ;
1411const fs = require ( 'fs' ) ;
1512const assert = require ( 'assert' ) ;
@@ -24,14 +21,11 @@ assert.strictEqual(finished, promisify(stream.finished));
2421{
2522 let finished = false ;
2623 const processed = [ ] ;
27- const expected = [
28- Buffer . from ( 'a' ) ,
29- Buffer . from ( 'b' ) ,
30- Buffer . from ( 'c' ) ,
31- ] ;
24+ const expected = [ Buffer . from ( 'a' ) , Buffer . from ( 'b' ) , Buffer . from ( 'c' ) ] ;
3225
3326 const read = new Readable ( {
34- read ( ) { }
27+ read ( ) {
28+ }
3529 } ) ;
3630
3731 const write = new Writable ( {
@@ -59,7 +53,8 @@ assert.strictEqual(finished, promisify(stream.finished));
5953// pipeline error
6054{
6155 const read = new Readable ( {
62- read ( ) { }
56+ read ( ) {
57+ }
6358 } ) ;
6459
6560 const write = new Writable ( {
@@ -101,3 +96,50 @@ assert.strictEqual(finished, promisify(stream.finished));
10196 code : 'ENOENT'
10297 } ) . then ( common . mustCall ( ) ) ;
10398}
99+
100+ {
101+ const streamObj = new Readable ( ) ;
102+ assert . throws ( ( ) => {
103+ // Passing cleanup option not as boolean
104+ // should throw error
105+ finished ( streamObj , { cleanup : 2 } ) ;
106+ } , { code : 'ERR_INVALID_ARG_TYPE' } ) ;
107+ }
108+
109+ // Below code should not throw any errors as the
110+ // streamObj is `Stream` and cleanup is boolean
111+ {
112+ const streamObj = new Readable ( ) ;
113+ finished ( streamObj , { cleanup : true } ) ;
114+ }
115+
116+
117+ // Cleanup function should not be called when cleanup is set to false
118+ // listenerCount should be 1 after calling finish
119+ {
120+ const streamObj = new Writable ( ) ;
121+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 0 ) ;
122+ finished ( streamObj , { cleanup : false } ) . then ( ( ) => {
123+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 1 ) ;
124+ } ) ;
125+ }
126+
127+ // Cleanup function should be called when cleanup is set to true
128+ // listenerCount should be 0 after calling finish
129+ {
130+ const streamObj = new Writable ( ) ;
131+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 0 ) ;
132+ finished ( streamObj , { cleanup : true } ) . then ( ( ) => {
133+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 0 ) ;
134+ } ) ;
135+ }
136+
137+ // Cleanup function should not be called when cleanup has not been set
138+ // listenerCount should be 1 after calling finish
139+ {
140+ const streamObj = new Writable ( ) ;
141+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 0 ) ;
142+ finished ( streamObj ) . then ( ( ) => {
143+ assert . strictEqual ( streamObj . listenerCount ( 'end' ) , 1 ) ;
144+ } ) ;
145+ }
0 commit comments