package main
import (
"os"
"regexp"
"strings"
"testing"
)
// The --repo map is not the fleet, and no closure may treat it as one.
//
// That map holds only the repositories named on the command line; on
// the forge that is quarry itself. Every other repository lives in the
// oltp registry and is found through repoPath. SavePipeline indexed the
// map directly and so could save a pipeline for one repository and
// answered every other with a bare 500 (a6110cdc). The push credential
// had made the same mistake earlier and was fixed the same way.
//
// This reads the source because the closures live in main and take the
// registry by value; the invariant is about which lookup they use, and
// that is only visible in the text. Anything that indexes repos[...]
// after the registry exists must be one of the two known-correct sites.
func TestNoClosureTreatsTheRepoFlagsAsTheFleet(t *testing.T) {
src, err := os.ReadFile("main.go")
if err != nil {
t.Fatal(err)
}
lines := strings.Split(string(src), "\n")
re := regexp.MustCompile(`\brepos\[`)
var hits []string
for i, l := range lines {
if !re.MatchString(l) {
continue
}
switch {
case strings.Contains(l, "if path, ok := repos[repo]; ok {"): // repoPath: checks the map FIRST, then the registry
case strings.Contains(l, "dup := repos[r.Name]"): // load-time duplicate check, before the registry is a thing
default:
hits = append(hits, strings.TrimSpace(l)+" (line "+itoa(i+1)+")")
}
}
if len(hits) > 0 {
t.Fatalf("FAIL: a lookup treats the --repo map as the whole fleet; resolve through repoPath instead:\n %s",
strings.Join(hits, "\n "))
}
t.Log("ok: every repository lookup outside repoPath goes through repoPath")
}
func itoa(n int) string {
if n == 0 {
return "0"
}
var b []byte
for n > 0 {
b = append([]byte{byte('0' + n%10)}, b...)
n /= 10
}
return string(b)
}