forked from joshdk/go-junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ingesters.go
99 lines (81 loc) · 2.26 KB
/
ingesters.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
// Copyright Josh Komoroske. All rights reserved.
// Use of this source code is governed by the MIT license,
// a copy of which can be found in the LICENSE.txt file.
// SPDX-License-Identifier: MIT
// Package junit exposes several library functions for the ingestion and
// parsing of JUnit XML reports.
package junit
import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
)
// IngestDir will search the given directory for XML files and return a slice
// of all contained JUnit test suite definitions.
func IngestDir(directory string) ([]Suite, error) {
var filenames []string
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Add all regular files that end with ".xml"
if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".xml") {
filenames = append(filenames, path)
}
return nil
})
if err != nil {
return nil, err
}
return IngestFiles(filenames)
}
// IngestFiles will parse the given XML files and return a slice of all
// contained JUnit test suite definitions.
func IngestFiles(filenames []string) ([]Suite, error) {
all := make([]Suite, 0)
for _, filename := range filenames {
suites, err := IngestFile(filename)
if err != nil {
return nil, err
}
all = append(all, suites...)
}
return all, nil
}
// IngestFile will parse the given XML file and return a slice of all contained
// JUnit test suite definitions.
func IngestFile(filename string) ([]Suite, error) {
file, err := os.Open(filename) //nolint:gosec
if err != nil {
return nil, err
}
defer file.Close() //nolint
return IngestReader(file)
}
// IngestReader will parse the given XML reader and return a slice of all
// contained JUnit test suite definitions.
func IngestReader(reader io.Reader) ([]Suite, error) {
var (
suiteChan = make(chan Suite)
suites = make([]Suite, 0)
)
nodes, err := parse(reader)
if err != nil {
return nil, err
}
go func() {
findSuites(nodes, suiteChan)
close(suiteChan)
}()
for suite := range suiteChan {
suites = append(suites, suite)
}
return suites, nil
}
// Ingest will parse the given XML data and return a slice of all contained
// JUnit test suite definitions.
func Ingest(data []byte) ([]Suite, error) {
return IngestReader(bytes.NewReader(data))
}