Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

refactor: the shared census home excludes test files, so the census whose subject IS tests keeps a private copy of the nested-checkout rule (PIN-246)

Luigi Agosti95857467f090
diff --git a/internal/srccensus/srccensus.go b/internal/srccensus/srccensus.go
index dd596cbf..11971307 100644
--- a/internal/srccensus/srccensus.go
+++ b/internal/srccensus/srccensus.go
@@ -135,6 +135,29 @@ var treeSkipDirs = map[string]bool{".git": true, "node_modules": true, "dist": t
// misses the worktree, which is the case that actually occurs.
func Tree(t testing.TB, root string, minFiles int) []SourceFile {
t.Helper()
+ return tree(t, root, minFiles, "non-test", func(path string) bool {
+ return strings.HasSuffix(path, ".go") && !strings.HasSuffix(path, "_test.go")
+ })
+}
+
+// TestTree enumerates every _test.go file under root, for a census whose SUBJECT is the
+// tests themselves — a rule about how tests must be written cannot be checked over the
+// files Tree returns, since it excludes exactly them.
+//
+// It shares Tree's walk so the EXCLUSIONS are inherited rather than re-derived. That is the
+// whole reason it exists here rather than as a bespoke WalkDir in the package that needs it:
+// a hand-rolled walk written to see test files loses the nested-checkout skip, and then a
+// worktree's copies of the very test files the rule polices are reported as violations
+// against paths that vanish.
+func TestTree(t testing.TB, root string, minFiles int) []SourceFile {
+ t.Helper()
+ return tree(t, root, minFiles, "test", func(path string) bool {
+ return strings.HasSuffix(path, "_test.go")
+ })
+}
+
+func tree(t testing.TB, root string, minFiles int, kind string, include func(path string) bool) []SourceFile {
+ t.Helper()
abs, err := filepath.Abs(root)
if err != nil {
@@ -155,7 +178,7 @@ func Tree(t testing.TB, root string, minFiles int) []SourceFile {
}
return nil
}
- if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
+ if !include(path) {
return nil
}
rel, relErr := filepath.Rel(abs, path)
diff --git a/internal/srccensus/srccensus_test.go b/internal/srccensus/srccensus_test.go
index 840b327d..83ecf601 100644
--- a/internal/srccensus/srccensus_test.go
+++ b/internal/srccensus/srccensus_test.go
@@ -391,3 +391,41 @@ func TestTreeCarriesEachFilesTextAndAModuleRelativeName(t *testing.T) {
t.Errorf("second file = %+v, want a slash-separated relative name and its text", files[1])
}
}
+
+// TestTree exists because a rule ABOUT tests cannot be checked over the files Tree returns —
+// it excludes exactly them. The point of it living here rather than as a bespoke walk in the
+// package that needs it is that the exclusions are inherited, so both halves are asserted:
+// the file class it selects, and the nested checkout it still skips.
+func TestTestTreeSelectsTestFilesAndInheritsTheExclusions(t *testing.T) {
+ root := writeTree(t, map[string]string{
+ "keep_test.go": "package p\n",
+ "nested/also_test.go": "package p\n",
+ "production.go": "package p\n",
+ "notes.txt": "text\n",
+ "scratch-copy/.git": "gitdir: /elsewhere\n",
+ "scratch-copy/leaked_test.go": "package p\n",
+ "node_modules/dep/vendored_test.go": "package p\n",
+ })
+
+ var names []string
+ for _, file := range TestTree(t, root, 2) {
+ names = append(names, file.Name)
+ }
+
+ want := []string{"keep_test.go", "nested/also_test.go"}
+ if strings.Join(names, ",") != strings.Join(want, ",") {
+ t.Errorf("TestTree = %v, want exactly %v — production sources and non-Go files are not its subject, and a nested checkout's copies are not module source", names, want)
+ }
+}
+
+// The floor message has to name the class it counted, or a census that walked the wrong file
+// class reads as a census that found too little.
+func TestTestTreeFloorNamesTheFileClassItCounted(t *testing.T) {
+ root := writeTree(t, map[string]string{"only_test.go": "package p\n"})
+
+ got := mustFatal(t, func(tb testing.TB) { TestTree(tb, root, 5) })
+
+ if !strings.Contains(got, "test files") {
+ t.Errorf("floor failure said %q, want it to name the file class counted", got)
+ }
+}
diff --git a/internal/testbrowser/profile_test.go b/internal/testbrowser/profile_test.go
index 36dee660..fd211f98 100644
--- a/internal/testbrowser/profile_test.go
+++ b/internal/testbrowser/profile_test.go
@@ -6,8 +6,9 @@ import (
"go/token"
"os"
"path/filepath"
- "strings"
"testing"
+
+ "github.com/pinchtab/pinchtab/internal/srccensus"
)
// The property the fix delivers, proven deterministically rather than sampled by
@@ -77,66 +78,43 @@ func (r *cleanupRecorder) Cleanup(f func()) { f() }
// own RemoveAll succeeded, and a browser still flushing its cache makes that fail on
// an unrelated card's gate. The rule is checked over the whole module, because the
// next browser test is as likely to be written in a package that has none today.
+// The subject is the tests themselves, so the enumeration comes from srccensus.TestTree
+// rather than a walk written here: the nested-checkout exclusion is the part a bespoke walk
+// silently loses, and it has one owner.
func TestNoBrowserProfileIsPointedAtATestTempDir(t *testing.T) {
- root := moduleRoot(t)
sites := 0
- err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
- if err != nil {
- return err
- }
- if d.IsDir() {
- if skipCensusDir(path, root, d.Name()) {
- return filepath.SkipDir
- }
- return nil
- }
- if !strings.HasSuffix(path, "_test.go") {
- return nil
- }
-
- file, parseErr := parser.ParseFile(token.NewFileSet(), path, nil, 0)
+ for _, file := range srccensus.TestTree(t, "../..", moduleTestFileFloor) {
+ parsed, parseErr := parser.ParseFile(token.NewFileSet(), file.Path, file.Text, 0)
if parseErr != nil {
- return nil
+ continue
}
- rel, _ := filepath.Rel(root, path)
- ast.Inspect(file, func(n ast.Node) bool {
+ ast.Inspect(parsed, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok || !isSelectorCall(call.Fun, "chromedp", "UserDataDir") || len(call.Args) != 1 {
return true
}
sites++
if inner, ok := call.Args[0].(*ast.CallExpr); ok && isMethodCall(inner.Fun, "TempDir") {
- t.Errorf("%s hands a Chrome profile to t.TempDir; use testbrowser.ProfileDir(t) — t.TempDir fails the test when the browser is still flushing its cache", rel)
+ t.Errorf("%s hands a Chrome profile to t.TempDir; use testbrowser.ProfileDir(t) — t.TempDir fails the test when the browser is still flushing its cache", file.Name)
}
return true
})
- return nil
- })
- if err != nil {
- t.Fatalf("walk %s: %v", root, err)
}
- if sites < 20 {
+
+ if sites < userDataDirSiteFloor {
t.Fatalf("found only %d chromedp.UserDataDir call sites; the census matched almost nothing and would pass vacuously", sites)
}
}
-// skipCensusDir decides which directories the census must not descend into. A name list
-// alone is not enough: a git worktree created inside the repo carries a .git FILE under an
-// arbitrary name, so it looks like an ordinary directory while holding a second copy of
-// every source file. The census would then report those copies as fresh violations, against
-// module-relative paths that will vanish with the worktree — and on a shared tree that reds
-// this package for whoever runs it next. The repo root carries .git too, so it is exempt.
-func skipCensusDir(path, root, name string) bool {
- if name == ".git" || name == "node_modules" || name == "dist" {
- return true
- }
- if path == root {
- return false
- }
- _, err := os.Stat(filepath.Join(path, ".git"))
- return err == nil
-}
+// Two floors, because they fail for different reasons: the TestTree floor catches a walk that
+// stopped seeing files at all, and the site floor catches a rule that stopped matching what
+// it polices. Both are set well under the real counts so ordinary growth or deletion does not
+// trip them.
+const (
+ moduleTestFileFloor = 200
+ userDataDirSiteFloor = 20
+)
func isSelectorCall(fun ast.Expr, pkg, name string) bool {
sel, ok := fun.(*ast.SelectorExpr)
@@ -151,70 +129,3 @@ func isMethodCall(fun ast.Expr, name string) bool {
sel, ok := fun.(*ast.SelectorExpr)
return ok && sel.Sel.Name == name
}
-
-func moduleRoot(t *testing.T) string {
- t.Helper()
- dir, err := os.Getwd()
- if err != nil {
- t.Fatal(err)
- }
- for {
- if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
- return dir
- }
- parent := filepath.Dir(dir)
- if parent == dir {
- t.Fatal("no go.mod above the test's working directory; the census cannot find the module root")
- }
- dir = parent
- }
-}
-
-// The census walks from the module root, so anything that puts a SECOND copy of the tree
-// inside the repo becomes a second set of findings. A git worktree does exactly that under
-// an arbitrary name, carrying a .git FILE rather than a directory, so a name list cannot see
-// it — and the violations it reports name paths that vanish with the worktree, which sends
-// the reader to fix a file that was never the problem.
-func TestCensusSkipsDirectoriesThatCarryTheirOwnGitMarker(t *testing.T) {
- root := t.TempDir()
-
- worktreeFile := filepath.Join(root, "wt-anything")
- if err := os.MkdirAll(worktreeFile, 0o755); err != nil {
- t.Fatal(err)
- }
- if err := os.WriteFile(filepath.Join(worktreeFile, ".git"), []byte("gitdir: /elsewhere\n"), 0o644); err != nil {
- t.Fatal(err)
- }
-
- worktreeDir := filepath.Join(root, "clone-anything")
- if err := os.MkdirAll(filepath.Join(worktreeDir, ".git"), 0o755); err != nil {
- t.Fatal(err)
- }
-
- ordinary := filepath.Join(root, "internal")
- if err := os.MkdirAll(ordinary, 0o755); err != nil {
- t.Fatal(err)
- }
-
- // The repo root carries .git too and must never be skipped, or the census covers nothing.
- if err := os.MkdirAll(filepath.Join(root, ".git"), 0o755); err != nil {
- t.Fatal(err)
- }
-
- for _, tc := range []struct {
- name string
- path string
- want bool
- }{
- {name: "a nested worktree, .git as a file", path: worktreeFile, want: true},
- {name: "a nested clone, .git as a directory", path: worktreeDir, want: true},
- {name: "an ordinary source directory", path: ordinary, want: false},
- {name: "the module root itself", path: root, want: false},
- } {
- t.Run(tc.name, func(t *testing.T) {
- if got := skipCensusDir(tc.path, root, filepath.Base(tc.path)); got != tc.want {
- t.Errorf("skipCensusDir(%s) = %v, want %v", tc.name, got, tc.want)
- }
- })
- }
-}