MCPcopy Index your code
hub / github.com/coder/coder / getTestPackageName

Function getTestPackageName

coderd/database/dbtestutil/broker.go:197–233  ·  view source on GitHub ↗

getTestPackageName returns the package name of the test that called it.

(t TBSubset)

Source from the content-addressed store, hash-verified

195
196// getTestPackageName returns the package name of the test that called it.
197func getTestPackageName(t TBSubset) string {
198 packageName := "unknown"
199 // Ask runtime.Callers for up to 100 program counters, including runtime.Callers itself.
200 pc := make([]uintptr, 100)
201 n := runtime.Callers(0, pc)
202 if n == 0 {
203 // No PCs available. This can happen if the first argument to
204 // runtime.Callers is large.
205 //
206 // Return now to avoid processing the zero Frame that would
207 // otherwise be returned by frames.Next below.
208 t.Logf("could not determine test package name: no PCs available")
209 return packageName
210 }
211
212 pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
213 frames := runtime.CallersFrames(pc)
214
215 // Loop to get frames.
216 // A fixed number of PCs can expand to an indefinite number of Frames.
217 for {
218 frame, more := frames.Next()
219
220 if strings.HasPrefix(frame.Function, "github.com/coder/coder/v2/") {
221 packageName = strings.SplitN(strings.TrimPrefix(frame.Function, "github.com/coder/coder/v2/"), ".", 2)[0]
222 }
223 if strings.HasPrefix(frame.Function, "testing") {
224 break
225 }
226
227 // Check whether there are more frames to process after this one.
228 if !more {
229 break
230 }
231 }
232 return packageName
233}

Callers 2

CreateMethod · 0.85
TestGetTestPackageNameFunction · 0.85

Calls 2

LogfMethod · 0.65
NextMethod · 0.65

Tested by 1

TestGetTestPackageNameFunction · 0.68