forked from goddenrich/go-junit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathingesters.go
92 lines (75 loc) · 1.8 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
// 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.
package junit
import (
"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) {
var all []Suite
for _, filename := range filenames {
suites, err := ingestFile(filename)
if err != nil {
return nil, err
}
all = append(all, suites...)
}
return all, nil
}
func ingestFile(filename string) (s []Suite, err error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer func() {
cerr := f.Close()
if err == nil {
err = cerr
}
}()
return Ingest(f)
}
// Ingest will parse the given XML data and return a slice of all contained
// JUnit test suite definitions.
func Ingest(r io.Reader) ([]Suite, error) {
var (
suiteChan = make(chan Suite)
suites []Suite
)
nodes, err := parse(r)
if err != nil {
return nil, err
}
go func() {
findSuites(nodes, suiteChan)
close(suiteChan)
}()
for suite := range suiteChan {
suites = append(suites, suite)
}
return suites, nil
}