go-junit-report/testdata/generate-golden.go
Joël Stemmer 3adb6bab4b testdata: Create generate-golden.go to generate report.xml files
The testdata directory contains various inputs and the resulting
report.xml that is expected to be generated. Making changes that result
in differences in the generated XML reports requires the report.xml
files to be manually updated. This is time consuming and error prone.

The generate-golden.go script (re)generates all report.xml files in the
testdata directory from the corresponding input. This can be done by
simply running `go generate` from within the testdata directory. Make
sure the generated reports contain the changes you expected before
committing them.

As part of this change, all reports have been regenerated. The diffs in
the report.xml files are caused by the following recent changes:
- XML attributes now appear in the order as defined in the structs in
  the junit package.
- Failure and error messages are now wrapped in <![CDATA[]]>.
- Package names are no longer truncated to the last path component.
2022-03-22 22:05:23 +00:00

130 lines
2.6 KiB
Go

//go:generate go run generate-golden.go -w
package main
import (
"encoding/xml"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/jstemmer/go-junit-report/v2/pkg/junit"
"github.com/jstemmer/go-junit-report/v2/pkg/parser/gotest"
)
var verbose bool
type Settings struct {
skipXMLHeader bool
packageName string
}
var fileSettings = map[string]Settings{
"05-no_xml_header.txt": {skipXMLHeader: true},
"06-mixed.txt": {skipXMLHeader: true},
"07-compiled_test.txt": {packageName: "test/package"},
}
func main() {
var writeFiles bool
flag.BoolVar(&verbose, "v", false, "verbose logging")
flag.BoolVar(&writeFiles, "w", false, "write output xml files")
flag.Parse()
files, err := filepath.Glob("*.txt")
if err != nil {
exitf("error listing files: %v\n", err)
}
for _, file := range files {
outName := outputName(file)
if err := createReportFromInput(file, outName, writeFiles); err != nil {
logf("error creating report: %v\n", err)
continue
}
if writeFiles {
logf("report written to %s\n", outName)
}
}
}
func logf(msg string, args ...interface{}) {
if verbose {
fmt.Printf(msg, args...)
}
}
func exitf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg, args...)
os.Exit(1)
}
func outputName(input string) string {
dir, name := filepath.Split(input)
var out string
if idx := strings.IndexByte(name, '-'); idx > -1 {
out = input[:idx+1] + "report.xml"
} else {
out = strings.TrimSuffix(name, filepath.Ext(name)) + "report.xml"
}
return filepath.Join(dir, out)
}
func createReportFromInput(inputFile, outputFile string, write bool) error {
in, err := os.Open(inputFile)
if err != nil {
return err
}
defer in.Close()
out := io.Discard
if write {
f, err := os.Create(outputFile)
if err != nil {
return err
}
defer f.Close()
out = f
}
return writeReport(in, out, fileSettings[inputFile])
}
func writeReport(in io.Reader, out io.Writer, settings Settings) error {
parser := gotest.New(
gotest.PackageName(settings.packageName),
gotest.TimestampFunc(func() time.Time {
return time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
}),
)
report, err := parser.Parse(in)
if err != nil {
return err
}
for i := range report.Packages {
report.Packages[i].SetProperty("go.version", "1.0")
}
testsuites := junit.CreateFromReport(report, "hostname")
if !settings.skipXMLHeader {
if _, err := fmt.Fprintf(out, xml.Header); err != nil {
return err
}
}
enc := xml.NewEncoder(out)
enc.Indent("", "\t")
if err := enc.Encode(testsuites); err != nil {
return err
}
if err := enc.Flush(); err != nil {
return err
}
_, err = fmt.Fprintf(out, "\n")
return err
}