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 "go/parser"
5 "go/token"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "strings"
10 "testing"
11)
12
13// preMoveRastrillo is rastrillo's module path before it moved to
14// amadan.net/rastrillo/rastrillo (v0.25.0). Nothing after v0.23.0 was
15// released under it, so code importing it gets no rastrillo fix, and
16// its own vendored-asset check cannot tell. That is how Memoria kept
17// zooming iOS on every tap for a month after rastrillo fixed it (polish
18// audit r3, item 1).
19const preMoveRastrillo = "github.com/carlosframework/rastrillo"
20
21// preMoveAllowed is the one file that may still import it, and why:
22// rastrillo/native's server takes the old path's *sessions.Sessions.
23// Delete the entry when native/server moves.
24var preMoveAllowed = map[string]string{
25 "internal/memoria/native.go": preMoveRastrillo + "/sessions",
26}
27
28// TestNoPreMoveRastrillo fails on any import of rastrillo's old module
29// path outside preMoveAllowed, anywhere in the repository's Go.
30func TestNoPreMoveRastrillo(t *testing.T) {
31 root, err := filepath.Abs(filepath.Join("..", ".."))
32 if err != nil {
33 t.Fatal(err)
34 }
35 fset := token.NewFileSet()
36 seen := 0
37 err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
38 if err != nil {
39 return err
40 }
41 if d.IsDir() {
42 switch d.Name() {
43 case ".git", "node_modules", "native", "releases":
44 return filepath.SkipDir
45 }
46 return nil
47 }
48 if !strings.HasSuffix(path, ".go") {
49 return nil
50 }
51 src, err := os.ReadFile(path)
52 if err != nil {
53 return err
54 }
55 f, err := parser.ParseFile(fset, path, src, parser.ImportsOnly)
56 if err != nil {
57 return nil // a file that does not parse is the compiler's problem
58 }
59 seen++
60 rel, _ := filepath.Rel(root, path)
61 rel = filepath.ToSlash(rel)
62 for _, im := range f.Imports {
63 imp := strings.Trim(im.Path.Value, `"`)
64 if (imp == preMoveRastrillo || strings.HasPrefix(imp, preMoveRastrillo+"/")) && preMoveAllowed[rel] != imp {
65 t.Errorf("%s: imports %q, rastrillo's frozen pre-move path; use amadan.net/rastrillo/rastrillo", rel, imp)
66 }
67 }
68 return nil
69 })
70 if err != nil {
71 t.Fatal(err)
72 }
73 // The walk found the app, or it would pass by reading nothing.
74 if seen < 50 {
75 t.Fatalf("walked only %d Go files from %s; the walk is not reaching the repository", seen, root)
76 }
77}
78