From c1c1d9ccf2114a0e81665d9202d50ae5bef92b29 Mon Sep 17 00:00:00 2001 From: Pascal Watteel Date: Mon, 9 Nov 2026 16:29:20 +0400 Subject: [PATCH 1/2] test: red proof for nested go modules --- internal/lint/gomod_test.go | 279 ++++++++++++++++++++++++++++++++++++ 1 file changed, 279 insertions(+) create mode 100644 internal/lint/gomod_test.go diff --git a/internal/lint/gomod_test.go b/internal/lint/gomod_test.go new file mode 100644 index 00000000..e5f29052 --- /dev/null +++ b/internal/lint/gomod_test.go @@ -0,0 +1,279 @@ +package lint + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "procoder/internal/tools" +) + +// goCall is one golangci-lint invocation the stub recorded. +type goCall struct { + dir string + args []string +} + +// stubGolangci puts a fake golangci-lint on PATH that records its working +// directory and arguments and answers every package pattern with one finding +// in that package's x.go, with the path relative to the directory it ran in +// — which is what the real binary prints. It returns a reader for the calls. +func stubGolangci(t *testing.T) func() []goCall { + t.Helper() + dir := t.TempDir() + log := filepath.Join(dir, "calls.txt") + script := `#!/bin/sh +printf 'CALL\n' >> ` + log + ` +printf 'pwd=%s\n' "$(pwd -P)" >> ` + log + ` +for a in "$@"; do printf 'arg=%s\n' "$a" >> ` + log + `; done +rc=0 +for a in "$@"; do + case "$a" in + ./*) p=${a#./}; p=${p%/} + if [ -z "$p" ]; then f=x.go; else f=$p/x.go; fi + printf '%s:3:1: boom (stub)\n' "$f"; rc=1 ;; + esac +done +exit $rc +` + if err := os.WriteFile(filepath.Join(dir, "golangci-lint"), []byte(script), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", dir) + t.Setenv("HOME", dir) + return func() []goCall { + raw, err := os.ReadFile(log) + if err != nil { + return nil + } + var calls []goCall + for _, chunk := range strings.Split(string(raw), "CALL\n")[1:] { + var c goCall + for _, line := range strings.Split(strings.TrimSpace(chunk), "\n") { + switch { + case strings.HasPrefix(line, "pwd="): + c.dir = strings.TrimPrefix(line, "pwd=") + case strings.HasPrefix(line, "arg="): + c.args = append(c.args, strings.TrimPrefix(line, "arg=")) + } + } + calls = append(calls, c) + } + return calls + } +} + +// moduleRepo lays out a root module and a nested one (momo's bench/ shape): +// its own go.mod, `replace => ../`, files that the root module does not own. +func moduleRepo(t *testing.T, workFile bool) string { + t.Helper() + root, err := filepath.EvalSymlinks(t.TempDir()) + if err != nil { + t.Fatal(err) + } + put := func(rel, body string) { + p := filepath.Join(root, filepath.FromSlash(rel)) + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(p, []byte(body), 0o644); err != nil { + t.Fatal(err) + } + } + put("go.mod", "module example.com/root\n\ngo 1.22\n") + put("pkg/x.go", "package pkg\n") + put("sub/go.mod", "module example.com/root/sub\n\ngo 1.22\n\nrequire example.com/root v0.0.0\n\nreplace example.com/root => ../\n") + put("sub/x/x.go", "package x\n") + if workFile { + put("go.work", "go 1.22\n\nuse (\n\t.\n\t./sub\n)\n") + } + return root +} + +func hasArg(c goCall, want string) bool { + for _, a := range c.args { + if a == want { + return true + } + } + return false +} + +func argAfter(c goCall, flag string) string { + for i, a := range c.args { + if a == flag && i+1 < len(c.args) { + return c.args[i+1] + } + } + return "" +} + +// A file in a nested module must be linted FROM that module, with patterns +// relative to it, and its findings must come back repo-root-relative. +// Run from the repo root golangci-lint fails with "main module (...) does +// not contain package ...", and the gate reports the file NOT checked — so +// every commit touching a nested module was blocked while correct. +// +// proved by: leave cmd.Dir at the repo root in lintGo — the call's dir is +// the root and the pattern is ./sub/x, so this fails on both. +func TestLintGoRunsANestedModuleFromItsOwnDirectory(t *testing.T) { + calls := stubGolangci(t) + root := moduleRepo(t, false) + file := filepath.Join(root, "sub", "x", "x.go") + + got := lintGo(root, []string{file}, false) + + cs := calls() + if len(cs) != 1 { + t.Fatalf("want exactly one golangci-lint run, got %d: %+v", len(cs), cs) + } + if want := filepath.Join(root, "sub"); cs[0].dir != want { + t.Errorf("ran in %s, want the nested module directory %s", cs[0].dir, want) + } + if !hasArg(cs[0], "./x") || hasArg(cs[0], "./sub/x") { + t.Errorf("patterns must be relative to the module dir (./x, not ./sub/x): %v", cs[0].args) + } + if len(got) != 1 || got[0].File != "sub/x/x.go" || got[0].Line != 3 { + t.Errorf("the finding must map back to the repo-root path sub/x/x.go, got %+v", got) + } +} + +// Files from two modules in one commit get one run each, none from the +// wrong directory, and each module's findings keep their own prefix. +// +// proved by: collapse the groups into one run — one call, not two. +func TestLintGoRunsOncePerModule(t *testing.T) { + calls := stubGolangci(t) + root := moduleRepo(t, false) + files := []string{filepath.Join(root, "pkg", "x.go"), filepath.Join(root, "sub", "x", "x.go")} + + got := lintGo(root, files, false) + + cs := calls() + if len(cs) != 2 { + t.Fatalf("want one run per module (2), got %d: %+v", len(cs), cs) + } + dirs := map[string]goCall{} + for _, c := range cs { + dirs[c.dir] = c + } + if c, ok := dirs[root]; !ok || !hasArg(c, "./pkg") { + t.Errorf("root module run missing or wrong: %+v", dirs) + } + if c, ok := dirs[filepath.Join(root, "sub")]; !ok || !hasArg(c, "./x") { + t.Errorf("nested module run missing or wrong: %+v", dirs) + } + seen := map[string]bool{} + for _, f := range got { + seen[f.File] = true + } + if !seen["pkg/x.go"] || !seen["sub/x/x.go"] { + t.Errorf("want findings in pkg/x.go and sub/x/x.go, got %+v", got) + } +} + +// A go.work at the root makes golangci-lint resolve the nested module +// itself, so today's single run from the root must stay exactly that. +// This is the regression guard for the fix, not a test of the bug. +// +// proved by: group by go.mod unconditionally — the go.work repo then gets +// a run from sub/ and this fails on the call count and directory. +func TestLintGoKeepsOneRootRunUnderGoWork(t *testing.T) { + calls := stubGolangci(t) + root := moduleRepo(t, true) + files := []string{filepath.Join(root, "pkg", "x.go"), filepath.Join(root, "sub", "x", "x.go")} + + got := lintGo(root, files, false) + + cs := calls() + if len(cs) != 1 || cs[0].dir != root { + t.Fatalf("a go.work repo keeps one run from the root, got %+v", cs) + } + if !hasArg(cs[0], "./pkg") || !hasArg(cs[0], "./sub/x") { + t.Errorf("root-relative patterns expected under go.work: %v", cs[0].args) + } + seen := map[string]bool{} + for _, f := range got { + seen[f.File] = true + } + if !seen["pkg/x.go"] || !seen["sub/x/x.go"] { + t.Errorf("findings must stay root-relative, got %+v", got) + } +} + +// The project's golangci config wins: a nested module without its own file +// inherits the ROOT config explicitly (it now runs from a directory the +// root file is not in), one with its own is left to find it, and a repo +// with neither gets the procoder baseline as before. +// +// proved by: drop the --config pass-through for the root file — the first +// case then runs without it and silently loses the project's linters. +func TestLintGoConfigSelectionPerModule(t *testing.T) { + t.Run("root config reaches a nested module that has none", func(t *testing.T) { + calls := stubGolangci(t) + root := moduleRepo(t, false) + cfg := filepath.Join(root, ".golangci.yml") + if err := os.WriteFile(cfg, []byte("version: \"2\"\n"), 0o644); err != nil { + t.Fatal(err) + } + lintGo(root, []string{filepath.Join(root, "sub", "x", "x.go")}, false) + cs := calls() + if len(cs) != 1 || argAfter(cs[0], "--config") != cfg { + t.Errorf("want --config %s, got %+v", cfg, cs) + } + }) + t.Run("the nested module's own config is not overridden", func(t *testing.T) { + calls := stubGolangci(t) + root := moduleRepo(t, false) + for _, p := range []string{filepath.Join(root, ".golangci.yml"), filepath.Join(root, "sub", ".golangci.yml")} { + if err := os.WriteFile(p, []byte("version: \"2\"\n"), 0o644); err != nil { + t.Fatal(err) + } + } + lintGo(root, []string{filepath.Join(root, "sub", "x", "x.go")}, false) + cs := calls() + if len(cs) != 1 || hasArg(cs[0], "--config") { + t.Errorf("the module's own config must be found by golangci-lint, not overridden: %+v", cs) + } + }) + t.Run("no project config means the baseline", func(t *testing.T) { + calls := stubGolangci(t) + root := moduleRepo(t, false) + lintGo(root, []string{filepath.Join(root, "sub", "x", "x.go")}, false) + cs := calls() + if len(cs) != 1 || !strings.Contains(argAfter(cs[0], "--config"), "procoder-golangci-") { + t.Errorf("want the procoder baseline config, got %+v", cs) + } + }) +} + +// The real binary, where one is installed: a nested module's file used to +// come back NOT checked ("main module ... does not contain package"). Here +// it must be checked and the finding must carry the repo-root path. +// +// proved by: revert the fix — the finding is the NOT-checked refusal, whose +// message names golangci-lint failing, not an errcheck on sub/x/x.go. +func TestGolangciFindsARealProblemInANestedModule(t *testing.T) { + if tools.Resolve(GolangciLint, "") == "" { + t.Skip("golangci-lint not installed; the stub tests carry the logic") + } + root := moduleRepo(t, false) + src := "package x\n\nimport \"os\"\n\nfunc F() { os.Remove(\"nope\") }\n" + file := filepath.Join(root, "sub", "x", "x.go") + if err := os.WriteFile(file, []byte(src), 0o644); err != nil { + t.Fatal(err) + } + got := lintGo(root, []string{file}, true) + if len(got) == 0 { + t.Fatal("an unchecked error in the nested module produced no finding") + } + for _, f := range got { + if strings.Contains(f.Message, "NOT checked") { + t.Fatalf("the nested module's file was not checked: %+v", f) + } + if f.File != "sub/x/x.go" { + t.Errorf("finding path %q, want sub/x/x.go", f.File) + } + } +} From ce279fad858d6781cba47f046c773b023d836a7f Mon Sep 17 00:00:00 2001 From: Pascal Watteel Date: Mon, 9 Nov 2026 16:29:21 +0400 Subject: [PATCH 2/2] test: red proof for nested go modules --- internal/testrun/gomod_test.go | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 internal/testrun/gomod_test.go diff --git a/internal/testrun/gomod_test.go b/internal/testrun/gomod_test.go new file mode 100644 index 00000000..12b01d7b --- /dev/null +++ b/internal/testrun/gomod_test.go @@ -0,0 +1,62 @@ +package testrun + +import ( + "strings" + "testing" +) + +// nestedModuleFixture is a root module plus a nested one (its own go.mod, +// `replace => ../`), each with one package and one test. +func nestedModuleFixture(t *testing.T, nestedPasses bool) string { + t.Helper() + root := t.TempDir() + write(t, root, "go.mod", "module demo\n\ngo 1.22\n") + write(t, root, "a/a.go", "package a\n") + write(t, root, "a/a_test.go", "package a\n\nimport \"testing\"\n\nfunc TestA(t *testing.T) {}\n") + write(t, root, "sub/go.mod", "module demo/sub\n\ngo 1.22\n\nrequire demo v0.0.0\n\nreplace demo => ../\n") + write(t, root, "sub/x/x.go", "package x\n") + body := "t.Fatal(\"deliberately red\")" + if nestedPasses { + body = "_ = t" + } + write(t, root, "sub/x/x_test.go", "package x\n\nimport \"testing\"\n\nfunc TestX(t *testing.T) { "+body+" }\n") + return root +} + +// A package in a nested module used to be named from the repo root and +// came back as "[setup failed] main module does not contain package": a +// correct commit blocked. Narrowed to it, the suite must run from the +// module's own directory. +// +// proved by: have runGo always run from root — the nested package fails to +// set up and this reads Fail with "does not contain package". +func TestGoNestedModulePackageIsTestedFromItsModule(t *testing.T) { + requireGo(t) + root := nestedModuleFixture(t, true) + r := runGo(root, []string{"sub/x"}, false, "") + if r.Verdict != Pass { + t.Fatalf("nested module package must pass from its own dir, got %s: %s", r.Verdict, r.Detail) + } + if !strings.Contains(r.Detail, "sub: pass") { + t.Errorf("the detail must say which module ran: %q", r.Detail) + } +} + +// Both modules in one commit run, and a red test in either one is red. +// +// proved by: drop the Fail branch of the merge in runGoModules — a failing +// nested module is reported as a pass. +func TestGoMixedModulesRunBothAndFailIfEitherFails(t *testing.T) { + requireGo(t) + root := nestedModuleFixture(t, false) + r := runGo(root, []string{"a", "sub/x"}, false, "") + if r.Verdict != Fail { + t.Fatalf("a red test in the nested module must fail the run, got %s: %s", r.Verdict, r.Detail) + } + if !strings.Contains(r.Detail, ".: pass") || !strings.Contains(r.Detail, "sub: ") { + t.Errorf("both modules must be reported: %q", r.Detail) + } + if !strings.Contains(r.Output+r.Detail, "TestX") { + t.Errorf("the failing test must be named: %q / %q", r.Detail, r.Output) + } +}