oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1// Package lint holds build-time guards over this repo's own source —
2// properties that used to be enforced only by prose in a doc comment,
3// promoted to something `go test ./...` (and so scripts/wire-gate.sh's
4// "go: vet, test, gofmt" step) cannot pass without.
5package lint
6
7import (
8 "go/ast"
9 "go/parser"
10 "go/token"
11 "os"
12 "path/filepath"
13 "strconv"
14 "strings"
15 "testing"
16)
17
18// bannedSessionsUserIDPackages and bannedSessionsUserIDFunc identify
19// the one call this repo's own code must never make. See
20// TestNoSessionsUserID's own doc comment for why. Both module paths:
21// native.go keeps one import of the pre-move package
22// (TestNoPreMoveRastrillo), and its UserID is the same trap.
23var bannedSessionsUserIDPackages = map[string]bool{
24 "amadan.net/rastrillo/rastrillo/sessions": true,
25 "github.com/carlosframework/rastrillo/sessions": true,
26}
27
28const bannedSessionsUserIDFunc = "UserID"
29
30// repoRoot resolves the module root from this test's own directory —
31// internal/lint, two levels below it — the same "../.." relative-path
32// convention testing.go's ReadFixture already uses from internal/docs
33// (also two levels below the root), which works because `go test`
34// always runs with the package's own directory as its working
35// directory.
36func repoRoot(t *testing.T) string {
37 t.Helper()
38 root, err := filepath.Abs(filepath.Join("..", ".."))
39 if err != nil {
40 t.Fatalf("lint: resolve repo root: %v", err)
41 }
42 if _, err := os.Stat(filepath.Join(root, "go.mod")); err != nil {
43 t.Fatalf("lint: %s does not look like the module root (no go.mod): %v", root, err)
44 }
45 return root
46}
47
48// skipDir reports whether WalkDir should not descend into a
49// directory: version control, and the two large generated/vendored
50// trees this repo's own build produces (fixtures/node_modules from
51// `npm ci`, core/target from `cargo build`) — neither holds Go source
52// this app wrote, and both are large enough that walking them would
53// only slow this test down for nothing.
54func skipDir(name string) bool {
55 switch name {
56 // .build is SwiftPM's and Xcode's: checkouts of other repositories
57 // (rastrillo/native's own server among them), never Docs' code.
58 case ".git", "node_modules", "target", ".build":
59 return true
60 }
61 return false
62}
63
64// TestNoSessionsUserID is a build-time guard, not a comment: this
65// repo's own code must never call sessions.UserID (rastrillo/auth's
66// underlying sessions package), because under the magic-link plugin
67// this app uses, the session Subject is the verified email address
68// itself — sessions.UserID's own contract can only ever return
69// (0, false) here, on every request, with no way for that to change
70// short of switching auth plugins. A version of this app's own code
71// that called it anyway would not error or panic; it would silently
72// scope a query to person/user id 0 and behave like an empty result —
73// the same failure class rastrillo's own scope.Owned has on a table
74// that grows sharing (store.go's own doc comment on `owned`): wrong
75// answers that look like "you just have nothing here," not a crash
76// that gets noticed.
77//
78// Four comments (models.go, store.go, handlers.go, testing.go) have
79// said "never sessions.UserID" since Task 1/2. A comment is a code
80// review that has to succeed every single time a future change is
81// made near this code; this test is what makes a violation impossible
82// to land instead of merely discouraged. It walks this repo's own Go
83// AST rather than grepping for the symbol, deliberately: a text scan
84// would have to carve out an exemption for every one of those four
85// comments (and grow that exemption list forever), and could never
86// tell a real call apart from a comment or a string that merely
87// mentions the name. Flagging only actual call expressions — Fun
88// resolving, through the file's own import spec, to
89// sessions.UserID — needs no exemption list at all and cannot be
90// fooled by a comment quoting the symbol, including this test's own.
91//
92// Credit: adapted from the Sheets session's TestNoSessionsUserID,
93// the same guard for the same reason under the same auth plugin.
94func TestNoSessionsUserID(t *testing.T) {
95 root := repoRoot(t)
96 fset := token.NewFileSet()
97
98 err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
99 if err != nil {
100 return err
101 }
102 if d.IsDir() {
103 if path != root && skipDir(d.Name()) {
104 return filepath.SkipDir
105 }
106 return nil
107 }
108 if !strings.HasSuffix(path, ".go") {
109 return nil
110 }
111
112 f, err := parser.ParseFile(fset, path, nil, parser.SkipObjectResolution)
113 if err != nil {
114 return err
115 }
116
117 // aliases maps this file's own local identifier for the
118 // banned package to true (an ordinary named or default
119 // import) — resolved per file, because the same package can
120 // legally be imported under different local names in
121 // different files. dotImported is set instead when the file
122 // dot-imports it, which puts UserID in scope unqualified
123 // (a bare *ast.Ident call, no selector at all) rather than as
124 // sessions.UserID — the AST shape a plain "sessions.UserID"
125 // text search would never even think to look for, and
126 // exactly the kind of gap a symbol-precise walk closes for
127 // free.
128 aliases := map[string]bool{}
129 dotImported := false
130 for _, imp := range f.Imports {
131 importPath, err := strconv.Unquote(imp.Path.Value)
132 if err != nil || !bannedSessionsUserIDPackages[importPath] {
133 continue
134 }
135 switch {
136 case imp.Name == nil:
137 aliases["sessions"] = true // the package's own default name
138 case imp.Name.Name == ".":
139 dotImported = true
140 case imp.Name.Name == "_":
141 // blank-imported for side effects only; nothing in
142 // this file can reference it by name at all.
143 default:
144 aliases[imp.Name.Name] = true
145 }
146 }
147 if len(aliases) == 0 && !dotImported {
148 // This file does not import the banned package under any
149 // name — nothing it writes can possibly call it, however
150 // often it mentions the name in a comment or a string.
151 return nil
152 }
153
154 ast.Inspect(f, func(n ast.Node) bool {
155 call, ok := n.(*ast.CallExpr)
156 if !ok {
157 return true
158 }
159 switch fn := call.Fun.(type) {
160 case *ast.SelectorExpr:
161 if fn.Sel.Name != bannedSessionsUserIDFunc {
162 return true
163 }
164 id, ok := fn.X.(*ast.Ident)
165 if !ok || !aliases[id.Name] {
166 return true
167 }
168 case *ast.Ident:
169 if !dotImported || fn.Name != bannedSessionsUserIDFunc {
170 return true
171 }
172 default:
173 return true
174 }
175 pos := fset.Position(call.Pos())
176 rel, relErr := filepath.Rel(root, pos.Filename)
177 if relErr != nil {
178 rel = pos.Filename
179 }
180 t.Errorf(
181 "%s:%d: calls sessions.UserID — banned in this app: under the magic-link plugin the session Subject is the verified email address, so this can only ever return (0, false) here; use auth.From(r).Address and Store.EnsurePerson instead (see handlers.go's owner helper)",
182 rel, pos.Line,
183 )
184 return true
185 })
186 return nil
187 })
188 if err != nil {
189 t.Fatalf("lint: walk %s: %v", root, err)
190 }
191}
192