diff --git a/lib/launchers/Base.js b/lib/launchers/Base.js index d91fef55d..dad008631 100644 --- a/lib/launchers/Base.js +++ b/lib/launchers/Base.js @@ -26,6 +26,7 @@ var BaseBrowser = function(id, emitter, captureTimeout, retryLimit) { this.start = function(url) { capturingUrl = url; + self.state = BEING_CAPTURED; try { log.debug('Creating temp dir at ' + self._tempDir); @@ -33,7 +34,6 @@ var BaseBrowser = function(id, emitter, captureTimeout, retryLimit) { } catch (e) {} self._start(capturingUrl + '?id=' + self.id); - self.state = BEING_CAPTURED; if (captureTimeout) { setTimeout(self._onTimeout, captureTimeout); @@ -104,13 +104,35 @@ var BaseBrowser = function(id, emitter, captureTimeout, retryLimit) { self._process = spawn(cmd, args); var errorOutput = ''; - self._process.stderr.on('data', function(data) { - errorOutput += data.toString(); - }); self._process.on('close', function(code) { self._onProcessExit(code, errorOutput); }); + + self._process.on('error', function(err) { + if (err.code === 'ENOENT') { + retryLimit = 0; + errorOutput = 'Can not find the binary ' + cmd + '\n\t' + + 'Please set env variable ' + self.ENV_CMD; + } else { + errorOutput += err.toString(); + } + }); + + // Node 0.8 does not emit the error + if (process.versions.node.indexOf('0.8') === 0) { + self._process.stderr.on('data', function(data) { + var msg = data.toString(); + + if (msg.indexOf('No such file or directory') !== -1) { + retryLimit = 0; + errorOutput = 'Can not find the binary ' + cmd + '\n\t' + + 'Please set env variable ' + self.ENV_CMD; + } else { + errorOutput += msg; + } + }); + } }; diff --git a/test/unit/launchers/Base.spec.coffee b/test/unit/launchers/Base.spec.coffee index 5496a1116..6ffe090cd 100644 --- a/test/unit/launchers/Base.spec.coffee +++ b/test/unit/launchers/Base.spec.coffee @@ -32,6 +32,7 @@ describe 'launchers Base', -> globals = process: platform: 'darwin' + versions: node: '0.10.x' env: TMP: '/tmp' nextTick: process.nextTick @@ -74,6 +75,30 @@ describe 'launchers Base', -> expect(browser._start).to.have.been.calledWith '/capture/url?id=123' + it 'should handle spawn ENOENT error and not even retry', (done) -> + browser = new m.BaseBrowser 123, new events.EventEmitter, 0, 3 + browser.DEFAULT_CMD = darwin: '/usr/bin/browser' + + error = new Error 'spawn ENOENT' + error.code = 'ENOENT' + + browser.start '/capture/url' + + spawnProcess = mockSpawn._processes[0] + mockSpawn.reset() + + spawnProcess.emit 'error', error + spawnProcess.emit 'close', 1 + + # do not retry + expect(mockSpawn).not.to.have.been.called + + browser.kill done + + # do not kill already dead process + expect(spawnProcess.kill).to.not.have.been.called + + describe 'kill', -> it 'should just fire done if already killed', (done) -> browser = new m.BaseBrowser 123, new events.EventEmitter, 0, 1 # disable retry