From 790a42912e68d7c66583fd27a4edadad24031c47 Mon Sep 17 00:00:00 2001 From: YXC0915 Date: Tue, 1 Dec 2026 08:22:58 +0800 Subject: [PATCH] fix(installer): expand home paths from reims-ctl show --- internal/config/config.go | 16 ++++++++++++++ internal/driver/reims/reims.go | 5 +++-- internal/driver/reims/reims_test.go | 21 +++++++++++++++++++ internal/installguide/progress.go | 6 +++++- internal/installguide/session_windows_test.go | 15 +++++++++++++ 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 347ea29..fbaae72 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "github.com/Nwflower/reims-go/internal/i18n" ) @@ -53,6 +54,21 @@ func DefaultDataRoot() string { return filepath.Join(home, "reims-go-data") } +// ExpandHomePath resolves a leading ~ for the current user. +func ExpandHomePath(path string) string { + if path != "~" && !strings.HasPrefix(path, "~/") && !strings.HasPrefix(path, `~\`) { + return path + } + home, err := os.UserHomeDir() + if err != nil { + return path + } + if path == "~" { + return home + } + return filepath.Join(home, path[2:]) +} + // CurrentDataRoot is the data folder in effect: REIMS_GO_DATA_ROOT, else the // folder chosen at first launch (LocationPath), else DefaultDataRoot. func CurrentDataRoot() string { diff --git a/internal/driver/reims/reims.go b/internal/driver/reims/reims.go index 17dcc5d..4785cac 100644 --- a/internal/driver/reims/reims.go +++ b/internal/driver/reims/reims.go @@ -548,6 +548,7 @@ func parseDisplayCount(v string) int { } // ParseShow parses the text output of `reims-ctl show ` while redacting MAC addresses. +// Paths under the user's home are abbreviated upstream (3731afd, cli.rs:1117,1189-1192). func ParseShow(name, text string) VMDetail { d := VMDetail{ Name: name, @@ -577,7 +578,7 @@ func ParseShow(name, text string) VMDetail { case "kind": d.Kind = v case "directory": - d.Directory = v + d.Directory = config.ExpandHomePath(v) case "cpus": d.CPUs, _ = strconv.Atoi(v) case "memory": @@ -622,7 +623,7 @@ func ParseShow(name, text string) VMDetail { d.HasOpenCore = v != "" case "installer": d.HasInstaller = v != "" && !strings.EqualFold(v, "none") - d.InstallerPath = v + d.InstallerPath = config.ExpandHomePath(v) case "cpu model": d.CPUModel = v case "audio": diff --git a/internal/driver/reims/reims_test.go b/internal/driver/reims/reims_test.go index 3f11d31..e399834 100644 --- a/internal/driver/reims/reims_test.go +++ b/internal/driver/reims/reims_test.go @@ -1,6 +1,7 @@ package reims import ( + "path/filepath" "reflect" "strings" "testing" @@ -63,6 +64,26 @@ func TestParseShowRedactsMACAndDetectsInstaller(t *testing.T) { } } +func TestParseShowExpandsHomeRelativePaths(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + vm := filepath.Join("Documents", "MacEmu", "data", "reims-ctl", "vms", "macos-15") + image := filepath.Join("Documents", "MacEmu", "data", "reims-ctl", "installers", "macOS-Sequoia-15.7-offline-installer.img") + tilde := "~" + string(filepath.Separator) + d := ParseShow("macos-15", " directory "+tilde+vm+"\n installer "+tilde+image+"\n") + if want := filepath.Join(home, vm); d.Directory != want { + t.Errorf("VM directory = %q, want %q", d.Directory, want) + } + if want := filepath.Join(home, image); !d.HasInstaller || d.InstallerPath != want { + t.Errorf("installer attached = %t, path = %q, want attached at %q", d.HasInstaller, d.InstallerPath, want) + } + d = ParseShow("macos-15", " directory ~\n installer none\n") + if d.Directory != home || d.HasInstaller { + t.Errorf("VM directory = %q, want %q, installer attached = %t, want false", d.Directory, home, d.HasInstaller) + } +} + func TestOfflineCreateTurnsNetworkOff(t *testing.T) { args, err := BuildCreateArgs(CreateOpts{Name: "m1", MacOS: "26.7.3", Offline: true}) if err != nil { diff --git a/internal/installguide/progress.go b/internal/installguide/progress.go index f03ff4e..ca44f8f 100644 --- a/internal/installguide/progress.go +++ b/internal/installguide/progress.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/Nwflower/reims-go/internal/config" ) const progressFile = ".reims-go-install-guide.json" @@ -55,7 +57,9 @@ func (g *Guide) Resume(dir, media string, recovery bool) bool { return recovery } if recovery { - if cp.Kind == g.Kind && strings.EqualFold(cp.Media, media) && cp.Step.recoveryStep(g.Kind) { + // Older checkpoints may contain the abbreviated installer path from show. + sameMedia := strings.EqualFold(config.ExpandHomePath(cp.Media), media) + if cp.Kind == g.Kind && sameMedia && cp.Step.recoveryStep(g.Kind) { g.initial = cp.Step } return true diff --git a/internal/installguide/session_windows_test.go b/internal/installguide/session_windows_test.go index 4e9edef..b6040ff 100644 --- a/internal/installguide/session_windows_test.go +++ b/internal/installguide/session_windows_test.go @@ -2,6 +2,7 @@ package installguide import ( "errors" + "path/filepath" "testing" "time" ) @@ -148,3 +149,17 @@ func TestGuideKeepsMediaAndVMProgressSeparate(t *testing.T) { t.Fatal("clone inherited source disk preparation") } } + +func TestGuideResumesWithExpandedInstallerPath(t *testing.T) { + home := t.TempDir() + t.Setenv("USERPROFILE", home) + dir := t.TempDir() + g := Guide{Name: "macos-15", Kind: Offline, PID: 33} + g.Resume(dir, `~\macOS-15-offline-installer.img`, true) + if err := g.save(command, 0); err != nil { + t.Fatal(err) + } + if !g.Resume(dir, filepath.Join(home, "macOS-15-offline-installer.img"), true) || g.initial != command { + t.Fatalf("guide resumed at %s, want %s", g.initial, command) + } +}