Skip to content

Commit

Permalink
Improve test.CompareGoldenFile
Browse files Browse the repository at this point in the history
When the golden file for a test doesn't exist, create it. This makes it
easier to get started using golden files without having to create the
empty file first. You can just edit the test to use that helper and it
will make the file for you when run with mage updatetestfiles

Signed-off-by: Carolyn Van Slyck <[email protected]>
  • Loading branch information
carolynvs committed Jan 27, 2022
1 parent 57cdb6f commit 27a8705
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions pkg/porter/testdata/version/version-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ porter v1.2.3 (abc123)

System
-------
os: darwin
os: linux
arch: amd64

Mixins
-------
---------------------------------
Name Version Author
---------------------------------
Expand Down
1 change: 0 additions & 1 deletion pkg/porter/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ os: {{.SysInfo.OS}}
arch: {{.SysInfo.Arch}}
{{if .Mixins}}
Mixins
-------
{{.Mixins.PrintMixinsTable}}{{end}}
`
tmpl, err := template.New("systemDebugInfo").Parse(plaintextTmpl)
Expand Down
5 changes: 5 additions & 0 deletions pkg/porter/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func TestPrintDebugInfoJsonVersion(t *testing.T) {
}

func TestPrintDebugInfoPlainTextVersion(t *testing.T) {
// Only run this on linux + amd64 machines to simplify the test (it has different output based on the os/arch)
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
t.Skip("skipping test because it is only for linux/amd64")
}

pkg.Commit = "abc123"
pkg.Version = "v1.2.3"

Expand Down
7 changes: 4 additions & 3 deletions pkg/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -63,13 +64,13 @@ func TestMainWithMockedCommandHandlers(m *testing.M) {
// When they are different and PORTER_UPDATE_TEST_FILES is true, the file is updated to match
// the new test output.
func CompareGoldenFile(t *testing.T, goldenFile string, got string) {
wantSchema, err := ioutil.ReadFile(goldenFile)
require.NoError(t, err)

if os.Getenv("PORTER_UPDATE_TEST_FILES") == "true" {
os.MkdirAll(filepath.Dir(goldenFile), 0700)
t.Logf("Updated test file %s to match latest test output", goldenFile)
require.NoError(t, ioutil.WriteFile(goldenFile, []byte(got), 0600), "could not update golden file %s", goldenFile)
} else {
wantSchema, err := ioutil.ReadFile(goldenFile)
require.NoError(t, err)
assert.Equal(t, string(wantSchema), got, "The test output doesn't match the expected output in %s. If this was intentional, run mage updateTestfiles to fix the tests.", goldenFile)
}
}

0 comments on commit 27a8705

Please sign in to comment.