From 3adb6bab4b4bc6f26b0f836cf6e29381b5ab8350 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Stemmer?= Date: Fri, 18 Mar 2022 22:22:11 +0000 Subject: [PATCH] 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 . - Package names are no longer truncated to the last path component. --- testdata/01-report.xml | 6 +- testdata/02-report.xml | 11 +-- testdata/03-report.xml | 6 +- testdata/04-report.xml | 6 +- testdata/05-report.xml | 6 +- testdata/06-report.xml | 17 +++-- testdata/07-report.xml | 6 +- testdata/08-report.xml | 6 +- testdata/09-report.xml | 6 +- testdata/10-report.xml | 10 +-- testdata/11-report.xml | 6 +- testdata/12-report.xml | 46 +++++++------ testdata/13-report.xml | 29 ++++---- testdata/131-report.xml | 17 +++++ testdata/14-report.xml | 14 ++-- testdata/15-report.xml | 2 +- testdata/16-report.xml | 8 +-- testdata/17-report.xml | 40 ++++++++++- testdata/18-report.xml | 10 +-- testdata/19-report.xml | 6 +- testdata/20-report.xml | 18 +++-- testdata/21-report.xml | 4 +- testdata/22-report.xml | 6 +- testdata/23-report.xml | 6 +- testdata/24-report.xml | 14 ++-- testdata/25-report.xml | 6 +- testdata/26-report.xml | 16 ++--- testdata/27-report.xml | 8 +-- testdata/28-report.xml | 4 +- testdata/29-report.xml | 4 +- testdata/30-report.xml | 80 ++++++++++++++-------- testdata/31-report.xml | 29 ++++---- testdata/32-report.xml | 8 +-- testdata/33-report.xml | 6 +- testdata/34-report.xml | 6 +- testdata/generate-golden.go | 129 ++++++++++++++++++++++++++++++++++++ 36 files changed, 414 insertions(+), 188 deletions(-) create mode 100644 testdata/131-report.xml create mode 100644 testdata/generate-golden.go diff --git a/testdata/01-report.xml b/testdata/01-report.xml index 503f071..11db67d 100644 --- a/testdata/01-report.xml +++ b/testdata/01-report.xml @@ -1,10 +1,10 @@ - + - - + + diff --git a/testdata/02-report.xml b/testdata/02-report.xml index 74fdb72..06a003f 100644 --- a/testdata/02-report.xml +++ b/testdata/02-report.xml @@ -1,13 +1,16 @@ - + - - file_test.go:11: Error message file_test.go:11: Longer error message. + + - + diff --git a/testdata/03-report.xml b/testdata/03-report.xml index 48a0fc8..4cb4d4a 100644 --- a/testdata/03-report.xml +++ b/testdata/03-report.xml @@ -1,12 +1,12 @@ - + - + - + diff --git a/testdata/04-report.xml b/testdata/04-report.xml index edcb4ea..23618ba 100644 --- a/testdata/04-report.xml +++ b/testdata/04-report.xml @@ -1,10 +1,10 @@ - + - - + + diff --git a/testdata/05-report.xml b/testdata/05-report.xml index 029550e..6709b1d 100644 --- a/testdata/05-report.xml +++ b/testdata/05-report.xml @@ -1,9 +1,9 @@ - + - - + + diff --git a/testdata/06-report.xml b/testdata/06-report.xml index 2271733..7994e4b 100644 --- a/testdata/06-report.xml +++ b/testdata/06-report.xml @@ -1,19 +1,22 @@ - + - - + + - + - - file_test.go:11: Error message file_test.go:11: Longer error message. + + - + diff --git a/testdata/07-report.xml b/testdata/07-report.xml index 0eb95bd..05f1479 100644 --- a/testdata/07-report.xml +++ b/testdata/07-report.xml @@ -1,10 +1,10 @@ - + - - + + diff --git a/testdata/08-report.xml b/testdata/08-report.xml index 9a69e95..2f5974a 100644 --- a/testdata/08-report.xml +++ b/testdata/08-report.xml @@ -1,10 +1,10 @@ - + - - + + diff --git a/testdata/09-report.xml b/testdata/09-report.xml index aa67423..b3742bd 100644 --- a/testdata/09-report.xml +++ b/testdata/09-report.xml @@ -1,11 +1,11 @@ - + - - + + diff --git a/testdata/10-report.xml b/testdata/10-report.xml index a9a4cb5..298aeda 100644 --- a/testdata/10-report.xml +++ b/testdata/10-report.xml @@ -1,18 +1,18 @@ - + - - + + - + - + diff --git a/testdata/11-report.xml b/testdata/11-report.xml index 8c494ec..0d1ba9d 100644 --- a/testdata/11-report.xml +++ b/testdata/11-report.xml @@ -1,10 +1,10 @@ - + - - + + diff --git a/testdata/12-report.xml b/testdata/12-report.xml index fcb9c48..619f9a3 100644 --- a/testdata/12-report.xml +++ b/testdata/12-report.xml @@ -1,36 +1,38 @@ - + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - example.go:12: Expected abc OBTAINED: xyz example.go:123: Expected and obtained are different. + + - + - - + + - - example.go:371: This should not fail! + + diff --git a/testdata/13-report.xml b/testdata/13-report.xml index a183850..ae66973 100644 --- a/testdata/13-report.xml +++ b/testdata/13-report.xml @@ -1,39 +1,42 @@ - + - + - + - + - + - - failing1/failing_test.go:15: undefined: x + + - + - - failing2/another_failing_test.go:20: undefined: y + + - + - - setupfailing1/failing_test.go:4: cannot find package "other/package" in any of: /path/vendor (vendor tree) /path/go/root (from $GOROOT) /path/go/path (from $GOPATH) + + diff --git a/testdata/131-report.xml b/testdata/131-report.xml new file mode 100644 index 0000000..5256322 --- /dev/null +++ b/testdata/131-report.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/testdata/14-report.xml b/testdata/14-report.xml index 57cce23..18e4b7f 100644 --- a/testdata/14-report.xml +++ b/testdata/14-report.xml @@ -1,19 +1,21 @@ - + - - panic: init stacktrace + + - + - - panic: init stacktrace + + diff --git a/testdata/15-report.xml b/testdata/15-report.xml index edc61a7..f97312d 100644 --- a/testdata/15-report.xml +++ b/testdata/15-report.xml @@ -1,6 +1,6 @@ - + diff --git a/testdata/16-report.xml b/testdata/16-report.xml index b14d270..56ae1e4 100644 --- a/testdata/16-report.xml +++ b/testdata/16-report.xml @@ -1,11 +1,11 @@ - + - - - + + + diff --git a/testdata/17-report.xml b/testdata/17-report.xml index 005a994..d272124 100644 --- a/testdata/17-report.xml +++ b/testdata/17-report.xml @@ -1,11 +1,45 @@ - + - - test output 2 0xc4200153d0 ================== WARNING: DATA RACE Write at 0x00c4200153d0 by goroutine 7: race_test.TestRace.func1() race_test.go:13 +0x3b Previous write at 0x00c4200153d0 by goroutine 6: race_test.TestRace() race_test.go:15 +0x136 testing.tRunner() /usr/local/Cellar/go/1.8.3/libexec/src/testing/testing.go:657 +0x107 Goroutine 7 (running) created at: race_test.TestRace() race_test.go:14 +0x125 testing.tRunner() /usr/local/Cellar/go/1.8.3/libexec/src/testing/testing.go:657 +0x107 Goroutine 6 (running) created at: testing.(*T).Run() /usr/local/Cellar/go/1.8.3/libexec/src/testing/testing.go:697 +0x543 testing.runTests.func1() /usr/local/Cellar/go/1.8.3/libexec/src/testing/testing.go:882 +0xaa testing.tRunner() /usr/local/Cellar/go/1.8.3/libexec/src/testing/testing.go:657 +0x107 testing.runTests() /usr/local/Cellar/go/1.8.3/libexec/src/testing/testing.go:888 +0x4e0 testing.(*M).Run() /usr/local/Cellar/go/1.8.3/libexec/src/testing/testing.go:822 +0x1c3 main.main() _test/_testmain.go:52 +0x20f ================== testing.go:610: race detected during execution of test + + diff --git a/testdata/18-report.xml b/testdata/18-report.xml index a9a4cb5..298aeda 100644 --- a/testdata/18-report.xml +++ b/testdata/18-report.xml @@ -1,18 +1,18 @@ - + - - + + - + - + diff --git a/testdata/19-report.xml b/testdata/19-report.xml index 503f071..11db67d 100644 --- a/testdata/19-report.xml +++ b/testdata/19-report.xml @@ -1,10 +1,10 @@ - + - - + + diff --git a/testdata/20-report.xml b/testdata/20-report.xml index 212d0cd..b78f72d 100644 --- a/testdata/20-report.xml +++ b/testdata/20-report.xml @@ -1,17 +1,21 @@ - + - - Message from first Supplemental from first parallel_test.go:14: FirstTest error + + - - Message from second parallel_test.go:23: SecondTest error + + - - Message from third parallel_test.go:32: ThirdTest error + + diff --git a/testdata/21-report.xml b/testdata/21-report.xml index 69e2bff..10cc8d2 100644 --- a/testdata/21-report.xml +++ b/testdata/21-report.xml @@ -1,9 +1,9 @@ - + - + diff --git a/testdata/22-report.xml b/testdata/22-report.xml index 3661e0c..5cccd17 100644 --- a/testdata/22-report.xml +++ b/testdata/22-report.xml @@ -1,11 +1,11 @@ - + - - + + diff --git a/testdata/23-report.xml b/testdata/23-report.xml index c406683..63ba3f9 100644 --- a/testdata/23-report.xml +++ b/testdata/23-report.xml @@ -1,11 +1,11 @@ - + - - + + diff --git a/testdata/24-report.xml b/testdata/24-report.xml index 396b06c..305d403 100644 --- a/testdata/24-report.xml +++ b/testdata/24-report.xml @@ -1,14 +1,14 @@ - + - - - - - - + + + + + + diff --git a/testdata/25-report.xml b/testdata/25-report.xml index 47e4af7..00fb0cf 100644 --- a/testdata/25-report.xml +++ b/testdata/25-report.xml @@ -1,10 +1,10 @@ - + - - + + diff --git a/testdata/26-report.xml b/testdata/26-report.xml index a758e87..273a6d4 100644 --- a/testdata/26-report.xml +++ b/testdata/26-report.xml @@ -1,21 +1,21 @@ - + - - + + - + - - - - + + + + diff --git a/testdata/27-report.xml b/testdata/27-report.xml index 6049ee4..792cf19 100644 --- a/testdata/27-report.xml +++ b/testdata/27-report.xml @@ -1,12 +1,12 @@ - + - - - + + + diff --git a/testdata/28-report.xml b/testdata/28-report.xml index 5208fb5..74a4fb0 100644 --- a/testdata/28-report.xml +++ b/testdata/28-report.xml @@ -1,10 +1,10 @@ - + - + diff --git a/testdata/29-report.xml b/testdata/29-report.xml index edb3434..e64584f 100644 --- a/testdata/29-report.xml +++ b/testdata/29-report.xml @@ -1,10 +1,10 @@ - + - + diff --git a/testdata/30-report.xml b/testdata/30-report.xml index a647d9c..8e91aae 100644 --- a/testdata/30-report.xml +++ b/testdata/30-report.xml @@ -1,43 +1,69 @@ - + - - multi line stdout single-line stdout example_test.go:13: single-line error example_test.go:14: multi line error + + - - multi line stdout single-line stdout + + - - example_test.go:26: single-line error example_test.go:27: multi line error + + - - + + - - - - - - + + + + + + - - 1 multi line stdout 1 single-line stdout example_test.go:65: 1 single-line error example_test.go:66: 1 multi line error + + - - 2 multi line stdout 2 single-line stdout + + - - example_test.go:78: 3 single-line error example_test.go:79: 3 multi line error + + - - + + - - - - + + + + diff --git a/testdata/31-report.xml b/testdata/31-report.xml index a183850..ae66973 100644 --- a/testdata/31-report.xml +++ b/testdata/31-report.xml @@ -1,39 +1,42 @@ - + - + - + - + - + - - failing1/failing_test.go:15: undefined: x + + - + - - failing2/another_failing_test.go:20: undefined: y + + - + - - setupfailing1/failing_test.go:4: cannot find package "other/package" in any of: /path/vendor (vendor tree) /path/go/root (from $GOROOT) /path/go/path (from $GOPATH) + + diff --git a/testdata/32-report.xml b/testdata/32-report.xml index 4666169..314ba57 100644 --- a/testdata/32-report.xml +++ b/testdata/32-report.xml @@ -1,12 +1,12 @@ - + - - - panic: panic + + + diff --git a/testdata/33-report.xml b/testdata/33-report.xml index 34eabc3..cf6436b 100644 --- a/testdata/33-report.xml +++ b/testdata/33-report.xml @@ -1,11 +1,11 @@ - + - - + + diff --git a/testdata/34-report.xml b/testdata/34-report.xml index 18e3f34..d5cb86d 100644 --- a/testdata/34-report.xml +++ b/testdata/34-report.xml @@ -1,16 +1,16 @@ - + - + - + diff --git a/testdata/generate-golden.go b/testdata/generate-golden.go new file mode 100644 index 0000000..5b38736 --- /dev/null +++ b/testdata/generate-golden.go @@ -0,0 +1,129 @@ +//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 +}