hub (unreleased): review fix 6 — the test-only helpers in production files refuse outside a test binary, and no production file may call one
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
1 parent
a6f4aa0b64
commit
efea838203
7 files changed
+144
No files matched your search
@@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Review finding 6: a helper named `…ForTest` is a way around the product's rules (SetEndNoticeForTest writes „told”
|
||||
// with any date; SetSendFnForTest replaces the mail service). Some must live in production files because another
|
||||
// package's tests call them. This test reads every non-test Go file of the hub and fails when one CALLS such a
|
||||
// helper — a definition (`func (…) XForTest(`) is not a call.
|
||||
func TestReview6_NoProductionFileCallsATestOnlyHelper(t *testing.T) {
|
||||
root := filepath.Join("..", "..")
|
||||
call := regexp.MustCompile(`\b[A-Za-z0-9_]+ForTest\(`)
|
||||
def := regexp.MustCompile(`^func (\([^)]*\) )?[A-Za-z0-9_]+ForTest\(`)
|
||||
files, defs := 0, 0
|
||||
err := filepath.WalkDir(root, func(p string, d os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() || !strings.HasSuffix(p, ".go") || strings.HasSuffix(p, "_test.go") {
|
||||
return nil
|
||||
}
|
||||
b, rerr := os.ReadFile(p)
|
||||
if rerr != nil {
|
||||
return rerr
|
||||
}
|
||||
files++
|
||||
for i, line := range strings.Split(string(b), "\n") {
|
||||
if def.MatchString(line) {
|
||||
defs++
|
||||
continue
|
||||
}
|
||||
if tr := strings.TrimSpace(line); strings.HasPrefix(tr, "//") {
|
||||
continue
|
||||
}
|
||||
if call.MatchString(line) {
|
||||
t.Errorf("%s:%d calls a test-only helper from production code: %s", p, i+1, strings.TrimSpace(line))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if files < 100 || defs < 5 {
|
||||
t.Fatalf("read %d files and found %d …ForTest definitions — the scan stopped reading the hub", files, defs)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user