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

Commit 3c2d032

Browse files
committed
skip tests support
1 parent fd13fea commit 3c2d032

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
import (
4+
"sync"
5+
)
6+
7+
var (
8+
testsToSkip map[string]struct{}
9+
10+
m sync.RWMutex
11+
)
12+
13+
func SetFqnToSkip(fqns ...string) {
14+
m.Lock()
15+
defer m.Unlock()
16+
17+
testsToSkip = map[string]struct{}{}
18+
for _, val := range fqns {
19+
testsToSkip[val] = struct{}{}
20+
}
21+
}
22+
23+
func GetSkipMap() map[string]struct{} {
24+
m.RLock()
25+
defer m.RUnlock()
26+
27+
return testsToSkip
28+
}

instrumentation/testing/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ func Init(m *testing.M) {
3131
tests = append(tests, testing.InternalTest{
3232
Name: test.Name,
3333
F: func(t *testing.T) { // Creating a new test function as an indirection of the original test
34+
if shouldSkipTest(t, funcPointer) {
35+
return
36+
}
3437
addAutoInstrumentedTest(t)
3538
tStruct := StartTestFromCaller(t, funcPointer)
3639
defer tStruct.end()

instrumentation/testing/testing.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go.undefinedlabs.com/scopeagent/errors"
1919
"go.undefinedlabs.com/scopeagent/instrumentation"
2020
"go.undefinedlabs.com/scopeagent/instrumentation/logging"
21+
"go.undefinedlabs.com/scopeagent/instrumentation/testing/config"
2122
"go.undefinedlabs.com/scopeagent/reflection"
2223
"go.undefinedlabs.com/scopeagent/runner"
2324
"go.undefinedlabs.com/scopeagent/tags"
@@ -135,11 +136,19 @@ func (test *Test) Context() context.Context {
135136

136137
// Runs an auto instrumented sub test
137138
func (test *Test) Run(name string, f func(t *testing.T)) bool {
139+
pc, _, _, _ := runtime.Caller(1)
138140
if test.span == nil { // No span = not instrumented
139-
return test.t.Run(name, f)
141+
return test.t.Run(name, func(cT *testing.T) {
142+
if shouldSkipTest(cT, pc) {
143+
return
144+
}
145+
f(cT)
146+
})
140147
}
141-
pc, _, _, _ := runtime.Caller(1)
142148
return test.t.Run(name, func(childT *testing.T) {
149+
if shouldSkipTest(childT, pc) {
150+
return
151+
}
143152
addAutoInstrumentedTest(childT)
144153
childTest := StartTestFromCaller(childT, pc)
145154
defer childTest.end()
@@ -252,3 +261,28 @@ func addAutoInstrumentedTest(t *testing.T) {
252261
defer autoInstrumentedTestsMutex.Unlock()
253262
autoInstrumentedTests[t] = true
254263
}
264+
265+
// Should skip test
266+
func shouldSkipTest(t *testing.T, pc uintptr) bool {
267+
fullTestName := runner.GetOriginalTestName(t.Name())
268+
testNameSlash := strings.IndexByte(fullTestName, '/')
269+
funcName := fullTestName
270+
if testNameSlash >= 0 {
271+
funcName = fullTestName[:testNameSlash]
272+
}
273+
274+
funcFullName := runtime.FuncForPC(pc).Name()
275+
funcNameIndex := strings.LastIndex(funcFullName, funcName)
276+
if funcNameIndex < 1 {
277+
funcNameIndex = len(funcFullName)
278+
}
279+
packageName := funcFullName[:funcNameIndex-1]
280+
281+
fqn := fmt.Sprintf("%s.%s", packageName, fullTestName)
282+
skipMap := config.GetSkipMap()
283+
if _, ok := skipMap[fqn]; ok {
284+
reflection.SkipAndFinishTest(t)
285+
return true
286+
}
287+
return false
288+
}

reflection/reflect.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,17 @@ func GetIsParallel(t *testing.T) bool {
4646
}
4747
return false
4848
}
49+
50+
func SkipAndFinishTest(t *testing.T) {
51+
mu := GetTestMutex(t)
52+
if mu != nil {
53+
mu.Lock()
54+
defer mu.Unlock()
55+
}
56+
if pointer, err := GetFieldPointerOf(t, "skipped"); err == nil {
57+
*(*bool)(pointer) = true
58+
}
59+
if pointer, err := GetFieldPointerOf(t, "finished"); err == nil {
60+
*(*bool)(pointer) = true
61+
}
62+
}

0 commit comments

Comments
 (0)