Fix tests for importing so they pass on github actions

This commit is contained in:
David Dworken 2022-09-17 21:56:39 -07:00
parent 80f0660ade
commit 55f0f97d29
4 changed files with 47 additions and 4 deletions

View File

@ -25,5 +25,4 @@ jobs:
run: |
sudo apt-get update || true
sudo apt-get install -y zsh || true
export HISTFILE=~/.zsh_history
make test

View File

@ -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

View File

@ -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`

View File

@ -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() {