Files
w3cjandClaude Opus 5.5 e6f64dae31 refactor(vm): one VM inventory per run
vm.Inventory (internal/vm/inventory.go) holds each project VM's state,
backend and Instance, read in one place: ReadStates for the states
alone, ReadInventory (UsedBackends or AllBackends) for the backends'
lists too, each within the host's list wait (30 s), with the backend's
Check saying why a list failed. It replaces Env.mounts, recordedVMs,
vm.List and vm.ToolsInUse, linkstate's clientStates listing, the
secret list's, Leftovers.Find's (and backendVMs), cli/space.go's two
(backendRows' list, vmFolders), images' state.Backends and sshconfig's
running list: sshconfig.Write takes the inventory's Running VMs.
state.ReadAll reads each VM folder on its own (only the inventory calls
it, a structure-test callers rule); state.Backends and Registry.Used go.

One failure policy:
- a backend that can't list fails a listing (Inventory.Err) only when
  some VM's state names it: devvm ls and the ssh config fail, and the
  ssh config stays as it was rather than lose that backend's entries;
  space and gc --orphans name it and leave its VMs alone;
- an unreadable vm.json is its own row: ls lists the VM with its name
  and status alone and warns (vm_state_unreadable); links, the secret
  list, rm VM and link targets leave it out; gc --orphans never takes it
  for an orphan; ToolsInUse and Mounts (images, gc, space, the SSH
  agent's mounted-key check) fail on it, naming the file;
- FolderOf is bounded (5 s): a folder on a hung share is there.

Mounts come from the states alone, stopped VMs and agents' folders
included, so the mounted-key check asks no backend. ui.Table shows every
cell through msg.OneLine, so ls's cells can't move the cursor or break
a row.

Docs: internals/overview's listing, troubleshooting's file devvm can't
read, ssh's mounted keys, contributing's owners table.

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

104 lines
2.9 KiB
Go

package ui
import (
"fmt"
"strings"
"testing"
"text/tabwriter"
)
// A plain table is what text/tabwriter writes with padding 2, so tables
// devvm printed with it stay byte for byte the same; a colored one is the
// same once its codes are taken out.
func TestTableLikeTabwriter(t *testing.T) {
t.Parallel()
rows := [][]string{
{"NAME", "STATE", "SIZE", "PATH"},
{"base", "current", "1.0G", "/a"},
{"docker+foo", "stale", "-", "/über/long path"},
{"é", "unfinished", "12K", "-"},
}
var want strings.Builder
w := tabwriter.NewWriter(&want, 0, 0, 2, ' ', 0)
for _, r := range rows {
_, _ = fmt.Fprintln(w, strings.Join(r, "\t"))
}
if err := w.Flush(); err != nil {
t.Fatal(err)
}
for _, on := range []bool{false, true} {
var out strings.Builder
tab := &Table{w: &out, st: style{on: on}}
tab.Header(rows[0]...)
tab.Row(Cell{"base", Plain}, Cell{"current", Good}, Cell{"1.0G", Plain}, Cell{"/a", Secondary})
tab.Row(Cell{"docker+foo", Plain}, Cell{"stale", Attention}, Cell{"-", Plain}, Cell{"/über/long path", Plain})
tab.Total(plainCells(rows[3]...)...)
if err := tab.Flush(); err != nil {
t.Fatal(err)
}
got := out.String()
if on {
for _, painted := range []string{"\033[1mNAME\033[0m ", "\033[32mcurrent\033[0m", "\033[33mstale\033[0m", "\033[2m/a\033[0m", "\033[1munfinished\033[0m"} {
if !strings.Contains(got, painted) {
t.Errorf("colored: no %q in %q", painted, got)
}
}
got = plain(got)
}
if got != want.String() {
t.Errorf("color %v: got\n%q\nwant\n%q", on, got, want.String())
}
}
}
// Break ends a table: the next lines up on its own, after a blank line.
func TestTableBreak(t *testing.T) {
t.Parallel()
var out strings.Builder
tab := &Table{w: &out}
tab.Header("A", "B")
tab.Row(plainCells("long name", "x")...)
if err := tab.Break(); err != nil {
t.Fatal(err)
}
tab.Header("C", "D")
if err := tab.Flush(); err != nil {
t.Fatal(err)
}
if got, want := out.String(), "A B\nlong name x\n\nC D\n"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}
// A cell's text is shown on one line, its control and bidi characters
// dropped: a VM's folder with a newline or an escape never breaks a row
// or moves the cursor.
func TestTableCellsOneLine(t *testing.T) {
t.Parallel()
var out strings.Builder
tab := &Table{w: &out}
tab.Row(Cell{Text: "devvm-x\033[2J"}, Cell{Text: "/p/a\nb" + string(rune(0x202e))})
tab.Total(Cell{Text: "\tall"}, Cell{Text: "1\r"})
if err := tab.Flush(); err != nil {
t.Fatal(err)
}
if got, want := out.String(), "devvm-x[2J /p/ab\nall 1\n"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}
// A UI's table is colored as its stdout is.
func TestUITable(t *testing.T) {
t.Parallel()
u, out, _ := testUI(t, "")
u.outStyle = style{on: true}
tab := u.Table()
tab.Header("A")
if err := tab.Flush(); err != nil {
t.Fatal(err)
}
if got := out.String(); got != "\033[1mA\033[0m\n" {
t.Errorf("got %q", got)
}
}