Skip to content

Commit

Permalink
Add spec for thrown spawn error
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinsawicki committed Apr 29, 2015
1 parent fd84c5f commit 920def7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
47 changes: 35 additions & 12 deletions spec/buffered-process-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,44 @@ describe "BufferedProcess", ->
window.onerror = oldOnError

describe "when there is an error handler specified", ->
it "calls the error handler and does not throw an exception", ->
process = new BufferedProcess
command: 'bad-command-nope'
args: ['nothing']
options: {}
describe "when an error event is emitted by the process", ->
it "calls the error handler and does not throw an exception", ->
process = new BufferedProcess
command: 'bad-command-nope'
args: ['nothing']
options: {}

errorSpy = jasmine.createSpy().andCallFake (error) -> error.handle()
process.onWillThrowError(errorSpy)
errorSpy = jasmine.createSpy().andCallFake (error) -> error.handle()
process.onWillThrowError(errorSpy)

waitsFor -> errorSpy.callCount > 0
waitsFor -> errorSpy.callCount > 0

runs ->
expect(window.onerror).not.toHaveBeenCalled()
expect(errorSpy).toHaveBeenCalled()
expect(errorSpy.mostRecentCall.args[0].error.message).toContain 'spawn bad-command-nope ENOENT'
runs ->
expect(window.onerror).not.toHaveBeenCalled()
expect(errorSpy).toHaveBeenCalled()
expect(errorSpy.mostRecentCall.args[0].error.message).toContain 'spawn bad-command-nope ENOENT'

describe "when an error is thrown spawning the process", ->
it "calls the error handler and does not throw an exception", ->
spyOn(ChildProcess, 'spawn').andCallFake ->
error = new Error('Something is really wrong')
error.code = 'EAGAIN'
throw error

process = new BufferedProcess
command: 'ls'
args: []
options: {}

errorSpy = jasmine.createSpy().andCallFake (error) -> error.handle()
process.onWillThrowError(errorSpy)

waitsFor -> errorSpy.callCount > 0

runs ->
expect(window.onerror).not.toHaveBeenCalled()
expect(errorSpy).toHaveBeenCalled()
expect(errorSpy.mostRecentCall.args[0].error.message).toContain 'Something is really wrong'

describe "when there is not an error handler specified", ->
it "calls the error handler and does not throw an exception", ->
Expand Down
4 changes: 2 additions & 2 deletions src/buffered-process.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ class BufferedProcess

spawn: (command, args, options) ->
try
process = ChildProcess.spawn(command, args, options)
spawned = ChildProcess.spawn(command, args, options)
catch spawnError
process.nextTick => @handleError(spawnError)
process
spawned

handleEvents: (stdout, stderr, exit) ->
stdoutClosed = true
Expand Down

0 comments on commit 920def7

Please sign in to comment.