From da748281f5d9d5d845eceb5e58519514840f4f42 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 15 Nov 2026 18:52:33 +0100 Subject: [PATCH] rbtest: a bench module nested below bench/ belongs to the repository too checkRoot accepted the root module or its direct child only (path.Dir(self) == root), so go-simd/floats, whose perf module is bench/perf, was refused as if it were another repository's binary. The rule is now ownsModule: the same module or any module below it, which still refuses a sibling that merely shares a prefix (github.com/o/rx). Tested both ways in TestOwnsModule and TestRootGuard (bench/perf allowed, a prefix sibling refused); restoring the direct-parent rule turns both red. Fixes #18 Co-Authored-By: Claude Opus 5.5 --- rbtest/rbtest.go | 21 ++++++++++++++------- rbtest/rbtest_test.go | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/rbtest/rbtest.go b/rbtest/rbtest.go index 7375d0e..22ff3e5 100644 --- a/rbtest/rbtest.go +++ b/rbtest/rbtest.go @@ -23,8 +23,8 @@ // -perf.run the test never times anything. -perf.record keeps the recorded // timings and only updates the allocation counts. // -// Nothing is written unless the repository root's go.mod names the parent of -// this binary's module (a bench/ module of that repository): a binary of one +// Nothing is written unless the repository root's go.mod names an ancestor of +// this binary's module (a bench/ or bench/perf/ module of that repository): a binary of one // repository run from another's bench/ directory refuses, naming both. package rbtest @@ -35,7 +35,6 @@ import ( "flag" "fmt" "os" - "path" "path/filepath" "runtime/debug" "strings" @@ -108,9 +107,17 @@ func modulePath(file string) (string, error) { return "", fmt.Errorf("%s: no module line", file) } +// ownsModule reports whether the repository whose root module is root owns +// the module self: the same module, or one nested anywhere below it +// (github.com/o/r owns github.com/o/r/bench and github.com/o/r/bench/perf). +// A sibling that merely shares a prefix (github.com/o/rx) is not owned. +func ownsModule(root, self string) bool { + return self == root || strings.HasPrefix(self, root+"/") +} + // checkRoot refuses to touch docs/ of a repository that is not the binary's -// own: the module of cfg.Root's go.mod must be the parent of the binary's -// module (github.com/o/r for github.com/o/r/bench), or the same module. +// own: the module of cfg.Root's go.mod must be the binary's module or an +// ancestor of it (github.com/o/r for github.com/o/r/bench or .../bench/perf). func checkRoot(cfg refbench.CIConfig) error { goMod := filepath.Join(cfg.Root, "go.mod") root, err := modulePath(goMod) @@ -121,8 +128,8 @@ func checkRoot(cfg refbench.CIConfig) error { if self == "" { return errors.New("refusing to write docs: this binary carries no module path") } - if self != root && path.Dir(self) != root { - return fmt.Errorf("refusing to write docs: this binary's module is %q, but %s declares module %q, which is not its parent (a binary of one repository was run from another's bench directory?)", self, goMod, root) + if !ownsModule(root, self) { + return fmt.Errorf("refusing to write docs: this binary's module is %q, but %s declares module %q, which is not an ancestor of it (a binary of one repository was run from another's bench directory?)", self, goMod, root) } return nil } diff --git a/rbtest/rbtest_test.go b/rbtest/rbtest_test.go index 97a8a50..3114788 100644 --- a/rbtest/rbtest_test.go +++ b/rbtest/rbtest_test.go @@ -485,8 +485,16 @@ func TestRootGuard(t *testing.T) { if got := outcome(func() { perf(&fakeT{}, cfg, refbench.Options{}) }); !strings.Contains(got, "cannot read the root module") { t.Errorf("no go.mod: %s", got) } - // The module itself (no bench/ module) is allowed, and so is its bench/. - for _, mod := range []string{"example.com/lib", "example.com/lib/bench"} { + // A sibling that only shares a prefix with the root module is not its own. + cfg = repo(t, fixedRef) + setFlags(t, false, true) + buildModule = func() string { return "example.com/libx/bench" } + if got := outcome(func() { perf(&fakeT{}, cfg, refbench.Options{}) }); !strings.HasPrefix(got, "fatal: refusing to write docs") { + t.Errorf("prefix sibling: %s", got) + } + // The module itself (no bench/ module) is allowed, and so are its bench/ + // and a bench module nested deeper (go-simd/floats keeps it at bench/perf). + for _, mod := range []string{"example.com/lib", "example.com/lib/bench", "example.com/lib/bench/perf"} { cfg = repo(t, fixedRef) setFlags(t, false, true) buildModule = func() string { return mod } @@ -502,6 +510,27 @@ func TestRootGuard(t *testing.T) { } } +// ownsModule answers both ways: what a repository owns, and what it must not. +func TestOwnsModule(t *testing.T) { + for _, c := range []struct { + root, self string + want bool + }{ + {"github.com/o/r", "github.com/o/r", true}, + {"github.com/o/r", "github.com/o/r/bench", true}, + {"github.com/o/r", "github.com/o/r/bench/perf", true}, + {"github.com/o/r", "github.com/o/rx", false}, + {"github.com/o/r", "github.com/o/rx/bench", false}, + {"github.com/o/r", "github.com/o", false}, + {"github.com/o/r", "github.com/p/r/bench", false}, + {"github.com/o/r/bench", "github.com/o/r", false}, + } { + if got := ownsModule(c.root, c.self); got != c.want { + t.Errorf("ownsModule(%q, %q) = %v, want %v", c.root, c.self, got, c.want) + } + } +} + func TestModulePath(t *testing.T) { dir := t.TempDir() for in, want := range map[string]string{