Move common go-junit-report code into separate internal package

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.
This commit is contained in:
Joël Stemmer
2022-04-06 00:52:52 +01:00
parent d84b066208
commit 3260a9d2e0
4 changed files with 152 additions and 170 deletions

View File

@ -0,0 +1,107 @@
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
}