oficina / memoria Public

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

Plain git — no account needed to clone.

Download

Download this file

1package memoriatest
2
3import (
4 "fmt"
5 "go/ast"
6 "go/parser"
7 "go/token"
8 "os"
9 "path/filepath"
10 "sort"
11 "strings"
12 "testing"
13)
14
15// Every plain-text error answer left in the app is here, by file and
16// function, with why it is not a page (shared chrome §3.3). A new
17// http.Error or http.NotFound anywhere else fails this gate: it is either
18// a page (errorPage) or it is classified here with its reason.
19//
20// Keyed by function, not line, so an edit above a site does not churn the
21// list; the count per function catches a second site added beside one.
22var plainTextErrors = map[string]struct {
23 n int
24 why string
25}{
26 "app.go:requireSession": {2, "a boot misconfiguration (no sign-in wired, or team mode configured with no roster runtime): every request is refused, and there is no viewer to draw chrome for"},
27 "auth.go:beginGuard": {1, "the same misconfiguration on the sign-in POST (no owners list)"},
28 "signin.go:signinDoorStatus": {1, "the same misconfiguration at the sign-in door (no sign-in wired): there is no door to draw"},
29 "signin.go:forget": {1, "the same misconfiguration on the door's Forget POST (no sign-in wired)"},
30 "errors.go:plainError": {1, "the plain answer itself, for a request that did not ask for a page"},
31 "errors.go:methodNotAllowed": {1, "405 to a method no route takes, with Allow set: never a navigation (a GET of a POST-only path gets the 404 page)"},
32 "handlers.go:writeOutcome": {1, "the upload tray's JSON path: a bare non-200 is what the tray reads as \"no answer\" (spec §3.3 keeps it)"},
33 "native.go:nativeSearch": {4, "the native client's JSON API; it shows these strings itself"},
34 "native.go:nativeNote": {3, "the native client's JSON API; it shows these strings itself"},
35 "render.go:renderStatus": {1, "a page template failed to execute: the error page is built from the same layout, so the plainest answer is the one left"},
36 "render.go:renderFragment": {1, "search.js's results fragment: a fetch, not a navigation, and search.js reads a non-200 as a search that did not run"},
37}
38
39func TestEveryPlainTextErrorIsClassified(t *testing.T) {
40 dir := filepath.Join("..", "memoria")
41 entries, err := os.ReadDir(dir)
42 if err != nil {
43 t.Fatal(err)
44 }
45 found := map[string]int{}
46 seen := 0
47 for _, e := range entries {
48 name := e.Name()
49 if e.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
50 continue
51 }
52 fset := token.NewFileSet()
53 file, err := parser.ParseFile(fset, filepath.Join(dir, name), nil, parser.SkipObjectResolution)
54 if err != nil {
55 t.Fatal(err)
56 }
57 for _, decl := range file.Decls {
58 fn, ok := decl.(*ast.FuncDecl)
59 if !ok || fn.Body == nil {
60 continue
61 }
62 ast.Inspect(fn.Body, func(n ast.Node) bool {
63 call, ok := n.(*ast.CallExpr)
64 if !ok {
65 return true
66 }
67 sel, ok := call.Fun.(*ast.SelectorExpr)
68 if !ok {
69 return true
70 }
71 pkg, ok := sel.X.(*ast.Ident)
72 if !ok || pkg.Name != "http" || (sel.Sel.Name != "Error" && sel.Sel.Name != "NotFound") {
73 return true
74 }
75 seen++
76 found[name+":"+fn.Name.Name]++
77 return true
78 })
79 }
80 }
81 if seen == 0 {
82 t.Fatal("found no http.Error at all: the walk is broken, not the app clean")
83 }
84 var problems []string
85 for key, n := range found {
86 want, ok := plainTextErrors[key]
87 switch {
88 case !ok:
89 problems = append(problems, fmt.Sprintf("%s: %d unclassified plain-text error(s) — render errorPage, or classify it here with its reason", key, n))
90 case want.n != n:
91 problems = append(problems, fmt.Sprintf("%s: %d plain-text error(s), classified %d", key, n, want.n))
92 case want.why == "":
93 problems = append(problems, key+": classified with no reason")
94 }
95 }
96 for key := range plainTextErrors {
97 if found[key] == 0 {
98 problems = append(problems, key+": classified, but no longer there — remove it from the list")
99 }
100 }
101 sort.Strings(problems)
102 for _, p := range problems {
103 t.Error(p)
104 }
105}
106