-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from WillAbides/golden
golden tests
- Loading branch information
Showing
10 changed files
with
264 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
# Code generated by bindown. DO NOT EDIT. | ||
|
||
set -e | ||
|
||
bindown_bin="$( | ||
CDPATH="" cd -- "$(dirname -- "$0")" | ||
"../../bindown" install "runnable" \ | ||
--to-cache \ | ||
--configfile "../.bindown.yaml" \ | ||
--cache "../cache" | ||
)" | ||
|
||
exec "$bindown_bin" "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.