Skip to content

Commit

Permalink
Merge pull request #167 from sclevine/fail-on-panic-nil
Browse files Browse the repository at this point in the history
`panic(nil)` in a test results in it immediately passing
  • Loading branch information
onsi committed Jun 10, 2015
2 parents 462326b + 74982cc commit a2e78ca
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
10 changes: 8 additions & 2 deletions internal/leafnodes/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ func (r *runner) runAsync() (outcome types.SpecState, failure types.SpecFailure)
done := make(chan interface{}, 1)

go func() {
finished := false

defer func() {
if e := recover(); e != nil {
if e := recover(); e != nil || !finished {
r.failer.Panic(codelocation.New(2), e)
select {
case <-done:
Expand All @@ -81,6 +83,7 @@ func (r *runner) runAsync() (outcome types.SpecState, failure types.SpecFailure)
}()

r.asyncFunc(done)
finished = true
}()

select {
Expand All @@ -93,15 +96,18 @@ func (r *runner) runAsync() (outcome types.SpecState, failure types.SpecFailure)
return
}
func (r *runner) runSync() (outcome types.SpecState, failure types.SpecFailure) {
finished := false

defer func() {
if e := recover(); e != nil {
if e := recover(); e != nil || !finished {
r.failer.Panic(codelocation.New(2), e)
}

failure, outcome = r.failer.Drain(r.nodeType, r.componentIndex, r.codeLocation)
}()

r.syncFunc()
finished = true

return
}
35 changes: 35 additions & 0 deletions internal/leafnodes/shared_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ func SynchronousSharedRunnerBehaviors(build func(body interface{}, timeout time.
Ω(failure.ForwardedPanic).Should(Equal("ack!"))
})
})

Context("when a panic occurs with a nil value", func() {
BeforeEach(func() {
outcome, failure = build(func() {
didRun = true
innerCodeLocation = codelocation.New(0)
panic(nil)
}, 0, failer, componentCodeLocation).Run()
})

It("should return the nil-valued panic", func() {
Ω(didRun).Should(BeTrue())

Ω(outcome).Should(Equal(types.SpecStatePanicked))
Ω(failure.ForwardedPanic).Should(Equal("<nil>"))
})
})

})
}

Expand Down Expand Up @@ -230,6 +248,23 @@ func AsynchronousSharedRunnerBehaviors(build func(body interface{}, timeout time
Ω(failure.ForwardedPanic).Should(Equal("ack!"))
})
})

Context("when the function panics with a nil value", func() {
BeforeEach(func() {
outcome, failure = build(func(done Done) {
didRun = true
innerCodeLocation = codelocation.New(0)
panic(nil)
}, 100*time.Millisecond, failer, componentCodeLocation).Run()
})

It("should return the nil-valued panic", func() {
Ω(didRun).Should(BeTrue())

Ω(outcome).Should(Equal(types.SpecStatePanicked))
Ω(failure.ForwardedPanic).Should(Equal("<nil>"))
})
})
})
}

Expand Down

0 comments on commit a2e78ca

Please sign in to comment.