116 lines
3.3 KiB
Go
116 lines
3.3 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 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)
|
|
}
|
|
}
|