From 55f0f97d296262f9e5e50dcc9371122871eb12bd Mon Sep 17 00:00:00 2001 From: David Dworken Date: Sat, 17 Sep 2022 21:56:39 -0700 Subject: [PATCH] Fix tests for importing so they pass on github actions --- .github/workflows/go-test.yml | 1 - Makefile | 2 +- client/client_test.go | 4 ++-- shared/testutils.go | 44 +++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/.github/workflows/go-test.yml b/.github/workflows/go-test.yml index c41d804..fb7172a 100644 --- a/.github/workflows/go-test.yml +++ b/.github/workflows/go-test.yml @@ -25,5 +25,4 @@ jobs: run: | sudo apt-get update || true sudo apt-get install -y zsh || true - export HISTFILE=~/.zsh_history make test diff --git a/Makefile b/Makefile index 6147901..56928e6 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ test: HISHTORY_TEST=1 go test -p 1 ./... acttest: - act push -j test -e .github/push_event.json --reuse + act push -j test -e .github/push_event.json --reuse --container-architecture linux/amd64 release: # Bump the version diff --git a/client/client_test.go b/client/client_test.go index 343d027..d34f84f 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -94,7 +94,7 @@ func (z zshTester) RunInteractiveShellRelaxed(t *testing.T, script string) (stri cmd.Stderr = &stderr err := cmd.Run() if err != nil { - return "", fmt.Errorf("unexpected error when running command=%#v, out=%#v, err=%#v: %v", script, stdout.String(), stderr.String(), err) + return stdout.String(), fmt.Errorf("unexpected error when running command=%#v, out=%#v, err=%#v: %v", script, stdout.String(), stderr.String(), err) } outStr := stdout.String() if strings.Contains(outStr, "hishtory fatal error") { @@ -313,7 +313,7 @@ echo thisisrecorded`) // Test the actual table output hostnameMatcher := `\S+` tableDividerMatcher := `\s+` - pathMatcher := `~/[a-zA-Z_0-9/-]+` + pathMatcher := `~?/[a-zA-Z_0-9/-]+` datetimeMatcher := `[a-zA-Z]{3}\s\d{2}\s\d{4}\s[0-9:]+\s[A-Z]{3}` runtimeMatcher := `[0-9.ms]+` exitCodeMatcher := `0` diff --git a/shared/testutils.go b/shared/testutils.go index c07390d..5547b58 100644 --- a/shared/testutils.go +++ b/shared/testutils.go @@ -3,6 +3,7 @@ package shared import ( "bytes" "fmt" + "io" "log" "net/http" "os" @@ -50,6 +51,25 @@ func BackupAndRestoreWithId(t *testing.T, id string) func() { _ = os.Rename(path.Join(homedir, HISHTORY_PATH, "hishtory"), path.Join(homedir, HISHTORY_PATH, "hishtory"+id+".bak")) _ = os.Rename(path.Join(homedir, HISHTORY_PATH, "config.sh"), path.Join(homedir, HISHTORY_PATH, "config.sh"+id+".bak")) _ = os.Rename(path.Join(homedir, HISHTORY_PATH, "config.zsh"), path.Join(homedir, HISHTORY_PATH, "config.zsh"+id+".bak")) + _ = copy(path.Join(homedir, ".zshrc"), path.Join(homedir, ".zshrc"+id+".bak")) + 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(`export HISTFILE=~/.zsh_history +export HISTSIZE=10000 +export SAVEHIST=1000 +setopt SHARE_HISTORY`) + checkError(err) + + _ = copy(path.Join(homedir, ".bashrc"), path.Join(homedir, ".bashrc"+id+".bak")) + _ = os.Rename(path.Join(homedir, ".bash_history"), path.Join(homedir, ".bash_history"+id+".bak")) + file, err := os.Create(path.Join(homedir, ".bash_history")) + checkError(err) + defer file.Close() + _ = os.Rename(path.Join(homedir, ".zsh_history"), path.Join(homedir, ".zsh_history"+id+".bak")) + file, err = os.Create(path.Join(homedir, ".zsh_history")) + checkError(err) + defer file.Close() return func() { checkError(os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH+id+".bak"), path.Join(homedir, HISHTORY_PATH, DB_PATH))) checkError(os.Rename(path.Join(homedir, HISHTORY_PATH, DB_WAL_PATH+id+".bak"), path.Join(homedir, HISHTORY_PATH, DB_WAL_PATH))) @@ -58,9 +78,33 @@ func BackupAndRestoreWithId(t *testing.T, id string) func() { checkError(os.Rename(path.Join(homedir, HISHTORY_PATH, "hishtory"+id+".bak"), path.Join(homedir, HISHTORY_PATH, "hishtory"))) checkError(os.Rename(path.Join(homedir, HISHTORY_PATH, "config.sh"+id+".bak"), path.Join(homedir, HISHTORY_PATH, "config.sh"))) checkError(os.Rename(path.Join(homedir, HISHTORY_PATH, "config.zsh"+id+".bak"), path.Join(homedir, HISHTORY_PATH, "config.zsh"))) + checkError(copy(path.Join(homedir, ".zshrc"+id+".bak"), path.Join(homedir, ".zshrc"))) + checkError(copy(path.Join(homedir, ".bashrc"+id+".bak"), path.Join(homedir, ".bashrc"))) + checkError(os.Rename(path.Join(homedir, ".bash_history"+id+".bak"), path.Join(homedir, ".bash_history"))) + checkError(os.Rename(path.Join(homedir, ".zsh_history"+id+".bak"), path.Join(homedir, ".zsh_history"))) } } +func copy(src, dst string) error { + 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 + } + return out.Close() +} + func BackupAndRestoreEnv(k string) func() { origValue := os.Getenv(k) return func() {