Skip to content

Commit 93e3ed9

Browse files
authored
Refactor to enable ingesting a reader directly (#34)
1 parent f9efe7c commit 93e3ed9

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

ingesters.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
package junit
66

77
import (
8-
"io/ioutil"
8+
"bytes"
9+
"io"
910
"os"
1011
"path/filepath"
1112
"strings"
@@ -55,23 +56,24 @@ func IngestFiles(filenames []string) ([]Suite, error) {
5556
// IngestFile will parse the given XML file and return a slice of all contained
5657
// JUnit test suite definitions.
5758
func IngestFile(filename string) ([]Suite, error) {
58-
data, err := ioutil.ReadFile(filename)
59+
file, err := os.Open(filename)
5960
if err != nil {
6061
return nil, err
6162
}
63+
defer file.Close()
6264

63-
return Ingest(data)
65+
return IngestReader(file)
6466
}
6567

66-
// Ingest will parse the given XML data and return a slice of all contained
67-
// JUnit test suite definitions.
68-
func Ingest(data []byte) ([]Suite, error) {
68+
// IngestReader will parse the given XML reader and return a slice of all
69+
// contained JUnit test suite definitions.
70+
func IngestReader(reader io.Reader) ([]Suite, error) {
6971
var (
7072
suiteChan = make(chan Suite)
7173
suites = make([]Suite, 0)
7274
)
7375

74-
nodes, err := parse(data)
76+
nodes, err := parse(reader)
7577
if err != nil {
7678
return nil, err
7779
}
@@ -87,3 +89,9 @@ func Ingest(data []byte) ([]Suite, error) {
8789

8890
return suites, nil
8991
}
92+
93+
// Ingest will parse the given XML data and return a slice of all contained
94+
// JUnit test suite definitions.
95+
func Ingest(data []byte) ([]Suite, error) {
96+
return IngestReader(bytes.NewReader(data))
97+
}

parse.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ import (
99
"encoding/xml"
1010
"errors"
1111
"html"
12+
"io"
1213
)
1314

14-
// reparentXML will wrap the given data (which is assumed to be valid XML), in
15-
// a fake root nodeAlias.
15+
// reparentXML will wrap the given reader (which is assumed to be valid XML),
16+
// in a fake root nodeAlias.
1617
//
1718
// This action is useful in the event that the original XML document does not
1819
// have a single root nodeAlias, which is required by the XML specification.
1920
// Additionally, Go's XML parser will silently drop all nodes after the first
2021
// that is encountered, which can lead to data loss from a parser perspective.
2122
// This function also enables the ingestion of blank XML files, which would
2223
// normally cause a parsing error.
23-
func reparentXML(data []byte) []byte {
24-
return append(append([]byte("<fake-root>"), data...), "</fake-root>"...)
24+
func reparentXML(reader io.Reader) io.Reader {
25+
return io.MultiReader(
26+
bytes.NewReader([]byte("<fake-root>")),
27+
reader,
28+
bytes.NewReader([]byte("</fake-root>")),
29+
)
2530
}
2631

2732
// extractContent parses the raw contents from an XML node, and returns it in a
@@ -96,10 +101,9 @@ func extractContent(data []byte) ([]byte, error) {
96101

97102
// parse unmarshalls the given XML data into a graph of nodes, and then returns
98103
// a slice of all top-level nodes.
99-
func parse(data []byte) ([]xmlNode, error) {
104+
func parse(reader io.Reader) ([]xmlNode, error) {
100105
var (
101-
buf = bytes.NewBuffer(reparentXML(data))
102-
dec = xml.NewDecoder(buf)
106+
dec = xml.NewDecoder(reparentXML(reader))
103107
root xmlNode
104108
)
105109

parse_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package junit
66

77
import (
8+
"bytes"
89
"encoding/xml"
910
"fmt"
11+
"io/ioutil"
1012
"testing"
1113

1214
"github.com/stretchr/testify/assert"
@@ -39,7 +41,9 @@ func TestReparent(t *testing.T) {
3941
name := fmt.Sprintf("#%d - %s", index+1, test.title)
4042

4143
t.Run(name, func(t *testing.T) {
42-
actual := reparentXML(test.input)
44+
reader := reparentXML(bytes.NewReader(test.input))
45+
actual, err := ioutil.ReadAll(reader)
46+
assert.NoError(t, err)
4347

4448
assert.Equal(t, test.expected, string(actual))
4549
})
@@ -168,8 +172,7 @@ func TestParse(t *testing.T) {
168172
name := fmt.Sprintf("#%d - %s", index+1, test.title)
169173

170174
t.Run(name, func(t *testing.T) {
171-
actual, err := parse(test.input)
172-
175+
actual, err := parse(bytes.NewReader(test.input))
173176
require.Nil(t, err)
174177

175178
assert.Equal(t, test.expected, actual)

0 commit comments

Comments
 (0)