@@ -7,18 +7,25 @@ const CaptureTimeoutLauncher = require('../../../lib/launchers/capture_timeout')
7
7
const ProcessLauncher = require ( '../../../lib/launchers/process' )
8
8
const EventEmitter = require ( '../../../lib/events' ) . EventEmitter
9
9
const createMockTimer = require ( '../mocks/timer' )
10
+ const logger = require ( '../../../lib/logger' )
10
11
11
12
describe ( 'launchers/process.js' , ( ) => {
12
13
let emitter
13
14
let mockSpawn
14
15
let mockTempDir
15
16
let launcher
17
+ let logErrorSpy
18
+ let logDebugSpy
16
19
17
20
const BROWSER_PATH = path . normalize ( '/usr/bin/browser' )
18
21
19
22
beforeEach ( ( ) => {
20
23
emitter = new EventEmitter ( )
21
24
launcher = new BaseLauncher ( 'fake-id' , emitter )
25
+ launcher . name = 'fake-name'
26
+ launcher . ENV_CMD = 'fake-ENV-CMD'
27
+ logErrorSpy = sinon . spy ( logger . create ( 'launcher' ) , 'error' )
28
+ logDebugSpy = sinon . spy ( logger . create ( 'launcher' ) , 'debug' )
22
29
23
30
mockSpawn = sinon . spy ( function ( cmd , args ) {
24
31
const process = new EventEmitter ( )
@@ -74,7 +81,7 @@ describe('launchers/process.js', () => {
74
81
} )
75
82
76
83
describe ( 'with RetryLauncher' , ( ) => {
77
- it ( 'should handle spawn ENOENT error and not even retry' , ( done ) => {
84
+ function assertSpawnError ( { errorCode , emitExit , expectedError } , done ) {
78
85
ProcessLauncher . call ( launcher , mockSpawn , mockTempDir )
79
86
RetryLauncher . call ( launcher , 2 )
80
87
launcher . _getCommand = ( ) => BROWSER_PATH
@@ -83,35 +90,56 @@ describe('launchers/process.js', () => {
83
90
emitter . on ( 'browser_process_failure' , failureSpy )
84
91
85
92
launcher . start ( 'http://host:9876/' )
86
- mockSpawn . _processes [ 0 ] . emit ( 'error' , { code : 'ENOENT' } )
87
- mockSpawn . _processes [ 0 ] . emit ( 'exit' , 1 )
93
+ mockSpawn . _processes [ 0 ] . emit ( 'error' , { code : errorCode } )
94
+ if ( emitExit ) {
95
+ mockSpawn . _processes [ 0 ] . emit ( 'exit' , 1 )
96
+ }
88
97
mockTempDir . remove . callArg ( 1 )
89
98
90
99
_ . defer ( ( ) => {
91
100
expect ( launcher . state ) . to . equal ( launcher . STATE_FINISHED )
92
101
expect ( failureSpy ) . to . have . been . called
102
+ expect ( logDebugSpy ) . to . have . been . callCount ( 5 )
103
+ expect ( logDebugSpy . getCall ( 0 ) ) . to . have . been . calledWithExactly ( 'null -> BEING_CAPTURED' )
104
+ expect ( logDebugSpy . getCall ( 1 ) ) . to . have . been . calledWithExactly ( `${ BROWSER_PATH } http://host:9876/?id=fake-id` )
105
+ expect ( logDebugSpy . getCall ( 2 ) ) . to . have . been . calledWithExactly ( 'Process fake-name exited with code -1 and signal null' )
106
+ expect ( logDebugSpy . getCall ( 3 ) ) . to . have . been . calledWithExactly ( 'fake-name failed (cannot start). Not restarting.' )
107
+ expect ( logDebugSpy . getCall ( 4 ) ) . to . have . been . calledWithExactly ( 'BEING_CAPTURED -> FINISHED' )
108
+ expect ( logErrorSpy ) . to . have . been . calledWith ( expectedError )
93
109
done ( )
94
110
} )
111
+ }
112
+
113
+ it ( 'should handle spawn ENOENT error and not even retry' , ( done ) => {
114
+ assertSpawnError ( {
115
+ errorCode : 'ENOENT' ,
116
+ emitExit : true ,
117
+ expectedError : `Cannot start fake-name\n\tCan not find the binary ${ BROWSER_PATH } \n\tPlease set env variable fake-ENV-CMD`
118
+ } , done )
95
119
} )
96
120
97
121
it ( 'should handle spawn EACCES error and not even retry' , ( done ) => {
98
- ProcessLauncher . call ( launcher , mockSpawn , mockTempDir )
99
- RetryLauncher . call ( launcher , 2 )
100
- launcher . _getCommand = ( ) => BROWSER_PATH
101
-
102
- const failureSpy = sinon . spy ( )
103
- emitter . on ( 'browser_process_failure' , failureSpy )
122
+ assertSpawnError ( {
123
+ errorCode : 'EACCES' ,
124
+ emitExit : true ,
125
+ expectedError : `Cannot start fake-name\n\tPermission denied accessing the binary ${ BROWSER_PATH } \n\tMaybe it's a directory?`
126
+ } , done )
127
+ } )
104
128
105
- launcher . start ( 'http://host:9876/' )
106
- mockSpawn . _processes [ 0 ] . emit ( 'error' , { code : 'EACCES' } )
107
- mockSpawn . _processes [ 0 ] . emit ( 'exit' , 1 )
108
- mockTempDir . remove . callArg ( 1 )
129
+ it ( 'should handle spawn ENOENT error and report the error when exit event is not emitted' , ( done ) => {
130
+ assertSpawnError ( {
131
+ errorCode : 'ENOENT' ,
132
+ emitExit : false ,
133
+ expectedError : `Cannot start fake-name\n\tCan not find the binary ${ BROWSER_PATH } \n\tPlease set env variable fake-ENV-CMD`
134
+ } , done )
135
+ } )
109
136
110
- _ . defer ( ( ) => {
111
- expect ( launcher . state ) . to . equal ( launcher . STATE_FINISHED )
112
- expect ( failureSpy ) . to . have . been . called
113
- done ( )
114
- } )
137
+ it ( 'should handle spawn EACCES error and report the error when exit event is not emitted' , ( done ) => {
138
+ assertSpawnError ( {
139
+ errorCode : 'EACCES' ,
140
+ emitExit : false ,
141
+ expectedError : `Cannot start fake-name\n\tPermission denied accessing the binary ${ BROWSER_PATH } \n\tMaybe it's a directory?`
142
+ } , done )
115
143
} )
116
144
} )
117
145
0 commit comments