Skip to content

Commit

Permalink
Merge pull request #34 from dnephin/add-no-output-value-for-summary
Browse files Browse the repository at this point in the history
Use go version as reported by the binary
  • Loading branch information
dnephin committed Dec 1, 2018
2 parents 23f01ef + c621fb5 commit dc92d87
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
6 changes: 4 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 27 additions & 5 deletions internal/junitxml/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ package junitxml
import (
"encoding/xml"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gotest.tools/gotestsum/testjson"
)

Expand Down Expand Up @@ -64,14 +67,15 @@ func Write(out io.Writer, exec *testjson.Execution) error {
}

func generate(exec *testjson.Execution) JUnitTestSuites {
version := goVersion()
suites := JUnitTestSuites{}
for _, pkgname := range exec.Packages() {
pkg := exec.Package(pkgname)
junitpkg := JUnitTestSuite{
Name: pkgname,
Tests: pkg.Total,
Time: testjson.FormatDurationAsSeconds(pkg.Elapsed(), 3),
Properties: packageProperties(),
Properties: packageProperties(version),
TestCases: packageTestCases(pkg),
Failures: len(pkg.Failed),
}
Expand All @@ -80,14 +84,32 @@ func generate(exec *testjson.Execution) JUnitTestSuites {
return suites
}

var goVersion = runtime.Version()

func packageProperties() []JUnitProperty {
func packageProperties(goVersion string) []JUnitProperty {
return []JUnitProperty{
{Name: "go.version", Value: goVersion},
}
}

// goVersion returns the version as reported by the go binary in PATH. This
// version will not be the same as runtime.Version, which is always the version
// of go used to build the gotestsum binary.
//
// To skip the os/exec call set the GOVERSION environment variable to the
// desired value.
func goVersion() string {
if version, ok := os.LookupEnv("GOVERSION"); ok {
return version
}
logrus.Debugf("exec: go version")
cmd := exec.Command("go", "version")
out, err := cmd.Output()
if err != nil {
logrus.WithError(err).Warn("failed to lookup go version for junit xml")
return "unknown"
}
return strings.TrimPrefix(strings.TrimSpace(string(out)), "go version ")
}

func packageTestCases(pkg *testjson.Package) []JUnitTestCase {
cases := []JUnitTestCase{}

Expand Down
21 changes: 14 additions & 7 deletions internal/junitxml/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package junitxml

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"runtime"
"testing"

"gotest.tools/assert"
"gotest.tools/env"
"gotest.tools/golden"
"gotest.tools/gotestsum/testjson"
)
Expand All @@ -15,7 +18,7 @@ func TestWrite(t *testing.T) {
out := new(bytes.Buffer)
exec := createExecution(t)

defer patchGoVersion("go7.7.7")()
defer env.Patch(t, "GOVERSION", "go7.7.7")()
err := Write(out, exec)
assert.NilError(t, err)
golden.Assert(t, out.String(), "junitxml-report.golden")
Expand Down Expand Up @@ -47,10 +50,14 @@ func (s *noopHandler) Err(string) error {
return nil
}

func patchGoVersion(version string) func() {
orig := goVersion
goVersion = version
return func() {
goVersion = orig
}
func TestGoVersion(t *testing.T) {
t.Run("unknown", func(t *testing.T) {
defer env.Patch(t, "PATH", "/bogus")()
assert.Equal(t, goVersion(), "unknown")
})

t.Run("current version", func(t *testing.T) {
expected := fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
assert.Equal(t, goVersion(), expected)
})
}

0 comments on commit dc92d87

Please sign in to comment.