-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathresults.go
More file actions
175 lines (161 loc) · 4 KB
/
Copy pathresults.go
File metadata and controls
175 lines (161 loc) · 4 KB
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
// Copyright the Open Container Initiative Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
"time"
)
type results struct {
Name string // name of current runner step, concatenated onto the parent's name
Children []*results
Parent *results
Status status
Errs []error
Output *bytes.Buffer
Start time.Time
Stop time.Time
Counts [statusMax]int
}
func resultsNew(name string, parent *results) *results {
fullName := name
if parent != nil && parent.Name != "" {
fullName = fmt.Sprintf("%s/%s", parent.Name, name)
}
return &results{
Name: fullName,
Parent: parent,
Output: &bytes.Buffer{},
Start: time.Now(),
}
}
func (r *results) Count(s string) int {
st := statusUnknown
err := st.UnmarshalText([]byte(s))
if err != nil || st < 0 || st >= statusMax {
return -1
}
return r.Counts[st]
}
func (r *results) ReportWalkErr(w io.Writer, prefix string) {
_, _ = fmt.Fprintf(w, "%s%s: %s\n", prefix, r.Name, r.Status)
if len(r.Children) == 0 && len(r.Errs) > 0 {
// show errors from leaf nodes
for _, err := range r.Errs {
_, _ = fmt.Fprintf(w, "%s - %s\n", prefix, err.Error())
}
}
if len(r.Children) > 0 {
for _, child := range r.Children {
child.ReportWalkErr(w, prefix+" ")
}
}
}
func (r *results) ToJunitTestCases() []junitTest {
jTests := []junitTest{}
if len(r.Children) == 0 {
// return the test case for a leaf node
jTest := junitTest{
Name: r.Name,
Time: fmt.Sprintf("%f", r.Stop.Sub(r.Start).Seconds()),
SystemErr: r.Output.String(),
Status: r.Status.ToJunit(),
}
if len(r.Errs) > 0 {
jTest.SystemOut = fmt.Sprintf("%v", errors.Join(r.Errs...))
}
jTests = append(jTests, jTest)
}
if len(r.Children) > 0 {
// recursively collect test cases from child nodes
for _, child := range r.Children {
jTests = append(jTests, child.ToJunitTestCases()...)
}
}
return jTests
}
type status int
const (
statusUnknown status = iota // status is undefined
statusDisabled // test was disabled by configuration
statusSkip // test was skipped
statusPass // test passed
statusFail // test detected a conformance failure
statusError // failure of the test engine itself
statusMax // only used for allocating arrays
)
func (s status) Set(set status) status {
// only set status to a higher level
if set > s {
return set
}
return s
}
func (s status) String() string {
switch s {
case statusPass:
return "Pass"
case statusSkip:
return "Skip"
case statusDisabled:
return "Disabled"
case statusFail:
return "FAIL"
case statusError:
return "Error"
default:
return "Unknown"
}
}
func (s status) MarshalText() ([]byte, error) {
ret := s.String()
if ret == "Unknown" {
return []byte(ret), fmt.Errorf("unknown status %d", s)
}
return []byte(ret), nil
}
func (s *status) UnmarshalText(text []byte) error {
switch strings.ToLower(string(text)) {
case "pass":
*s = statusPass
case "skip":
*s = statusSkip
case "disabled":
*s = statusDisabled
case "fail":
*s = statusFail
case "error":
*s = statusError
case "unknown":
*s = statusUnknown
default:
return fmt.Errorf("unknown status %s", string(text))
}
return nil
}
func (s status) ToJunit() string {
switch s {
case statusPass:
return junitPassed
case statusSkip, statusDisabled:
return junitSkipped
case statusFail:
return junitFailure
default:
return junitError
}
}