2022-01-09 06:59:28 +01:00
|
|
|
package shared
|
|
|
|
|
|
|
|
import (
|
2022-04-06 08:31:24 +02:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2022-04-07 03:18:46 +02:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
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-01-09 06:59:28 +01:00
|
|
|
)
|
|
|
|
|
2022-04-06 08:31:24 +02:00
|
|
|
func ResetLocalState(t *testing.T) {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve homedir: %v", err)
|
|
|
|
}
|
2022-04-07 03:18:46 +02:00
|
|
|
|
2022-04-08 06:40:22 +02:00
|
|
|
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, DB_PATH))
|
|
|
|
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH))
|
2022-04-15 05:18:49 +02:00
|
|
|
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, "hishtory"))
|
|
|
|
_ = os.Remove(path.Join(homedir, HISHTORY_PATH, "config.sh"))
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
|
2022-01-09 06:59:28 +01:00
|
|
|
func BackupAndRestore(t *testing.T) func() {
|
|
|
|
homedir, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve homedir: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-04-08 06:40:22 +02:00
|
|
|
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH), path.Join(homedir, HISHTORY_PATH, DB_PATH+".bak"))
|
|
|
|
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH), path.Join(homedir, HISHTORY_PATH, CONFIG_PATH+".bak"))
|
2022-04-15 05:18:49 +02:00
|
|
|
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, "hishtory"), path.Join(homedir, HISHTORY_PATH, "hishtory.bak"))
|
|
|
|
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, "config.sh"), path.Join(homedir, HISHTORY_PATH, "config.sh.bak"))
|
2022-01-09 06:59:28 +01:00
|
|
|
return func() {
|
2022-04-08 06:40:22 +02:00
|
|
|
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, DB_PATH+".bak"), path.Join(homedir, HISHTORY_PATH, DB_PATH))
|
|
|
|
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, CONFIG_PATH+".bak"), path.Join(homedir, HISHTORY_PATH, CONFIG_PATH))
|
2022-04-15 05:18:49 +02:00
|
|
|
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, "hishtory.bak"), path.Join(homedir, HISHTORY_PATH, "hishtory"))
|
|
|
|
_ = os.Rename(path.Join(homedir, HISHTORY_PATH, "config.sh.bak"), path.Join(homedir, HISHTORY_PATH, "config.sh"))
|
2022-01-09 06:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:31:24 +02:00
|
|
|
func buildServer(t *testing.T) {
|
2022-04-09 20:55:00 +02:00
|
|
|
for {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to getwd: %v", err)
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(wd, "/hishtory") {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = os.Chdir("../")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
|
|
}
|
|
|
|
if wd == "/" {
|
|
|
|
t.Fatalf("failed to cd into hishtory dir!")
|
|
|
|
}
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
2022-04-09 08:56:02 +02:00
|
|
|
cmd := exec.Command("go", "build", "-o", "/tmp/server", "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-09 20:55:00 +02:00
|
|
|
err := cmd.Start()
|
2022-04-06 08:31:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to start to build server: %v, stderr=%#v, stdout=%#v", err, stderr.String(), stdout.String())
|
|
|
|
}
|
|
|
|
err = cmd.Wait()
|
|
|
|
if err != nil {
|
2022-04-09 20:55:00 +02:00
|
|
|
wd, _ := os.Getwd()
|
|
|
|
t.Fatalf("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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunTestServer(t *testing.T) func() {
|
2022-04-07 03:18:46 +02:00
|
|
|
os.Setenv("HISHTORY_SERVER", "http://localhost:8080")
|
2022-04-06 08:31:24 +02:00
|
|
|
buildServer(t)
|
2022-04-07 03:18:46 +02:00
|
|
|
cmd := exec.Command("/tmp/server")
|
|
|
|
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 {
|
|
|
|
t.Fatalf("failed to start server: %v", err)
|
|
|
|
}
|
2022-04-14 06:30:27 +02:00
|
|
|
// TODO: Optimize this by streaming stdout and waiting until we see the "listening ..." message
|
2022-04-07 03:18:46 +02:00
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
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-06 08:31:24 +02:00
|
|
|
t.Fatalf("failed to kill process: %v", err)
|
|
|
|
}
|
2022-04-09 08:47:13 +02:00
|
|
|
if strings.Contains(stderr.String()+stdout.String(), "failed to") {
|
|
|
|
t.Fatalf("server failed to do something: stderr=%#v, stdout=%#v", stderr.String(), stdout.String())
|
|
|
|
}
|
2022-04-08 06:40:22 +02:00
|
|
|
fmt.Printf("stderr=%#v, stdout=%#v\n", stderr.String(), stdout.String())
|
2022-04-06 08:31:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:07:01 +02:00
|
|
|
func Check(t *testing.T, err error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|