From 376ab29457fe2552ec2f861ef689a07abf764f36 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Mon, 5 Oct 2026 11:54:51 +0200 Subject: [PATCH] Answer architecture round 2: read go.mod's module path as its first field, unquoted, and fail when no import matches it --- imports_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/imports_test.go b/imports_test.go index ac3c4b9..35807ba 100644 --- a/imports_test.go +++ b/imports_test.go @@ -17,15 +17,17 @@ func TestPackageImports(t *testing.T) { if err != nil { t.Fatal(err) } - module, found := "", false + module := "" for _, line := range strings.Split(string(mod), "\n") { - if module, found = strings.CutPrefix(strings.TrimSpace(line), "module "); found { + if f := strings.Fields(line); len(f) > 1 && f[0] == "module" { + module = strings.Trim(f[1], `"`) break } } - if !found { + if module == "" { t.Fatal("go.mod names no module") } + checked := 0 allowed := map[string][]string{ ".": {"internal/drawstate"}, "cmd/fejkdata": {"."}, @@ -58,6 +60,7 @@ func TestPackageImports(t *testing.T) { if imp != module && !strings.HasPrefix(imp, module+"/") { continue } + checked++ rel := strings.TrimPrefix(strings.TrimPrefix(imp, module), "/") if rel == "" { rel = "." @@ -71,6 +74,9 @@ func TestPackageImports(t *testing.T) { if err != nil { t.Fatal(err) } + if checked == 0 { + t.Fatalf("no package imports a package of module %s, which go.mod names", module) + } for dir := range allowed { if !seen[dir] { t.Errorf("listed package %s does not exist", dir)