Skip to content

Commit b7d8273

Browse files
committed
Fixed tests
1 parent 553ee3d commit b7d8273

File tree

5 files changed

+421
-258
lines changed

5 files changed

+421
-258
lines changed

lib/ast2js/_spawnStream.js

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var EventEmitter = require('events')
2-
var spawn = require('child_process').spawn
3-
var stream = require('stream')
1+
const EventEmitter = require('events')
2+
const spawn = require('child_process').spawn
3+
const stream = require('stream')
44

5-
var Duplex = stream.Duplex
6-
var Writable = stream.Writable
5+
const Duplex = stream.Duplex
6+
const Readable = stream.Readable
7+
const Writable = stream.Writable
78

89

910
function noop(){}
@@ -25,19 +26,19 @@ function wrapStdio(command, argv, options)
2526
var stderr = stdio[2]
2627

2728
// Wrap stdio
28-
if(typeof stdin === 'string'
29+
if(typeof stdin === 'string' || typeof stdin === 'number'
2930
|| stdin && stdin.constructor.name === 'ReadStream')
3031
stdin = null
3132
else
3233
stdio[0] = 'pipe'
3334

34-
if(typeof stdout === 'string'
35+
if(typeof stdout === 'string' || typeof stdout === 'number'
3536
|| stdout && stdout.constructor.name === 'WriteStream')
3637
stdout = null
3738
else
3839
stdio[1] = 'pipe'
3940

40-
if(typeof stderr === 'string'
41+
if(typeof stderr === 'string' || typeof stderr === 'number'
4142
|| stderr && stderr.constructor.name === 'WriteStream')
4243
stderr = null
4344
else
@@ -47,17 +48,17 @@ function wrapStdio(command, argv, options)
4748
var cp = spawn(command, argv, options)
4849

4950
// Adjust events, pipe streams and restore stdio
50-
if(stdin)
51+
if(stdin != null)
5152
{
5253
stdin.pipe(cp.stdin)
5354
cp.stdin = null
5455
}
55-
if(stdout)
56+
if(stdout != null)
5657
{
5758
cp.stdout.pipe(stdout)
5859
cp.stdout = null
5960
}
60-
if(stderr)
61+
if(stderr != null)
6162
{
6263
cp.stderr.pipe(stderr)
6364
cp.stderr = null
@@ -83,47 +84,44 @@ function spawnStream(command, argv, options)
8384
var stdout = stdio[1]
8485
var stderr = stdio[2]
8586

86-
if(stdin == null) stdin = 'pipe'
87-
if(stdout == null) stdout = 'pipe'
88-
if(stderr == null) stderr = 'pipe'
89-
9087
var cp = wrapStdio(command, argv, options)
9188

92-
stdin = (stdin === 'pipe') ? cp.stdin : null
93-
stdout = (stdout === 'pipe') ? cp.stdout : null
94-
stderr = (stderr === 'pipe') ? cp.stderr : null
89+
stdin = (stdin == null || stdin === 'pipe') ? cp.stdin : null
90+
stdout = (stdout == null || stdout === 'pipe') ? cp.stdout : null
91+
stderr = (stderr == null || stderr === 'pipe') ? cp.stderr : null
9592

9693
var result
9794

98-
// Both `stdin` and `stdout` are open, probably the normal case.
99-
// Create a `Duplex` object with them so command can be used as a filter.
100-
if(stdin && stdout)
101-
{
102-
result = Duplex()
103-
result._read = noop
104-
result._write = stdin.write.bind(stdin)
95+
// Both `stdin` and `stdout` are open, probably the normal case. Create a
96+
// `Duplex` object with them so command can be used as a filter
97+
if(stdin && stdout) result = Duplex()
10598

106-
result.on('finish', stdin.end.bind(stdin))
99+
// Only `stdout` is open, use it directly
100+
else if(stdout) result = Readable()
107101

108-
stdout
109-
.on('data', result.push.bind(result))
110-
.on('end' , result.emit.bind(result, 'end'))
111-
}
102+
// Only `stdin` is open, ensure is always 'only' `Writable`
103+
else if(stdin) result = Writable()
104+
105+
// Both `stdin` and `stdout` are clossed
106+
else result = new EventEmitter()
112107

113-
// Only `stdin` is open, ensure is always 'only' writable.
114-
else if(stdin)
108+
// Connect stdio streams
109+
if(stdin)
115110
{
116-
result = Writable()
117111
result._write = stdin.write.bind(stdin)
118-
119-
result.on('finish', stdin.end.bind(stdin))
112+
result.once('finish', stdin.end.bind(stdin))
120113
}
121114

122-
// Only `stdout` is open, use it directly.
123-
else if(stdout) result = stdout
115+
if(stdout)
116+
{
117+
result._read = noop
118+
stdout.on ('data', result.push.bind(result))
119+
stdout.once('end' , result.push.bind(result, null))
120+
}
124121

125-
// Both `stdin` and `stdout` are clossed.
126-
else result = new EventEmitter()
122+
// Use child process `exit` event instead of missing `stdout` `end` event
123+
else
124+
cp.once('exit', result.emit.bind(result, 'end'))
127125

128126
if(stderr)
129127
{
@@ -138,12 +136,10 @@ function spawnStream(command, argv, options)
138136
if(out_stderr) out_stderr.pipe(stderr)
139137
}
140138

139+
// Propagate process events
141140
cp.on('error', result.emit.bind(result, 'error'))
142141
cp.on('exit' , result.emit.bind(result, 'exit' ))
143142

144-
// No `stdout`, emit `end` event when child process exit
145-
if(!stdout) cp.once('exit', result.emit.bind(result, 'end'))
146-
147143
return result
148144
}
149145

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"concat-stream": "^1.5.2",
3535
"easy-coveralls": "0.0.1",
3636
"mocha": "^3.1.2",
37-
"string-to-stream": "^1.1.0"
37+
"string-to-stream": "^1.1.0",
38+
"tmp": "0.0.31"
3839
}
3940
}

test.js

Lines changed: 0 additions & 215 deletions
This file was deleted.

test/fixture.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
asdf

0 commit comments

Comments
 (0)