Skip to content

Commit

Permalink
fix handle nil result
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Nov 26, 2023
1 parent 8d47c99 commit 7c98e30
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (e *EnvT) CacheResult(f FixtureFunction, options ...CacheOptions) interface

var fWithoutCleanup FixtureCallbackFunc = func() (res interface{}, err error) {
result, err := f()
if result == nil {
return nil, err
}

resCleanupFunc = result.Cleanup
return result.Value, err
}
Expand Down
20 changes: 20 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,26 @@ func Test_Env_CacheResult(t *testing.T) {
rndFix(e, "first")
})
})
t.Run("WithNilResult", func(t *testing.T) {
at := assert.New(t)
tMock := &internal.TestMock{TestName: "mock"}
e := newTestEnv(tMock)

testErr := errors.New("test err")

failedFix := func(e Env) int {
return e.CacheResult(func() (*Result, error) {
return nil, testErr
}).(int)
}
done := make(chan bool)
go func() {
defer close(done)
failedFix(e)
}()
<-done
at.Equal(1, len(tMock.Fatals))
})
}

func Test_FixtureWrapper(t *testing.T) {
Expand Down

0 comments on commit 7c98e30

Please sign in to comment.