Delete functions that no product code calls, and fold "simple plus WithOptions" pairs into the one signature callers actually use. Rename the live macOS lifecycle functions that were labelled Legacy. The architecture tests now share one source loader and reject functions without a product caller and catalog messages nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
100 lines
3.5 KiB
Go
100 lines
3.5 KiB
Go
package datamigrate
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/macos/guest"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
"github.com/Nwflower/reims-go/internal/plugin/builtin"
|
|
)
|
|
|
|
func TestUnstampedReleaseUpgradeRestoresOriginalBackup(t *testing.T) {
|
|
root := t.TempDir()
|
|
vmDir := mkVM(t, root, "mac", map[string]string{"vm.toml": "name = 'mac'"})
|
|
store := plugin.NewStateStore(plugin.NewPluginsLayout(root).Root)
|
|
if err := store.Save(&plugin.StateFile{Plugins: map[string]*plugin.PluginRecord{
|
|
builtin.IDCore: {ID: builtin.IDCore, Enabled: true, AppSettings: map[string]any{guest.SettingWHPXFallback: false, guest.SettingWin10ColdReboot: false}},
|
|
guest.LegacyInstallGuideID: {ID: guest.LegacyInstallGuideID, Enabled: false},
|
|
"test.plugin": {ID: "test.plugin", VMSettings: map[string]map[string]any{"mac": {"custom": "off"}}},
|
|
}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
original, err := os.ReadFile(store.Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res, err := Run(root, "dev")
|
|
if err != nil || res.From != 0 || res.To != CurrentVersion {
|
|
t.Fatalf("Run = %+v, %v", res, err)
|
|
}
|
|
backup, err := os.ReadFile(store.Path() + ".bak")
|
|
if err != nil || string(backup) != string(original) {
|
|
t.Fatalf("original backup changed: %v", err)
|
|
}
|
|
if got := marker(t, vmDir); got != driver.BuiltinReimsMacOSDriverID {
|
|
t.Fatalf("owner marker = %q", got)
|
|
}
|
|
st, err := store.Load()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rec := st.Plugins[guest.ID]
|
|
if rec == nil || guest.SettingOn(rec.AppSettings, guest.SettingWHPXFallback) || guest.SettingOn(rec.AppSettings, guest.SettingInstallGuide) {
|
|
t.Fatalf("guest settings = %+v", rec)
|
|
}
|
|
key := driver.NewVMRef(driver.BuiltinReimsMacOSDriverID, "mac").Key()
|
|
if got := st.Plugins["test.plugin"].VMSettings[key]["custom"]; got != "off" {
|
|
t.Fatalf("VM setting = %v", got)
|
|
}
|
|
beforeState, _ := os.ReadFile(store.Path())
|
|
beforeBackup, _ := os.ReadFile(store.Path() + ".bak")
|
|
if _, err := Run(root, "dev"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
afterState, _ := os.ReadFile(store.Path())
|
|
afterBackup, _ := os.ReadFile(store.Path() + ".bak")
|
|
if string(beforeState) != string(afterState) || string(beforeBackup) != string(afterBackup) {
|
|
t.Fatal("idempotent run changed state or backup")
|
|
}
|
|
}
|
|
|
|
func TestMigrationBackupFailureLeavesStateAndStampUntouched(t *testing.T) {
|
|
root := t.TempDir()
|
|
store := plugin.NewStateStore(plugin.NewPluginsLayout(root).Root)
|
|
if err := store.Save(&plugin.StateFile{Plugins: map[string]*plugin.PluginRecord{
|
|
builtin.IDCore: {ID: builtin.IDCore, AppSettings: map[string]any{guest.SettingWHPXFallback: false}},
|
|
}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
original, err := os.ReadFile(store.Path())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(store.Path()+".bak", 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := writeStamp(root, Stamp{DataVersion: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Run(root, "dev"); err == nil {
|
|
t.Fatal("migration ignored backup failure")
|
|
}
|
|
got, err := os.ReadFile(store.Path())
|
|
if err != nil || string(got) != string(original) {
|
|
t.Fatalf("state changed after backup failure: %v", err)
|
|
}
|
|
if _, err := os.Stat(store.Path() + ".tmp"); !os.IsNotExist(err) {
|
|
t.Fatalf("temporary state file remains: %v", err)
|
|
}
|
|
st, err := ReadStamp(root)
|
|
if err != nil || st.DataVersion != 1 {
|
|
t.Fatalf("stamp changed after backup failure: %+v, %v", st, err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(root, StampFile)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|