-
Notifications
You must be signed in to change notification settings - Fork 18
/
result.go
174 lines (144 loc) · 4.32 KB
/
result.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
package allure
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"testing"
"github.com/pkg/errors"
)
// result is the top level report object for a test
type result struct {
UUID string `json:"uuid,omitempty"`
TestCaseID string `json:"testCaseId,omitempty"`
HistoryID string `json:"historyId,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Status string `json:"status,omitempty"`
StatusDetails *statusDetails `json:"statusDetails,omitempty"`
Stage string `json:"stage,omitempty"`
Steps []stepObject `json:"steps,omitempty"`
Attachments []attachment `json:"attachments,omitempty"`
Parameters []parameter `json:"parameters,omitempty"`
Start int64 `json:"start,omitempty"`
Stop int64 `json:"stop,omitempty"`
Children []string `json:"children,omitempty"`
FullName string `json:"fullName,omitempty"`
Labels []label `json:"labels,omitempty"`
Links []link `json:"links,omitempty"`
Test func() `json:"-"`
}
func (r *result) addReason(reason string) {
testStatusDetails := r.StatusDetails
if testStatusDetails == nil {
r.StatusDetails = &statusDetails{}
}
r.StatusDetails.Message = reason
}
func (r *result) addDescription(description string) {
r.Description = description
}
func (r *result) addParameter(name string, value interface{}) {
r.Parameters = append(r.Parameters, parseParameter(name, value))
}
func (r *result) addParameters(parameters map[string]interface{}) {
for key, value := range parameters {
r.Parameters = append(r.Parameters, parseParameter(key, value))
}
}
func (r *result) addName(name string) {
r.Name = name
}
func (r *result) addAction(action func()) {
r.Test = action
}
type FailureMode string
func (r *result) getAttachments() []attachment {
return r.Attachments
}
func (r *result) addAttachment(attachment attachment) {
r.Attachments = append(r.Attachments, attachment)
}
func (r *result) getSteps() []stepObject {
return r.Steps
}
func (r *result) addStep(step stepObject) {
r.Steps = append(r.Steps, step)
}
func (r *result) addFullName(FullName string) {
r.FullName = FullName
}
func (r *result) setStatus(status string) {
r.Status = status
}
func (r *result) getStatus() string {
return r.Status
}
func (r *result) getStatusDetails() *statusDetails {
return r.StatusDetails
}
func (r *result) setStatusDetails(details statusDetails) {
r.StatusDetails = &details
}
func (r *result) setDefaultLabels(t *testing.T) {
wsd := os.Getenv(wsPathEnvKey)
programCounters := make([]uintptr, 10)
callersCount := runtime.Callers(0, programCounters)
var testFile string
for i := 0; i < callersCount; i++ {
_, testFile, _, _ = runtime.Caller(i)
if strings.Contains(testFile, "_test.go") {
break
}
}
testPackage := strings.TrimSuffix(strings.Replace(strings.TrimPrefix(testFile, wsd+"/"), "/", ".", -1), ".go")
r.addLabel("package", testPackage)
r.addLabel("testClass", testPackage)
r.addLabel("testMethod", t.Name())
if len(wsd) == 0 {
r.addFullName(fmt.Sprintf("%s:%s", testFile, t.Name()))
} else {
r.addFullName(fmt.Sprintf("%s:%s", strings.TrimPrefix(testFile, wsd+"/"), t.Name()))
}
if hostname, err := os.Hostname(); err == nil {
r.addLabel("host", hostname)
}
r.addLabel("language", "golang")
//TODO: these labels are available, but should be handled separately.
// Thread string
// Framework string
}
func (r *result) addLink(url, name string, linkType LinkType) {
r.Links = append(r.Links, link{
Url: url,
Name: name,
Type: linkType,
})
}
func (r *result) addLabel(name string, value string) {
r.Labels = append(r.Labels, label{
Name: name,
Value: value,
})
}
func (r *result) writeResultsFile() error {
createFolderOnce.Do(createFolderIfNotExists)
copyEnvFileOnce.Do(copyEnvFileIfExists)
j, err := json.Marshal(r)
if err != nil {
return errors.Wrap(err, "Failed to marshall result into JSON")
}
err = ioutil.WriteFile(fmt.Sprintf("%s/%s-result.json", resultsPath, r.UUID), j, 0777)
if err != nil {
return errors.Wrap(err, "Failed to write in file")
}
return nil
}
func newResult() *result {
return &result{
UUID: generateUUID(),
Start: getTimestampMs(),
}
}