fix(display): address PR #189 review feedback on guest_lang order and pending change merge
This commit is contained in:
1 parent
e25c7952ca
commit
e0a25bb243
3 files changed
+63
-32
No files matched your search
@@ -189,7 +189,7 @@ func (a *App) ApplyVMSettings(opts reims.SetOpts) (err error) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if diffs := pendingVMSettingsChanges(outcome); len(diffs) > 0 {
|
||||
if diffs := pendingVMSettingsChanges(outcome, opts); len(diffs) > 0 {
|
||||
a.mu.Lock()
|
||||
key := vmKey(opts.Name)
|
||||
a.pendingChanges[key] = mergePendingChanges(a.pendingChanges[key], diffs)
|
||||
@@ -203,26 +203,36 @@ func (a *App) ApplyVMSettings(opts reims.SetOpts) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
func pendingVMSettingsChanges(outcome macosdriver.SettingsOutcome) []PendingChange {
|
||||
func pendingVMSettingsChanges(outcome macosdriver.SettingsOutcome, opts reims.SetOpts) []PendingChange {
|
||||
if !outcome.Valid || !outcome.Running {
|
||||
return nil
|
||||
}
|
||||
before, after := outcome.Before, outcome.After
|
||||
return slices.DeleteFunc([]PendingChange{
|
||||
{Field: "cpus", From: strconv.Itoa(before.CPUs), To: strconv.Itoa(after.CPUs)},
|
||||
{Field: "memory", From: before.Memory, To: after.Memory},
|
||||
{Field: "auto_resolution", From: strconv.FormatBool(before.AutoResolution), To: strconv.FormatBool(after.AutoResolution)},
|
||||
{Field: "resolution", From: before.Resolution, To: after.Resolution},
|
||||
{Field: "gpu", From: before.GPU, To: after.GPU},
|
||||
{Field: "displays", From: strconv.Itoa(before.Displays), To: strconv.Itoa(after.Displays)},
|
||||
{Field: "disk_controller", From: before.DiskController, To: after.DiskController},
|
||||
{Field: "disk_cache", From: before.DiskCache, To: after.DiskCache},
|
||||
{Field: "borderless", From: strconv.FormatBool(before.Borderless), To: strconv.FormatBool(after.Borderless)},
|
||||
{Field: "nested_virt", From: strconv.FormatBool(before.NestedVirt), To: strconv.FormatBool(after.NestedVirt)},
|
||||
{Field: "audio", From: strconv.FormatBool(before.Audio), To: strconv.FormatBool(after.Audio)},
|
||||
{Field: "audio_backend", From: before.AudioBackend, To: after.AudioBackend},
|
||||
{Field: "guest_lang", From: before.GuestLang, To: after.GuestLang},
|
||||
}, noPendingChange)
|
||||
candidates := []struct {
|
||||
touched bool
|
||||
change PendingChange
|
||||
}{
|
||||
{opts.CPUs > 0, PendingChange{Field: "cpus", From: strconv.Itoa(before.CPUs), To: strconv.Itoa(after.CPUs)}},
|
||||
{opts.Memory != "", PendingChange{Field: "memory", From: before.Memory, To: after.Memory}},
|
||||
{opts.AutoResolution != nil, PendingChange{Field: "auto_resolution", From: strconv.FormatBool(before.AutoResolution), To: strconv.FormatBool(after.AutoResolution)}},
|
||||
{opts.Resolution != "", PendingChange{Field: "resolution", From: before.Resolution, To: after.Resolution}},
|
||||
{opts.GPU != "", PendingChange{Field: "gpu", From: before.GPU, To: after.GPU}},
|
||||
{opts.Displays > 0, PendingChange{Field: "displays", From: strconv.Itoa(before.Displays), To: strconv.Itoa(after.Displays)}},
|
||||
{strings.TrimSpace(opts.DiskController) != "", PendingChange{Field: "disk_controller", From: before.DiskController, To: after.DiskController}},
|
||||
{strings.TrimSpace(opts.DiskCache) != "", PendingChange{Field: "disk_cache", From: before.DiskCache, To: after.DiskCache}},
|
||||
{opts.Borderless != nil, PendingChange{Field: "borderless", From: strconv.FormatBool(before.Borderless), To: strconv.FormatBool(after.Borderless)}},
|
||||
{opts.NestedVirt != nil, PendingChange{Field: "nested_virt", From: strconv.FormatBool(before.NestedVirt), To: strconv.FormatBool(after.NestedVirt)}},
|
||||
{opts.Audio != nil, PendingChange{Field: "audio", From: strconv.FormatBool(before.Audio), To: strconv.FormatBool(after.Audio)}},
|
||||
{opts.AudioBackend != "", PendingChange{Field: "audio_backend", From: before.AudioBackend, To: after.AudioBackend}},
|
||||
{strings.TrimSpace(opts.GuestLang) != "", PendingChange{Field: "guest_lang", From: before.GuestLang, To: after.GuestLang}},
|
||||
}
|
||||
out := make([]PendingChange, 0, len(candidates))
|
||||
for _, item := range candidates {
|
||||
if item.touched || !noPendingChange(item.change) {
|
||||
out = append(out, item.change)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func mergePendingChanges(existing, incoming []PendingChange) []PendingChange {
|
||||
|
||||
@@ -401,6 +401,24 @@ func TestMergePendingChangesKeepsRunningValues(t *testing.T) {
|
||||
if got := mergePendingChanges(existing, incoming); !slices.Equal(got, want) {
|
||||
t.Fatalf("merge = %+v, want %+v", got, want)
|
||||
}
|
||||
// When show reports live running value (4 -> 4) while user explicitly sets cpus back to 4,
|
||||
// pendingVMSettingsChanges still emits cpus so mergePendingChanges removes stale 4 -> 8
|
||||
// without disturbing unrelated pending entries.
|
||||
liveOutcome := macosdriver.SettingsOutcome{
|
||||
Valid: true,
|
||||
Running: true,
|
||||
Before: macosdriver.SettingsState{CPUs: 4, Memory: "8G"},
|
||||
After: macosdriver.SettingsState{CPUs: 4, Memory: "8G"},
|
||||
}
|
||||
diffs := pendingVMSettingsChanges(liveOutcome, reims.SetOpts{Name: displayFixtureVM, CPUs: 4})
|
||||
got := mergePendingChanges([]PendingChange{
|
||||
{Field: "cpus", From: "4", To: "8"},
|
||||
{Field: "memory", From: "8G", To: "12G"},
|
||||
}, diffs)
|
||||
wantAfterRevert := []PendingChange{{Field: "memory", From: "8G", To: "12G"}}
|
||||
if !slices.Equal(got, wantAfterRevert) {
|
||||
t.Fatalf("merge after live-value revert = %+v, want %+v", got, wantAfterRevert)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyAutoResolutionTransaction(t *testing.T) {
|
||||
@@ -513,20 +531,15 @@ func TestApplyAutoResolutionRollsBackDisplayOnLaterFailure(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
on := true
|
||||
opts := reims.SetOpts{Name: displayFixtureVM, AutoResolution: &on, NestedVirt: &on}
|
||||
opts := reims.SetOpts{Name: displayFixtureVM, Memory: "4G", AutoResolution: &on, NestedVirt: &on}
|
||||
if tc.blockedFile == ".reims-go-guest-lang" {
|
||||
opts.GuestLang = "zh-Hans:0"
|
||||
}
|
||||
if err := a.ApplyVMSettings(opts); err == nil || !strings.Contains(err.Error(), tc.blockedFile) {
|
||||
t.Fatalf("save = %v, want error writing %s", err, tc.blockedFile)
|
||||
}
|
||||
var want []string
|
||||
if tc.state == "stopped" && opts.GuestLang != "" {
|
||||
res, _ := host.RecommendAutoResolution(host.PrimaryDisplayOf(host.ConnectedDisplays()), false, true)
|
||||
want = []string{"set test-vm --resolution " + res, "set test-vm --resolution " + initial}
|
||||
}
|
||||
if commands := fixture().Commands; !slices.Equal(commands, want) {
|
||||
t.Fatalf("commands = %v, want %v", commands, want)
|
||||
if commands := fixture().Commands; len(commands) != 0 {
|
||||
t.Fatalf("commands = %v, want no reims-ctl set call before local failures", commands)
|
||||
}
|
||||
if got := vmstart.ReadVMDisplay(dir); got != prev || len(a.pendingChanges) != 0 {
|
||||
t.Fatalf("display = %+v, want %+v; pending = %+v", got, prev, a.pendingChanges)
|
||||
|
||||
@@ -440,12 +440,13 @@ func ApplyVMOptions(ctx context.Context, c *reims.Client, cur reims.VMDetail, op
|
||||
}
|
||||
}
|
||||
}
|
||||
ctlCommitted := false
|
||||
if nextDisplay != prevDisplay {
|
||||
if _, err := vmstart.SaveVMDisplay(cur.Directory, nextDisplay); err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
if err != nil && !ctlCommitted {
|
||||
if _, rollbackErr := vmstart.SaveVMDisplay(cur.Directory, prevDisplay); rollbackErr != nil {
|
||||
err = errors.Join(err, rollbackErr)
|
||||
}
|
||||
@@ -462,25 +463,32 @@ func ApplyVMOptions(ctx context.Context, c *reims.Client, cur reims.VMDetail, op
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
if err != nil && !ctlCommitted {
|
||||
if rollbackErr := vmstart.SaveVMNestedVirt(cur.Directory, prevNestedVirt); rollbackErr != nil {
|
||||
err = errors.Join(err, rollbackErr)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
guestLangAfterSet := !live && opts.Graphics != "" && opts.Graphics != cur.Graphics
|
||||
if !guestLangAfterSet {
|
||||
if err := applyGuestLang(cur); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if reims.HasCtlSetChanges(opts) {
|
||||
if err := c.Set(ctx, opts, stdout, stderr); err != nil {
|
||||
return err
|
||||
}
|
||||
ctlCommitted = true
|
||||
}
|
||||
if err := applyGuestLang(cur); err != nil {
|
||||
if syncAutoResolution && opts.Resolution != "" && opts.Resolution != cur.Resolution {
|
||||
if rollbackErr := c.Set(context.WithoutCancel(ctx), reims.SetOpts{Name: opts.Name, Resolution: cur.Resolution}, stdout, stderr); rollbackErr != nil {
|
||||
return errors.Join(err, rollbackErr)
|
||||
if guestLangAfterSet {
|
||||
if err := applyGuestLang(cur); err != nil {
|
||||
if outcome != nil {
|
||||
*outcome = settingsOutcome(cur, opts, nextDisplay.AutoResolution, live)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
if outcome != nil {
|
||||
*outcome = settingsOutcome(cur, opts, nextDisplay.AutoResolution, live)
|
||||
|
||||
Reference in new issue
Block a user