Files
w3cjandClaude Opus 5.5 e132a29b5a refactor(cli): one per-run app holds the run's services
NewRoot makes one app for the run's UI and hands it to every command.
The app makes each service the first time a command asks for it and
keeps it, or its error, for the rest of the run: devvm's folders
(dirs), a test build's overrides, the secrets store, the backends'
registry, the shared tools, the VM environment (vmEnv) and the image
builder of the backend new VMs get (builder). The retention that runs
before a command and the command itself share them.

- paths.FromEnv: 23 of 24 calls go through a.dirs; partNames, which the
  parser's flag and word checks call before any command, keeps its own.
- The registry is made once (8 newRegistry calls and newEnv's).
- builder(u) (6 calls) and cli's newBuilder are gone: a.builder and the
  per-backend builders in images, space and gc are vm.Env.Builder,
  exported, the only builder. setup's feature choice takes a build.Env
  of its folders and prompts alone.
- The shared tools are made once (newTools and vm.NewEnv's), and the
  secrets store once (companyProxy's, newTools', vm.NewEnv's and
  _proxy's): vm.NewEnv takes the run's tools and store.
- withEnv (46 calls) is a.env; autoRetain, retainOrphans and gc
  --orphans use the run's VM environment.
- thisHost and readOverrides' other callers read the overrides once
  (a.host); space and gc --caches time out on the registry's host.

TestAppMakesEachServiceOnce checks that one run makes each service once
and that the VM environment and builder share the run's registry and
tools; TestAppKeepsAFailure that a failure is kept.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 00:53:51 -07:00

71 lines
1.7 KiB
Go

package cli
import (
"errors"
"testing"
)
// One run makes each service once, however many commands and services
// ask for it, and the services share it: the VM environment's registry
// and tools are the run's, and so are the builder's.
func TestAppMakesEachServiceOnce(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("XDG_DATA_HOME", t.TempDir())
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("DEVVM_LIMA_HOME", t.TempDir())
u, _, _ := testUI()
a := newApp(u)
for range 3 {
if _, err := a.builder(); err != nil {
t.Fatal(err)
}
if _, err := a.vmEnv(); err != nil {
t.Fatal(err)
}
if _, err := a.registry(); err != nil {
t.Fatal(err)
}
if _, err := a.tools(); err != nil {
t.Fatal(err)
}
if _, err := a.secrets(); err != nil {
t.Fatal(err)
}
if _, err := a.dirs(); err != nil {
t.Fatal(err)
}
a.host()
}
for name, n := range map[string]int{
"dirs": a.folders.n, "overrides": a.overrides.n, "secrets": a.store.n, "registry": a.reg.n,
"tools": a.tl.n, "vm env": a.vm.n, "builder": a.b.n,
} {
if n != 1 {
t.Errorf("%s made %d times, want once", name, n)
}
}
env, _ := a.vmEnv()
reg, _ := a.registry()
tl, _ := a.tools()
b, _ := a.builder()
if env.Registry != reg || env.Tools != tl || b.Tools != tl {
t.Error("a service isn't the run's")
}
}
// A service that fails to be made fails for the rest of the run, without
// being made again.
func TestAppKeepsAFailure(t *testing.T) {
var m made[int]
calls := 0
fail := errors.New("no")
for range 2 {
if _, err := m.get(func() (int, error) { calls++; return 0, fail }); !errors.Is(err, fail) {
t.Errorf("err = %v", err)
}
}
if calls != 1 || m.n != 1 {
t.Errorf("made %d times", calls)
}
}