2022-10-28 06:53:47 +02:00
|
|
|
package testutils
|
2022-01-09 06:59:28 +01:00
|
|
|
|
|
|
|
import (
|
2022-04-06 08:31:24 +02:00
|
|
|
"bytes"
|
2022-04-15 20:20:23 +02:00
|
|
|
"fmt"
|
2022-09-18 06:56:39 +02:00
|
|
|
"io"
|
2022-05-23 04:45:46 +02:00
|
|
|
"log"
|
2022-04-28 20:26:55 +02:00
|
|
|
"net/http"
|
2022-04-07 03:18:46 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2022-09-19 04:20:21 +02:00
|
|
|
"path/filepath"
|
2022-04-12 07:36:52 +02:00
|
|
|
"runtime"
|
2022-04-09 08:47:13 +02:00
|
|
|
"strings"
|
2022-04-07 03:18:46 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
2022-10-28 06:53:47 +02:00
|
|
|
|
|
|
|
"github.com/ddworken/hishtory/client/data"
|
2023-02-11 18:09:48 +01:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2023-09-22 22:16:24 +02:00
|
|
|
"github.com/google/uuid"
|
2022-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
2022-05-02 04:37:26 +02:00
|
|
|
const (
|
2022-10-28 06:53:47 +02:00
|
|
|
DB_WAL_PATH = data.DB_PATH + "-wal"
|
|
|
|
DB_SHM_PATH = data.DB_PATH + "-shm"
|
2022-05-02 04:37:26 +02:00
|
|
|
)
|
|
|
|
|
2023-02-11 18:09:48 +01:00
|
|
|
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/")
|
2023-02-14 05:49:41 +01:00
|
|
|
dir := components[0] + "/hishtory"
|
|
|
|
if IsGithubAction() {
|
|
|
|
dir += "/hishtory"
|
|
|
|
}
|
|
|
|
return dir
|
2023-02-11 18:09:48 +01:00
|
|
|
}
|
|
|
|
|
2022-04-06 08:31:24 +02:00
|
|
|
func ResetLocalState(t *testing.T) {
|
2023-09-05 03:03:46 +02:00
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
Check(t, err)
|
2022-11-17 05:28:25 +01:00
|
|
|
persistLog()
|
2022-11-12 15:42:10 +01:00
|
|
|
_ = BackupAndRestoreWithId(t, "-reset-local-state")
|
2023-09-05 03:03:46 +02:00
|
|
|
_ = os.RemoveAll(path.Join(homedir, data.GetHishtoryPath()))
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
|
2023-08-30 08:23:21 +02:00
|
|
|
func BackupAndRestore(t testing.TB) func() {
|
2022-05-02 04:37:26 +02:00
|
|
|
return BackupAndRestoreWithId(t, "")
|
|
|
|
}
|
|
|
|
|
2022-11-03 03:41:49 +01:00
|
|
|
func getBackPath(file, id string) string {
|
2022-12-17 07:22:57 +01:00
|
|
|
if strings.Contains(file, "/"+data.GetHishtoryPath()+"/") {
|
|
|
|
return strings.Replace(file, data.GetHishtoryPath(), data.GetHishtoryPath()+".test", 1) + id
|
2022-11-03 21:16:45 +01:00
|
|
|
}
|
|
|
|
return file + ".bak" + id
|
2022-09-23 08:06:28 +02:00
|
|
|
}
|
|
|
|
|
2023-08-30 08:23:21 +02:00
|
|
|
func BackupAndRestoreWithId(t testing.TB, id string) func() {
|
2022-10-28 06:53:47 +02:00
|
|
|
ResetFakeHistoryTimestamp()
|
2022-01-09 06:59:28 +01:00
|
|
|
homedir, err := os.UserHomeDir()
|
2022-10-30 02:53:40 +02:00
|
|
|
Check(t, err)
|
|
|
|
initialWd, err := os.Getwd()
|
|
|
|
Check(t, err)
|
2022-12-17 07:22:57 +01:00
|
|
|
Check(t, os.MkdirAll(path.Join(homedir, data.GetHishtoryPath()+".test"), os.ModePerm))
|
2022-01-09 06:59:28 +01:00
|
|
|
|
2022-09-18 08:02:57 +02:00
|
|
|
renameFiles := []string{
|
2022-12-17 07:22:57 +01:00
|
|
|
path.Join(homedir, data.GetHishtoryPath(), data.DB_PATH),
|
|
|
|
path.Join(homedir, data.GetHishtoryPath(), DB_WAL_PATH),
|
|
|
|
path.Join(homedir, data.GetHishtoryPath(), DB_SHM_PATH),
|
|
|
|
path.Join(homedir, data.GetHishtoryPath(), data.CONFIG_PATH),
|
|
|
|
path.Join(homedir, data.GetHishtoryPath(), "config.sh"),
|
|
|
|
path.Join(homedir, data.GetHishtoryPath(), "config.zsh"),
|
|
|
|
path.Join(homedir, data.GetHishtoryPath(), "config.fish"),
|
2023-09-03 05:09:22 +02:00
|
|
|
path.Join(homedir, data.GetHishtoryPath(), "hishtory"),
|
2022-09-18 08:02:57 +02:00
|
|
|
path.Join(homedir, ".bash_history"),
|
|
|
|
path.Join(homedir, ".zsh_history"),
|
2022-11-04 06:32:55 +01:00
|
|
|
path.Join(homedir, ".local/share/fish/fish_history"),
|
2022-09-18 08:02:57 +02:00
|
|
|
}
|
|
|
|
for _, file := range renameFiles {
|
|
|
|
touchFile(file)
|
2023-09-05 03:03:46 +02:00
|
|
|
Check(t, os.Rename(file, getBackPath(file, id)))
|
2022-09-18 08:02:57 +02:00
|
|
|
}
|
|
|
|
copyFiles := []string{
|
|
|
|
path.Join(homedir, ".zshrc"),
|
|
|
|
path.Join(homedir, ".bashrc"),
|
2022-11-04 05:01:57 +01:00
|
|
|
path.Join(homedir, ".bash_profile"),
|
2022-09-18 08:02:57 +02:00
|
|
|
}
|
|
|
|
for _, file := range copyFiles {
|
2022-09-18 18:14:34 +02:00
|
|
|
touchFile(file)
|
2022-11-16 08:20:19 +01:00
|
|
|
Check(t, copy(file, getBackPath(file, id)))
|
2022-09-18 08:02:57 +02:00
|
|
|
}
|
2022-09-18 06:59:13 +02:00
|
|
|
configureZshrc(homedir)
|
|
|
|
touchFile(path.Join(homedir, ".bash_history"))
|
|
|
|
touchFile(path.Join(homedir, ".zsh_history"))
|
2022-11-04 06:32:55 +01:00
|
|
|
touchFile(path.Join(homedir, ".local/share/fish/fish_history"))
|
2022-01-09 06:59:28 +01:00
|
|
|
return func() {
|
2023-05-20 02:16:17 +02:00
|
|
|
cmd := exec.Command("killall", "hishtory", "tmux")
|
|
|
|
stdout, err := cmd.Output()
|
|
|
|
if err != nil && err.Error() != "exit status 1" {
|
|
|
|
t.Fatalf("failed to execute killall hishtory, stdout=%#v: %v", string(stdout), err)
|
2022-11-16 08:20:19 +01:00
|
|
|
}
|
2022-11-17 05:28:25 +01:00
|
|
|
persistLog()
|
2023-09-05 03:03:46 +02:00
|
|
|
Check(t, os.RemoveAll(path.Join(homedir, data.GetHishtoryPath())))
|
2022-12-17 07:22:57 +01:00
|
|
|
Check(t, os.MkdirAll(path.Join(homedir, data.GetHishtoryPath()), os.ModePerm))
|
2022-09-18 08:02:57 +02:00
|
|
|
for _, file := range renameFiles {
|
2023-09-05 03:03:46 +02:00
|
|
|
checkError(os.Rename(getBackPath(file, id), file))
|
2022-09-18 08:02:57 +02:00
|
|
|
}
|
|
|
|
for _, file := range copyFiles {
|
2022-11-03 03:41:49 +01:00
|
|
|
checkError(copy(getBackPath(file, id), file))
|
2022-09-18 08:02:57 +02:00
|
|
|
}
|
2022-10-30 02:53:40 +02:00
|
|
|
checkError(os.Chdir(initialWd))
|
2022-09-18 06:56:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 06:59:13 +02:00
|
|
|
func touchFile(p string) {
|
2022-09-18 08:02:57 +02:00
|
|
|
_, err := os.Stat(p)
|
|
|
|
if os.IsNotExist(err) {
|
2022-09-19 06:05:51 +02:00
|
|
|
checkError(os.MkdirAll(filepath.Dir(p), os.ModePerm))
|
2022-09-19 03:34:27 +02:00
|
|
|
file, err := os.Create(p)
|
2022-09-18 08:02:57 +02:00
|
|
|
checkError(err)
|
|
|
|
defer file.Close()
|
|
|
|
} else {
|
|
|
|
currentTime := time.Now().Local()
|
|
|
|
err := os.Chtimes(p, currentTime, currentTime)
|
|
|
|
checkError(err)
|
|
|
|
}
|
2022-09-18 06:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func configureZshrc(homedir string) {
|
2022-11-27 20:59:06 +01:00
|
|
|
zshrcHistConfig := `export HISTFILE=~/.zsh_history
|
2022-09-18 06:59:13 +02:00
|
|
|
export HISTSIZE=10000
|
|
|
|
export SAVEHIST=1000
|
2022-09-23 03:22:06 +02:00
|
|
|
setopt SHARE_HISTORY
|
2022-11-27 20:59:06 +01:00
|
|
|
`
|
|
|
|
dat, err := os.ReadFile(path.Join(homedir, ".zshrc"))
|
|
|
|
checkError(err)
|
|
|
|
if strings.Contains(string(dat), zshrcHistConfig) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path.Join(homedir, ".zshrc"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
checkError(err)
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(zshrcHistConfig)
|
2022-09-18 06:59:13 +02:00
|
|
|
checkError(err)
|
|
|
|
}
|
|
|
|
|
2022-09-18 06:56:39 +02:00
|
|
|
func copy(src, dst string) error {
|
2023-09-03 03:50:52 +02:00
|
|
|
// Copy the contents of the file
|
2022-09-18 06:56:39 +02:00
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-05-23 04:45:46 +02:00
|
|
|
}
|
2023-09-03 03:50:52 +02:00
|
|
|
err = out.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// And copy the permissions
|
|
|
|
srcStat, err := in.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Chmod(dst, srcStat.Mode())
|
2022-05-23 04:45:46 +02:00
|
|
|
}
|
|
|
|
|
2022-06-13 06:28:19 +02:00
|
|
|
func BackupAndRestoreEnv(k string) func() {
|
|
|
|
origValue := os.Getenv(k)
|
|
|
|
return func() {
|
2022-11-14 01:53:37 +01:00
|
|
|
if origValue == "" {
|
|
|
|
os.Unsetenv(k)
|
|
|
|
} else {
|
|
|
|
os.Setenv(k, origValue)
|
|
|
|
}
|
2022-06-13 06:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 04:45:46 +02:00
|
|
|
func checkError(err error) {
|
2022-09-18 07:45:07 +02:00
|
|
|
if err != nil {
|
2022-05-23 04:45:46 +02:00
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
2022-05-28 07:06:20 +02:00
|
|
|
_, cf, cl, _ := runtime.Caller(2)
|
2022-05-28 04:00:02 +02:00
|
|
|
log.Fatalf("testutils fatal error at %s:%d (caller: %s:%d): %v", filename, line, cf, cl, err)
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-07 02:46:45 +01:00
|
|
|
func buildServer() string {
|
2022-11-06 23:42:01 +01:00
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to getwd: %v", err))
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(wd, "hishtory") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = os.Chdir("../")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to chdir: %v", err))
|
|
|
|
}
|
|
|
|
if wd == "/" {
|
|
|
|
panic("failed to cd into hishtory dir!")
|
2022-04-09 20:55:00 +02:00
|
|
|
}
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
2022-04-20 06:05:54 +02:00
|
|
|
version, err := os.ReadFile("VERSION")
|
|
|
|
if err != nil {
|
2023-05-20 02:16:17 +02:00
|
|
|
panic(fmt.Sprintf("failed to read VERSION file: %v", err))
|
2022-04-20 06:05:54 +02:00
|
|
|
}
|
2022-11-07 02:46:45 +01:00
|
|
|
f, err := os.CreateTemp("", "server")
|
|
|
|
checkError(err)
|
|
|
|
fn := f.Name()
|
|
|
|
cmd := exec.Command("go", "build", "-o", fn, "-ldflags", fmt.Sprintf("-X main.ReleaseVersion=v0.%s", version), "backend/server/server.go")
|
2022-04-06 08:31:24 +02:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
2022-04-20 06:05:54 +02:00
|
|
|
err = cmd.Start()
|
2022-04-06 08:31:24 +02:00
|
|
|
if err != nil {
|
2022-04-15 20:20:23 +02:00
|
|
|
panic(fmt.Sprintf("failed to start to build server: %v, stderr=%#v, stdout=%#v", err, stderr.String(), stdout.String()))
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
2022-04-09 20:55:00 +02:00
|
|
|
wd, _ := os.Getwd()
|
2022-04-15 20:20:23 +02:00
|
|
|
panic(fmt.Sprintf("failed to build server: %v, wd=%#v, stderr=%#v, stdout=%#v", err, wd, stderr.String(), stdout.String()))
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
2022-11-07 02:46:45 +01:00
|
|
|
return fn
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
|
2023-09-14 05:23:09 +02:00
|
|
|
func killExistingTestServers() {
|
|
|
|
_ = exec.Command("bash", "-c", "lsof -i tcp:8080 | grep LISTEN | awk '{print $2}' | xargs kill -9").Run()
|
|
|
|
}
|
|
|
|
|
2022-04-15 20:20:23 +02:00
|
|
|
func RunTestServer() func() {
|
2023-09-14 05:23:09 +02:00
|
|
|
killExistingTestServers()
|
2022-04-07 03:18:46 +02:00
|
|
|
os.Setenv("HISHTORY_SERVER", "http://localhost:8080")
|
2022-11-07 02:46:45 +01:00
|
|
|
fn := buildServer()
|
|
|
|
cmd := exec.Command(fn)
|
2022-04-07 03:18:46 +02:00
|
|
|
var stdout bytes.Buffer
|
2022-04-06 08:31:24 +02:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
2022-04-07 03:18:46 +02:00
|
|
|
err := cmd.Start()
|
|
|
|
if err != nil {
|
2022-04-15 20:20:23 +02:00
|
|
|
panic(fmt.Sprintf("failed to start server: %v", err))
|
2022-04-07 03:18:46 +02:00
|
|
|
}
|
2022-04-15 20:20:23 +02:00
|
|
|
time.Sleep(time.Second * 5)
|
2022-04-07 03:18:46 +02:00
|
|
|
go func() {
|
2022-04-08 06:40:22 +02:00
|
|
|
_ = cmd.Wait()
|
2022-04-07 03:18:46 +02:00
|
|
|
}()
|
2022-04-06 08:31:24 +02:00
|
|
|
return func() {
|
|
|
|
err := cmd.Process.Kill()
|
2022-04-15 05:18:49 +02:00
|
|
|
if err != nil && err.Error() != "os: process already finished" {
|
2022-04-15 20:20:23 +02:00
|
|
|
panic(fmt.Sprintf("failed to kill server process: %v", err))
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
2022-12-12 05:31:50 +01:00
|
|
|
allOutput := stdout.String() + stderr.String()
|
|
|
|
if strings.Contains(allOutput, "failed to") && IsOnline() {
|
2022-04-15 20:20:23 +02:00
|
|
|
panic(fmt.Sprintf("server failed to do something: stderr=%#v, stdout=%#v", stderr.String(), stdout.String()))
|
2022-04-09 08:47:13 +02:00
|
|
|
}
|
2022-12-12 05:31:50 +01:00
|
|
|
if strings.Contains(allOutput, "ERROR:") || strings.Contains(allOutput, "http: panic serving") {
|
2023-09-21 20:35:24 +02:00
|
|
|
panic(fmt.Sprintf("server experienced an error\n\n\nstderr=\n%s\n\n\nstdout=%s", stderr.String(), stdout.String()))
|
2022-04-16 20:37:43 +02:00
|
|
|
}
|
2023-09-14 05:23:09 +02:00
|
|
|
// Persist test server logs for debugging
|
|
|
|
f, err := os.OpenFile("/tmp/hishtory-server.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
checkError(err)
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.Write([]byte(stdout.String() + "\n"))
|
|
|
|
checkError(err)
|
|
|
|
_, err = f.Write([]byte(stderr.String() + "\n"))
|
|
|
|
checkError(err)
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 08:23:21 +02:00
|
|
|
func Check(t testing.TB, err error) {
|
2022-04-05 07:07:01 +02:00
|
|
|
if err != nil {
|
2022-04-12 07:36:52 +02:00
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("Unexpected error at %s:%d: %v", filename, line, err)
|
2022-04-05 07:07:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckWithInfo(t *testing.T, err error, additionalInfo string) {
|
|
|
|
if err != nil {
|
2022-04-12 07:36:52 +02:00
|
|
|
_, filename, line, _ := runtime.Caller(1)
|
|
|
|
t.Fatalf("Unexpected error: %v at %s:%d! Additional info: %v", err, filename, line, additionalInfo)
|
2022-04-05 07:07:01 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 20:26:55 +02:00
|
|
|
|
|
|
|
func IsOnline() bool {
|
|
|
|
_, err := http.Get("https://hishtory.dev")
|
|
|
|
return err == nil
|
|
|
|
}
|
2022-10-28 06:53:47 +02:00
|
|
|
|
|
|
|
var fakeHistoryTimestamp int64 = 1666068191
|
|
|
|
|
|
|
|
func ResetFakeHistoryTimestamp() {
|
|
|
|
fakeHistoryTimestamp = 1666068191
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeFakeHistoryEntry(command string) data.HistoryEntry {
|
|
|
|
fakeHistoryTimestamp += 5
|
|
|
|
return data.HistoryEntry{
|
|
|
|
LocalUsername: "david",
|
|
|
|
Hostname: "localhost",
|
|
|
|
Command: command,
|
|
|
|
CurrentWorkingDirectory: "/tmp/",
|
|
|
|
HomeDirectory: "/home/david/",
|
|
|
|
ExitCode: 2,
|
2023-09-09 21:28:01 +02:00
|
|
|
StartTime: time.Unix(fakeHistoryTimestamp, 0).UTC(),
|
|
|
|
EndTime: time.Unix(fakeHistoryTimestamp+3, 0).UTC(),
|
2023-09-21 20:35:24 +02:00
|
|
|
DeviceId: "fake_device_id",
|
2023-09-22 22:16:24 +02:00
|
|
|
EntryId: uuid.Must(uuid.NewRandom()).String(),
|
2022-10-28 06:53:47 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-12 15:46:37 +01:00
|
|
|
|
|
|
|
func IsGithubAction() bool {
|
|
|
|
return os.Getenv("GITHUB_ACTION") != ""
|
|
|
|
}
|
2022-11-16 08:20:19 +01:00
|
|
|
|
2023-08-30 08:23:21 +02:00
|
|
|
func TestLog(t testing.TB, line string) {
|
2022-11-16 08:20:19 +01:00
|
|
|
f, err := os.OpenFile("/tmp/test.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
Check(t, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(line + "\n")
|
|
|
|
if err != nil {
|
|
|
|
Check(t, err)
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 05:28:25 +01:00
|
|
|
|
|
|
|
func persistLog() {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
checkError(err)
|
2022-12-17 07:22:57 +01:00
|
|
|
fp := path.Join(homedir, data.GetHishtoryPath(), "hishtory.log")
|
2022-11-17 05:28:25 +01:00
|
|
|
log, err := os.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile("/tmp/hishtory.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
checkError(err)
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.Write(log)
|
|
|
|
checkError(err)
|
|
|
|
_, err = f.WriteString("\n")
|
|
|
|
checkError(err)
|
|
|
|
}
|
2023-02-11 18:09:48 +01:00
|
|
|
|
2023-08-30 08:23:21 +02:00
|
|
|
func CompareGoldens(t testing.TB, out, goldenName string) {
|
2023-02-11 18:09:48 +01:00
|
|
|
out = normalizeHostnames(out)
|
|
|
|
goldenPath := path.Join(initialWd, "client/lib/goldens/", goldenName)
|
|
|
|
expected, err := os.ReadFile(goldenPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2023-02-14 05:20:45 +01:00
|
|
|
expected = []byte("ERR_FILE_NOT_FOUND:" + goldenPath)
|
2023-02-11 18:09:48 +01:00
|
|
|
} 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 {
|
2023-08-27 20:42:17 +02:00
|
|
|
hostnames := []string{"Davids-MacBook-Air.local", "ghaction-runner-hostname", "Davids-Air"}
|
2023-02-11 18:09:48 +01:00
|
|
|
for _, hostname := range hostnames {
|
|
|
|
data = strings.ReplaceAll(data, hostname, "ghaction-runner-hostname")
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|