Files
mesh-controller/cmd/mesh-controller/merge_check_nested_test.go
T
jschoubben 7ee2b42230
mesh/merge-gate head 7ee2b42230dc merged into trunk b8c810a3499c beside mesh-catalog#348@40eed3a0 — pass: builds build-agent, mesh-controller, route-proxy …
mesh/repo-check head 7ee2b42230dc merged into trunk b8c810a3499c beside mesh-catalog#348@40eed3a0 — pass: THE CHANGE ALTERS ITS OWN CHECK (merge-check.sh):…
mesh/delivery delivered
mesh/delivery-group group feat/move-a-module delivered: every member is delivered
issue 1025: the nested-module check is a file of its own, and the gate test keeps the app module in its repository
2026-10-17 17:27:46 +02:00

71 lines
2.5 KiB
Go

package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// **A repository's merge check runs every module's unit tests, a Go module of its own below the root included**
// (novox/hq ADR 0420 §5, issue 1025): `./...` stops at a nested go.mod. This runs the script
// check-nested-modules.sh in a tree with a `go` that only records where it was run and fails for a module named bad.
func TestTheMergeCheckVetsAndTestsEveryNestedGoModule(t *testing.T) {
script, err := filepath.Abs("../../check-nested-modules.sh")
if err != nil {
t.Fatal(err)
}
bin, tree := t.TempDir(), t.TempDir()
log := filepath.Join(t.TempDir(), "go.log")
fake := "#!/bin/sh\necho \"$(basename \"$PWD\") $1 GOFLAGS=[$GOFLAGS]\" >>\"$LOG\"\n" +
"[ \"$(basename \"$PWD\")\" = bad ] && [ \"$1\" = test ] && exit 1\nexit 0\n"
if err := os.WriteFile(filepath.Join(bin, "go"), []byte(fake), 0o755); err != nil {
t.Fatal(err)
}
run := func() (string, error) {
cmd := exec.Command("sh", script)
cmd.Dir = tree
cmd.Env = append(os.Environ(), "PATH="+bin+":"+os.Getenv("PATH"), "LOG="+log, "GOFLAGS=-mod=vendor")
out, err := cmd.CombinedOutput()
return string(out), err
}
// No modules directory: a pass, nothing run.
if out, err := run(); err != nil {
t.Fatalf("a repository with no nested module failed: %v\n%s", err, out)
}
if _, err := os.Stat(log); err == nil {
t.Fatal("go ran with no nested module")
}
for _, m := range []string{"good", "bad", "other"} {
dir := filepath.Join(tree, "modules", m)
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module x\n"), 0o644); err != nil {
t.Fatal(err)
}
}
// A directory with no go.mod is no Go module of its own.
if err := os.MkdirAll(filepath.Join(tree, "modules", "typescript"), 0o755); err != nil {
t.Fatal(err)
}
out, err := run()
if err == nil {
t.Fatalf("a nested module whose tests fail passed the check:\n%s", out)
}
raw, _ := os.ReadFile(log)
ran := string(raw)
for _, want := range []string{"good vet GOFLAGS=[]", "good test GOFLAGS=[]", "other test GOFLAGS=[]", "bad test GOFLAGS=[]"} {
if !strings.Contains(ran, want) {
t.Errorf("%q was not run, nor outside the vendor directory:\n%s", want, ran)
}
}
if strings.Contains(ran, "typescript") {
t.Errorf("a directory with no go.mod was run:\n%s", ran)
}
if !strings.Contains(out, "nested module modules/bad FAILED") {
t.Errorf("the failing module is not named:\n%s", out)
}
}