Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions rbtest/rbtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -35,7 +35,6 @@ import (
"flag"
"fmt"
"os"
"path"
"path/filepath"
"runtime/debug"
"strings"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
33 changes: 31 additions & 2 deletions rbtest/rbtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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{
Expand Down
Loading