-
Notifications
You must be signed in to change notification settings - Fork 18
/
step.go
156 lines (132 loc) · 3.83 KB
/
step.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
package allure
import (
"log"
"testing"
"github.com/pkg/errors"
"github.com/jtolds/gls"
)
type stepObject struct {
Name string `json:"name,omitempty"`
Status string `json:"status,omitempty"`
StatusDetails *statusDetails `json:"statusDetails,omitempty"`
Stage string `json:"stage"`
ChildrenSteps []stepObject `json:"steps"`
Attachments []attachment `json:"attachments"`
Parameters []parameter `json:"parameters"`
Start int64 `json:"start"`
Stop int64 `json:"stop"`
Action func() `json:"-"`
}
func (s *stepObject) addReason(reason string) {
testStatusDetails := s.StatusDetails
if testStatusDetails == nil {
s.StatusDetails = &statusDetails{}
}
s.StatusDetails.Message = reason
}
func (s *stepObject) addLink(url, name string, linkType LinkType) {
// Step doesn't have links
}
func (s *stepObject) addLabel(key string, value string) {
// Step doesn't have labels
}
func (s *stepObject) addDescription(description string) {
s.Name = description
}
func (s *stepObject) addParameter(name string, value interface{}) {
s.Parameters = append(s.Parameters, parseParameter(name, value))
}
func (s *stepObject) addParameters(parameters map[string]interface{}) {
for key, value := range parameters {
s.Parameters = append(s.Parameters, parseParameter(key, value))
}
}
func (s *stepObject) addName(name string) {
s.Name = name
}
func (s *stepObject) addAction(action func()) {
s.Action = action
}
func (s *stepObject) getSteps() []stepObject {
return s.ChildrenSteps
}
func (s *stepObject) addStep(step stepObject) {
s.ChildrenSteps = append(s.ChildrenSteps, step)
}
func (s *stepObject) getAttachments() []attachment {
return s.Attachments
}
func (s *stepObject) addAttachment(attachment attachment) {
s.Attachments = append(s.Attachments, attachment)
}
func (s *stepObject) setStatus(status string) {
s.Status = status
}
func (s *stepObject) getStatus() string {
return s.Status
}
// SkipStep doesn't execute the action and marks the step as skipped in report
// Reason won't appear in report until https://github.com/allure-framework/allure2/issues/774 is fixed
func SkipStep(stepOptions ...Option) {
stepObject := newStep()
stepObject.Start = getTimestampMs()
for _, option := range stepOptions {
option(stepObject)
}
stepObject.Status = "skipped"
stepObject.Stage = "finished"
stepObject.Stop = getTimestampMs()
if currentStepObj, ok := ctxMgr.GetValue(nodeKey); ok {
currentStep := currentStepObj.(hasSteps)
currentStep.addStep(*stepObject)
} else {
log.Fatalln("could not retrieve current allure node")
}
}
// Step is meant to be wrapped around actions
func Step(stepOptions ...Option) {
stepObject := newStep()
stepObject.Start = getTimestampMs()
for _, option := range stepOptions {
option(stepObject)
}
if stepObject.Action == nil {
stepObject.Action = func() {}
}
defer func() {
panicObject := recover()
stepObject.Stop = getTimestampMs()
manipulateOnObjectFromCtx(
testInstanceKey,
func(testInstance interface{}) {
if panicObject != nil {
Break(errors.Errorf("%+v", panicObject))
}
if testInstance.(*testing.T).Failed() ||
panicObject != nil {
if stepObject.Status == "" {
stepObject.Status = "failed"
}
}
})
stepObject.Stage = "finished"
if stepObject.Status == "" {
stepObject.Status = "passed"
}
manipulateOnObjectFromCtx(nodeKey, func(currentStepObj interface{}) {
currentStep := currentStepObj.(hasSteps)
currentStep.addStep(*stepObject)
})
if panicObject != nil {
panic(panicObject)
}
}()
ctxMgr.SetValues(gls.Values{nodeKey: stepObject}, stepObject.Action)
}
func newStep() *stepObject {
return &stepObject{
Attachments: make([]attachment, 0),
ChildrenSteps: make([]stepObject, 0),
Parameters: make([]parameter, 0),
}
}