package main
import (
"os"
"regexp"
"strings"
"testing"
)
// TestGoPinsAgree: go.mod is the one Go pin. deploy.sh reads it; the
// Dockerfile cannot read a file before FROM, so it carries a default
// that must say the same thing, or the image quietly builds with an
// older Go than every other path.
func TestGoPinsAgree(t *testing.T) {
mod, err := os.ReadFile("../../go.mod")
if err != nil {
t.Fatal(err)
}
m := regexp.MustCompile(`(?m)^go (\S+)$`).FindSubmatch(mod)
if m == nil {
t.Fatal("go.mod has no go line")
}
want := string(m[1])
df, err := os.ReadFile("../../Dockerfile")
if err != nil {
t.Fatal(err)
}
d := regexp.MustCompile(`(?m)^ARG GO_VERSION=(\S+)$`).FindSubmatch(df)
if d == nil {
t.Fatal("Dockerfile has no ARG GO_VERSION default")
}
if got := string(d[1]); got != want {
t.Errorf("Dockerfile GO_VERSION=%s, go.mod says go %s", got, want)
}
if strings.Contains(string(df), "FROM golang:1.") {
t.Error("Dockerfile names a golang image by number; use ${GO_VERSION}")
}
}