1
- const _ = require ( 'lodash' ) ;
2
-
3
1
describe ( 'exec' , ( ) => {
4
2
let logger ;
5
3
let exec ;
6
4
let cpp ;
7
5
8
6
beforeEach ( ( ) => {
9
7
jest . mock ( './logger' ) ;
10
- jest . mock ( 'child-process-promise' ) ;
11
8
logger = require ( './logger' ) ;
9
+
10
+ jest . mock ( 'child-process-promise' ) ;
12
11
cpp = require ( 'child-process-promise' ) ;
12
+
13
13
exec = require ( './exec' ) ;
14
14
} ) ;
15
15
@@ -20,9 +20,7 @@ describe('exec', () => {
20
20
} ) ;
21
21
22
22
it ( `exec command with no arguments successfully` , async ( ) => {
23
- const successfulResult = returnSuccessfulNoValue ( ) ;
24
- const resolvedPromise = Promise . resolve ( successfulResult ) ;
25
- cpp . exec . mockReturnValueOnce ( resolvedPromise ) ;
23
+ mockCppSuccessful ( cpp ) ;
26
24
await exec . execWithRetriesAndLogs ( 'bin' ) ;
27
25
expect ( cpp . exec ) . toHaveBeenCalledWith ( `bin` , { timeout : 0 } ) ;
28
26
} ) ;
@@ -48,17 +46,39 @@ describe('exec', () => {
48
46
expect ( cpp . exec ) . toHaveBeenCalledWith ( `export MY_PREFIX && bin --argument 123` , { timeout : 0 } ) ;
49
47
} ) ;
50
48
51
- it ( `exec command with arguments and status logs successfully` , async ( ) => {
52
- mockCppSuccessful ( cpp ) ;
49
+ it ( `exec command with arguments and try-based status logs successfully, with status logging` , async ( ) => {
50
+ cpp . exec
51
+ . mockRejectedValueOnce ( returnErrorWithValue ( 'error result' ) )
52
+ . mockResolvedValueOnce ( returnSuccessfulWithValue ( 'successful result' ) ) ;
53
+
54
+ const options = { args : `--argument 123` } ;
55
+ const statusLogs = {
56
+ trying : 'trying status log' ,
57
+ successful : 'successful status log' ,
58
+ } ;
59
+ await exec . execWithRetriesAndLogs ( 'bin' , options , statusLogs ) ;
60
+
61
+ expect ( cpp . exec ) . toHaveBeenCalledWith ( `bin --argument 123` , { timeout : 0 } ) ;
62
+ expect ( logger . debug ) . toHaveBeenCalledWith ( { event : 'EXEC_TRY' , retryNumber : 1 } , statusLogs . trying ) ;
63
+ expect ( logger . debug ) . toHaveBeenCalledWith ( { event : 'EXEC_TRY' , retryNumber : 2 } , statusLogs . trying ) ;
64
+ expect ( logger . trace ) . toHaveBeenCalledWith ( { event : 'EXEC_TRY_FAIL' } , 'error result' ) ;
65
+ } ) ;
66
+
67
+ it ( `exec command with arguments and retry-based status logs successfully, with status logging` , async ( ) => {
68
+ cpp . exec
69
+ . mockRejectedValueOnce ( returnErrorWithValue ( 'error result' ) )
70
+ . mockResolvedValueOnce ( returnSuccessfulWithValue ( 'successful result' ) ) ;
53
71
54
72
const options = { args : `--argument 123` } ;
55
73
const statusLogs = {
56
- trying : `trying status log` ,
57
- successful : `successful status log`
74
+ retrying : true ,
58
75
} ;
59
76
await exec . execWithRetriesAndLogs ( 'bin' , options , statusLogs ) ;
60
77
61
78
expect ( cpp . exec ) . toHaveBeenCalledWith ( `bin --argument 123` , { timeout : 0 } ) ;
79
+ expect ( logger . debug ) . toHaveBeenCalledWith ( { event : 'EXEC_RETRY' , retryNumber : 2 } , '(Retry #1)' , 'bin --argument 123' ) ;
80
+ expect ( logger . debug ) . not . toHaveBeenCalledWith ( { event : 'EXEC_RETRY' , retryNumber : 1 } , expect . any ( String ) , expect . any ( String ) ) ;
81
+ expect ( logger . trace ) . toHaveBeenCalledWith ( { event : 'EXEC_TRY_FAIL' } , 'error result' ) ;
62
82
} ) ;
63
83
64
84
it ( `exec command should output success and err logs` , async ( ) => {
@@ -75,7 +95,7 @@ describe('exec', () => {
75
95
exitCode : 0
76
96
}
77
97
} ;
78
- cpp . exec . mockReturnValueOnce ( Promise . resolve ( cppResult ) ) ;
98
+ cpp . exec . mockResolvedValueOnce ( cppResult ) ;
79
99
80
100
await exec . execWithRetriesAndLogs ( 'bin' ) ;
81
101
@@ -103,9 +123,7 @@ describe('exec', () => {
103
123
} ) ;
104
124
105
125
it ( `exec command and fail with error code` , async ( ) => {
106
- const errorResult = returnErrorWithValue ( 'error result' ) ;
107
- const rejectedPromise = Promise . reject ( errorResult ) ;
108
- cpp . exec . mockReturnValueOnce ( rejectedPromise ) ;
126
+ mockCppFailure ( cpp ) ;
109
127
110
128
try {
111
129
await exec . execWithRetriesAndLogs ( 'bin' , null , '' , 0 , 1 ) ;
@@ -118,9 +136,7 @@ describe('exec', () => {
118
136
} ) ;
119
137
120
138
it ( `exec command and fail with error code, report only to debug log if verbosity is low` , async ( ) => {
121
- const errorResult = returnErrorWithValue ( 'error result' ) ;
122
- const rejectedPromise = Promise . reject ( errorResult ) ;
123
- cpp . exec . mockReturnValueOnce ( rejectedPromise ) ;
139
+ mockCppFailure ( cpp ) ;
124
140
125
141
try {
126
142
await exec . execWithRetriesAndLogs ( 'bin' , { verbosity : 'low' } , '' , 0 , 1 ) ;
@@ -133,9 +149,7 @@ describe('exec', () => {
133
149
} ) ;
134
150
135
151
it ( `exec command and fail with timeout` , async ( ) => {
136
- const errorResult = returnErrorWithValue ( 'error result' ) ;
137
- const rejectedPromise = Promise . reject ( errorResult ) ;
138
- cpp . exec . mockReturnValueOnce ( rejectedPromise ) ;
152
+ mockCppFailure ( cpp ) ;
139
153
140
154
try {
141
155
await exec . execWithRetriesAndLogs ( 'bin' , { timeout : 1 } , '' , 0 , 1 ) ;
@@ -148,13 +162,14 @@ describe('exec', () => {
148
162
149
163
it ( `exec command with multiple failures` , async ( ) => {
150
164
const errorResult = returnErrorWithValue ( 'error result' ) ;
151
- const rejectedPromise = Promise . reject ( errorResult ) ;
152
- cpp . exec . mockReturnValueOnce ( rejectedPromise )
153
- . mockReturnValueOnce ( rejectedPromise )
154
- . mockReturnValueOnce ( rejectedPromise )
155
- . mockReturnValueOnce ( rejectedPromise )
156
- . mockReturnValueOnce ( rejectedPromise )
157
- . mockReturnValueOnce ( rejectedPromise ) ;
165
+ cpp . exec
166
+ . mockRejectedValueOnce ( errorResult )
167
+ . mockRejectedValueOnce ( errorResult )
168
+ . mockRejectedValueOnce ( errorResult )
169
+ . mockRejectedValueOnce ( errorResult )
170
+ . mockRejectedValueOnce ( errorResult )
171
+ . mockRejectedValueOnce ( errorResult ) ;
172
+
158
173
try {
159
174
await exec . execWithRetriesAndLogs ( 'bin' , null , '' , 5 , 1 ) ;
160
175
fail ( 'expected execWithRetriesAndLogs() to throw' ) ;
@@ -167,16 +182,15 @@ describe('exec', () => {
167
182
168
183
it ( `exec command with multiple failures and then a success` , async ( ) => {
169
184
const errorResult = returnErrorWithValue ( 'error result' ) ;
170
- const rejectedPromise = Promise . reject ( errorResult ) ;
171
185
const successfulResult = returnSuccessfulWithValue ( 'successful result' ) ;
172
- const resolvedPromise = Promise . resolve ( successfulResult ) ;
173
186
174
- cpp . exec . mockReturnValueOnce ( rejectedPromise )
175
- . mockReturnValueOnce ( rejectedPromise )
176
- . mockReturnValueOnce ( rejectedPromise )
177
- . mockReturnValueOnce ( rejectedPromise )
178
- . mockReturnValueOnce ( rejectedPromise )
179
- . mockReturnValueOnce ( resolvedPromise ) ;
187
+ cpp . exec
188
+ . mockRejectedValueOnce ( errorResult )
189
+ . mockRejectedValueOnce ( errorResult )
190
+ . mockRejectedValueOnce ( errorResult )
191
+ . mockRejectedValueOnce ( errorResult )
192
+ . mockRejectedValueOnce ( errorResult )
193
+ . mockResolvedValueOnce ( successfulResult ) ;
180
194
181
195
await exec . execWithRetriesAndLogs ( 'bin' , null , '' , 6 , 1 ) ;
182
196
expect ( cpp . exec ) . toHaveBeenCalledWith ( `bin` , { timeout : 0 } ) ;
@@ -298,42 +312,36 @@ function toStream(string) {
298
312
return stream ;
299
313
}
300
314
301
- function returnSuccessfulWithValue ( value ) {
302
- const result = {
315
+ const returnSuccessfulWithValue = ( value ) => ( {
303
316
stdout : JSON . stringify ( value ) ,
304
317
stderr : "err" ,
305
318
childProcess : {
306
319
exitCode : 0
307
320
}
308
- } ;
309
- return result ;
310
- }
321
+ } ) ;
311
322
312
- function returnErrorWithValue ( value ) {
313
- const result = {
323
+ const returnErrorWithValue = ( value ) => ( {
314
324
stdout : "out" ,
315
325
stderr : value ,
316
326
childProcess : {
317
327
exitCode : 1
318
328
}
319
- } ;
320
- return result ;
321
- }
329
+ } ) ;
322
330
323
- function returnSuccessfulNoValue ( ) {
324
- const result = {
331
+ const returnSuccessfulNoValue = ( ) => ( {
325
332
childProcess : {
326
333
exitCode : 0
327
334
}
328
- } ;
329
- return result ;
330
- }
335
+ } ) ;
331
336
332
337
function mockCppSuccessful ( cpp ) {
333
338
const successfulResult = returnSuccessfulWithValue ( 'successful result' ) ;
334
- const resolvedPromise = Promise . resolve ( successfulResult ) ;
335
- cpp . exec . mockReturnValueOnce ( resolvedPromise ) ;
336
-
339
+ cpp . exec . mockResolvedValueOnce ( successfulResult ) ;
337
340
return successfulResult ;
338
341
}
339
342
343
+ function mockCppFailure ( cpp ) {
344
+ const errorResult = returnErrorWithValue ( 'error result' ) ;
345
+ cpp . exec . mockRejectedValueOnce ( errorResult ) ;
346
+ return errorResult ;
347
+ }
0 commit comments