// Package repocheck holds tests that guard repository-wide rules. package repocheck import ( "bufio" "io/fs" "os" "path/filepath" "strings" "testing" ) // textExt lists the file types that are checked. var textExt = map[string]bool{ ".go": true, ".md": true, ".toml": true, ".sql": true, ".yml": true, ".yaml": true, ".sh": true, ".example": true, ".service": true, ".ts": true, ".tsx": true, ".css": true, ".html": true, ".json": true, ".txt": true, "": true, } var skipDirs = map[string]bool{ ".git": true, ".tools": true, "node_modules": true, "dist": true, "data": true, "bin": true, } func repoRoot(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("go.mod not found") } dir = parent } } // TestNoEmDashes enforces the project rule that no text uses em dashes: // not code comments, not docs, not translations. func TestNoEmDashes(t *testing.T) { root := repoRoot(t) const emDash = "\u2014" err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } if d.IsDir() { if skipDirs[d.Name()] { return filepath.SkipDir } return nil } if !textExt[filepath.Ext(path)] || d.Name() == "package-lock.json" { return nil } f, err := os.Open(path) if err != nil { return err } defer f.Close() sc := bufio.NewScanner(f) sc.Buffer(make([]byte, 64*1024), 4*1024*1024) for n := 1; sc.Scan(); n++ { if strings.Contains(sc.Text(), emDash) { rel, _ := filepath.Rel(root, path) t.Errorf("%s:%d contains an em dash", rel, n) } } return nil }) if err != nil { t.Fatal(err) } }