-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
327 lines (292 loc) · 8.88 KB
/
parser.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"errors"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"strconv"
"github.com/harness-community/parse-test-reports/gojunit"
"github.com/mattn/go-zglob"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
func getPaths(globVal string) []string {
paths := make([]string, 0)
globValSplit := strings.Split(globVal, ",")
for _, val := range globValSplit {
if val == "" {
continue
}
val = strings.TrimSpace(val)
paths = append(paths, val)
}
return paths
}
// ParseTests parses XMLs and returns error if there are any failures
func ParseTests(paths []string, log *logrus.Logger) (TestStats, error) {
files := getFiles(paths, log)
stats := TestStats{}
if len(files) == 0 {
log.Errorln("could not find any files matching the provided report path")
return stats, nil
}
for _, file := range files {
suites, err := gojunit.IngestFile(file)
if err != nil {
log.WithError(err).WithField("file", file).Errorln("could not parse file")
continue
}
fileStats := TestStats{}
for _, suite := range suites {
for _, test := range suite.Tests {
fileStats.TestCount++
switch test.Result.Status {
case "passed":
fileStats.PassCount++
case "failed":
fileStats.FailCount++
case "skipped":
fileStats.SkippedCount++
case "error":
fileStats.ErrorCount++
}
}
}
log.WithFields(logrus.Fields{
"file": file,
"total": fileStats.TestCount,
"passed": fileStats.PassCount,
"failed": fileStats.FailCount,
"skipped": fileStats.SkippedCount,
"errors": fileStats.ErrorCount,
}).Infoln("File processed")
// Aggregate stats
stats.TestCount += fileStats.TestCount
stats.PassCount += fileStats.PassCount
stats.FailCount += fileStats.FailCount
stats.SkippedCount += fileStats.SkippedCount
stats.ErrorCount += fileStats.ErrorCount
}
if stats.FailCount > 0 || stats.ErrorCount > 0 {
return stats, errors.New("failed tests and errors found")
}
return stats, nil
}
// getFiles returns unique file paths after expanding the input paths
func getFiles(paths []string, log *logrus.Logger) []string {
var files []string
for _, p := range paths {
path, err := expandTilde(p)
if err != nil {
log.WithError(err).WithField("path", p).Errorln("error expanding path")
continue
}
matches, err := zglob.Glob(path)
if err != nil {
log.WithError(err).WithField("path", path).Errorln("error resolving path regex")
continue
}
files = append(files, matches...)
}
return uniqueItems(files)
}
func uniqueItems(items []string) []string {
var result []string
set := make(map[string]bool)
for _, item := range items {
if !set[item] {
result = append(result, item)
set[item] = true
}
}
return result
}
// expandTilde expands the given path to include the home directory if prefixed with `~`.
func expandTilde(path string) (string, error) {
if path == "" {
return path, nil
}
if path[0] != '~' {
return path, nil
}
if len(path) > 1 && path[1] != '/' && path[1] != '\\' {
return "", errors.New("cannot expand user-specific home dir")
}
dir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(dir, path[1:]), nil
}
// LoadYAML reads a YAML file from either a URL or a local file
func LoadYAML(source string) (map[string]interface{}, error) {
log := logrus.New()
log.Infoln("Loading YAML from source:", source)
var data []byte
var err error
if isURL(source) {
resp, err := http.Get(source)
if err != nil {
log.WithError(err).Errorln("Failed to fetch YAML from URL")
return nil, err
}
defer resp.Body.Close()
data, err = io.ReadAll(resp.Body)
if err != nil {
log.WithError(err).Errorln("Failed to read YAML data from URL")
return nil, err
}
} else {
data, err = os.ReadFile(source)
if err != nil {
log.WithError(err).Errorln("Failed to read local YAML file")
return nil, err
}
}
var result map[string]interface{}
err = yaml.Unmarshal(data, &result)
if err != nil {
log.WithError(err).Errorln("Failed to parse YAML")
return nil, err
}
log.Infoln("Successfully loaded and parsed YAML")
return result, nil
}
func isURL(source string) bool {
return strings.HasPrefix(source, "http")
}
// ParseTestsWithQuarantine parses XMLs, considers quarantined tests, and returns errors if any non-quarantined failures are found
func ParseTestsWithQuarantine(paths []string, quarantineList map[string]interface{}, log *logrus.Logger) (TestStats, error) {
files := getFiles(paths, log)
stats := TestStats{}
nonQuarantinedFailures := 0
expiredTests := 0
if len(files) == 0 {
log.Errorln("could not find any files matching the provided report path")
return stats, nil
}
log.Infoln("Starting to parse tests with quarantine list")
for _, file := range files {
suites, err := gojunit.IngestFile(file)
if err != nil {
log.WithError(err).WithField("file", file).Errorln("could not parse file")
continue
}
fileStats := TestStats{}
for _, suite := range suites {
for _, test := range suite.Tests {
fileStats.TestCount++
testIdentifier := test.Classname + "." + test.Name
switch test.Result.Status {
case "passed":
fileStats.PassCount++
case "failed":
if !isQuarantined(testIdentifier, quarantineList, log) {
log.Infoln("Not Quarantined test failed:", testIdentifier)
nonQuarantinedFailures++
} else if isExpired(testIdentifier, quarantineList, log) {
log.Infoln("Quarantined test expired:", testIdentifier)
expiredTests++
}
fileStats.FailCount++
case "skipped":
fileStats.SkippedCount++
case "error":
fileStats.ErrorCount++
}
}
}
log.WithFields(logrus.Fields{
"file": file,
"total": fileStats.TestCount,
"passed": fileStats.PassCount,
"failed": fileStats.FailCount,
"skipped": fileStats.SkippedCount,
"errors": fileStats.ErrorCount,
}).Infoln("File processed")
stats.TestCount += fileStats.TestCount
stats.PassCount += fileStats.PassCount
stats.FailCount += fileStats.FailCount
stats.SkippedCount += fileStats.SkippedCount
stats.ErrorCount += fileStats.ErrorCount
}
if nonQuarantinedFailures > 0 || expiredTests > 0 {
// Construct the error message by concatenating string values
errorMessage := "Non-quarantined failures: " + strconv.Itoa(nonQuarantinedFailures) +
", Expired tests: " + strconv.Itoa(expiredTests) + " found"
return stats, errors.New(errorMessage)
}
return stats, nil
}
func isQuarantined(testIdentifier string, quarantineList map[string]interface{}, log *logrus.Logger) bool {
log.Infoln("Checking if test is quarantined:", testIdentifier)
tests, ok := quarantineList["quarantine_tests"].([]interface{})
if !ok {
log.Warnln("Quarantine list invalid or missing 'quarantine_tests'")
return false
}
for _, test := range tests {
if testMap, ok := test.(map[interface{}]interface{}); ok {
if quarantinedIdentifier, found := matchTestIdentifier(testMap, testIdentifier, log); found {
log.Infoln("Test is quarantined:", quarantinedIdentifier)
return true
}
}
}
log.Infoln("Test is not quarantined:", testIdentifier)
return false
}
func isExpired(testIdentifier string, quarantineList map[string]interface{}, log *logrus.Logger) bool {
tests, ok := quarantineList["quarantine_tests"].([]interface{})
if !ok {
log.Warnln("Quarantine list invalid or missing 'quarantine_tests'")
return false
}
for _, test := range tests {
if testMap, ok := test.(map[interface{}]interface{}); ok {
if quarantinedIdentifier, found := matchTestIdentifier(testMap, testIdentifier, log); found {
startDate, startOk := testMap["start_date"].(string)
endDate, endOk := testMap["end_date"].(string)
if startOk && endOk {
currentDate := time.Now()
startTime, err := time.Parse("2006-01-02", startDate)
if err != nil {
log.WithError(err).Warnln("Failed to parse start_date")
continue
}
endTime, err := time.Parse("2006-01-02", endDate)
if err != nil {
log.WithError(err).Warnln("Failed to parse end_date")
continue
}
if currentDate.Before(startTime) || currentDate.After(endTime) {
log.WithFields(logrus.Fields{
"test": quarantinedIdentifier,
"currentDate": currentDate,
"startDate": startTime,
"endDate": endTime,
}).Infoln("Current Date lies outside start_date and end_date.")
return true
}
}
}
}
}
log.Infoln("Test has no expiration set:", testIdentifier)
return false
}
func matchTestIdentifier(testMap map[interface{}]interface{}, identifier string, log *logrus.Logger) (string, bool) {
quarantinedClassname, classnameOk := testMap["classname"].(string)
quarantinedName, nameOk := testMap["name"].(string)
if classnameOk && nameOk {
quarantinedIdentifier := quarantinedClassname + "." + quarantinedName
if quarantinedIdentifier == identifier {
log.Infoln("Test", identifier, "is quarantined")
return quarantinedIdentifier, true
}
}
return "", false
}