Files
w3cjandClaude Opus 5.5 33ec9eb0dc refactor(tools): one definition per toolchain
Each toolchain is one image.ToolDef in image.SharedTools, which also
holds its guest environment, its PATH folders and its version files. They
leave base's devvm-tools.sh for /etc/devvm/toolchains.sh, which every
launch writes, so base.sh changes and every image rebuilds once. The
structure test's names rule fails on a toolchain's name as a literal
outside internal/image, TestToolchainsComplete checks the toolchains
page, and Contributing gains Adding a toolchain.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-08 10:05:29 -07:00

98 lines
3.5 KiB
Go

package assets
import (
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// heredoc is the body of the heredoc that base.sh starts with this line,
// up to its end marker.
func heredoc(t *testing.T, start, end string) string {
t.Helper()
b, err := fs.ReadFile(FS, "layers/base.sh")
if err != nil {
t.Fatal(err)
}
_, rest, ok := strings.Cut(string(b), start+"\n")
block, _, closed := strings.Cut(rest, "\n"+end+"\n")
if !ok || !closed {
t.Fatalf("base.sh has no %q", start)
}
return block
}
// Every VM's shells get the tools' environment from base's
// /etc/profile.d/devvm-tools.sh, which reads the toolchains' environment
// (their telemetry off, their global installs into the VM's home) and
// their folders for the PATH from /etc/devvm/toolchains.sh, and the
// agents' (their self-updates, version checks and telemetry off) from
// /etc/devvm/agents.sh, which every launch's setup writes, so base holds
// none of it (internal/vm's TestToolchainsOwnTheirEnvironment): their
// installs are read-only shared tools, and mise pins their versions.
// Non-login shells read it too (BASH_ENV), but not devvm-telemetry.sh, so
// no agent's switch is there.
func TestToolsEnvironment(t *testing.T) {
block := heredoc(t, "sudo tee /etc/profile.d/devvm-tools.sh >/dev/null <<'SH'", "SH")
for _, line := range []string{
"if [ -r /etc/devvm/toolchains.sh ]; then . /etc/devvm/toolchains.sh; fi",
` export PATH="$devvm_path$HOME/.local/bin:$HOME/.local/share/mise/shims:$PATH" ;;`,
"if [ -r /etc/devvm/agents.sh ]; then . /etc/devvm/agents.sh; fi",
} {
if !strings.Contains("\n"+block+"\n", "\n"+line+"\n") {
t.Errorf("devvm-tools.sh lacks %q", line)
}
}
telemetry := heredoc(t, "sudo tee /etc/profile.d/devvm-telemetry.sh >/dev/null <<'SH'", "SH")
for _, v := range []string{"DISABLE_AUTOUPDATER=", "DISABLE_TELEMETRY=", "DISABLE_ERROR_REPORTING=", "OPENCODE_", "PI_"} {
if strings.Contains(block, "export "+v) || strings.Contains(telemetry, "export "+v) {
t.Errorf("base sets %s, an agent's, which the setup's agents.sh sets", v)
}
}
}
// base.sh's devvm-fetch keeps a download only when its checksum is the
// one given: a SHA-256, or a SHA-512 by its length.
func TestDevvmFetch(t *testing.T) {
for _, c := range []string{"sh", "curl", "sha256sum", "sha512sum", "cut"} {
if _, err := exec.LookPath(c); err != nil {
t.Skip("no " + c)
}
}
dir := t.TempDir()
script := filepath.Join(dir, "devvm-fetch")
if err := os.WriteFile(script, []byte(heredoc(t, "sudo tee /usr/local/bin/devvm-fetch >/dev/null <<'SH'", "SH")+"\n"), 0o600); err != nil {
t.Fatal(err)
}
src := filepath.Join(dir, "src")
body := []byte("an archive\n")
if err := os.WriteFile(src, body, 0o600); err != nil {
t.Fatal(err)
}
s256, s512 := sha256.Sum256(body), sha512.Sum512(body)
for _, tc := range []struct {
name, sum string
ok bool
}{
{"its SHA-256", hex.EncodeToString(s256[:]), true},
{"its SHA-512", hex.EncodeToString(s512[:]), true},
{"another SHA-256", strings.Repeat("0", 64), false},
{"another SHA-512", strings.Repeat("0", 128), false},
{"its SHA-256 twice", strings.Repeat(hex.EncodeToString(s256[:]), 2), false},
} {
out := filepath.Join(dir, "out")
err := exec.CommandContext(t.Context(), "sh", script, "file://"+src, tc.sum, out).Run() //nolint:gosec // base.sh's devvm-fetch, on the test's own files
_, kept := os.Stat(out)
if (err == nil) != tc.ok || (kept == nil) != tc.ok {
t.Errorf("%s: %v, kept %v", tc.name, err, kept == nil)
}
_ = os.Remove(out)
}
}