Move goldens func to testutils and use it for some simple horizontal scrolling tests

This commit is contained in:
David Dworken
2023-02-11 09:09:48 -08:00
parent e1cb97f7c6
commit fea151261f
7 changed files with 205 additions and 87 deletions

View File

@ -16,6 +16,7 @@ import (
"time"
"github.com/ddworken/hishtory/client/data"
"github.com/google/go-cmp/cmp"
)
const (
@ -23,6 +24,24 @@ const (
DB_SHM_PATH = data.DB_PATH + "-shm"
)
var initialWd string
func init() {
initialWd = getInitialWd()
}
func getInitialWd() string {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
if !strings.Contains(cwd, "/hishtory/") {
return cwd
}
components := strings.Split(cwd, "/hishtory/")
return components[0] + "/hishtory"
}
func ResetLocalState(t *testing.T) {
homedir, err := os.UserHomeDir()
Check(t, err)
@ -321,3 +340,32 @@ func persistLog() {
_, err = f.WriteString("\n")
checkError(err)
}
func CompareGoldens(t *testing.T, out, goldenName string) {
out = normalizeHostnames(out)
goldenPath := path.Join(initialWd, "client/lib/goldens/", goldenName)
expected, err := os.ReadFile(goldenPath)
if err != nil {
if os.IsNotExist(err) {
expected = []byte("ERR_FILE_NOT_FOUND")
} else {
Check(t, err)
}
}
if diff := cmp.Diff(string(expected), out); diff != "" {
if os.Getenv("HISHTORY_UPDATE_GOLDENS") == "" {
_, filename, line, _ := runtime.Caller(1)
t.Fatalf("hishtory golden mismatch for %s at %s:%d (-expected +got):\n%s\nactual=\n%s", goldenName, filename, line, diff, out)
} else {
Check(t, os.WriteFile(goldenPath, []byte(out), 0644))
}
}
}
func normalizeHostnames(data string) string {
hostnames := []string{"Davids-MacBook-Air.local", "ghaction-runner-hostname"}
for _, hostname := range hostnames {
data = strings.ReplaceAll(data, hostname, "ghaction-runner-hostname")
}
return data
}