Skip to content

Commit ec60e89

Browse files
authored
Merge pull request #25 Print name and position of failed fixture.
2 parents 7f21018 + 3203ea0 commit ec60e89

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

env.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ func (e *EnvT) T() T {
5858
// Cache must be called direct from fixture - it use runtime stacktrace for
5959
// detect called method - it is part of cache key.
6060
// params - part of cache key. Usually - parameters, passed to fixture.
61-
// it allow use parametrized fixtures with different results.
62-
// params must be json serializable.
61+
//
62+
// it allow use parametrized fixtures with different results.
63+
// params must be json serializable.
64+
//
6365
// opt - fixture options, nil for default options.
6466
// f - callback - fixture body.
6567
// Cache guarantee for call f exactly once for same Cache called and params combination.
@@ -71,8 +73,10 @@ func (e *EnvT) Cache(params interface{}, opt *FixtureOptions, f FixtureCallbackF
7173
// CacheWithCleanup must be called direct from fixture - it use runtime stacktrace for
7274
// detect called method - it is part of cache key.
7375
// params - part of cache key. Usually - parameters, passed to fixture.
74-
// it allow use parametrized fixtures with different results.
75-
// params must be json serializable.
76+
//
77+
// it allow use parametrized fixtures with different results.
78+
// params must be json serializable.
79+
//
7680
// opt - fixture options, nil for default options.
7781
// f - callback - fixture body.
7882
// cleanup, returned from f called while fixture cleanup
@@ -107,13 +111,32 @@ func (e *EnvT) cache(params interface{}, opt *FixtureOptions, f FixtureCallbackF
107111
// return not reacheble after Fatalf
108112
return nil
109113
}
114+
110115
wrappedF := e.fixtureCallWrapper(key, f, opt)
111116
res, err := e.c.GetOrSet(key, wrappedF)
112117
if err != nil {
113118
if errors.Is(err, ErrSkipTest) {
114119
e.T().SkipNow()
115120
} else {
116-
e.t.Fatalf("failed to call fixture func: %v", err)
121+
// Get fixture name
122+
externalCallerLevel := 4
123+
var pc = make([]uintptr, externalCallerLevel)
124+
var extCallerFrame runtime.Frame
125+
if externalCallerLevel == runtime.Callers(opt.additionlSkipExternalCalls, pc) {
126+
frames := runtime.CallersFrames(pc)
127+
frames.Next() // callers
128+
frames.Next() // the function
129+
frames.Next() // caller of the function (env private function)
130+
extCallerFrame, _ = frames.Next() // external caller
131+
}
132+
133+
fixtureDesctiption := fmt.Sprintf(
134+
"%v (%v:%v)",
135+
extCallerFrame.Function,
136+
extCallerFrame.File,
137+
extCallerFrame.Line,
138+
)
139+
e.t.Fatalf("failed to call fixture func \"%v\": %v", fixtureDesctiption, err)
117140
}
118141

119142
// panic must be not reachable after SkipNow or Fatalf

0 commit comments

Comments
 (0)