Files
nmon/testdata/gen/main_test.go

59 lines
1.6 KiB
Go

package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
// TestCommittedFixturesAreCurrent fails when testdata/snmprec has drifted from
// the generator, and when a fixture lacks a reviewed expect.yaml.
func TestCommittedFixturesAreCurrent(t *testing.T) {
dir := t.TempDir()
if err := run(dir); err != nil {
t.Fatal(err)
}
ents, _ := os.ReadDir(dir)
if len(ents) < 40 {
t.Fatalf("generated only %d files", len(ents))
}
names := map[string]bool{}
for _, e := range ents {
want, err := os.ReadFile(filepath.Join(dir, e.Name()))
if err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(filepath.Join("..", "snmprec", e.Name()))
if err != nil {
t.Errorf("%s is not committed: run go run ./testdata/gen", e.Name())
continue
}
if !bytes.Equal(got, want) {
t.Errorf("%s is stale: run go run ./testdata/gen", e.Name())
}
if n, ok := strings.CutSuffix(e.Name(), ".agent.yaml"); ok {
names[n] = true
}
if n, ok := strings.CutSuffix(e.Name(), ".snmprec"); ok && !strings.Contains(n, ".") {
names[n] = true
}
}
for n := range names {
if _, err := os.Stat(filepath.Join("..", "snmprec", n+".expect.yaml")); err != nil {
t.Errorf("fixture %s has no %s.expect.yaml", n, n)
}
}
// Nothing committed may be left over from a removed fixture.
committed, _ := os.ReadDir(filepath.Join("..", "snmprec"))
for _, e := range committed {
if strings.HasSuffix(e.Name(), ".expect.yaml") {
continue
}
if _, err := os.Stat(filepath.Join(dir, e.Name())); err != nil {
t.Errorf("%s is committed but no longer generated", e.Name())
}
}
}