-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.go
191 lines (165 loc) · 4.36 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package fixenv
import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"runtime"
"strings"
"sync"
)
const packageScopeName = "TestMain"
var (
globalCache = newCache()
globalEmptyFixtureOptions = &FixtureOptions{}
globalMutex = &sync.Mutex{}
globalScopeInfo = make(map[string]*scopeInfo)
)
type EnvT struct {
t T
c *cache
m sync.Locker
scopes map[string]*scopeInfo
}
func NewEnv(t T) *EnvT {
env := newEnv(t, globalCache, globalMutex, globalScopeInfo)
env.onCreate()
return env
}
func newEnv(t T, c *cache, m sync.Locker, scopes map[string]*scopeInfo) *EnvT {
return &EnvT{
t: t,
c: c,
m: m,
scopes: scopes,
}
}
func (e *EnvT) T() T {
return e.t
}
func (e *EnvT) Cache(params interface{}, opt *FixtureOptions, f FixtureCallbackFunc) interface{} {
if opt == nil {
opt = globalEmptyFixtureOptions
}
key, err := makeCacheKey(e.t.Name(), params, opt, false)
if err != nil {
e.t.Fatalf("failed to create cache key: %v", err)
// return not reacheble after Fatalf
return nil
}
wrappedF := e.fixtureCallWrapper(key, f, opt)
res, err := e.c.GetOrSet(key, wrappedF)
if err != nil {
e.t.Fatalf("failed to call fixture func: %v", err)
// return not reachable after Fatalf
return nil
}
return res
}
func (e *EnvT) Cleanup(f func()) {
e.T().Cleanup(f)
}
func (e *EnvT) tearDown() {
e.m.Lock()
defer e.m.Unlock()
testName := e.t.Name()
if si, ok := e.scopes[testName]; ok {
cacheKeys := si.Keys()
e.c.DeleteKeys(cacheKeys...)
delete(e.scopes, testName)
} else {
e.t.Fatalf("unexpected call env tearDown for test: %q", testName)
}
}
func (e *EnvT) onCreate() {
e.m.Lock()
defer e.m.Unlock()
testName := e.t.Name()
if _, ok := e.scopes[testName]; ok {
e.t.Fatalf("Env exist already for scope: %q", testName)
} else {
e.scopes[testName] = newScopeInfo(e.t)
e.t.Cleanup(e.tearDown)
}
}
// makeCacheKey generate cache key
// must be called from first level of env functions - for detect external caller
func makeCacheKey(testname string, params interface{}, opt *FixtureOptions, testCall bool) (cacheKey, error) {
externalCallerLevel := 4
var pc = make([]uintptr, externalCallerLevel)
var extCallerFrame runtime.Frame
if externalCallerLevel == runtime.Callers(0, pc) {
frames := runtime.CallersFrames(pc)
frames.Next() // callers
frames.Next() // the function
frames.Next() // caller of the function
extCallerFrame, _ = frames.Next() // external caller
}
scopeName := scopeName(testname, opt.Scope)
return makeCacheKeyFromFrame(params, opt.Scope, extCallerFrame, scopeName, testCall)
}
func makeCacheKeyFromFrame(params interface{}, scope CacheScope, f runtime.Frame, scopeName string, testCall bool) (cacheKey, error) {
switch {
case f.Function == "":
return "", errors.New("failed to detect caller func name")
case f.File == "":
return "", errors.New("failed to detect caller func file")
default:
// pass
}
key := struct {
Scope CacheScope `json:"scope"`
ScopeName string `json:"scope_name"`
FunctionName string `json:"func"`
FileName string `json:"fname"`
Params interface{} `json:"params"`
}{
Scope: scope,
ScopeName: scopeName,
FunctionName: f.Function,
FileName: f.File,
Params: params,
}
if testCall {
key.FileName = ".../" + filepath.Base(key.FileName)
}
keyBytes, err := json.Marshal(key)
if err != nil {
return "", fmt.Errorf("failed to serialize params to json: %v", err)
}
return cacheKey(keyBytes), nil
}
func (e *EnvT) fixtureCallWrapper(key cacheKey, f FixtureCallbackFunc, opt *FixtureOptions) FixtureCallbackFunc {
return func() (res interface{}, err error) {
scopeName := scopeName(e.t.Name(), opt.Scope)
e.m.Lock()
si := e.scopes[scopeName]
e.m.Unlock()
if si == nil {
e.t.Fatalf("Unexpected scope. Create env for test %q", scopeName)
// not reachable
return nil, nil
}
defer func() {
si.AddKey(key)
}()
res, err = f()
if opt.CleanupFunc != nil {
si.t.Cleanup(opt.CleanupFunc)
}
return res, err
}
}
func scopeName(testName string, scope CacheScope) string {
switch scope {
case ScopePackage:
return packageScopeName
case ScopeTest:
return testName
case ScopeTestAndSubtests:
parts := strings.SplitN(testName, "/", 2)
return parts[0]
default:
panic(fmt.Sprintf("Unknown scope: %v", scope))
}
}