gotest.tools/gotestsum@v1.11.0/internal/junitxml/report_test.go (about)

     1  package junitxml
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"runtime"
     9  	"testing"
    10  	"time"
    11  
    12  	"gotest.tools/gotestsum/testjson"
    13  	"gotest.tools/v3/assert"
    14  	"gotest.tools/v3/env"
    15  	"gotest.tools/v3/golden"
    16  )
    17  
    18  func TestWrite(t *testing.T) {
    19  	out := new(bytes.Buffer)
    20  	exec := createExecution(t)
    21  
    22  	env.Patch(t, "GOVERSION", "go7.7.7")
    23  	err := Write(out, exec, Config{
    24  		ProjectName:     "test",
    25  		customTimestamp: new(time.Time).Format(time.RFC3339),
    26  		customElapsed:   "2.1",
    27  	})
    28  	assert.NilError(t, err)
    29  	golden.Assert(t, out.String(), "junitxml-report.golden")
    30  }
    31  
    32  func TestWrite_HideEmptyPackages(t *testing.T) {
    33  	out := new(bytes.Buffer)
    34  	exec := createExecution(t)
    35  
    36  	env.Patch(t, "GOVERSION", "go7.7.7")
    37  	err := Write(out, exec, Config{
    38  		ProjectName:       "test",
    39  		HideEmptyPackages: true,
    40  		customTimestamp:   new(time.Time).Format(time.RFC3339),
    41  		customElapsed:     "2.1",
    42  	})
    43  	assert.NilError(t, err)
    44  	golden.Assert(t, out.String(), "junitxml-report-skip-empty.golden")
    45  }
    46  
    47  func createExecution(t *testing.T) *testjson.Execution {
    48  	exec, err := testjson.ScanTestOutput(testjson.ScanConfig{
    49  		Stdout: readTestData(t, "out"),
    50  		Stderr: readTestData(t, "err"),
    51  	})
    52  	assert.NilError(t, err)
    53  	return exec
    54  }
    55  
    56  func readTestData(t *testing.T, stream string) io.Reader {
    57  	raw, err := ioutil.ReadFile("../../testjson/testdata/input/go-test-json." + stream)
    58  	assert.NilError(t, err)
    59  	return bytes.NewReader(raw)
    60  }
    61  
    62  func TestGoVersion(t *testing.T) {
    63  	t.Run("unknown", func(t *testing.T) {
    64  		env.Patch(t, "PATH", "/bogus")
    65  		assert.Equal(t, goVersion(), "unknown")
    66  	})
    67  
    68  	t.Run("current version", func(t *testing.T) {
    69  		expected := fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
    70  		assert.Equal(t, goVersion(), expected)
    71  	})
    72  }