...
1
2
3
4
5 package junit
6
7 import (
8 "bytes"
9 "io"
10 "os"
11 "path/filepath"
12 "strings"
13 )
14
15
16
17 func IngestDir(directory string) ([]Suite, error) {
18 var filenames []string
19
20 err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
21 if err != nil {
22 return err
23 }
24
25
26 if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".xml") {
27 filenames = append(filenames, path)
28 }
29
30 return nil
31 })
32
33 if err != nil {
34 return nil, err
35 }
36
37 return IngestFiles(filenames)
38 }
39
40
41
42 func IngestFiles(filenames []string) ([]Suite, error) {
43 var all = make([]Suite, 0)
44
45 for _, filename := range filenames {
46 suites, err := IngestFile(filename)
47 if err != nil {
48 return nil, err
49 }
50 all = append(all, suites...)
51 }
52
53 return all, nil
54 }
55
56
57
58 func IngestFile(filename string) ([]Suite, error) {
59 file, err := os.Open(filename)
60 if err != nil {
61 return nil, err
62 }
63 defer file.Close()
64
65 return IngestReader(file)
66 }
67
68
69
70 func IngestReader(reader io.Reader) ([]Suite, error) {
71 var (
72 suiteChan = make(chan Suite)
73 suites = make([]Suite, 0)
74 )
75
76 nodes, err := parse(reader)
77 if err != nil {
78 return nil, err
79 }
80
81 go func() {
82 findSuites(nodes, suiteChan)
83 close(suiteChan)
84 }()
85
86 for suite := range suiteChan {
87 suites = append(suites, suite)
88 }
89
90 return suites, nil
91 }
92
93
94
95 func Ingest(data []byte) ([]Suite, error) {
96 return IngestReader(bytes.NewReader(data))
97 }
98
View as plain text