Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proc/test: replace old func testName with T.Name #3347

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions pkg/proc/test/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,34 +237,12 @@ func RunTestsWithFixtures(m *testing.M) int {
var recordingAllowed = map[string]bool{}
var recordingAllowedMu sync.Mutex

// testName returns the name of the test being run using runtime.Caller.
// On go1.8 t.Name() could be called instead, this is a workaround to
// support <=go1.7
func testName(t testing.TB) string {
for i := 1; i < 10; i++ {
pc, _, _, ok := runtime.Caller(i)
if !ok {
break
}
fn := runtime.FuncForPC(pc)
if fn == nil {
continue
}
name := fn.Name()
v := strings.Split(name, ".")
if strings.HasPrefix(v[len(v)-1], "Test") {
return name
}
}
return "unknown"
}

// AllowRecording allows the calling test to be used with a recording of the
// fixture.
func AllowRecording(t testing.TB) {
recordingAllowedMu.Lock()
defer recordingAllowedMu.Unlock()
name := testName(t)
name := t.Name()
t.Logf("enabling recording for %s", name)
recordingAllowed[name] = true
}
Expand Down Expand Up @@ -292,7 +270,7 @@ func AllowRecording(t testing.TB) {
func MustHaveRecordingAllowed(t testing.TB) {
recordingAllowedMu.Lock()
defer recordingAllowedMu.Unlock()
name := testName(t)
name := t.Name()
if !recordingAllowed[name] {
t.Skipf("recording not allowed for %s", name)
}
Expand Down