mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 16:27:33 -10:00
A first run built its tool images one after another, each paying a build VM's boot (5.7 s on vz, 9.3 s on QEMU) before a download of a few seconds to a minute. Share.Build now hands the images out to up to four slots (buildAll), each slot's builds one after another in a build VM of its own: - slots: as many as the host's memory, less what the backend's running VMs have, and its CPUs give a build VM of 2 GiB and 2 CPUs each after keeping 6 GiB and 2 CPUs, at most backend.MaxToolSlots (4) and the number of images; one alone keeps base's size. A backend that can't list its VMs builds one at a time. A running VM's memory counts whole (7.5 GiB, not 7): TestBuildSlotsFit's running VMs fail when it isn't counted or is truncated. - plan lowers the slots until the backend's disks have room: Floors.ForTools counts Tools once for each build VM at once, as on a disk without copy on write each is a whole copy of base. - backend.ToolsSpec gets Slot, Slots, Size and Hold; Lima's slot VMs are the constants ToolsVM to ToolsVM4, sized with --cpus/--memory, and the first slot's build deletes what a killed run left in the slots this run doesn't use. Builds hold the share's lock, so one devvm uses the slots. - A failed build's VM goes in the run's cleanup, which runs after the build let go of the share's lock: it deletes the VM only once it has the lock again (ToolsSpec.Hold, Env.holdShare's TryLockFile), and leaves it while another devvm holds it (a build, gc or the daily refresh) (TestBuildToolsCleanupHolds, TestBuildHoldsShareForCleanup). The next tools build deletes what it finds in the slots, and so does devvm gc, which holds the share's lock: backend.Images' DeleteToolsLeft, Lima's deleting the slots' build VMs that are there and no other VM, which gc asks of every backend first, without asking, a backend that can't being a run log line. It returns what it deleted, which gc names in one line (tools_build_vms_deleted), and gc then never says nothing_to_clean or nothing_deleted (gc_tools_build.txtar, TestDeleteToolsLeft in lima and vm, the fake's ToolsLeft). The cli skill's judgment.md names this exception to "gc --force deletes exactly what gc lists". The conformance suite's delete-tools-left checks that it deletes no other VM, not even the project VM of a folder named tools (devvm-tools-<hash>), and no image (TestConformanceFinds: deletesAllVMs, deletesImage, deletesByPrefix). The share's lock is the data folder's, so devvms with their own data folders and one Lima home don't exclude each other's builds: the docs say not to build tools or run gc in two at once (a lock per build VM in the Lima home would close it, but it would replace Hold and needs e2e). - Side by side, one status line covers the builds (building_tools_together), and their commands' lines in the run log are tagged with their tool (msg.WithLogTag): the UI's LoggedCmd prefixes them and shows no status line of its own. Every write starts with the tag, as does every line in it, one ended by "\r" included, so no build can pass a line off as another's (TestTaggedWrites), and a tool's lines are found after another's "\r" (taggedLines splits there, a progress bar's updates staying one line: TestTaggedLines). The first failure cancels the others and is what Build returns, tagged (backend.LoggedIn), so ErrorLogged shows that tool's lines' end. - A test build's DEVVM_TOOL_SLOTS caps the slots; the CLI tests set 1, so the stub's calls keep their order, and tools_side_by_side.txtar sets 3, with an interrupt that leaves no build VM. The stub keeps each build VM's folder by its name, and the fake backend's BuildTools runs its builds outside its lock, as a real backend's would, passing ToolsBuild its context. Docs: images.md's Building tools and Free space, ports-size.md, space.md, images.md (using) and gc's help (the build VMs gc deletes), settings.md (one Lima home for two data folders), extending.md, testing.md and tests.md; the cli skill's judgment.md; the CHANGELOG's first highlight and the release page's A faster first run. New messages: building_tools_together, tools_build_vms_deleted. Co-Authored-By: Claude Opus 5.5 <[email protected]>
217 lines
7.6 KiB
Go
217 lines
7.6 KiB
Go
package ui
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"unicode/utf8"
|
|
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
)
|
|
|
|
// sgr matches the SGR codes devvm prints.
|
|
var sgr = regexp.MustCompile("\033\\[[0-9;]*m")
|
|
|
|
func plain(s string) string { return sgr.ReplaceAllString(s, "") }
|
|
|
|
func TestColorOn(t *testing.T) {
|
|
t.Parallel()
|
|
for _, tc := range []struct {
|
|
env env
|
|
terminal bool
|
|
want bool
|
|
}{
|
|
{env{}, true, true},
|
|
{env{}, false, false},
|
|
{env{"TERM": "xterm-256color"}, true, true},
|
|
{env{"TERM": "dumb"}, true, false},
|
|
{env{"NO_COLOR": "1"}, true, false},
|
|
{env{"NO_COLOR": ""}, true, true}, // set but empty: not set
|
|
{env{"FORCE_COLOR": "1"}, false, true},
|
|
{env{"CLICOLOR_FORCE": "1"}, false, true},
|
|
{env{"FORCE_COLOR": "1", "TERM": "dumb"}, false, true},
|
|
{env{"FORCE_COLOR": "0"}, false, false},
|
|
{env{"CLICOLOR_FORCE": "0"}, true, true},
|
|
{env{"FORCE_COLOR": "1", "NO_COLOR": "1"}, true, false},
|
|
{env{"DEVVM_MESSAGES": "names"}, true, false},
|
|
{env{"DEVVM_MESSAGES": "names", "FORCE_COLOR": "1"}, true, false},
|
|
} {
|
|
if got := colorOn(tc.env.get, tc.terminal); got != tc.want {
|
|
t.Errorf("colorOn(%v, terminal %v) = %v, want %v", tc.env, tc.terminal, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMessagesColored(t *testing.T) {
|
|
t.Parallel()
|
|
linked := msg.StopFailed("v")
|
|
plainText, link := linked.Parts()
|
|
if link == "" || strings.Contains(plainText, link) {
|
|
t.Fatalf("stop_failed: want a doc link apart from the text, got %q, %q", plainText, link)
|
|
}
|
|
// Its value bold, the rest as it is.
|
|
var text string
|
|
segs, _ := linked.Segments()
|
|
for _, s := range segs {
|
|
if s.Value {
|
|
text += "\033[1m" + s.Text + "\033[0m"
|
|
} else {
|
|
text += s.Text
|
|
}
|
|
}
|
|
if !strings.Contains(text, "\033[1mv\033[0m") || plain(text) != plainText {
|
|
t.Fatalf("stop_failed's value: %q", text)
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
print func(*UI, msg.M)
|
|
stdout, stder string
|
|
}{
|
|
{"say", (*UI).Say, text + "\n\033[4;34m" + link + "\033[0m\n", ""},
|
|
{"info", (*UI).Info, "\033[1m==>\033[0m " + text + "\n\033[4;34m" + link + "\033[0m\n", ""},
|
|
{"note", (*UI).Note, "", text + "\n\033[4;34m" + link + "\033[0m\n"},
|
|
{"warn", (*UI).Warn, "", "\033[1;33mdevvm:\033[0m " + text + "\n\033[1;33mdevvm:\033[0m \033[4;34m" + link + "\033[0m\n"},
|
|
{"error", (*UI).Error, "", "\033[1;31mdevvm:\033[0m " + text + "\n\033[1;31mdevvm:\033[0m \033[4;34m" + link + "\033[0m\n"},
|
|
} {
|
|
u, out, errOut := testUI(t, "")
|
|
u.outStyle, u.errStyle = style{on: true}, style{on: true}
|
|
tc.print(u, linked)
|
|
if out.String() != tc.stdout || errOut.String() != tc.stder {
|
|
t.Errorf("%s colored: stdout %q, stderr %q; want %q, %q", tc.name, out, errOut, tc.stdout, tc.stder)
|
|
}
|
|
// Plain, it's the same text without the codes.
|
|
u, out, errOut = testUI(t, "")
|
|
u.outStyle, u.errStyle = style{}, style{}
|
|
tc.print(u, linked)
|
|
if got := out.String() + errOut.String(); got != plain(tc.stdout+tc.stder) {
|
|
t.Errorf("%s plain: %q", tc.name, got)
|
|
}
|
|
}
|
|
if plainText+"\n"+link != linked.String() {
|
|
t.Errorf("String: %q", linked.String())
|
|
}
|
|
}
|
|
|
|
// DEVVM_MESSAGES=names gives a stream's style names mode, without color: a
|
|
// message shows as its names, one segment, no link, wherever the UI shows
|
|
// it, and a terminal gets no codes, so e2e's checks of one read the names
|
|
// as they are.
|
|
func TestNamesMode(t *testing.T) {
|
|
t.Parallel()
|
|
names := env{"DEVVM_MESSAGES": "names", "FORCE_COLOR": "1"}.get
|
|
st := newStyle(names, true)
|
|
if st != (style{names: true}) || newStyle(env{}.get, false) != (style{}) {
|
|
t.Fatalf("newStyle: %+v", st)
|
|
}
|
|
m := msg.StopFailed("v")
|
|
if segs, link := st.segments(m); len(segs) != 1 || segs[0] != (msg.Segment{Text: m.Names()}) || link != "" {
|
|
t.Errorf("segments: %+v, %q", segs, link)
|
|
}
|
|
if st.message(m) != m.Names() || st.line(m) != m.Names() || st.line(msg.M{}) != "" {
|
|
t.Errorf("message %q, line %q", st.message(m), st.line(m))
|
|
}
|
|
if text := (style{}).line(m); text != m.Text() {
|
|
t.Errorf("line, not names mode: %q", text)
|
|
}
|
|
u, out, errOut := testUI(t, "")
|
|
u.Say(m)
|
|
u.Warn(m)
|
|
if out.String() != m.Names()+"\n" || errOut.String() != "devvm: "+m.Names()+"\n" || u.Text(m) != m.Names() || u.Table().Text(m) != m.Names() {
|
|
t.Errorf("stdout %q, stderr %q, Text %q", out, errOut, u.Text(m))
|
|
}
|
|
// The messages themselves don't change: String is still the text.
|
|
if m.String() == m.Names() {
|
|
t.Errorf("String in names mode: %q", m.String())
|
|
}
|
|
}
|
|
|
|
func TestErrorLoggedColored(t *testing.T) {
|
|
u, _, errOut := testUI(t, "")
|
|
u.errStyle = style{on: true, names: true}
|
|
if err := u.logged(t.Context(), msg.M{}, "sh", "-c", "printf 'one\\n\\033[31mtwo\\n'"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u.ErrorLogged(msg.MsgUnknown("x"), "")
|
|
got := errOut.String()
|
|
// Each line of the log's end dim, the log's own codes dropped; then the
|
|
// message, its label red.
|
|
head := "\033[2m--- " + msg.LogEnd(u.logPath()).Names() + "\033[0m\n"
|
|
want := "\033[2mone\033[0m\n\033[2m[31mtwo\033[0m\n\033[2m---\033[0m\n" +
|
|
"\033[1;31mdevvm:\033[0m msg_unknown name=x\n"
|
|
if !strings.HasPrefix(got, head) || !strings.HasSuffix(got, want) {
|
|
t.Errorf("ErrorLogged colored:\n%q\nwant it to end\n%q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestColoredStatusLineFits(t *testing.T) {
|
|
t.Parallel()
|
|
u, _, errOut := testUI(t, "")
|
|
u.stderrTTY, u.errStyle = true, style{on: true, names: true}
|
|
u.termWidth = func() int { return 40 }
|
|
u.getenv = env{"LC_ALL": "C"}.get
|
|
if err := u.logged(t.Context(), msg.MsgUnknown("x"), "sh", "-c", "echo "+strings.Repeat("progress", 10)+"; sleep 0.5"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := errOut.String()
|
|
if !strings.Contains(got, "\033[2m progress") {
|
|
t.Errorf("status line: want the log's line dim: %q", got)
|
|
}
|
|
// The plain text fits in the width, whatever the codes add.
|
|
for line := range strings.SplitSeq(got, "\033[?7l") {
|
|
text, _, _ := strings.Cut(line, "\033[?7h")
|
|
if n := utf8.RuneCountInString(plain(text)); n > 39 {
|
|
t.Errorf("wider than the terminal: %d runes: %q", n, text)
|
|
}
|
|
}
|
|
// What fit counts the plain text, so clearing covers the line drawn.
|
|
line, n := u.errStyle.fit([]part{{strings.Repeat("a", 30), ""}, {strings.Repeat("b", 30), sgrDim}}, 39)
|
|
if n != 39 || plain(line) != strings.Repeat("a", 30)+strings.Repeat("b", 9) || clearLine(n, 40) != "\r\033[K" {
|
|
t.Errorf("fit: %q, %d", line, n)
|
|
}
|
|
}
|
|
|
|
// A status line's values are bold, and it's cut on its plain text, a
|
|
// value included, with the value's code closed.
|
|
func TestStatusLineValuesBold(t *testing.T) {
|
|
t.Parallel()
|
|
u, _, errOut := testUI(t, "")
|
|
u.stderrTTY, u.errStyle = true, style{on: true}
|
|
u.getenv = env{"LC_ALL": "C"}.get
|
|
m := msg.StopFailed("a-long-vm-name")
|
|
for _, cols := range []int{80, 12} {
|
|
errOut.Reset()
|
|
u.termWidth = func() int { return cols }
|
|
u.Status(m)()
|
|
got := errOut.String()
|
|
text, _, _ := strings.Cut(strings.TrimPrefix(got, "\r\033[K\033[?7l"), "\033[?7h")
|
|
if n := utf8.RuneCountInString(plain(text)); n > cols-1 {
|
|
t.Errorf("%d columns: %d runes: %q", cols, n, text)
|
|
}
|
|
want := "\033[1ma-long-vm-name\033[0m"
|
|
if cols == 12 {
|
|
want = "\033[1ma-lon\033[0m" // 11 columns: " | " and 5 runes of the value
|
|
}
|
|
if !strings.Contains(text, want) {
|
|
t.Errorf("%d columns: no %q in %q", cols, want, text)
|
|
}
|
|
}
|
|
// Plain, it's the same text.
|
|
u.errStyle = style{}
|
|
u.termWidth = func() int { return 80 }
|
|
errOut.Reset()
|
|
u.Status(m)()
|
|
if !strings.Contains(errOut.String(), " | "+m.Text()+" 0s") {
|
|
t.Errorf("plain: %q", errOut.String())
|
|
}
|
|
}
|
|
|
|
func TestDoneMarkGreen(t *testing.T) {
|
|
t.Parallel()
|
|
u, _, _ := testUI(t, "")
|
|
u.getenv = env{"LC_ALL": "C"}.get
|
|
u.errStyle = style{on: true}
|
|
if got, want := u.doneLine([]part{{"starting ", ""}, {"x", sgrBold}}, 5), " \033[32mok\033[0m starting \033[1mx\033[0m (5s)\n"; got != want {
|
|
t.Errorf("doneLine: got %q, want %q", got, want)
|
|
}
|
|
}
|