| 1 | package guards |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // cgoSQLiteDriverForbidden reports whether imp is a cgo SQLite driver. |
| 9 | // The stack is cgo-free by design (AGENTS.md / Deploys: "One binary, |
| 10 | // CGO_ENABLED=0, cgo-free SQLite. A cgo driver sneaking in is a bug."), |
| 11 | // so either of these landing in go.mod's dependency graph is a bug, not |
| 12 | // a style preference. |
| 13 | func cgoSQLiteDriverForbidden(imp string) bool { |
| 14 | return strings.HasPrefix(imp, "github.com/glebarez/") || imp == "gorm.io/driver/sqlite" |
| 15 | } |
| 16 | |
| 17 | // TestNoCgoSQLiteDriver walks every .go file's import block and fails |
| 18 | // on any import of glebarez/* or gorm.io/driver/sqlite. |
| 19 | func TestNoCgoSQLiteDriver(t *testing.T) { |
| 20 | root := repoRoot(t) |
| 21 | for _, path := range goFiles(t, root) { |
| 22 | rel := relPath(t, root, path) |
| 23 | for _, imp := range fileImports(t, path) { |
| 24 | if cgoSQLiteDriverForbidden(imp) { |
| 25 | t.Errorf("%s: imports %q, a cgo SQLite driver forbidden by AGENTS.md (Deploys: cgo-free SQLite)", rel, imp) |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // preMoveRastrillo is rastrillo's module path before it moved to |
| 32 | // amadan.net/rastrillo/rastrillo (v0.25.0). Nothing after v0.23.0 was |
| 33 | // released under it, so code importing it gets no rastrillo fix — which |
| 34 | // is how Sheets kept zooming iOS on every tap for a month after rastrillo |
| 35 | // fixed it (polish audit r3, item 1). |
| 36 | const preMoveRastrillo = "github.com/carlosframework/rastrillo" |
| 37 | |
| 38 | // preMoveAllowed is the one file that may still import it, and why: |
| 39 | // rastrillo/native's server takes the old path's *sessions.Sessions. |
| 40 | // Delete the entry when native/server moves. |
| 41 | var preMoveAllowed = map[string]string{ |
| 42 | "internal/sheets/native.go": preMoveRastrillo + "/sessions", |
| 43 | } |
| 44 | |
| 45 | // TestNoPreMoveRastrillo fails on any import of rastrillo's old module |
| 46 | // path outside preMoveAllowed. |
| 47 | func TestNoPreMoveRastrillo(t *testing.T) { |
| 48 | root := repoRoot(t) |
| 49 | for _, path := range goFiles(t, root) { |
| 50 | rel := relPath(t, root, path) |
| 51 | for _, imp := range fileImports(t, path) { |
| 52 | if importUnder(imp, preMoveRastrillo) && preMoveAllowed[rel] != imp { |
| 53 | t.Errorf("%s: imports %q, rastrillo's frozen pre-move path; use amadan.net/rastrillo/rastrillo", rel, imp) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |