getTestPackageName returns the package name of the test that called it.
(t TBSubset)
| 195 | |
| 196 | // getTestPackageName returns the package name of the test that called it. |
| 197 | func 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 | } |