mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-05-21 03:14:29 -05:00

There was some code duplication between the go-junit-report binary, its tests and the testdata/generate-golden script. This has been moved into an internal package. The go-junit-report binary can now just focus on flag parsing and validation, and it should be less likely that the binary, tests and golden report generator behave differently.
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package gojunitreport
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
const testDataDir = "../../testdata/"
|
|
|
|
var matchTest = flag.String("match", "", "only test testdata matching this pattern")
|
|
|
|
var testConfigs = map[int]Config{
|
|
5: {SkipXMLHeader: true},
|
|
6: {SkipXMLHeader: true},
|
|
7: {PackageName: "test/package"},
|
|
}
|
|
|
|
func TestRun(t *testing.T) {
|
|
matchRegex := compileMatch(t)
|
|
|
|
files, err := filepath.Glob(testDataDir + "*.txt")
|
|
if err != nil {
|
|
t.Fatalf("error finding files in testdata: %v", err)
|
|
}
|
|
|
|
for _, file := range files {
|
|
if !matchRegex.MatchString(file) {
|
|
continue
|
|
}
|
|
|
|
conf, reportFile, err := testFileConfig(strings.TrimPrefix(file, testDataDir))
|
|
if err != nil {
|
|
t.Errorf("testFileConfig error: %v", err)
|
|
continue
|
|
}
|
|
|
|
t.Run(file, func(t *testing.T) {
|
|
testRun(file, testDataDir+reportFile, conf, t)
|
|
})
|
|
}
|
|
}
|
|
|
|
func testRun(inputFile, reportFile string, config Config, t *testing.T) {
|
|
input, err := os.Open(inputFile)
|
|
if err != nil {
|
|
t.Fatalf("error opening input file: %v", err)
|
|
}
|
|
defer input.Close()
|
|
|
|
wantReport, err := os.ReadFile(reportFile)
|
|
if os.IsNotExist(err) {
|
|
t.Skipf("Skipping test with missing report file: %s", reportFile)
|
|
} else if err != nil {
|
|
t.Fatalf("error loading report file: %v", err)
|
|
}
|
|
|
|
config.Parser = "gotest"
|
|
if strings.HasSuffix(inputFile, ".gojson.txt") {
|
|
config.Parser = "gojson"
|
|
}
|
|
config.Hostname = "hostname"
|
|
config.Properties = map[string]string{"go.version": "1.0"}
|
|
config.TimestampFunc = func() time.Time {
|
|
return time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
if _, err := config.Run(input, &output); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if diff := cmp.Diff(output.String(), string(wantReport)); diff != "" {
|
|
t.Errorf("Unexpected report diff (-want, +got):\n%v", diff)
|
|
}
|
|
}
|
|
|
|
func testFileConfig(filename string) (config Config, reportFile string, err error) {
|
|
var prefix string
|
|
if idx := strings.IndexByte(filename, '-'); idx < 0 {
|
|
return config, "", fmt.Errorf("testdata file does not contain a dash (-); expected name `{id}-{name}.txt` got `%s`", filename)
|
|
} else {
|
|
prefix = filename[:idx]
|
|
}
|
|
id, err := strconv.Atoi(prefix)
|
|
if err != nil {
|
|
return config, "", fmt.Errorf("testdata file did not start with a valid number: %w", err)
|
|
}
|
|
return testConfigs[id], fmt.Sprintf("%s-report.xml", prefix), nil
|
|
}
|
|
|
|
func compileMatch(t *testing.T) *regexp.Regexp {
|
|
rx, err := regexp.Compile(*matchTest)
|
|
if err != nil {
|
|
t.Fatalf("Error compiling -match flag %q: %v", *matchTest, err)
|
|
}
|
|
return rx
|
|
}
|