1
1
// vim: expandtab:ts=2:sw=2
2
2
3
- const
3
+ var
4
4
fs = require ( 'fs' ) ,
5
5
path = require ( 'path' ) ,
6
- existsSync = fs . existsSync || path . existsSync ,
6
+ exists = fs . exists || path . exists ,
7
7
spawn = require ( 'child_process' ) . spawn ;
8
8
9
+ const ISTANBUL_PATH = path . join ( __dirname , '..' , 'node_modules' , 'istanbul' , 'lib' , 'cli.js' ) ;
9
10
10
- module . exports . genericChildProcess = function spawnGenericChildProcess ( configFile , cb ) {
11
- const
12
- configFilePath = path . join ( __dirname , 'outband' , configFile ) ,
13
- command_args = [ path . join ( __dirname , 'spawn-generic.js' ) , configFilePath ] ;
11
+ module . exports . genericChildProcess = _spawnProcess ( 'spawn-generic.js' ) ;
12
+ module . exports . childProcess = _spawnProcess ( 'spawn-custom.js' ) ;
14
13
15
- // make sure that the config file exists
16
- if ( ! existsSync ( configFilePath ) )
17
- return cb ( new Error ( 'ENOENT: configFile ' + configFilePath + ' does not exist' ) ) ;
14
+ function _spawnProcess ( spawnFile ) {
15
+ return function ( testCase , configFile , cb ) {
16
+ var
17
+ configFilePath = path . join ( __dirname , 'outband' , configFile ) ,
18
+ commandArgs = [ path . join ( __dirname , spawnFile ) , configFilePath ] ;
18
19
19
- _do_spawn ( command_args , cb ) ;
20
- } ;
20
+ exists ( configFilePath , function ( configExists ) {
21
+ if ( configExists ) return _doSpawn ( commandArgs , cb ) ;
21
22
22
- module . exports . childProcess = function spawnChildProcess ( configFile , cb , detach ) {
23
- var
24
- configFilePath = path . join ( __dirname , 'outband' , configFile ) ,
25
- command_args = [ path . join ( __dirname , 'spawn-custom.js' ) , configFilePath ] ;
26
-
27
- // make sure that the config file exists
28
- if ( ! existsSync ( configFilePath ) )
29
- return cb ( new Error ( 'ENOENT: configFile ' + configFilePath + ' does not exist' ) ) ;
30
-
31
- if ( arguments . length > 2 ) {
32
- for ( var i = 2 ; i < arguments . length ; i ++ ) {
33
- command_args . push ( arguments [ i ] ) ;
34
- }
35
- }
36
-
37
- _do_spawn ( command_args , cb , detach ) ;
23
+ cb ( new Error ( 'ENOENT: configFile ' + configFilePath + ' does not exist' ) ) ;
24
+ } ) ;
25
+ } ;
38
26
}
39
27
40
- function _do_spawn ( command_args , cb , detach ) {
41
- const
28
+ function _doSpawn ( commandArgs , cb ) {
29
+ var
42
30
node_path = process . argv [ 0 ] ,
43
31
stdoutBufs = [ ] ,
44
- stderrBufs = [ ] ;
45
-
46
- var
32
+ stderrBufs = [ ] ,
47
33
child ,
48
34
done = false ,
49
35
stderrDone = false ,
50
36
stdoutDone = false ;
51
37
38
+ if ( process . env . running_under_istanbul ) {
39
+ commandArgs = [
40
+ ISTANBUL_PATH , 'cover' , '--report' , 'none' , '--print' , 'none' ,
41
+ '--dir' , path . join ( 'coverage' , 'json' ) , '--include-pid' ,
42
+ commandArgs [ 0 ] , '--' , commandArgs [ 1 ]
43
+ ] ;
44
+ }
45
+
52
46
// spawn doesn’t have the quoting problems that exec does,
53
47
// especially when going for Windows portability.
54
- child = spawn ( node_path , command_args , detach ? { detached : true } : undefined ) ;
48
+ child = spawn ( node_path , commandArgs ) ;
55
49
child . stdin . end ( ) ;
56
- // TODO:we no longer support node <0.10.0
50
+
51
+ // TODO we no longer support node 0.6
57
52
// Cannot use 'close' event because not on node-0.6.
58
53
function _close ( ) {
59
- const
54
+ var
60
55
stderr = _bufferConcat ( stderrBufs ) . toString ( ) ,
61
56
stdout = _bufferConcat ( stdoutBufs ) . toString ( ) ;
57
+
62
58
if ( stderrDone && stdoutDone && ! done ) {
63
59
done = true ;
64
60
cb ( null , stderr , stdout ) ;
65
61
}
66
62
}
63
+
67
64
child . on ( 'error' , function _spawnError ( err ) {
68
65
if ( ! done ) {
69
66
done = true ;
70
67
cb ( err ) ;
71
68
}
72
69
} ) ;
70
+
73
71
child . stdout . on ( 'data' , function _stdoutData ( data ) {
74
72
stdoutBufs . push ( data ) ;
75
73
} ) . on ( 'close' , function _stdoutEnd ( ) {
76
74
stdoutDone = true ;
77
75
_close ( ) ;
78
76
} ) ;
77
+
79
78
child . stderr . on ( 'data' , function _stderrData ( data ) {
80
79
stderrBufs . push ( data ) ;
81
80
} ) . on ( 'close' , function _stderrEnd ( ) {
@@ -87,13 +86,13 @@ function _do_spawn(command_args, cb, detach) {
87
86
function _bufferConcat ( buffers ) {
88
87
if ( Buffer . concat ) {
89
88
return Buffer . concat . apply ( this , arguments ) ;
90
- } else {
91
- return new Buffer ( buffers . reduce ( function ( acc , buf ) {
92
- for ( var i = 0 ; i < buf . length ; i ++ ) {
93
- acc . push ( buf [ i ] ) ;
94
- }
95
- return acc ;
96
- } , [ ] ) ) ;
97
89
}
90
+
91
+ return new Buffer ( buffers . reduce ( function ( acc , buf ) {
92
+ for ( var i = 0 ; i < buf . length ; i ++ ) {
93
+ acc . push ( buf [ i ] ) ;
94
+ }
95
+ return acc ;
96
+ } , [ ] ) ) ;
98
97
}
99
98
0 commit comments