Skip to content

Commit

Permalink
test: improve golden test error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobgy committed Jan 23, 2022
1 parent 9aeeaa3 commit 4c00359
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package main_test

import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
Expand All @@ -29,7 +29,7 @@ import (

var update = flag.Bool("update", false, "update golden files")

func TestCsvCmd(t *testing.T) {
func TestCsvCommandE2E(t *testing.T) {
var tests = []struct {
workdir string
}{
Expand All @@ -42,14 +42,14 @@ func TestCsvCmd(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// This installs go-licenses CLI.
// This builds and installs go-licenses CLI to $(go env GOPATH)/bin.
cmd := exec.Command("go", "install", ".")
_, err = cmd.Output()
if err != nil {
t.Fatal(err)
}
for _, tc := range tests {
t.Run(fmt.Sprintf("go-licenses csv . in %s", tc.workdir), func(t *testing.T) {
t.Run(tc.workdir, func(t *testing.T) {
err := os.Chdir(filepath.Join(originalWorkDir, tc.workdir))
if err != nil {
t.Fatal(err)
Expand All @@ -60,12 +60,14 @@ func TestCsvCmd(t *testing.T) {
t.Fatalf("downloading go modules:\n%s", string(log))
}
cmd = exec.Command("go-licenses", "csv", ".")
// Capture stderr to buffer.
var stderr bytes.Buffer
cmd.Stderr = &stderr
t.Logf("%s $ go-licenses csv .", tc.workdir)
output, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
t.Fatalf("running go-licenses csv .:\n%s", string(exitErr.Stderr))
}
t.Fatal(err)
t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
t.Fatalf("running go-licenses csv: %s. Full log shown above.", err)
}
got := string(output)
goldenFilePath := "licenses.csv"
Expand All @@ -84,7 +86,11 @@ func TestCsvCmd(t *testing.T) {
}
golden := string(goldenBytes)
if got != golden {
t.Fatalf("result of go-licenses csv . does not match the golden file. Update the golden by running `go test --update .`. Diff -golden +got:\n%s", cmp.Diff(golden, got))
t.Logf("\n=== start of log ===\n%s=== end of log ===\n\n\n", stderr.String())
t.Fatalf("result of go-licenses csv does not match the golden file.\n"+
"Diff -golden +got:\n%s\n"+
"Update the golden by running `go test --update .`",
cmp.Diff(golden, got))
}
})
}
Expand Down

0 comments on commit 4c00359

Please sign in to comment.