1
1
var createDefaultStream = require ( './lib/default_stream' ) ;
2
2
var Render = require ( './lib/render' ) ;
3
3
var Test = require ( './lib/test' ) ;
4
+ var through = require ( 'through' ) ;
4
5
5
6
var canEmitExit = typeof process !== 'undefined' && process
6
7
&& typeof process . on === 'function'
7
8
;
8
9
var canExit = typeof process !== 'undefined' && process
9
10
&& typeof process . exit === 'function'
10
11
;
11
- var onexit = ( function ( ) {
12
- var stack = [ ] ;
13
- if ( canEmitExit ) process . on ( 'exit' , function ( code ) {
14
- for ( var i = 0 ; i < stack . length ; i ++ ) stack [ i ] ( code ) ;
15
- } ) ;
16
- return function ( cb ) { stack . push ( cb ) } ;
17
- } ) ( ) ;
18
12
19
13
var nextTick = typeof setImmediate !== 'undefined'
20
14
? setImmediate
@@ -28,138 +22,32 @@ exports.Test = Test;
28
22
var exitInterval ;
29
23
30
24
function createHarness ( conf_ ) {
31
- var pending = [ ] ;
32
- var running = false ;
33
25
var count = 0 ;
34
-
35
- var began = false ;
36
- var only = false ;
37
- var closed = false ;
38
- var out = new Render ( ) ;
39
- if ( ! conf_ ) conf_ = { } ;
40
-
41
- var tests = [ ] ;
42
- if ( conf_ . exit === false && exitInterval ) clearInterval ( exitInterval ) ;
43
-
44
- exitInterval = ! exitInterval && conf_ . exit !== false && canEmitExit
45
- && typeof process . _getActiveHandles === 'function'
46
- && setInterval ( function ( ) {
47
- if ( / ^ v 0 \. 8 \. / . test ( process . version )
48
- && process . _getActiveHandles ( ) . length === 1 ) {
49
- tests . forEach ( function ( t ) { t . _exit ( ) } ) ;
50
- }
51
- } , 200 ) ;
52
-
53
26
var exitCode = 0 ;
54
- var exit = function ( c ) { exitCode = c } ;
55
-
56
- out . on ( 'end' , function ( ) {
57
- nextTick ( function ( ) {
58
- clearInterval ( exitInterval ) ;
59
- if ( canExit && conf_ . exit !== false ) process . exit ( exitCode ) ;
60
- } ) ;
27
+ var output = through ( null , function ( ) {
28
+ if ( -- count === 0 && ! closed ) {
29
+ closed = true
30
+ out . close ( ) ;
31
+ }
61
32
} ) ;
33
+ output . pause ( ) ;
34
+ nextTick ( function ( ) { output . resume ( ) } ) ;
62
35
63
36
var test = function ( name , conf , cb ) {
64
37
count ++ ;
65
38
var t = new Test ( name , conf , cb ) ;
66
- tests . push ( t ) ;
67
- if ( ! conf || typeof conf !== 'object' ) conf = conf_ ;
68
-
69
- if ( conf . exit !== false ) {
70
- onexit ( function ( code ) {
71
- t . _exit ( ) ;
72
- if ( ! closed ) {
73
- closed = true
74
- out . close ( ) ;
75
- }
76
- if ( ! code && ! t . _ok && ( ! only || name === only ) ) {
77
- exit ( 1 ) ;
78
- }
79
- } ) ;
80
- }
81
-
82
- nextTick ( function ( ) {
83
- if ( ! out . piped ) out . pipe ( createDefaultStream ( ) ) ;
84
- if ( ! began ) out . begin ( ) ;
85
- began = true ;
86
-
87
- var run = function ( ) {
88
- running = true ;
89
- out . push ( t ) ;
90
- t . run ( ) ;
91
- } ;
92
-
93
- if ( only && name !== only ) {
94
- count -- ;
95
- return ;
96
- }
97
-
98
- if ( running || pending . length ) {
99
- pending . push ( run ) ;
100
- }
101
- else run ( ) ;
102
- } ) ;
103
-
104
39
t . on ( 'test' , function sub ( st ) {
105
- count ++ ;
106
- st . on ( 'test' , sub ) ;
107
- st . on ( 'end' , onend ) ;
40
+ console . log ( 'SUBTEST' ) ;
41
+ } ) ;
42
+ t . on ( 'result' , function ( r ) {
43
+ console . dir ( r ) ;
44
+ if ( ! r . ok ) exitCode = 1
45
+ } ) ;
46
+ nextTick ( function ( ) {
47
+ t . run ( ) ;
108
48
} ) ;
109
- t . on ( 'result' , function ( r ) { if ( ! r . ok ) exitCode = 1 } ) ;
110
-
111
- t . on ( 'end' , onend ) ;
112
-
113
49
return t ;
114
-
115
- function onend ( ) {
116
- count -- ;
117
- if ( this . _progeny . length ) {
118
- var unshifts = map ( this . _progeny , function ( st ) {
119
- return function ( ) {
120
- running = true ;
121
- out . push ( st ) ;
122
- st . run ( ) ;
123
- } ;
124
- } ) ;
125
- pending . unshift . apply ( pending , unshifts ) ;
126
- }
127
-
128
- process . nextTick ( function ( ) {
129
- running = false ;
130
- if ( pending . length ) return pending . shift ( ) ( ) ;
131
- if ( count === 0 && ! closed ) {
132
- closed = true
133
- out . close ( ) ;
134
- }
135
- if ( conf . exit !== false && canExit && ! t . _ok ) {
136
- exit ( 1 ) ;
137
- }
138
- } ) ;
139
- }
140
50
} ;
141
51
142
- test . only = function ( name ) {
143
- if ( only ) {
144
- throw new Error ( "there can only be one only test" ) ;
145
- }
146
-
147
- only = name ;
148
-
149
- return test . apply ( null , arguments ) ;
150
- } ;
151
-
152
- test . stream = out ;
153
52
return test ;
154
53
}
155
-
156
- function map ( xs , f ) {
157
- if ( xs . map ) return xs . map ( f ) ;
158
- var res = [ ] ;
159
- for ( var i = 0 ; i < xs . length ; i ++ ) {
160
- res . push ( f ( xs [ i ] ) ) ;
161
- }
162
- return res ;
163
- }
164
-
165
- // vim: set softtabstop=4 shiftwidth=4:
0 commit comments