Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit c046b83

Browse files
committed
initial
1 parent b1a706b commit c046b83

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

instrumentation/testing/init.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ package testing
22

33
import (
44
"flag"
5+
"os"
56
"reflect"
7+
"runtime"
68
"testing"
79

10+
"github.com/undefinedlabs/go-mpatch"
11+
812
"go.undefinedlabs.com/scopeagent/instrumentation"
913
"go.undefinedlabs.com/scopeagent/reflection"
1014
)
@@ -56,4 +60,28 @@ func Init(m *testing.M) {
5660
}
5761
*intBenchmarks = benchmarks
5862
}
63+
64+
if envDMPatch, set := os.LookupEnv("SCOPE_DISABLE_MONKEY_PATCHING"); !set || envDMPatch == "" {
65+
// We monkey patch the `testing.T.Run()` func to auto instrument sub tests
66+
var t *testing.T
67+
tType := reflect.TypeOf(t)
68+
if tRunMethod, ok := tType.MethodByName("Run"); ok {
69+
var runPatch *mpatch.Patch
70+
var err error
71+
runPatch, err = mpatch.PatchMethodByReflect(tRunMethod, func(t *testing.T, name string, f func(t *testing.T)) bool {
72+
logOnError(runPatch.Unpatch())
73+
defer func() {
74+
logOnError(runPatch.Patch())
75+
}()
76+
pc, _, _, _ := runtime.Caller(1)
77+
return t.Run(name, func(childT *testing.T) {
78+
addAutoInstrumentedTest(childT)
79+
childTest := StartTestFromCaller(childT, pc)
80+
defer childTest.end()
81+
f(childT)
82+
})
83+
})
84+
logOnError(err)
85+
}
86+
}
5987
}

0 commit comments

Comments
 (0)