180 lines
5.1 KiB
Go
180 lines
5.1 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
// isolate points the user config dir and the legacy default at temp folders.
|
|
func isolate(t *testing.T) (legacy string) {
|
|
t.Helper()
|
|
cfgHome := t.TempDir()
|
|
if runtime.GOOS == "windows" {
|
|
t.Setenv("APPDATA", cfgHome)
|
|
} else {
|
|
t.Setenv("XDG_CONFIG_HOME", cfgHome)
|
|
t.Setenv("HOME", cfgHome)
|
|
}
|
|
t.Setenv("REIMS_GO_DATA_ROOT", "")
|
|
t.Setenv("REIMS_GO_LOCATION_FILE", "")
|
|
legacy = filepath.Join(t.TempDir(), "legacy")
|
|
old := legacyDataRoot
|
|
legacyDataRoot = func() string { return legacy }
|
|
t.Cleanup(func() { legacyDataRoot = old })
|
|
return legacy
|
|
}
|
|
|
|
func TestFirstLaunchNeedsSetupUntilAFolderIsChosen(t *testing.T) {
|
|
isolate(t)
|
|
_, needs, err := Resolve()
|
|
if err != nil || !needs {
|
|
t.Fatalf("fresh machine: needs=%v err=%v", needs, err)
|
|
}
|
|
chosen := filepath.Join(t.TempDir(), "Data", "reims-go")
|
|
cfg := Default()
|
|
cfg.DataRoot = chosen
|
|
if err := Save("", cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := SaveLocation(chosen); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, needs, err := Resolve()
|
|
if err != nil || needs || got.DataRoot != chosen {
|
|
t.Fatalf("after choosing: root=%q needs=%v err=%v", got.DataRoot, needs, err)
|
|
}
|
|
if CurrentDataRoot() != chosen {
|
|
t.Fatalf("CurrentDataRoot = %q", CurrentDataRoot())
|
|
}
|
|
}
|
|
|
|
func TestLoadFullscreenHotkeyFields(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, raw, hotkey string
|
|
enabled bool
|
|
}{
|
|
{"invalid key", `{"auto_pin_pcores":false,"fullscreen_hotkey":123,"fullscreen_hotkey_enabled":true}`, "", true},
|
|
{"invalid enabled", `{"auto_pin_pcores":false,"fullscreen_hotkey":" Ctrl+F ","fullscreen_hotkey_enabled":"yes"}`, "Ctrl+F", false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "reims-go.json")
|
|
if err := os.WriteFile(path, []byte(tc.raw), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.AutoPinPCores || cfg.FullscreenHotkey != tc.hotkey || cfg.FullscreenHotkeyEnabled != tc.enabled {
|
|
t.Fatalf("unexpected config: %+v", cfg)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExistingInstallIsAdoptedWithoutAsking(t *testing.T) {
|
|
legacy := isolate(t)
|
|
cfg := Default()
|
|
cfg.DataRoot = legacy
|
|
cfg.ActiveRuntime = "3731afd"
|
|
if err := Save(filepath.Join(legacy, "reims-go.json"), cfg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, needs, err := Resolve()
|
|
if err != nil || needs || got.ActiveRuntime != "3731afd" {
|
|
t.Fatalf("legacy: %+v needs=%v err=%v", got, needs, err)
|
|
}
|
|
if root, ok := ReadLocation(); !ok || root != legacy {
|
|
t.Fatalf("location not recorded: %q %v", root, ok)
|
|
}
|
|
}
|
|
|
|
func TestEnvOverridesEverything(t *testing.T) {
|
|
isolate(t)
|
|
env := t.TempDir()
|
|
t.Setenv("REIMS_GO_DATA_ROOT", env)
|
|
cfg, needs, err := Resolve()
|
|
if err != nil || needs || cfg.DataRoot != env {
|
|
t.Fatalf("env: root=%q needs=%v err=%v", cfg.DataRoot, needs, err)
|
|
}
|
|
if _, err := os.Stat(LocationPath()); !os.IsNotExist(err) {
|
|
t.Fatal("env override must not write the location file")
|
|
}
|
|
}
|
|
|
|
func TestLegacyCandidateChannelReadsAsAlpha(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "reims-go.json")
|
|
if err := os.WriteFile(p, []byte(`{"data_root":"x","runtime_channel":"candidate"}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := Load(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.RuntimeChannel != "alpha" {
|
|
t.Fatalf("runtime_channel = %q, want alpha", cfg.RuntimeChannel)
|
|
}
|
|
}
|
|
|
|
// The first-run test mode must not be able to repoint the real data folder
|
|
// record, so it needs its own location file.
|
|
func TestFirstRunNeedsItsOwnLocationFile(t *testing.T) {
|
|
t.Setenv("REIMS_GO_FIRST_RUN", "1")
|
|
t.Setenv("REIMS_GO_LOCATION_FILE", "")
|
|
if err := CheckFirstRunIsolation(); !errors.Is(err, ErrFirstRunNotIsolated) {
|
|
t.Fatalf("FIRST_RUN without location file: err = %v", err)
|
|
}
|
|
t.Setenv("REIMS_GO_LOCATION_FILE", filepath.Join(t.TempDir(), "location.json"))
|
|
if err := CheckFirstRunIsolation(); err != nil {
|
|
t.Fatalf("FIRST_RUN with location file: %v", err)
|
|
}
|
|
t.Setenv("REIMS_GO_FIRST_RUN", "")
|
|
t.Setenv("REIMS_GO_LOCATION_FILE", "")
|
|
if err := CheckFirstRunIsolation(); err != nil {
|
|
t.Fatalf("normal launch: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestHypervisorSwitchDefaultsOffAndRoundTrips(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "reims-go.json")
|
|
|
|
def := Default()
|
|
def.DataRoot = dir
|
|
if def.EnableHypervisorSwitch {
|
|
t.Fatalf("Default().EnableHypervisorSwitch must be false by default")
|
|
}
|
|
if err := Save(path, def); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(raw) == "" || filepath.Base(path) != "reims-go.json" {
|
|
t.Fatalf("unexpected saved config: %q", string(raw))
|
|
}
|
|
loaded, err := Load(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if loaded.EnableHypervisorSwitch {
|
|
t.Fatalf("loaded default config must keep EnableHypervisorSwitch=false")
|
|
}
|
|
|
|
loaded.EnableHypervisorSwitch = true
|
|
if err := Save(path, loaded); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reloaded, err := Load(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reloaded.EnableHypervisorSwitch {
|
|
t.Fatalf("expected EnableHypervisorSwitch=true after opting in")
|
|
}
|
|
}
|