Files
w3cjandClaude Opus 5.5 1bd598c791 feat(cosign): ReleaseBuild, the co-signer's own build of a tag
ReleaseBuild rebuilds a release with the co-signer's own devvm: a new
clone of its checkout at its pinned commit (checked clean), the tag and
main fetched into it, the tag's SSH signature checked against the pinned
signers (VerifyTag), the tag naming itself and its commit on main, then
the clone's release.sh --tag with those signers as git's
allowedSignersFile, so release.sh checks the tag again. Nothing of the
tag's runs on the host.

- The clone, its git config and allowed signers and release.sh's log go
  in Src, the state folder's src/, which no build's container can reach,
  as release.sh and git run from it on the host; only release.sh's
  TMPDIR, where its containers work, goes in Work, the build folder. A
  container that could write the clone could otherwise edit release.sh
  as bash reads it, or set gpg.ssh.program or a filter in its .git/config
  for git to run on the host. release.sh's download cache, which only it
  reads, on the host, goes in the state folder too, so nothing that
  escaped a container can plant a link for its next download.
- Build returns Built: the checked tag, its tag object's ID, and the tag
  commit's Homebrew
  formula template (packaging/homebrew/devvm.rb) and CHANGELOG.md, read
  with git before release.sh runs, so no build's container can change
  them. The co-signer renders the tap's formulas and the release's notes
  from them and the signed SHA256SUMS.
- The checkout is root's, so git reads it as the co-signer's user only
  with it named safe on its own command line: its worktree for git
  status, and its .git for a clone, whose upload-pack checks that path.
  A full run on Fedora under the units found the clone refused with the
  worktree alone; TestCheckoutNamedSafe holds both.
- git runs with no system or global config, no hooks or fsmonitor, no
  replace objects, files and HTTPS alone, an environment of its own and
  its output capped.
- The structure test's commandsFree is now keyed by the command, then the
  file, so build.go may run git and no other guarded command; its
  execSites and statusFree entries, and the owners table, say why.

Tests: TestReleaseBuild (the template and changelog read, then a stub
release.sh that deletes the clone's .git, as a build could, and says
where it ran: its script in Src and its TMPDIR in Work, which fails with
the clone in Work, reverted once to see it; the tag object), the
existing refusals (an unpinned signer, off main, a dirty or moved
checkout, a lightweight tag, a tag naming another, release.sh failing),
TestCheckoutNamedSafe, and TestRulesFail's "another command in a file
commandsFree lists for git", which fails with commandsFree keyed by file
alone (reverted once to see it).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-30 21:36:37 -07:00

256 lines
27 KiB
Go

package structure
import (
"flag"
"os"
"slices"
"strings"
"testing"
"github.com/devvmhq/devvm/internal/agents"
)
// update rewrites Contributing's table of the checks on owners from
// ownerDocs: go test ./internal/structure -run TestOwnersDoc -update.
var update = flag.Bool("update", false, "rewrite Contributing's checks on owners from ownerDocs")
// The page that holds the table, and the lines around it.
const (
ownersPage = root + "/docs/content/docs/contributing/structure.md"
ownersBegin = "<!-- The table is generated from ownerDocs in internal/structure/owners_doc_test.go: go test ./internal/structure -run TestOwnersDoc -update -->"
ownersEnd = "<!-- end of the generated table -->"
)
// An ownerDoc is one owner's check, as Contributing's table says it: what
// checks it, the owner and why it is the only one, and where else the code
// may be.
type ownerDoc struct {
check, owner, elsewhere string
}
// ownerDocs are the checks on owners, forbidigo's and the structure
// test's, in the order the table has them. The structure test's own are
// devvm()'s tables, and TestOwnersAgree checks that each owner they name is
// in one of these.
func ownerDocs() []ownerDoc {
return []ownerDoc{
{
check: "forbidigo, `testbuild.On`",
owner: "`internal/cli`, whose `readOverrides` is the only code that asks whether this is a test build",
elsewhere: "nowhere: other packages take the overrides as fields",
},
{
check: "forbidigo, `os.FindProcess`, `syscall.Kill`, `unix.Kill` and `os.Process`'s `Kill` and `Signal`",
owner: "`internal/daemon`, which signals a background process only while its pid file and its arguments say it's still that process (`Proc`), or through the process it started (`Handle`)",
elsewhere: "`internal/platform`; tests, which signal the children they started; and a child that hasn't been waited for yet, whose pid is still its own, with a `nolint` saying so",
},
{
check: "forbidigo, `unicode.IsGraphic`, `IsPrint`, `IsControl` and `Bidi_Control`",
owner: "`msg.Safe` in `internal/msg/safe.go`, the one rule for which characters reach the terminal, which `msg.Printable`, `msg.OneLine`, the UI's renderers and devvm agent save's diff follow, so a second rule can't let through what this one refuses ([Colors](cli.md#colors) says what it refuses)",
elsewhere: "a check that isn't about the terminal, such as a path one line long, with a `//nolint:forbidigo` saying so",
},
{
check: "forbidigo, HTTP clients",
owner: "`internal/fetch`, whose `NewClient` sets each client's timeout and redirect policy (`HTTPSOnly` or `SameScheme`), and whose `Retry`, `Transient`, `ReadCapped`, `Get`, `Resolve` and `Download` try again after the failures that may pass, cap what a body or a download may hold, follow a HEAD's redirects to where they end and check a download's SHA-256 before anything uses it. `fetch.Client` is its own type, so a client that `NewClient` didn't make, such as `&fetch.Client{}`, fails every request, and a caller changes a client's transport through its `Transport` method",
elsewhere: "`internal/proxy`, which is the standard library only, and tests, which make their own servers' clients",
},
{
check: "forbidigo, `x509`'s `UnknownAuthorityError`, `HostnameError` and `CertificateInvalidError` and `tls.CertificateVerificationError`",
owner: "`fetch.Untrusted`, `fetch.BadCertificate` and `fetch.Unverified` in `internal/fetch/fetch.go`, beside `fetch.Gone`: a server's certificate that no CA this machine trusts signed, which the user's CA mends, one a trusted CA signed that's for another host, out of date or not valid yet, which no CA mends, and any failure to verify one, with its reason. `Transient` asks `Unverified`, so none is tried again. Ubuntu's lookups and the cloud image's download ask all three, so each names the same cause, and host mise's asks `Untrusted`",
elsewhere: "tests, which make those errors",
},
{
check: "forbidigo, `netip.Addr`'s `IsPrivate`",
owner: "`proxy.Public` in `internal/proxy/proxy.go`, the one judge of whether an address is on the internet: netip's classes, the table of the other ranges that aren't (`notPublic`), and the IPv4 address in a NAT64 or 6to4 address. The proxy's floor, `devvm network lock`, a `--clone` VM's partial-clone remotes and `--from`'s registries all ask it, so a range added to it holds for each",
elsewhere: "`internal/proxy`",
},
{
check: "forbidigo, `redact.Secret`'s `Reveal`; the structure test, a struct that embeds `redact.Secret` and an interface that declares `Reveal() string`",
owner: "`redact.Secret` in `internal/redact/secret.go`, the type of the values that let whoever holds them act as the user or a VM, but for those [Secrets in the code](#secrets-in-the-code) names: a VM's proxy password, a secret's value, a login's keys and tokens once they leave its file, and the company proxy's password and its `Proxy-Authorization`. Every way of printing one shows `<redacted>` or, in an unexported field, an address, and encoding one as JSON or text is an error, never a stand-in, so a file that holds the real value writes it through its own wire type, and a value can't be lost by going somewhere it isn't meant to",
elsewhere: "where the value is used, each call to `Reveal` with a `//nolint:forbidigo` that says why: the proxy's password check, its config file, the headers it puts values in, the company proxy's credentials, the VM's CA key as it signs and the broker's frames to its child; the store's file and Keychain; the VM's proxy URL and its key in `vm.json`; a login's file, its refresh and its claims; and tests. No struct embeds `redact.Secret` and no interface declares `Reveal() string` outside `internal/redact`",
},
{
check: "forbidigo, `os.WriteFile`, `os.Create` and `os.O_TRUNC`",
owner: "`internal/state`, whose `WriteFile` and `WriteFileMode` write a file whole through a temporary file in its folder, renamed over it, so a reader never sees half of it",
elsewhere: "`internal/proxy`, which imports only the standard library and has its own writer, `writeAtomic`; `internal/msg/gen`, which imports no devvm package; `internal/stubtest`; tests; and, with a `nolint` saying why, a file written another way: a log that a process writes as it runs, a download that streams into its file, and `internal/daemon`'s pid and status files, each written in one write that a reader takes as missing until its line ends",
},
{
check: "forbidigo, `os.Open`, `os.OpenFile`, `os.ReadFile`, `os.OpenRoot`, `os.OpenInRoot`, `os.DirFS` and `os.Root`'s `Open`, `OpenFile`, `ReadFile` and `FS`, in the code that reads files a VM can write or swap: `internal/gitprotect`, `internal/agentlogin`, `internal/vm`'s `devcontainer.go`, `detect.go`, `paste.go` and `agentsave*.go`, `internal/netpolicy/rules.go`, `internal/vmlog`, and `internal/proxy`'s `config.go`, `policy.go`, `rotate.go` and `nixcache.go`",
owner: "`internal/safefile`, which imports only the standard library, so the proxy reads with it too. Its `Read` and `Open` open a file without blocking and, unless the caller passes `Follow`, without following a symbolic link at the path's end. They then check the file they opened, not its name, so a FIFO, a device or a folder is refused with `ErrNotRegular`, a file past its cap with `ErrTooLong`, and, with `OneLink`, a file with another hard link with `ErrLinked`. A FIFO that the VM swaps in after a check can't hold devvm, and a link to `/dev/zero` can't flood it. `Checked` checks a file its caller opened another way in the same way, such as devvm paste's copied file, which `platform.OpenNoLinks` opens following no link but the system's, through `openat`, which the standard library doesn't have on macOS. `OpenRoot` opens beneath a folder through `os.Root`, never through a link out of it, and its `FS` lists and stats what's there but opens only regular files and, never through a link at the name's end, folders to list. `Walk` walks a folder that a VM can fill, reading each folder in batches and stopping at its bound on the entries it reads, as `devvm agent import` walks an agent's folder",
elsewhere: "every other file, which is devvm's own or the user's; tests, which make their own fixtures; and, with a `nolint` saying why, what opens a file to write it or a folder to lock it, such as the proxy's logs and the Nix cache's lock, each opened never through a link or waiting on a FIFO and checked once open",
},
{
check: "forbidigo, `os.Chmod`, `os.Chtimes`, `os.Chown`, `os.Lchown` and `os.Truncate`, in the code that changes files a VM can write or swap: `internal/proxy`, `internal/vmlog` and `internal/vmssh`",
owner: "the file's own descriptor, from `internal/safefile`, an `os.Root` or the proxy's `OpenLog`: `os.File`'s `Chmod`, `Chown` and `Truncate`, an `os.Root`'s methods, and the proxy's `touchFile` (`futimes`, or `utimensat` with no path), because a VM can swap a name for a link between the open and the change, which would then reach a file of yours",
elsewhere: "every other package, whose files are devvm's own or the user's; tests, which make their own fixtures; and, with a `nolint` saying why, a unix socket devvm has just made in its own folder (the proxy's SSH agent relay and the link session's sockets), whose mode only its path can set",
},
{
check: "forbidigo, `os.Setenv`, `os.Unsetenv`, `os.Clearenv` and `syscall`'s",
owner: "each command's own environment, its `exec.Cmd`'s `Env`, because every command and background process devvm starts inherits devvm's. A VM's SSH agent is one: the finder's `target` carries its socket, its proxy's relay (`netpolicy.Proxy.Prepare`), which goes to the backend in `backend.ExecOpts.Agent`, `backend.ShellOpts.Agent` and `Confinement.AgentSocket`, and Lima sets `SSH_AUTH_SOCK` on that VM's `limactl` commands alone, so a run that reaches several VMs never gives one another's",
elsewhere: "`daemon.Ready` in `internal/daemon/status.go`, which takes the status file's name out of a background process's own environment; `stubtest.NoGitRepo`, which takes git's variables that pick a repo out of a test's environment until the test ends; and tests, which use `t.Setenv`",
},
{
check: "the structure test's `owners`, host commands",
owner: "the runners: the UI's `LoggedCmd`, which a package takes as a field and which runs a command into the run log, a backend's `Runner`, and `internal/hostgit`. A host command that one of them doesn't run is made only in the files `execSites` lists: those that make the command a runner then runs, such as Lima's `limactl` and mise's, and those whose command can't go into the run log, because it streams for as long as a link, the port forwarder or the desktop's relay lasts, asks on the terminal, outlives the run, or carries a secret",
elsewhere: "tests",
},
{
check: "the structure test's `owners`, a backend's other interfaces",
owner: "`backend.As` in `internal/backend/guard.go`, because the registry's backends are behind its guard, whose type has only `backend.Backend`'s methods, so a type assertion to `backend.Forgetter` or `*lima.Lima` on one is always false",
elsewhere: "tests",
},
{
check: "the structure test's `owners`, git",
owner: "`internal/hostgit`, which runs git without the repo's fsmonitor, hooks, filters or signing programs, over ssh only, without the environment's `GIT_*` variables, prompts, replace refs or grafts. It's also the only package whose string literals may be `\"push\"`, whole (`names`), and none is: a `--clone` VM's copy goes through the backend's `Exec` (`internal/vm/cloneseed.go`), never git's transport, whose receive-pack refuses shallow and SHA-256 repos. Nor may any other package's be `\"diff-files\"`, which enters each submodule, where that repo's own config can name a filter: `hostgit.DiffFiles` doesn't. In tests, which run plain git to make their repos, it's `internal/stubtest`'s `Git`, which leaves out git's variables that pick a repo, its files or its config (`GitEnv`), so a git that runs the tests, such as a hook's or `git rebase --exec`'s, never sends a test's git to the checkout itself",
elsewhere: "the files `commandsFree` lists for git, which may run no other guarded command: the co-signer's `internal/cosign/build.go`, a program of its own that imports none of devvm's packages but `internal/minisign`, whose git fetches a release's tag over HTTPS into repositories it makes, with no config but its own",
},
{
check: "forbidigo, `os.UserHomeDir`, and the structure test's `owners`, `HOME`",
owner: "`paths.FromEnv` in `internal/paths/dirs.go`, which reads `HOME` once into `paths.Dirs.Home`, so a test or an e2e run that sets its own `HOME` moves every path devvm takes from it",
elsewhere: "tests",
},
{
check: "the structure test's `owners`, `DEVVM_` variables",
owner: "`readOverrides` in `internal/cli/overrides.go`, which reads a test build's stand-ins, while a release reads none",
elsewhere: "the settings in `envSettings`, and tests",
},
{
check: "the structure test's `owners`, signatures",
owner: "`internal/minisign`, whose `PublicKey.Check` checks a minisign signature and its trusted comment, exactly, and whose `Keys.Check` checks a manifest, such as a release's `SHA256SUMS`, against any two of a kind's three keys, by role, each signer counted once. A call of `ed25519`'s `Verify` or `VerifyWithOptions`, `rsa`'s `VerifyPKCS1v15` or `VerifyPSS`, or `ecdsa`'s `Verify` or `VerifyASN1`, an import of one of those packages with a dot, a call of a method named `CheckSignature`, `CheckSignatureFrom` or `CheckCRLSignature`, as `crypto/x509`'s are, and an import of `golang.org/x/crypto/blake2b`, fail, so devvm's own signatures are checked in one place, with the key's role and kind",
elsewhere: "`internal/minisign/minisigntest`, which signs for tests; the files in `signatureFree`, which check other projects' formats, Ubuntu's OpenPGP signatures (`internal/ubuntu/pgp.go`), cache.nixos.org's (`internal/proxy/nixcache.go`) and git's SSH signatures on release tags, which the co-signer checks against its pinned signers (`internal/cosign/sshsig.go`); and tests",
},
{
check: "the structure test's `owners`, shell quoting",
owner: "`internal/shell`, whose `Quote` and `QuoteJoin` quote words for a POSIX shell, so a value never runs as a command, and `QuoteLine` a word on one line for bash",
elsewhere: "tests",
},
{
check: "the structure test's `owners`, the VM-name grammar",
owner: "`internal/vmname`, whose `IsVM` tells a project VM's name from an image's, so gc never takes a project VM for an image",
elsewhere: "tests",
},
{
check: "the structure test's `owners`, VM names",
owner: "`internal/vmname`, whose `VM` is the name of every machine devvm runs on a backend: a project VM, an image's machine, a build VM, or a VM's old machine while a remake makes the new one. A name is made a `VM` only where it's read: `Parse` takes a project VM's name from an argument or a file name, and also reads `vm.json`'s name and runs in `project.Nth`, `ParseListed` one a backend lists, `VM.UnmarshalText` one in a JSON file (one path element that doesn't start with `-`, or none, and any machine's, so a field that must hold a project VM's is still checked with `IsProject`), `Image` names an image's machine and `VM.Aside` a remake's old one, and a backend's build VMs are typed constants. " +
"A struct field, a parameter or a result named for a VM (`vm`, `vms`, `otherVM`, `vmName`) whose type is a string, a pointer to one, a list of them or a map keyed on them fails, and so do a conversion of anything but a literal to `vmname.VM` and a type alias of it, so a path, an alias or a folder's name is never passed for a VM's name. The check goes by names, so it doesn't see a VM's name in a string named otherwise (`target`, `client`, `parent`, `to`, `name`), `vmname` imported with a dot or reached through a type parameter, or a VM joined to a string constant, whose type is still `vmname.VM` (`l.VM+lineNoLinks` in `internal/vmssh/links.go`)",
elsewhere: "`msg.M.VM` in `internal/msg/msg.go`, which reads back the VM a message's function took; `internal/stubtest`, whose stubs stand in for `limactl` and `ssh`, whose arguments are strings; the names `notNames` lists that aren't a VM's, such as `backend.Registry.NewVMs`, a backend's name; and tests",
},
{
check: "the structure test's `owners`, hosts",
owner: "`internal/proxy/host.go`, whose `NormHost` reads a host as the proxy judges it, the one form every judge of a host compares: a host of printable ASCII lowercased, in ASCII alone, and its one trailing dot dropped unless it's an address, whose dot can only be its IPv6 zone's, and any other host left as it is, which `HostOK` refuses, so no Unicode fold makes another name match. An address is bare, or an IPv6 address in one pair of brackets, never one with a stray bracket. Its `Host` is what a matcher takes, such as a rule's `matchHost` and `NO_PROXY`'s. " +
"A call to package `strings` that drops a dot (`Trim`, `TrimLeft` or `TrimRight` with a cutset that holds `.`, `TrimSuffix` or `CutSuffix` with `\".\"`), or that folds or trims an argument named for a host (`ToLower`, `ToUpper`, `EqualFold`, `TrimFunc`, `TrimLeftFunc` or `TrimRightFunc` of `host`, `sni` or `u.Hostname()`), fails, and so does a conversion to `proxy.Host` of anything but a literal. The check goes by names and calls, so it doesn't see a host's case folded in a variable named otherwise, a dot dropped by slicing, or a host mapped rune by rune (`strings.Map`)",
elsewhere: "the functions `free` lists by file and name, each with why: `trimDots` in `internal/proxy/request.go`, which drops a path's segments' trailing dots; `urlHost` in `internal/vm/cloneseed.go`, which reads a remote's ssh host as ssh does, to find a VM's alias, its case folded and its trailing dot kept; `safeIndexPath` in `internal/vm/cloneworktree.go`, which drops a path segment's trailing dots and spaces as Windows does; and `machines.fillPassword` in `internal/backend/lima/start.go` and `transportHelper` in `internal/vm/cloneconfig.go`, which check that a string has only the characters of a set that holds `.`; and tests",
},
{
check: "the structure test's `owners`, the coding agents",
owner: "`internal/agents`, whose `Defs` hold every fact devvm keeps of each agent, so no copy of an agent's list or paths goes stale when one is added. A string literal that is an agent's name or a login file's kind, whole, or that holds an agent's folder below the home folder, fails, and code of one agent's own names the agent by its constant there. Comparing with that constant, or with one named after it such as `secrets.ClaudeToken`, a `case` for it, looking for it with `slices.Contains` or `slices.Index`, looking it up in a map, a map literal keyed on it and `strings.HasPrefix`, `HasSuffix`, `EqualFold` or `Contains` with it fail too, outside the files named for the agent, such as `claude.go`, so the rest of devvm asks the agent's `Def` and a new agent changes none of it. A list that holds every agent's name, tests included, fails as a copy of `agents.Names()`, and so does a CLI test script's line that lists agents as a message does, for which the runner gives `$AGENT_CHOICES`, `$LOGIN_AGENTS` and `$KEY_AGENTS` ([adding a coding agent](extending.md#adding-a-coding-agent))",
elsewhere: "`internal/stubtest`, whose stubs are named after the commands they stand in for, and tests; for a name, the value of a constant that `devvmNameFree` lists with its reason and the only files that may use it: `vm.CursorCommand`, the Cursor editor's `cursor` command, which `devvm cursor` runs and isn't the agent's, used in `internal/vm/ssh.go` and `internal/cli/cursor.go` alone and compared with as an agent's constant is; for the comparisons, the files whose names hold the agent's name as a word, and the constants `devvmSwitchFree` lists in one file each with its reason: the claude secret's own checks, which are a secret's, and Codex's binding name in `logins.go`",
},
{
check: "the structure test's `owners`, the toolchains",
owner: "`internal/image`, whose `SharedTools` holds every fact devvm keeps of each toolchain: its members, the files that call for it, its installer, its guest environment, its `PATH` folders and its version files. A string literal that is a toolchain's name or a member's, whole, fails, so code elsewhere asks the table, and a new toolchain is an entry there ([adding a toolchain](extending.md#adding-a-toolchain))",
elsewhere: "`internal/stubtest`, and tests",
},
{
check: "the structure test's `owners`, image names",
owner: "`internal/image`, whose `Name` is an image's name and splits it into its layers (`Layers`, `Parent`, `Has`), whose `NameOf` joins layers into one, and whose `ParseName` reads the name the user gives, which `build.Names`' `Parse` checks against the layers there are. `devvm build`, `devvm rebuild` and `devvm images rm` read their names there once, and the rest of devvm passes the `image.Name` on",
elsewhere: "tests",
},
{
check: "the structure test's `owners`, `state.ReadHash` and `project.Parents`",
owner: "the finder in `internal/vm/finder.go`, whose `target` finds the VM every command acts on: the folder's own, the nearest parent folder's under `--here` and `--parent` (`resolveFolder`), or one named (`devvm rm VM`). It finds a folder's VM by the state that records the folder (`finder.state`, by the path's hash first, then `recorded`), offers to move a VM whose folder moved there (`moveHere` in `internal/vm/move.go`, the match `here.movedFrom`), and names a new VM by the first of the folder's names whose VM folder is free (`newName`, `project.Nth`). Its `need` says what the command needs of the VM, and `need.check` is the one place that refuses one that isn't there, one made for an earlier folder at its path, one whose remake didn't finish and one whose creation didn't, the last with `refuseUnfinished`, which every start (`starter.bringUp` in `internal/vm/start.go`) makes too. It reads the folder devvm runs in only then, so the commands that act on no folder's VM run in a folder that's gone. The VM it finds is a `target` (`internal/vm/target.go`). It holds what the VM's backend told the finder, which each start and stop of the VM then updates, and it runs what a command does with that VM: `exec`, `shell`, `stop`, `running`, `ownSSH` and the name of its ssh config entry",
elsewhere: "`internal/state`, and tests",
},
{
check: "the structure test's `owners`, `hostsafety.Walk`",
owner: "the start in `internal/vm/start.go`, whose `starter.walk` walks a VM's project folder once for each check of the folder on the host, for the git repos nested in it, which `.git` protection goes by (`gitprotect.Protector.Nested`), and on Linux hosts its set-id files, so a check never walks the folder twice. It walks when a VM is made and when a check finds it stopped, at a launch or at `devvm stop` (`starter.hostChecks`), and at `devvm git-protect` on a stopped VM, never at a launch into a running VM",
elsewhere: "`internal/hostsafety`, and tests",
},
{
check: "the structure test's `owners`, `state.ReadAll`",
owner: "the inventory in `internal/vm/inventory.go`, which reads every VM's state (`ReadStates`), and with `ReadInventory` the backends' lists too, each within its wait, for every listing of VMs: `devvm ls`, `devvm space`, `devvm images` and `devvm gc`, the ssh config, `devvm gc --orphans`, the links, `devvm secret`'s list, finding a VM by its name and the mounts that devvm's SSH agent checks a key against. Its one failure policy is that a backend that can't list fails a listing (`Inventory.Err`) only when some VM's state names it, and that a state that can't be read is a row of its own, which the readers leave out and `ToolsInUse` and `Mounts` fail on. `state.VMNames`, which fails on such a state, stays for the readers below `internal/vm` that must count every VM: `netpolicy`'s secrets, `ports` and `sshconfig.ChooseAlias`; `state.ReadableVMs`, which leaves such a state out, is for those that only warn: `netpolicy`'s warnings of every VM and doctor's policy warnings",
elsewhere: "`internal/state`, and tests",
},
{
check: "the structure test's `owners`, `paths.Dirs.AgentSettings` and `paths.Dirs.AgentLogins`",
owner: "devvm's agent store: `agentStore` in `internal/vm/agents.go` for the agents' settings, which the mounts, the setup's copies, a remake's home and `devvm agent import` take, and `secrets.LoginFile` in `internal/secrets/secrets.go` for their logins, which the setup's copies and the proxy read, so nothing reads an agent's files from the home folder but `vm.AgentImport`",
elsewhere: "`internal/paths`, and tests",
},
{
check: "the structure test's `owners`, paths in devvm's folders",
owner: "`internal/paths`, whose `Dirs` and `VM` name every file and folder devvm keeps, so the layout, its golden listing in `testdata/script/layout.txtar` and a VM's teardown miss none. A `filepath.Join` onto a `Data` or `Config` field fails, and so does an `os.MkdirTemp` or `os.CreateTemp` there whose name is a literal; a name from `paths`, such as `paths.ToolsBuildPrefix`, passes",
elsewhere: "tests",
},
}
}
// ownersTable is the table ownerDocs make, in Markdown.
func ownersTable(docs []ownerDoc) string {
var b strings.Builder
b.WriteString("| Check | Owner | Allowed elsewhere |\n| --- | --- | --- |\n")
for _, d := range docs {
b.WriteString("| " + d.check + " | " + d.owner + " | " + d.elsewhere + " |\n")
}
return b.String()
}
// Contributing's table of the checks on owners is ownerDocs'.
func TestOwnersDoc(t *testing.T) {
page, err := os.ReadFile(ownersPage)
if err != nil {
t.Fatal(err)
}
before, rest, ok := strings.Cut(string(page), ownersBegin+"\n\n")
table, after, ok2 := strings.Cut(rest, "\n"+ownersEnd)
if !ok || !ok2 {
t.Fatalf("%s: no %q and %q around the owners' table", ownersPage, ownersBegin, ownersEnd)
}
want := ownersTable(ownerDocs())
if table+"\n" == want {
return
}
if !*update {
t.Fatalf("%s: the owners' table isn't ownerDocs': run go test ./internal/structure -run TestOwnersDoc -update", ownersPage)
}
if err := os.WriteFile(ownersPage, []byte(before+ownersBegin+"\n\n"+want+ownersEnd+after), 0o644); err != nil { //nolint:gosec // a docs page, read by all
t.Fatal(err)
}
}
// Every owner devvm()'s tables name has its row in ownerDocs, so the table
// lists each check the structure test makes.
func TestOwnersAgree(t *testing.T) {
r := devvm()
owners := []string{r.guardOwner, r.envOwner, r.pathsOwner, r.hosts.owner, r.signatureOwner}
for _, m := range []map[string]string{r.commands, r.testCommands, r.envOwners, r.callers} {
for _, o := range m {
owners = append(owners, o)
}
}
for _, m := range []map[string]literalOwner{r.literals, r.names, r.separators} {
for _, o := range m {
owners = append(owners, o.dir)
}
}
docs := ownerDocs()
for _, o := range owners {
if !slices.ContainsFunc(docs, func(d ownerDoc) bool { return strings.Contains(d.owner, "`"+o+"`") }) {
t.Errorf("%s owns a structure test's check, but no ownerDocs row names it as an owner", o)
}
}
}
// An agent's paths below the home folder are its settings folder, with a
// slash when it's one folder's name, and its history outside that folder.
func TestAgentHomePaths(t *testing.T) {
defs := []agents.Def{
{Folder: ".a", History: []string{".a", ".a.json"}},
{Folder: ".config/b", History: []string{".config/b/sessions", ".local/state/b"}},
}
got := agentHomePaths(defs)
want := []string{".a/", ".a.json", ".config/b", ".local/state/b"}
if !slices.Equal(got, want) {
t.Errorf("agentHomePaths = %q, want %q", got, want)
}
}