-
Notifications
You must be signed in to change notification settings - Fork 4
/
testrail.go
201 lines (162 loc) · 3.98 KB
/
testrail.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package insrail
import (
"bytes"
"regexp"
"runtime"
"strings"
"sync"
"github.com/insolar/assured-ledger/ledger-core/log"
"github.com/insolar/assured-ledger/ledger-core/log/global"
"github.com/insolar/assured-ledger/ledger-core/log/logcommon"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
var skipList = sync.Map{}
type Testing interface {
Name() string
Failed() bool
Skipped() bool
Log(args ...interface{})
Skip(args ...interface{})
Helper()
Cleanup(func())
}
var (
panicSymptom1 = []byte("/usr/local/go/src/runtime/panic.go")
panicSymptom2 = []byte("panic(")
)
func checkPanicInStack() bool {
buf := make([]byte, 65536)
for {
n := runtime.Stack(buf, false)
if n < len(buf) {
buf = buf[:n]
break
}
buf = make([]byte, 2*len(buf))
}
return bytes.Contains(buf, panicSymptom1) && bytes.Contains(buf, panicSymptom2)
}
var trimLambdaSuffixRe = regexp.MustCompile(`(.*)\.func(?:[\d]+\.)*[\d]+`)
func trimLambdaSuffix(fullName string) string {
pos := trimLambdaSuffixRe.FindStringSubmatch(fullName)
if len(pos) > 0 {
return pos[1]
}
return fullName
}
func trimFunctionName(fullName string) string {
nameParts := strings.Split(fullName, ".")
return strings.Join(nameParts[:len(nameParts)-1], ".")
}
func lastNotInternalPC() uintptr {
var lastNotInternalPC uintptr
for i := 0; ; i++ {
pc, file, _, ok := runtime.Caller(i)
if !ok {
return lastNotInternalPC
}
if strings.HasSuffix(file, "src/testing/testing.go") ||
strings.Contains(file, "src/runtime") {
continue
} else {
lastNotInternalPC = pc
}
}
}
func getTestingPackage() string {
pc := lastNotInternalPC()
if pc == 0 {
panic(throw.IllegalState())
}
name := runtime.FuncForPC(pc).Name()
name = trimLambdaSuffix(name)
name = trimFunctionName(name)
return name
}
type logCaseHeader struct {
*log.Msg `txt:"testrail"`
ID string
TestPackage string
TestName string
}
func (h logCaseHeader) ConstructFooter() logCaseFooter {
return logCaseFooter{
ID: h.ID,
TestPackage: h.TestPackage,
TestName: h.TestName,
}
}
type logCaseFooter struct {
*log.Msg `txt:"testrail"`
ID string
TestPackage string
TestName string
Status string
SkippedLink string `opt:""`
}
func LogSkip(target Testing, jiraLink string) {
target.Helper()
skipList.Store(target.Name(), jiraLink)
target.Skip(jiraLink)
}
func logCaseConstructor(target Testing, footer logCaseFooter) func() {
return func() {
if footer.Status == "" {
switch {
case target.Skipped():
footer.Status = "SKIP"
if skippedLink, present := skipList.Load(target.Name()); present {
footer.SkippedLink = skippedLink.(string)
}
case target.Failed():
footer.Status = "FAIL"
case checkPanicInStack():
footer.Status = "FAIL"
default:
footer.Status = "PASS"
}
}
// this output will be made at end of the test, just before FAIL/PASS/SKIP mark
global.Logger().Event(logcommon.NoLevel, footer)
}
}
type Option interface {
modifyConfig(target Testing, footer *logCaseFooter)
applyAfter(target Testing)
}
type SkipOption struct {
link string
}
func Skip(jiraLink string) Option {
return SkipOption{link: jiraLink}
}
func (s SkipOption) modifyConfig(_ Testing, footer *logCaseFooter) {
footer.Status = "SKIP"
footer.SkippedLink = s.link
}
func (s SkipOption) applyAfter(target Testing) {
target.Skip(s.link)
}
func LogCaseExt(target Testing, name string, opts ...Option) {
target.Helper()
header := logCaseHeader{
ID: name,
TestName: target.Name(),
TestPackage: getTestingPackage(),
}
global.Logger().Event(logcommon.NoLevel, header)
footer := header.ConstructFooter()
for _, opt := range opts {
opt.modifyConfig(target, &footer)
}
target.Cleanup(logCaseConstructor(target, footer))
for _, opt := range opts {
opt.applyAfter(target)
}
}
func LogCase(target Testing, name string) {
LogCaseExt(target, name)
}
func LogSkipCase(target Testing, name string, jiraLink string) {
LogCaseExt(target, name, Skip(jiraLink))
}