2013-09-18 05:58:41 +04:00
|
|
|
Task = require '../src/task'
|
2015-02-19 22:16:50 +03:00
|
|
|
Grim = require 'grim'
|
2013-06-13 01:09:22 +04:00
|
|
|
|
2013-06-14 04:12:23 +04:00
|
|
|
describe "Task", ->
|
2013-06-15 03:09:38 +04:00
|
|
|
describe "@once(taskPath, args..., callback)", ->
|
|
|
|
it "terminates the process after it completes", ->
|
|
|
|
handlerResult = null
|
2013-09-18 03:15:53 +04:00
|
|
|
task = Task.once require.resolve('./fixtures/task-spec-handler'), (result) ->
|
2013-06-15 03:09:38 +04:00
|
|
|
handlerResult = result
|
|
|
|
|
|
|
|
processErrored = false
|
|
|
|
childProcess = task.childProcess
|
|
|
|
spyOn(childProcess, 'kill').andCallThrough()
|
|
|
|
task.childProcess.on 'error', -> processErrored = true
|
|
|
|
|
|
|
|
waitsFor ->
|
|
|
|
handlerResult?
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
expect(handlerResult).toBe 'hello'
|
|
|
|
expect(childProcess.kill).toHaveBeenCalled()
|
|
|
|
expect(processErrored).toBe false
|
2015-01-10 03:57:53 +03:00
|
|
|
|
|
|
|
it "calls listeners registered with ::on when events are emitted in the task", ->
|
|
|
|
task = new Task(require.resolve('./fixtures/task-spec-handler'))
|
|
|
|
|
|
|
|
eventSpy = jasmine.createSpy('eventSpy')
|
|
|
|
task.on("some-event", eventSpy)
|
|
|
|
|
|
|
|
waitsFor (done) -> task.start(done)
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
expect(eventSpy).toHaveBeenCalledWith(1, 2, 3)
|
|
|
|
|
|
|
|
it "unregisters listeners when the Disposable returned by ::on is disposed", ->
|
|
|
|
task = new Task(require.resolve('./fixtures/task-spec-handler'))
|
|
|
|
|
|
|
|
eventSpy = jasmine.createSpy('eventSpy')
|
|
|
|
disposable = task.on("some-event", eventSpy)
|
|
|
|
disposable.dispose()
|
|
|
|
|
|
|
|
waitsFor (done) -> task.start(done)
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
expect(eventSpy).not.toHaveBeenCalled()
|
2015-02-19 22:16:50 +03:00
|
|
|
|
|
|
|
it "reports deprecations in tasks", ->
|
|
|
|
jasmine.snapshotDeprecations()
|
2015-02-20 01:53:15 +03:00
|
|
|
handlerPath = require.resolve('./fixtures/task-handler-with-deprecations')
|
|
|
|
task = new Task(handlerPath)
|
2015-02-19 22:16:50 +03:00
|
|
|
|
|
|
|
waitsFor (done) -> task.start(done)
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
deprecations = Grim.getDeprecations()
|
|
|
|
expect(deprecations.length).toBe 1
|
2015-02-20 01:53:15 +03:00
|
|
|
expect(deprecations[0].getStacks()[0][1].fileName).toBe handlerPath
|
2015-02-19 22:16:50 +03:00
|
|
|
jasmine.restoreDeprecationsSnapshot()
|
2015-06-01 19:35:41 +03:00
|
|
|
|
|
|
|
it "adds data listeners to standard out and error to report output", ->
|
|
|
|
task = new Task(require.resolve('./fixtures/task-spec-handler'))
|
|
|
|
{stdout, stderr} = task.childProcess
|
|
|
|
|
|
|
|
task.start()
|
|
|
|
task.start()
|
|
|
|
expect(stdout.listeners('data').length).toBe 1
|
|
|
|
expect(stderr.listeners('data').length).toBe 1
|
|
|
|
|
|
|
|
task.terminate()
|
|
|
|
expect(stdout.listeners('data').length).toBe 0
|
|
|
|
expect(stderr.listeners('data').length).toBe 0
|
2015-07-07 03:23:59 +03:00
|
|
|
|
2015-07-07 03:33:55 +03:00
|
|
|
it "does not throw an error for forked processes missing stdout/stderr", ->
|
|
|
|
spyOn(require('child_process'), 'fork').andCallFake ->
|
|
|
|
Events = require 'events'
|
|
|
|
fakeProcess = new Events()
|
|
|
|
fakeProcess.send = ->
|
|
|
|
fakeProcess.kill = ->
|
|
|
|
fakeProcess
|
2015-07-07 03:23:59 +03:00
|
|
|
|
2015-07-07 03:33:55 +03:00
|
|
|
task = new Task(require.resolve('./fixtures/task-spec-handler'))
|
|
|
|
expect(-> task.start()).not.toThrow()
|
2015-07-07 03:23:59 +03:00
|
|
|
expect(-> task.terminate()).not.toThrow()
|
2015-06-11 01:30:25 +03:00
|
|
|
|
|
|
|
describe "::cancel()", ->
|
|
|
|
it "dispatches 'task:cancelled' when invoked on an active task", ->
|
|
|
|
task = new Task(require.resolve('./fixtures/task-spec-handler'))
|
|
|
|
cancelledEventSpy = jasmine.createSpy('eventSpy')
|
|
|
|
task.on('task:cancelled', cancelledEventSpy)
|
|
|
|
completedEventSpy = jasmine.createSpy('eventSpy')
|
|
|
|
task.on('task:completed', completedEventSpy)
|
|
|
|
|
|
|
|
expect(task.cancel()).toBe(true)
|
|
|
|
expect(cancelledEventSpy).toHaveBeenCalled()
|
|
|
|
expect(completedEventSpy).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
it "does not dispatch 'task:cancelled' when invoked on an inactive task", ->
|
|
|
|
handlerResult = null
|
|
|
|
task = Task.once require.resolve('./fixtures/task-spec-handler'), (result) ->
|
|
|
|
handlerResult = result
|
|
|
|
|
|
|
|
waitsFor ->
|
|
|
|
handlerResult?
|
|
|
|
|
|
|
|
runs ->
|
|
|
|
cancelledEventSpy = jasmine.createSpy('eventSpy')
|
|
|
|
task.on('task:cancelled', cancelledEventSpy)
|
|
|
|
expect(task.cancel()).toBe(false)
|
|
|
|
expect(cancelledEventSpy).not.toHaveBeenCalled()
|