@@ -20,6 +20,7 @@ import (
20
20
"encoding/xml"
21
21
"fmt"
22
22
"io/ioutil"
23
+ "os"
23
24
"path/filepath"
24
25
"regexp"
25
26
"strings"
@@ -65,13 +66,15 @@ func (s SkipReason) MarshalText() ([]byte, error) {
65
66
}
66
67
67
68
// MergeJUnit merges all junit xml files found in sourceDirectories into a single xml file at destination, using the filter.
69
+ // The merging removes duplicate skipped tests. The original files are deleted.
68
70
func MergeJUnit (testFilter string , sourceDirectories []string , destination string ) error {
69
71
var junit TestSuite
70
72
var data []byte
71
73
72
74
re := regexp .MustCompile (testFilter )
73
75
74
76
var mergeErrors []string
77
+ var filesToDelete []string
75
78
for _ , dir := range sourceDirectories {
76
79
files , err := ioutil .ReadDir (dir )
77
80
if err != nil {
@@ -83,7 +86,9 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
83
86
if ! strings .HasSuffix (file .Name (), ".xml" ) {
84
87
continue
85
88
}
86
- data , err := ioutil .ReadFile (filepath .Join (dir , file .Name ()))
89
+ fullFilename := filepath .Join (dir , file .Name ())
90
+ filesToDelete = append (filesToDelete , fullFilename )
91
+ data , err := ioutil .ReadFile (fullFilename )
87
92
if err != nil {
88
93
return err
89
94
}
@@ -122,6 +127,17 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
122
127
123
128
if mergeErrors != nil {
124
129
return fmt .Errorf ("Problems reading junit files; partial merge has been performed: %s" , strings .Join (mergeErrors , " " ))
130
+ } else {
131
+ // Only delete original files if everything went well.
132
+ var removeErrors []string
133
+ for _ , filename := range filesToDelete {
134
+ if err := os .Remove (filename ); err != nil {
135
+ removeErrors = append (removeErrors , err .Error ())
136
+ }
137
+ }
138
+ if removeErrors != nil {
139
+ return fmt .Errorf ("Problem removing original junit results: %s" , strings .Join (removeErrors , " " ))
140
+ }
125
141
}
126
142
return nil
127
143
}
0 commit comments