-
Notifications
You must be signed in to change notification settings - Fork 18
/
error.go
74 lines (66 loc) · 2.31 KB
/
error.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
package allure
import (
"runtime/debug"
"strings"
"testing"
)
// Fail sets the current step as well as the entire test script as failed and stores the stack trace in the result object.
// Script execution is not interrupted, script would still run after this call.
func Fail(err error) {
allureError(err, "failed", false)
}
// FailNow sets the current step as well as the entire test script as failed and stores the stack trace in the result object.
// Script execution is interrupted, script stops immediately.
func FailNow(err error) {
allureError(err, "failed", true)
}
// Break sets the current step as well as the entire test script as broken and stores the stack trace in the result object.
// Script execution is not interrupted, script would still run after this call.
func Break(err error) {
allureError(err, "broken", false)
}
// BreakNow sets the current step as well as the entire test script as broken and stores the stack trace in the result object.
// Script execution is interrupted, script stops immediately.
func BreakNow(err error) {
allureError(err, "broken", true)
}
func allureError(err error, status string, now bool) {
manipulateOnObjectFromCtx(
testResultKey,
func(testResult interface{}) {
testStatusDetails := testResult.(hasStatusDetails).getStatusDetails()
if testStatusDetails == nil {
testStatusDetails = &statusDetails{}
}
testStatusDetails.Trace = filterStackTrace(debug.Stack())
testStatusDetails.Message = err.Error()
testResult.(hasStatusDetails).setStatusDetails(*testStatusDetails)
testResult.(hasStatus).setStatus(status)
})
manipulateOnObjectFromCtx(
nodeKey,
func(node interface{}) {
node.(hasStatus).setStatus(status)
})
manipulateOnObjectFromCtx(
testInstanceKey,
func(testInstance interface{}) {
testInstance.(*testing.T).Fail()
if now {
panic(err)
}
})
}
func filterStackTrace(stack []byte) string {
stringTraces := strings.Split(string(stack), "\n")
result := stringTraces[0] + "\n"
for i := 1; i+1 < len(stringTraces); i = i + 2 {
// for vendored code calls
if !strings.Contains(stringTraces[i+1], "allure-go/vendor/") &&
// for allure-go specific function calls
!strings.HasPrefix(stringTraces[i], "github.com/dailymotion/allure-go.") {
result += stringTraces[i] + "\n" + stringTraces[i+1] + "\n"
}
}
return result
}