20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
22
'use strict' ;
23
- const common = require ( '../common' ) ;
23
+ const {
24
+ isWindows,
25
+ mustCall,
26
+ mustCallAtLeast,
27
+ } = require ( '../common' ) ;
24
28
const assert = require ( 'assert' ) ;
25
29
const os = require ( 'os' ) ;
26
30
const spawn = require ( 'child_process' ) . spawn ;
31
+ const debug = require ( 'util' ) . debuglog ( 'test' ) ;
27
32
28
33
// We're trying to reproduce:
29
34
// $ echo "hello\nnode\nand\nworld" | grep o | sed s/o/a/
30
35
31
36
let grep , sed , echo ;
32
37
33
- if ( common . isWindows ) {
38
+ if ( isWindows ) {
34
39
grep = spawn ( 'grep' , [ '--binary' , 'o' ] ) ;
35
40
sed = spawn ( 'sed' , [ '--binary' , 's/o/O/' ] ) ;
36
41
echo = spawn ( 'cmd.exe' ,
@@ -54,62 +59,66 @@ if (common.isWindows) {
54
59
55
60
56
61
// 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 } ` ) ;
59
64
if ( ! grep . stdin . write ( data ) ) {
60
65
echo . stdout . pause ( ) ;
61
66
}
62
- } ) ;
67
+ } ) ) ;
63
68
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 ) => {
65
72
echo . stdout . resume ( ) ;
66
73
} ) ;
67
74
68
75
// Propagate end from echo to grep
69
- echo . stdout . on ( 'end' , function ( code ) {
76
+ echo . stdout . on ( 'end' , mustCall ( ( code ) => {
70
77
grep . stdin . end ( ) ;
71
- } ) ;
78
+ } ) ) ;
72
79
73
- echo . on ( 'exit' , function ( ) {
74
- console . error ( 'echo exit' ) ;
75
- } ) ;
80
+ echo . on ( 'exit' , mustCall ( ( ) => {
81
+ debug ( 'echo exit' ) ;
82
+ } ) ) ;
76
83
77
- grep . on ( 'exit' , function ( ) {
78
- console . error ( 'grep exit' ) ;
79
- } ) ;
84
+ grep . on ( 'exit' , mustCall ( ( ) => {
85
+ debug ( 'grep exit' ) ;
86
+ } ) ) ;
80
87
81
- sed . on ( 'exit' , function ( ) {
82
- console . error ( 'sed exit' ) ;
83
- } ) ;
88
+ sed . on ( 'exit' , mustCall ( ( ) => {
89
+ debug ( 'sed exit' ) ;
90
+ } ) ) ;
84
91
85
92
86
93
// 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 } ` ) ;
89
96
if ( ! sed . stdin . write ( data ) ) {
90
97
grep . stdout . pause ( ) ;
91
98
}
92
- } ) ;
99
+ } ) ) ;
93
100
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 ) => {
95
104
grep . stdout . resume ( ) ;
96
105
} ) ;
97
106
98
107
// 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' ) ;
101
110
sed . stdin . end ( ) ;
102
- } ) ;
111
+ } ) ) ;
103
112
104
113
105
114
let result = '' ;
106
115
107
116
// print sed's output
108
- sed . stdout . on ( 'data' , function ( data ) {
117
+ sed . stdout . on ( 'data' , mustCallAtLeast ( ( data ) => {
109
118
result += data . toString ( 'utf8' , 0 , data . length ) ;
110
- console . log ( data ) ;
111
- } ) ;
119
+ debug ( data ) ;
120
+ } ) ) ;
112
121
113
- sed . stdout . on ( 'end' , function ( code ) {
122
+ sed . stdout . on ( 'end' , mustCall ( ( code ) => {
114
123
assert . strictEqual ( result , `hellO${ os . EOL } nOde${ os . EOL } wOrld${ os . EOL } ` ) ;
115
- } ) ;
124
+ } ) ) ;
0 commit comments