/
skip.go
111 lines (88 loc) 路 2.12 KB
/
skip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package snaps
import (
"go/ast"
"go/parser"
"go/token"
"path"
"regexp"
"strings"
"github.com/gkampitakis/go-snaps/internal/colors"
)
var (
skippedTests = newSyncSlice()
skippedMsg = colors.Sprint(colors.Yellow, skipSymbol+"Snapshot skipped")
)
// Wrapper of testing.Skip
//
// Keeps track which snapshots are getting skipped and not marked as obsolete.
func Skip(t testingT, args ...any) {
t.Helper()
trackSkip(t)
t.Skip(args...)
}
// Wrapper of testing.Skipf
//
// Keeps track which snapshots are getting skipped and not marked as obsolete.
func Skipf(t testingT, format string, args ...any) {
t.Helper()
trackSkip(t)
t.Skipf(format, args...)
}
// Wrapper of testing.SkipNow
//
// Keeps track which snapshots are getting skipped and not marked as obsolete.
func SkipNow(t testingT) {
t.Helper()
trackSkip(t)
t.SkipNow()
}
func trackSkip(t testingT) {
t.Helper()
t.Log(skippedMsg)
skippedTests.append(t.Name())
}
/*
This checks if the parent test is skipped,
or provided a 'runOnly' the testID is part of it
e.g
func TestParallel (t *testing.T) {
snaps.Skip(t)
...
}
Then every "child" test should be skipped
*/
func testSkipped(testID, runOnly string) bool {
// testID form: Test.*/runName - 1
testName := strings.Split(testID, " - ")[0]
for _, name := range skippedTests.values {
if testName == name || strings.HasPrefix(testName, name+"/") {
return true
}
}
matched, _ := regexp.MatchString(runOnly, testID)
return !matched
}
func isFileSkipped(dir, filename, runOnly string) bool {
// When a file is skipped through CLI with -run flag we can track it
if runOnly == "" {
return false
}
testFilePath := path.Join(dir, "..", strings.TrimSuffix(filename, snapsExt)+".go")
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, testFilePath, nil, parser.ParseComments)
if err != nil {
return false
}
for _, decls := range file.Decls {
funcDecl, ok := decls.(*ast.FuncDecl)
if !ok {
continue
}
// If the TestFunction is inside the file then it's not skipped
matched, _ := regexp.MatchString(runOnly, funcDecl.Name.String())
if matched {
return false
}
}
return true
}