CI / test (push) Successful in 1m41s
There was no reference. Four narrative documents explain how to install and operate llmgw, and the only way to learn that `lease_ttl` exists was to read the one that happened to mention it — or the struct. Written by hand it would have been wrong within a fortnight. Six settings arrived in the last two weeks alone: store, servers, lease_ttl, the two retention windows, min_disk_gb. A reference nobody maintains is worse than none, because it is the same list with the setting you came to look up quietly missing from it. So it is derived from the structs, with the field comments as the descriptions — which means the only way for it to be wrong is for the code to be wrong, and it makes the comments worth writing twice over. 1163 lines, 24 blocks, 87 keys. Two tests keep that true after everyone has forgotten this exists. One regenerates and compares, so adding a setting without regenerating is a failure rather than an omission — checked by adding one and watching it fail. The other walks the tree for koanf tags and fails on a configuration file that is in neither the source list nor the short list of structs an operator never writes, because a list is a thing to forget. The generator finds the repository from its own path rather than the working directory, so `go generate ./...` produces the same document from anywhere. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
139 lines
3.7 KiB
Go
139 lines
3.7 KiB
Go
package docgen
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// repoRoot is where the source paths in Sources are relative to.
|
|
func repoRoot(t *testing.T) string {
|
|
t.Helper()
|
|
_, file, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
t.Fatal("cannot locate this test")
|
|
}
|
|
return filepath.Join(filepath.Dir(file), "..", "..")
|
|
}
|
|
|
|
func sourcePaths(t *testing.T) []string {
|
|
t.Helper()
|
|
root := repoRoot(t)
|
|
out := make([]string, len(Sources))
|
|
for i, s := range Sources {
|
|
out[i] = filepath.Join(root, s)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestTheReferenceIsNotStale(t *testing.T) {
|
|
// The whole point. A generated document that nobody regenerates is the
|
|
// same lie as a hand-written one, arrived at more slowly: it describes the
|
|
// configuration of some earlier week, and the setting somebody came to
|
|
// look up is the one that is missing.
|
|
blocks, err := Collect(sourcePaths(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := Render(blocks)
|
|
|
|
path := filepath.Join(repoRoot(t), "docs", "reference.md")
|
|
got, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("docs/reference.md is missing; run go generate ./...: %v", err)
|
|
}
|
|
if string(got) != want {
|
|
t.Error("docs/reference.md no longer matches the configuration structs.\n" +
|
|
"Run: go generate ./...")
|
|
}
|
|
}
|
|
|
|
func TestEveryConfigurationFileIsListed(t *testing.T) {
|
|
// Sources is a list, and a list is a thing to forget. A new file full of
|
|
// settings would otherwise be absent from the reference with nothing
|
|
// saying so — which is exactly the failure the reference exists to fix.
|
|
root := repoRoot(t)
|
|
var tagged []string
|
|
err := filepath.Walk(filepath.Join(root, "internal"), func(p string, info os.FileInfo, err error) error {
|
|
if err != nil || info.IsDir() || !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") {
|
|
return err
|
|
}
|
|
b, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !strings.Contains(string(b), `koanf:"`) {
|
|
return nil
|
|
}
|
|
rel, err := filepath.Rel(root, p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tagged = append(tagged, rel)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(tagged) == 0 {
|
|
t.Fatal("found no configuration files at all, so this test proves nothing")
|
|
}
|
|
|
|
for _, f := range tagged {
|
|
if slices.Contains(Sources, f) {
|
|
continue
|
|
}
|
|
// Deliberately out: these hold koanf tags for structs an operator
|
|
// never writes — inventory and catalog entries come from the API or
|
|
// from a model file, not from the configuration.
|
|
if slices.Contains(notReference, f) {
|
|
continue
|
|
}
|
|
t.Errorf("%s declares configuration and is not in Sources or notReference", f)
|
|
}
|
|
}
|
|
|
|
func TestADocumentedKeyKeepsItsComment(t *testing.T) {
|
|
// The descriptions are the field comments, which is what makes the
|
|
// reference worth reading rather than a list of names. A field whose
|
|
// comment was the only explanation anywhere must not render blank.
|
|
blocks, err := Collect(sourcePaths(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var lease Field
|
|
for _, b := range blocks {
|
|
for _, f := range b.Fields {
|
|
if f.Key == "lease_ttl" {
|
|
lease = f
|
|
}
|
|
}
|
|
}
|
|
if lease.Key == "" {
|
|
t.Fatal("lease_ttl is not in the reference")
|
|
}
|
|
if !strings.Contains(lease.Doc, "shared store") {
|
|
t.Errorf("lease_ttl lost its explanation: %q", lease.Doc)
|
|
}
|
|
}
|
|
|
|
func TestAFieldMarkedNotConfigurableIsLeftOut(t *testing.T) {
|
|
// koanf:"-" means the field is carried in the struct and never read from
|
|
// a file. Listing it would send somebody looking for a setting that does
|
|
// not exist.
|
|
blocks, err := Collect(sourcePaths(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, b := range blocks {
|
|
for _, f := range b.Fields {
|
|
if f.Key == "-" || f.Key == "" {
|
|
t.Errorf("%s lists a non-configurable field: %+v", b.Name, f)
|
|
}
|
|
}
|
|
}
|
|
}
|