fix(installer): expand home paths from reims-ctl show

This commit is contained in:
YXC0915 committed 2026-12-01 08:22:58 +08:00
1 parent 9eb8d95443
commit 790a42912e
5 files changed
+60 -3

No files matched your search

+16
View File
@@ -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 {
+3 -2
View File
@@ -548,6 +548,7 @@ func parseDisplayCount(v string) int {
}
// ParseShow parses the text output of `reims-ctl show <name>` 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":
+21
View File
@@ -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 {
+5 -1
View File
@@ -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
@@ -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)
}
}