51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package gameserver
|
|
|
|
import (
|
|
"fmt"
|
|
"gac-hkrpg-go/internal/testutil"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
var testWorkspaceRoot string
|
|
|
|
func TestMain(m *testing.M) {
|
|
_, source, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
fmt.Fprintln(os.Stderr, "cannot locate gameserver test helper source")
|
|
os.Exit(2)
|
|
}
|
|
root := filepath.Clean(filepath.Join(filepath.Dir(source), "..", ".."))
|
|
temporary, err := os.MkdirTemp("", "gac-gameserver-tests-")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if err := testutil.CopyRuntime(root, temporary); err != nil {
|
|
panic(err)
|
|
}
|
|
testWorkspaceRoot = temporary
|
|
if err := os.Chdir(temporary); err != nil {
|
|
fmt.Fprintf(os.Stderr, "cannot change to repository root %s: %v\n", root, err)
|
|
os.Exit(2)
|
|
}
|
|
code := m.Run()
|
|
_ = os.Chdir(root)
|
|
_ = os.RemoveAll(temporary)
|
|
os.Exit(code)
|
|
}
|
|
|
|
func chdirRepositoryRoot(t *testing.T) {
|
|
t.Helper()
|
|
_, source, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
t.Fatal("cannot locate gameserver test helper source")
|
|
}
|
|
root := filepath.Clean(filepath.Join(filepath.Dir(source), "..", ".."))
|
|
if _, err := os.Stat(filepath.Join(root, "go.mod")); err != nil {
|
|
t.Fatalf("cannot locate repository root from %s: %v", source, err)
|
|
}
|
|
t.Chdir(testWorkspaceRoot)
|
|
}
|