TestEveryAgentInLibrary (internal/library) went red on main for a new agent four times:399010befand68cb92e8e(11-10),7e8214d60(11-12), andeffdd1bebtoday after00b2c11a6(Open Code Review). A change that adds an agent runs internal/agent, and the guard is in another package, so each was found after the push. TestEveryAgentNamedForTheLibrary reads internal/library's targets.go and every_agent_test.go (library imports agent, so it reads the files) and fails when an agent on this machine's list is in neither a targetOf case nor noLibrary. The library test still checks a target has something in it. Same shape as internal/gui's TestEveryRouteIsNamedForTheCLI. LESSONS.md: the route/setting guard lesson now covers agents, seen a third day. code-standards criterion 3 names both twins. Run (mote, ateffdd1beb+ this change): - go test -count=1 -timeout 45m ./internal/agent ./internal/library: ok - broken by hand (open-code-review's noLibrary line removed): red, "open-code-review (Open Code Review) isn't named in internal/library"; restored - win-run and wsl-run, -run TestEveryAgentNamedForTheLibrary: ok - gofmt -l: clean Not run: the full ./... suite (test-only change in one package).
52 lines
1.7 KiB
Go
52 lines
1.7 KiB
Go
package agent
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Every agent is named in internal/library: a case of targetOf
|
|
// (targets.go) or a line of noLibrary (every_agent_test.go). A change that
|
|
// adds an agent runs this package, and a new agent named in neither left
|
|
// library's TestEveryAgentInLibrary red on main again and again (399010bef,
|
|
// 68cb92e8e, 7e8214d60, effdd1beb), each found after the push. That test
|
|
// still checks the target has something in it; this one only checks the
|
|
// agent was named at all. library imports agent, so this reads its files.
|
|
func TestEveryAgentNamedForTheLibrary(t *testing.T) {
|
|
dir := filepath.Join("..", "library")
|
|
targets, err := os.ReadFile(filepath.Join(dir, "targets.go"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
list, err := os.ReadFile(filepath.Join(dir, "every_agent_test.go"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var cases strings.Builder
|
|
for _, l := range strings.Split(string(targets), "\n") {
|
|
if l = strings.TrimSpace(l); strings.HasPrefix(l, "case ") {
|
|
cases.WriteString(l + "\n")
|
|
}
|
|
}
|
|
h := t.TempDir()
|
|
n := 0
|
|
for _, a := range onMachine(h, filepath.Join(h, ".config")) {
|
|
if a.WSL != "" || a.Plugin != "" || strings.Contains(a.ID, "#") {
|
|
continue // a distro's twin, a plugin's agent and another profile have their own
|
|
}
|
|
n++
|
|
q := regexp.QuoteMeta(`"` + a.ID + `"`)
|
|
inCase := regexp.MustCompile(q).MatchString(cases.String())
|
|
listed := regexp.MustCompile(`(?m)^\s*` + q + `:`).Match(list)
|
|
if !inCase && !listed {
|
|
t.Errorf("%s (%s) isn't named in internal/library: give it a case in targetOf (targets.go), or a reason in noLibrary (every_agent_test.go)", a.ID, a.Name)
|
|
}
|
|
}
|
|
if n < 50 {
|
|
t.Fatalf("read %d agents", n)
|
|
}
|
|
}
|