Files
devvm/internal/cli/setup_test.go
T
w3cjandClaude Opus 5.5 28e289c190 feat(setup): ask for new VMs' CPUs and memory at first run and in devvm setup
The first run, in a terminal, before base's features are first chosen,
asks for the CPUs and memory new VMs get, when no default size is saved
and neither --cpus nor --memory is given; devvm setup asks after the
backend and features, and --cpus/--memory choose without asking (with
only them, the backend and features stay). Each question starts from the
saved size, else base.yaml's fitted to this machine (FitCPUs/FitMemory);
Enter keeps it, a value Size.Check refuses is warned and asked again.
Only typed values are saved, in the size file devvm size -g writes
(a saved disk kept); keeping both writes nothing. Without a terminal
nothing is asked.

- internal/vm/sizechoice.go: SizeChoice (Ask, Set, Fitted), writeDefaultSize,
  creation.firstRunSize; SetSize -g uses SizeChoice.Set
- internal/vm/size.go: baseSetting (BaseDisk on it), readDefaultSize
- internal/vm/env.go: Env.Answer and Env.Interactive (stdin and stdout
  terminals); vm.UI gains Answer and StdoutIsTerminal
- internal/vm/up.go: plan asks before reading the default size
- internal/ui: UI.Answer, a line prompt; largeTypes allows UI 41 methods
- internal/cli/setup.go: setupChoice, --cpus/--memory, saveBackend,
  saveFeatures; Short/Long updated
- messages: size_question, size_cpus_prompt, size_memory_prompt
  (refusals reuse too_many_cpus, too_much_memory, cpus_not_number;
  size_defaults after saving)
- tests: TestSizeChoiceAsk, TestSizeChoiceSet, TestFirstRunSize, setup's
  size prompts; setup.txtar (--cpus/--memory, refusals, no terminal),
  up_size.txtar (first run without a terminal)
- docs: first-vm (paragraph, diagram), ports-size (Choosing new VMs'
  size), customizing, backends, settings; CLI reference regenerated

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-30 23:44:33 -07:00

141 lines
4.8 KiB
Go

package cli
import (
"errors"
"io/fs"
"os"
"path/filepath"
"slices"
"testing"
"github.com/w3cj/devvm/internal/msg"
"github.com/w3cj/devvm/internal/paths"
"github.com/w3cj/devvm/internal/state"
"github.com/w3cj/devvm/internal/ui"
"github.com/w3cj/devvm/internal/vm"
)
// asked is what setup's backend picker was shown.
type asked struct {
help string
choices []ui.Choice
}
// setupSandbox points devvm's folders at a temporary HOME, with a feature
// of the user's, and returns a setup that sees a terminal (or not) and
// picks the backend answer names, keeping the size with Enter, on a host
// of 8 CPUs and 32 GiB; the folders; what the picker was shown, nil until
// it's asked; and the size prompts shown.
func setupSandbox(t *testing.T, terminal bool, answer string) (setup, paths.Dirs, func() *asked, *[]string) {
t.Helper()
t.Setenv("DEVVM_MESSAGES", "names")
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "config"))
t.Setenv("DEVVM_LIMA_HOME", "")
d, err := paths.FromEnv()
if err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(d.Features(), 0o750); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(d.Features(), "mine.sh"), []byte("#!/bin/bash\n# mine\n"), 0o600); err != nil {
t.Fatal(err)
}
var a *asked
var sized []string
u, _, _ := testUI()
s := setup{
ui: u,
terminal: func() bool { return terminal },
pickOne: func(help msg.M, cs []ui.Choice) int {
if a != nil {
t.Error("asked for the backend twice")
}
a = &asked{help.String(), slices.Clone(cs)}
return slices.IndexFunc(cs, func(c ui.Choice) bool { return c.Name == answer })
},
answer: func(m msg.M) string { sized = append(sized, m.Names()); return "" },
host: func() (vm.Host, error) { return vm.Host{CPUs: 8, MemoryGiB: 32}, nil },
}
return s, d, func() *asked { return a }, &sized
}
// readBackend is the default backend file's name, "" without one.
func readBackend(t *testing.T, d paths.Dirs) string {
t.Helper()
name, err := state.ReadBackend(d)
if err != nil {
t.Fatal(err)
}
return name
}
// In a terminal, setup with neither flag asks for the backend (starting
// from the current one, smolvm marked experimental), then the features;
// --backend, --features or no terminal ask no backend.
func TestSetupAsksForTheBackend(t *testing.T) {
none := ""
for _, tc := range []struct {
name string
terminal bool
flag string
features *string
current string // the default backend file's; "" for none
ask bool
want string // the default backend file's afterwards
start string // the choice the picker starts on
chosen bool // base-features written
}{
{name: "a terminal, nothing set", terminal: true, ask: true, start: "lima", want: "smolvm", chosen: true},
{name: "a terminal, smolvm set", terminal: true, current: "smolvm", ask: true, start: "smolvm", want: "smolvm", chosen: true},
{name: "a terminal, an unknown one set", terminal: true, current: "qemu", ask: true, start: "lima", want: "smolvm", chosen: true},
{name: "no terminal", want: "", chosen: true},
{name: "--backend", terminal: true, flag: "lima", want: "lima"},
{name: "--features", terminal: true, features: &none, current: "lima", want: "lima", chosen: true},
} {
t.Run(tc.name, func(t *testing.T) {
s, d, shown, sized := setupSandbox(t, tc.terminal, "smolvm")
if tc.current != "" {
if err := state.WriteBackend(d, tc.current); err != nil {
t.Fatal(err)
}
}
if err := s.run(setupChoice{features: tc.features, backend: tc.flag}); err != nil {
t.Fatal(err)
}
wantSized := []string(nil)
if tc.ask {
wantSized = []string{"size_cpus_prompt cpus=4", "size_memory_prompt memory=8"}
}
if !slices.Equal(*sized, wantSized) {
t.Errorf("size prompts %q, want %q", *sized, wantSized)
}
if _, err := os.Stat(d.Size()); !errors.Is(err, fs.ErrNotExist) {
t.Errorf("Enter wrote the size file: %v", err)
}
if a := shown(); (a != nil) != tc.ask {
t.Fatalf("asked %v, want %v", a != nil, tc.ask)
} else if a != nil {
want := []ui.Choice{
{Name: "docker", Desc: "backend_experimental", On: tc.start == "docker"},
{Name: "lima", On: tc.start == "lima"},
{Name: "smolvm", Desc: "backend_experimental", On: tc.start == "smolvm"},
}
if a.help != "backend_help" || !slices.Equal(a.choices, want) {
t.Errorf("shown %q %+v, want backend_help %+v", a.help, a.choices, want)
}
}
if got := readBackend(t, d); got != tc.want {
t.Errorf("default backend %q, want %q", got, tc.want)
}
_, err := os.Stat(d.BaseFeatures())
if written := !errors.Is(err, fs.ErrNotExist); written != tc.chosen {
t.Errorf("base-features written %v, want %v", written, tc.chosen)
}
})
}
}