2020// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
2222'use strict' ;
23- const common = require ( '../common' ) ;
23+ const {
24+ isWindows,
25+ mustCall,
26+ mustCallAtLeast,
27+ } = require ( '../common' ) ;
2428const assert = require ( 'assert' ) ;
2529const os = require ( 'os' ) ;
2630const spawn = require ( 'child_process' ) . spawn ;
31+ const debug = require ( 'util' ) . debuglog ( 'test' ) ;
2732
2833// We're trying to reproduce:
2934// $ echo "hello\nnode\nand\nworld" | grep o | sed s/o/a/
3035
3136let grep , sed , echo ;
3237
33- if ( common . isWindows ) {
38+ if ( isWindows ) {
3439 grep = spawn ( 'grep' , [ '--binary' , 'o' ] ) ;
3540 sed = spawn ( 'sed' , [ '--binary' , 's/o/O/' ] ) ;
3641 echo = spawn ( 'cmd.exe' ,
@@ -54,62 +59,66 @@ if (common.isWindows) {
5459
5560
5661// pipe echo | grep
57- echo . stdout . on ( 'data' , function ( data ) {
58- console . error ( `grep stdin write ${ data . length } ` ) ;
62+ echo . stdout . on ( 'data' , mustCallAtLeast ( ( data ) => {
63+ debug ( `grep stdin write ${ data . length } ` ) ;
5964 if ( ! grep . stdin . write ( data ) ) {
6065 echo . stdout . pause ( ) ;
6166 }
62- } ) ;
67+ } ) ) ;
6368
64- grep . stdin . on ( 'drain' , function ( data ) {
69+ // TODO(@jasnell): This does not appear to ever be
70+ // emitted. It's not clear if it is necessary.
71+ grep . stdin . on ( 'drain' , ( data ) => {
6572 echo . stdout . resume ( ) ;
6673} ) ;
6774
6875// Propagate end from echo to grep
69- echo . stdout . on ( 'end' , function ( code ) {
76+ echo . stdout . on ( 'end' , mustCall ( ( code ) => {
7077 grep . stdin . end ( ) ;
71- } ) ;
78+ } ) ) ;
7279
73- echo . on ( 'exit' , function ( ) {
74- console . error ( 'echo exit' ) ;
75- } ) ;
80+ echo . on ( 'exit' , mustCall ( ( ) => {
81+ debug ( 'echo exit' ) ;
82+ } ) ) ;
7683
77- grep . on ( 'exit' , function ( ) {
78- console . error ( 'grep exit' ) ;
79- } ) ;
84+ grep . on ( 'exit' , mustCall ( ( ) => {
85+ debug ( 'grep exit' ) ;
86+ } ) ) ;
8087
81- sed . on ( 'exit' , function ( ) {
82- console . error ( 'sed exit' ) ;
83- } ) ;
88+ sed . on ( 'exit' , mustCall ( ( ) => {
89+ debug ( 'sed exit' ) ;
90+ } ) ) ;
8491
8592
8693// pipe grep | sed
87- grep . stdout . on ( 'data' , function ( data ) {
88- console . error ( `grep stdout ${ data . length } ` ) ;
94+ grep . stdout . on ( 'data' , mustCallAtLeast ( ( data ) => {
95+ debug ( `grep stdout ${ data . length } ` ) ;
8996 if ( ! sed . stdin . write ( data ) ) {
9097 grep . stdout . pause ( ) ;
9198 }
92- } ) ;
99+ } ) ) ;
93100
94- sed . stdin . on ( 'drain' , function ( data ) {
101+ // TODO(@jasnell): This does not appear to ever be
102+ // emitted. It's not clear if it is necessary.
103+ sed . stdin . on ( 'drain' , ( data ) => {
95104 grep . stdout . resume ( ) ;
96105} ) ;
97106
98107// Propagate end from grep to sed
99- grep . stdout . on ( 'end' , function ( code ) {
100- console . error ( 'grep stdout end' ) ;
108+ grep . stdout . on ( 'end' , mustCall ( ( code ) => {
109+ debug ( 'grep stdout end' ) ;
101110 sed . stdin . end ( ) ;
102- } ) ;
111+ } ) ) ;
103112
104113
105114let result = '' ;
106115
107116// print sed's output
108- sed . stdout . on ( 'data' , function ( data ) {
117+ sed . stdout . on ( 'data' , mustCallAtLeast ( ( data ) => {
109118 result += data . toString ( 'utf8' , 0 , data . length ) ;
110- console . log ( data ) ;
111- } ) ;
119+ debug ( data ) ;
120+ } ) ) ;
112121
113- sed . stdout . on ( 'end' , function ( code ) {
122+ sed . stdout . on ( 'end' , mustCall ( ( code ) => {
114123 assert . strictEqual ( result , `hellO${ os . EOL } nOde${ os . EOL } wOrld${ os . EOL } ` ) ;
115- } ) ;
124+ } ) ) ;
0 commit comments