Skip to content

Commit

Permalink
golden tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed Dec 9, 2023
1 parent 1969d79 commit bf59aa0
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 12 deletions.
14 changes: 5 additions & 9 deletions cmd/bindown/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
package main

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"github.com/willabides/bindown/v4/internal/testutil"
)

func Test_bootstrapCmd(t *testing.T) {
output := filepath.Join(t.TempDir(), "foo", "bootstrap.sh")
targetDir := filepath.Join(t.TempDir(), "target")
output := filepath.Join(targetDir, "bootstrap.sh")
runner := newCmdRunner(t)

server := testutil.ServeFile(
t,
testdataPath("build-bootstrapper/checksums.txt"),
"testdata/bootstrap/checksums.txt",
"/WillAbides/bindown/releases/download/v4.8.0/checksums.txt",
"",
)
want, err := os.ReadFile(testdataPath("build-bootstrapper/bootstrap-bindown.sh"))
require.NoError(t, err)
result := runner.run("bootstrap", "--output", output, "--tag", "4.8.0", "--base-url", server.URL)
result.assertState(resultState{})
got, err := os.ReadFile(output)
require.NoError(t, err)
require.Equal(t, string(want), string(got))

testutil.CheckGoldenDir(t, targetDir, filepath.FromSlash("testdata/golden/bootstrap"))
}
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))
}
4 changes: 1 addition & 3 deletions script/lint
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ set -e

CDPATH="" cd -- "$(dirname -- "$0")/.."

script/bindown -q install golangci-lint shellcheck shfmt
script/bindown -q install golangci-lint shellcheck

bin/golangci-lint run
# Don't check bootstrap-bindown.sh because it's dynamically generated
find script -type f -not -name 'bootstrap-bindown.sh' -print0 | xargs -0 bin/shellcheck
bin/shellcheck -s sh internal/build-bootstrapper/assets/*.sh
bin/shellcheck -s sh internal/bindown/testdata/build-bootstrapper/bootstrap-bindown.sh
bin/shfmt -i 2 -ci -sr --diff internal/bindown/testdata/build-bootstrapper/bootstrap-bindown.sh

0 comments on commit bf59aa0

Please sign in to comment.