@@ -20,6 +20,7 @@ import (
2020 "encoding/xml"
2121 "fmt"
2222 "io/ioutil"
23+ "os"
2324 "path/filepath"
2425 "regexp"
2526 "strings"
@@ -65,13 +66,15 @@ func (s SkipReason) MarshalText() ([]byte, error) {
6566}
6667
6768// 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.
6870func MergeJUnit (testFilter string , sourceDirectories []string , destination string ) error {
6971 var junit TestSuite
7072 var data []byte
7173
7274 re := regexp .MustCompile (testFilter )
7375
7476 var mergeErrors []string
77+ var filesToDelete []string
7578 for _ , dir := range sourceDirectories {
7679 files , err := ioutil .ReadDir (dir )
7780 if err != nil {
@@ -83,7 +86,9 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
8386 if ! strings .HasSuffix (file .Name (), ".xml" ) {
8487 continue
8588 }
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 )
8792 if err != nil {
8893 return err
8994 }
@@ -122,6 +127,17 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
122127
123128 if mergeErrors != nil {
124129 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+ }
125141 }
126142 return nil
127143}
0 commit comments