Skip to content

Commit

Permalink
Revert "use goldie"
Browse files Browse the repository at this point in the history
This reverts commit dbba832.
  • Loading branch information
WillAbides committed Dec 9, 2023
1 parent ee96826 commit 4f74a92
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 44 deletions.
2 changes: 1 addition & 1 deletion cmd/bindown/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ func Test_bootstrapCmd(t *testing.T) {
result := runner.run("bootstrap", "--output", output, "--tag", "4.8.0", "--base-url", server.URL)
result.assertState(resultState{})

testutil.AssertGoldenFile(t, output)
testutil.CheckGoldenDir(t, targetDir, filepath.FromSlash("testdata/golden/bootstrap"))
}
5 changes: 2 additions & 3 deletions cmd/bindown/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ url_checksums:
result := runner.run("wrap", "runnable", "--bindown", testutil.BindownBin(), "--output", runnable)
result.assertState(resultState{stdout: runnable})
testutil.AssertFile(t, runnable, true, false)
testutil.AssertGoldenFile(t, runnable)
testutil.CheckGoldenDir(t, outputDir, filepath.FromSlash("testdata/golden/wrap/bindown-path"))

// make sure it runs
cmd := exec.Command("sh", "-c", filepath.ToSlash(runnable))
Expand Down Expand Up @@ -535,8 +535,7 @@ url_checksums:
)
result.assertState(resultState{stdout: bindown + "\n" + runnable})
testutil.AssertFile(t, runnable, true, false)
testutil.AssertGoldenFile(t, runnable)
testutil.AssertFile(t, bindown, true, false)
testutil.AssertGoldenFile(t, bindown)
testutil.CheckGoldenDir(t, outputDir, filepath.FromSlash("testdata/golden/wrap/wrap-bindown"))
})
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ require (
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab // indirect
github.com/sebdah/goldie/v2 v2.5.3 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/therootcompany/xz v1.0.1 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
Expand Down
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ github.com/nwaples/rardecode/v2 v2.0.0-beta.2/go.mod h1:yntwv/HfMc/Hbvtq9I19D1n5
github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ=
github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.2.3 h1:NP0eAhjcjImqslEwo/1hq7gpajME0fTLTezBKDqfXqo=
Expand All @@ -172,15 +171,9 @@ github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 h1:OkMGxebDj
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06/go.mod h1:+ePHsJ1keEjQtpvf9HHw0f4ZeJ0TLRsxhunSI2hYJSs=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 h1:lZUw3E0/J3roVtGQ+SCrUrg3ON6NgVqpn3+iol9aGu4=
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1/go.mod h1:uToXkOrWAZ6/Oc07xWQrPOhJotwFIyu2bBVN41fcDUY=
github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -385,10 +378,8 @@ google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
117 changes: 117 additions & 0 deletions internal/testutil/golden.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package testutil

import (
"bytes"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func updateGoldenDir(t *testing.T, resultDir, goldenDir string) {
t.Helper()
if os.Getenv("UPDATE_GOLDEN") == "" {
return
}
require.NoError(t, os.RemoveAll(goldenDir))
err := filepath.WalkDir(resultDir, func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() {
return err
}
fmt.Println(path)
relName := mustRel(t, resultDir, path)
return copyFile(path, filepath.Join(goldenDir, relName))
})
require.NoError(t, err)
}

func copyFile(src, dst string) (errOut error) {
err := os.MkdirAll(filepath.Dir(dst), 0o777)
if err != nil {
return err
}
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer func() {
e := dstFile.Close()
if errOut == nil {
errOut = e
}
}()
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer func() {
e := srcFile.Close()
if errOut == nil {
errOut = e
}
}()
_, err = io.Copy(dstFile, srcFile)
return err
}

func CheckGoldenDir(t *testing.T, resultDir, goldenDir string) {
t.Helper()
golden := true
t.Cleanup(func() {
t.Helper()
if !golden {
t.Log("To regenerate golden files run `UPDATE_GOLDEN=1 script/test`")
}
})
updateGoldenDir(t, resultDir, goldenDir)
checked := map[string]bool{}
_, err := os.Stat(goldenDir)
if err == nil {
assert.NoError(t, filepath.WalkDir(goldenDir, func(wantPath string, info fs.DirEntry, err error) error {
relPath := mustRel(t, goldenDir, wantPath)
if err != nil || info.IsDir() {
return err
}
if !assertEqualFiles(t, wantPath, filepath.Join(resultDir, relPath)) {
golden = false
}
checked[relPath] = true
return nil
}))
}
assert.NoError(t, filepath.Walk(resultDir, func(resultPath string, info fs.FileInfo, err error) error {
relPath := mustRel(t, resultDir, resultPath)
if err != nil || info.IsDir() || checked[relPath] {
return err
}
golden = false
return fmt.Errorf("found unexpected file:\n%s", relPath)
}))
}

func mustRel(t *testing.T, base, target string) string {
t.Helper()
rel, err := filepath.Rel(base, target)
require.NoError(t, err)
return rel
}

func assertEqualFiles(t *testing.T, want, got string) bool {
t.Helper()
wantBytes, err := os.ReadFile(want)
if !assert.NoError(t, err) {
return false
}
wantBytes = bytes.ReplaceAll(wantBytes, []byte("\r\n"), []byte("\n"))
gotBytes, err := os.ReadFile(got)
if !assert.NoError(t, err) {
return false
}
gotBytes = bytes.ReplaceAll(gotBytes, []byte("\r\n"), []byte("\n"))
return assert.Equal(t, string(wantBytes), string(gotBytes))
}
29 changes: 0 additions & 29 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
"os/exec"
"path/filepath"
"runtime"
"runtime/debug"
"sync"
"testing"

"github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -137,30 +135,3 @@ func RepoRoot() string {
}
return abs
}

func AssertGoldenFile(t *testing.T, file string) {
t.Helper()
content, err := os.ReadFile(file)
if !assert.NoError(t, err) {
return
}
failedBefore := t.Failed()
goldie.New(
t,
goldie.WithTestNameForDir(true),
goldie.WithSubTestNameForDir(true),
goldie.WithNameSuffix(""),
goldie.WithFixtureDir(filepath.Join("testdata", "golden")),
).Assert(t, filepath.Base(file), content)
if !failedBefore && t.Failed() {
buildInfo, ok := debug.ReadBuildInfo()
require.True(t, ok, "unable to read build info")
t.Logf(`
update golden files for this test with:
go test %s -run %q -update
update golden files for all package tests with:
go test %s -update
`, buildInfo.Path, t.Name(), buildInfo.Path)

}
}

0 comments on commit 4f74a92

Please sign in to comment.