diff --git a/internal/modules/renode/renode.go b/internal/modules/renode/renode.go index 25da265..b722281 100644 --- a/internal/modules/renode/renode.go +++ b/internal/modules/renode/renode.go @@ -5,7 +5,6 @@ package renode import ( "bytes" "context" - "errors" "embed" "fmt" "os" @@ -188,5 +187,50 @@ func (m Module) Verify(ctx context.Context) error { } func (m Module) PlanForce(ctx context.Context) (module.Plan, error) { - return module.Plan{}, errors.New("PlanForce not implemented") + bin, err := binPath() + if err != nil { + return module.Plan{}, err + } + rendered, err := m.renderSim() + if err != nil { + return module.Plan{}, err + } + simPath := filepath.Join(bin, "arduino-sim") + wokwiPath := filepath.Join(bin, "wokwi-cli") + + actions := []module.Action{ + { + Description: "ensure yay installed", + Kind: module.ActionPackageInstall, + Exec: func(ctx context.Context) error { return m.aur.EnsureInstalled(ctx) }, + }, + { + Description: "yay -S (force) renode-bin", + Kind: module.ActionPackageInstall, + Exec: func(ctx context.Context) error { return m.aur.Reinstall(ctx, []string{"renode-bin"}) }, + }, + { + Description: "pacman -S (force) arm-none-eabi-gdb", + Kind: module.ActionPackageInstall, + Exec: func(ctx context.Context) error { return m.pm.Reinstall(ctx, []string{"arm-none-eabi-gdb"}) }, + }, + { + Description: "rewrite arduino-sim helper (force)", + Kind: module.ActionFileWrite, + Exec: func(ctx context.Context) error { + return platform.WriteUserFile(simPath, rendered, 0o755) + }, + }, + { + Description: "redownload wokwi-cli " + m.cfg.WokwiVersion + " (force)", + Kind: module.ActionFileWrite, + Exec: func(ctx context.Context) error { + if err := os.MkdirAll(filepath.Dir(wokwiPath), 0o755); err != nil { + return err + } + return platform.Download(ctx, m.cfg.WokwiURL, wokwiPath, 0o755) + }, + }, + } + return module.Plan{Actions: actions}, nil } diff --git a/internal/modules/renode/renode_test.go b/internal/modules/renode/renode_test.go index efc29ff..2a14859 100644 --- a/internal/modules/renode/renode_test.go +++ b/internal/modules/renode/renode_test.go @@ -142,4 +142,52 @@ func TestApplyRendersSimScriptAndDownloadsWokwi(t *testing.T) { } } +func TestPlanForceReinstallsEverything(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Write([]byte("wokwi-forced")) + })) + defer srv.Close() + + // Even with everything already "installed", force plan should be non-empty. + os.MkdirAll(filepath.Join(home, ".local", "bin"), 0o755) + os.WriteFile(filepath.Join(home, ".local", "bin", "wokwi-cli"), []byte("stale"), 0o755) + r := &fakeRunner{installed: map[string]bool{"yay": true, "renode-bin": true, "arm-none-eabi-gdb": true}} + m := New(sampleCfg(srv.URL), platform.NewPacman(r), platform.NewYay(r)) + + plan, err := m.PlanForce(context.Background()) + if err != nil { + t.Fatalf("PlanForce: %v", err) + } + if plan.IsEmpty() { + t.Fatal("force plan must not be empty") + } + for _, a := range plan.Actions { + if err := a.Exec(context.Background()); err != nil { + t.Fatalf("Exec %q: %v", a.Description, err) + } + } + var sawYayReinstall, sawPacmanReinstall bool + for _, c := range r.runCalls { + if len(c) >= 3 && c[0] == "yay" && c[1] == "-S" && c[2] == "--noconfirm" { + sawYayReinstall = true + } + if len(c) >= 4 && c[0] == "sudo" && c[1] == "pacman" && c[2] == "-S" && c[3] == "--noconfirm" { + sawPacmanReinstall = true + } + } + if !sawYayReinstall { + t.Errorf("no yay reinstall call; calls=%v", r.runCalls) + } + if !sawPacmanReinstall { + t.Errorf("no pacman reinstall call; calls=%v", r.runCalls) + } + // wokwi-cli should have been overwritten with the fresh payload + wokwi, _ := os.ReadFile(filepath.Join(home, ".local", "bin", "wokwi-cli")) + if string(wokwi) != "wokwi-forced" { + t.Errorf("wokwi-cli not overwritten: %q", wokwi) + } +} + var _ module.Module = Module{}