1
1
import { TestScheduler } from 'rxjs/testing' ;
2
+ import { CloseEvent } from './command' ;
2
3
import { CompletionListener , SuccessCondition } from './completion-listener' ;
3
4
import { createFakeCloseEvent , FakeCommand } from './fixtures/fake-command' ;
4
5
5
6
let commands : FakeCommand [ ] ;
6
7
let scheduler : TestScheduler ;
7
8
beforeEach ( ( ) => {
8
- commands = [ new FakeCommand ( 'foo' ) , new FakeCommand ( 'bar' ) ] ;
9
+ commands = [
10
+ new FakeCommand ( 'foo' , 'echo' , 0 ) ,
11
+ new FakeCommand ( 'bar' , 'echo' , 1 ) ,
12
+ new FakeCommand ( 'baz' , 'echo' , 2 ) ,
13
+ ] ;
9
14
scheduler = new TestScheduler ( ( ) => true ) ;
10
15
} ) ;
11
16
@@ -15,12 +20,18 @@ const createController = (successCondition?: SuccessCondition) =>
15
20
scheduler,
16
21
} ) ;
17
22
23
+ const emitFakeCloseEvent = (
24
+ command : FakeCommand ,
25
+ event ?: Partial < CloseEvent > ,
26
+ ) => command . close . next ( createFakeCloseEvent ( { ...event , command, index : command . index } ) ) ;
27
+
18
28
describe ( 'with default success condition set' , ( ) => {
19
29
it ( 'succeeds if all processes exited with code 0' , ( ) => {
20
30
const result = createController ( ) . listen ( commands ) ;
21
31
22
32
commands [ 0 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
23
33
commands [ 1 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
34
+ commands [ 2 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
24
35
25
36
scheduler . flush ( ) ;
26
37
@@ -32,6 +43,7 @@ describe('with default success condition set', () => {
32
43
33
44
commands [ 0 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
34
45
commands [ 1 ] . close . next ( createFakeCloseEvent ( { exitCode : 1 } ) ) ;
46
+ commands [ 2 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
35
47
36
48
scheduler . flush ( ) ;
37
49
@@ -45,6 +57,7 @@ describe('with success condition set to first', () => {
45
57
46
58
commands [ 1 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
47
59
commands [ 0 ] . close . next ( createFakeCloseEvent ( { exitCode : 1 } ) ) ;
60
+ commands [ 2 ] . close . next ( createFakeCloseEvent ( { exitCode : 1 } ) ) ;
48
61
49
62
scheduler . flush ( ) ;
50
63
@@ -56,6 +69,7 @@ describe('with success condition set to first', () => {
56
69
57
70
commands [ 1 ] . close . next ( createFakeCloseEvent ( { exitCode : 1 } ) ) ;
58
71
commands [ 0 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
72
+ commands [ 2 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
59
73
60
74
scheduler . flush ( ) ;
61
75
@@ -69,6 +83,7 @@ describe('with success condition set to last', () => {
69
83
70
84
commands [ 1 ] . close . next ( createFakeCloseEvent ( { exitCode : 1 } ) ) ;
71
85
commands [ 0 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
86
+ commands [ 2 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
72
87
73
88
scheduler . flush ( ) ;
74
89
@@ -80,10 +95,117 @@ describe('with success condition set to last', () => {
80
95
81
96
commands [ 1 ] . close . next ( createFakeCloseEvent ( { exitCode : 0 } ) ) ;
82
97
commands [ 0 ] . close . next ( createFakeCloseEvent ( { exitCode : 1 } ) ) ;
98
+ commands [ 2 ] . close . next ( createFakeCloseEvent ( { exitCode : 1 } ) ) ;
83
99
84
100
scheduler . flush ( ) ;
85
101
86
102
return expect ( result ) . rejects . toEqual ( expect . anything ( ) ) ;
87
103
} ) ;
88
104
89
105
} ) ;
106
+
107
+ describe . each ( [
108
+ // Use the middle command for both cases to make it more difficult to make a mess up
109
+ // in the implementation cause false passes.
110
+ [ 'command-bar' as const , 'bar' ] ,
111
+ [ 'command-1' as const , 1 ] ,
112
+ ] ) ( 'with success condition set to %s' , ( condition , nameOrIndex ) => {
113
+ it ( `succeeds if command ${ nameOrIndex } exits with code 0` , ( ) => {
114
+ const result = createController ( condition ) . listen ( commands ) ;
115
+
116
+ emitFakeCloseEvent ( commands [ 0 ] , { exitCode : 1 } ) ;
117
+ emitFakeCloseEvent ( commands [ 1 ] , { exitCode : 0 } ) ;
118
+ emitFakeCloseEvent ( commands [ 2 ] , { exitCode : 1 } ) ;
119
+
120
+ scheduler . flush ( ) ;
121
+
122
+ return expect ( result ) . resolves . toEqual ( expect . anything ( ) ) ;
123
+ } ) ;
124
+
125
+ it ( `succeeds if all commands ${ nameOrIndex } exit with code 0` , ( ) => {
126
+ commands = [ commands [ 0 ] , commands [ 1 ] , commands [ 1 ] ] ;
127
+ const result = createController ( condition ) . listen ( commands ) ;
128
+
129
+ emitFakeCloseEvent ( commands [ 0 ] , { exitCode : 1 } ) ;
130
+ emitFakeCloseEvent ( commands [ 1 ] , { exitCode : 0 } ) ;
131
+ emitFakeCloseEvent ( commands [ 2 ] , { exitCode : 0 } ) ;
132
+
133
+ scheduler . flush ( ) ;
134
+
135
+ return expect ( result ) . resolves . toEqual ( expect . anything ( ) ) ;
136
+ } ) ;
137
+
138
+ it ( `fails if command ${ nameOrIndex } exits with non-0 code` , ( ) => {
139
+ const result = createController ( condition ) . listen ( commands ) ;
140
+
141
+ emitFakeCloseEvent ( commands [ 0 ] , { exitCode : 0 } ) ;
142
+ emitFakeCloseEvent ( commands [ 1 ] , { exitCode : 1 } ) ;
143
+ emitFakeCloseEvent ( commands [ 2 ] , { exitCode : 0 } ) ;
144
+
145
+ scheduler . flush ( ) ;
146
+
147
+ return expect ( result ) . rejects . toEqual ( expect . anything ( ) ) ;
148
+ } ) ;
149
+
150
+ it ( `fails if some commands ${ nameOrIndex } exit with non-0 code` , ( ) => {
151
+ commands = [ commands [ 0 ] , commands [ 1 ] , commands [ 1 ] ] ;
152
+ const result = createController ( condition ) . listen ( commands ) ;
153
+
154
+ emitFakeCloseEvent ( commands [ 0 ] , { exitCode : 1 } ) ;
155
+ emitFakeCloseEvent ( commands [ 1 ] , { exitCode : 0 } ) ;
156
+ emitFakeCloseEvent ( commands [ 2 ] , { exitCode : 1 } ) ;
157
+
158
+ scheduler . flush ( ) ;
159
+
160
+ return expect ( result ) . resolves . toEqual ( expect . anything ( ) ) ;
161
+ } ) ;
162
+
163
+ it ( `fails if command ${ nameOrIndex } doesn't exist` , ( ) => {
164
+ const result = createController ( condition ) . listen ( [ commands [ 0 ] ] ) ;
165
+
166
+ emitFakeCloseEvent ( commands [ 0 ] , { exitCode : 0 } ) ;
167
+ scheduler . flush ( ) ;
168
+
169
+ return expect ( result ) . rejects . toEqual ( expect . anything ( ) ) ;
170
+ } ) ;
171
+ } ) ;
172
+
173
+ describe . each ( [
174
+ // Use the middle command for both cases to make it more difficult to make a mess up
175
+ // in the implementation cause false passes.
176
+ [ '!command-bar' as const , 'bar' ] ,
177
+ [ '!command-1' as const , 1 ] ,
178
+ ] ) ( 'with success condition set to %s' , ( condition , nameOrIndex ) => {
179
+ it ( `succeeds if all commands but ${ nameOrIndex } exit with code 0` , ( ) => {
180
+ const result = createController ( condition ) . listen ( commands ) ;
181
+
182
+ emitFakeCloseEvent ( commands [ 0 ] , { exitCode : 0 } ) ;
183
+ emitFakeCloseEvent ( commands [ 1 ] , { exitCode : 1 } ) ;
184
+ emitFakeCloseEvent ( commands [ 2 ] , { exitCode : 0 } ) ;
185
+
186
+ scheduler . flush ( ) ;
187
+
188
+ return expect ( result ) . resolves . toEqual ( expect . anything ( ) ) ;
189
+ } ) ;
190
+
191
+ it ( `fails if any commands but ${ nameOrIndex } exit with non-0 code` , ( ) => {
192
+ const result = createController ( condition ) . listen ( commands ) ;
193
+
194
+ emitFakeCloseEvent ( commands [ 0 ] , { exitCode : 1 } ) ;
195
+ emitFakeCloseEvent ( commands [ 1 ] , { exitCode : 1 } ) ;
196
+ emitFakeCloseEvent ( commands [ 2 ] , { exitCode : 0 } ) ;
197
+
198
+ scheduler . flush ( ) ;
199
+
200
+ return expect ( result ) . rejects . toEqual ( expect . anything ( ) ) ;
201
+ } ) ;
202
+
203
+ it ( `succeeds if command ${ nameOrIndex } doesn't exist` , ( ) => {
204
+ const result = createController ( condition ) . listen ( [ commands [ 0 ] ] ) ;
205
+
206
+ emitFakeCloseEvent ( commands [ 0 ] , { exitCode : 0 } ) ;
207
+ scheduler . flush ( ) ;
208
+
209
+ return expect ( result ) . resolves . toEqual ( expect . anything ( ) ) ;
210
+ } ) ;
211
+ } ) ;
0 commit comments