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

Commit b168cb4

Browse files
committed
initial
1 parent 977e2a1 commit b168cb4

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
@@ -1,9 +1,13 @@
11
package testing
22

33
import (
4+
"os"
45
"reflect"
6+
"runtime"
57
"testing"
68

9+
"github.com/undefinedlabs/go-mpatch"
10+
711
"go.undefinedlabs.com/scopeagent/reflection"
812
)
913

@@ -43,4 +47,28 @@ func Init(m *testing.M) {
4347
}
4448
*intBenchmarks = benchmarks
4549
}
50+
51+
if envDMPatch, set := os.LookupEnv("SCOPE_DISABLE_MONKEY_PATCHING"); !set || envDMPatch == "" {
52+
// We monkey patch the `testing.T.Run()` func to auto instrument sub tests
53+
var t *testing.T
54+
tType := reflect.TypeOf(t)
55+
if tRunMethod, ok := tType.MethodByName("Run"); ok {
56+
var runPatch *mpatch.Patch
57+
var err error
58+
runPatch, err = mpatch.PatchMethodByReflect(tRunMethod, func(t *testing.T, name string, f func(t *testing.T)) bool {
59+
logOnError(runPatch.Unpatch())
60+
defer func() {
61+
logOnError(runPatch.Patch())
62+
}()
63+
pc, _, _, _ := runtime.Caller(1)
64+
return t.Run(name, func(childT *testing.T) {
65+
addAutoInstrumentedTest(childT)
66+
childTest := StartTestFromCaller(childT, pc)
67+
defer childTest.end()
68+
f(childT)
69+
})
70+
})
71+
logOnError(err)
72+
}
73+
}
4674
}

0 commit comments

Comments
 (0)