| 1 | package fichastest |
| 2 | |
| 3 | import ( |
| 4 | "go/parser" |
| 5 | "go/token" |
| 6 | "io/fs" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "regexp" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | ) |
| 14 | |
| 15 | // preMoveRastrillo is rastrillo's module path before it moved to |
| 16 | // amadan.net/rastrillo/rastrillo (2026-08-31). Nothing after v0.23.0 was |
| 17 | // released under it, so code importing it gets no rastrillo fix — and the |
| 18 | // vendored-asset test cannot tell, because it compares against the same |
| 19 | // frozen ui. That is how Fichas kept iOS zooming on every tap until it |
| 20 | // carried its own copy of the fix (polish audit r3, S13). |
| 21 | const preMoveRastrillo = "github.com/carlosframework/rastrillo" |
| 22 | |
| 23 | // rastrilloPath is where it lives now. |
| 24 | const rastrilloPath = "amadan.net/rastrillo/rastrillo" |
| 25 | |
| 26 | // repoRoot is the module root, two levels above this package. |
| 27 | func repoRoot(t *testing.T) string { |
| 28 | t.Helper() |
| 29 | root, err := filepath.Abs(filepath.Join("..", "..")) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if _, err := os.Stat(filepath.Join(root, "go.mod")); err != nil { |
| 34 | t.Fatalf("no go.mod at %s: %v", root, err) |
| 35 | } |
| 36 | return root |
| 37 | } |
| 38 | |
| 39 | // goImports is every import of every .go file under root, by file. |
| 40 | func goImports(t *testing.T, root string) map[string][]string { |
| 41 | t.Helper() |
| 42 | out := map[string][]string{} |
| 43 | fset := token.NewFileSet() |
| 44 | err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | if d.IsDir() && (d.Name() == ".git" || d.Name() == "releases") { |
| 49 | return filepath.SkipDir |
| 50 | } |
| 51 | if d.IsDir() || !strings.HasSuffix(path, ".go") { |
| 52 | return nil |
| 53 | } |
| 54 | f, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | rel, _ := filepath.Rel(root, path) |
| 59 | for _, imp := range f.Imports { |
| 60 | p, _ := strconv.Unquote(imp.Path.Value) |
| 61 | out[rel] = append(out[rel], p) |
| 62 | } |
| 63 | return nil |
| 64 | }) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | return out |
| 69 | } |
| 70 | |
| 71 | // TestNoPreMoveRastrillo fails on any import of rastrillo's frozen |
| 72 | // pre-move path, test files, scripts and browser-tagged files included. |
| 73 | func TestNoPreMoveRastrillo(t *testing.T) { |
| 74 | for file, imps := range goImports(t, repoRoot(t)) { |
| 75 | for _, imp := range imps { |
| 76 | if imp == preMoveRastrillo || strings.HasPrefix(imp, preMoveRastrillo+"/") { |
| 77 | t.Errorf("%s imports %q, rastrillo's frozen pre-move path; use %s", file, imp, rastrilloPath) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // TestReleaseStampsTheServedVersion: `make release` stamps BuildVersion |
| 84 | // with -X, and the Go linker ignores an -X naming a package the binary |
| 85 | // does not contain — no error, no warning. Stamp the old path while the |
| 86 | // binary serves the new one and /api/version answers "dev" for every |
| 87 | // release, which is exactly the answer the platform's deploy check |
| 88 | // cannot tell from a binary that was never recycled. The stamped package |
| 89 | // must be the rastrillo cmd/fichas serves through. |
| 90 | func TestReleaseStampsTheServedVersion(t *testing.T) { |
| 91 | root := repoRoot(t) |
| 92 | mk, err := os.ReadFile(filepath.Join(root, "Makefile")) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | m := regexp.MustCompile(`-X '?([^'=\s]+)\.BuildVersion=`).FindSubmatch(mk) |
| 97 | if m == nil { |
| 98 | t.Fatal("the Makefile's release does not stamp BuildVersion with -X") |
| 99 | } |
| 100 | stamped := string(m[1]) |
| 101 | if stamped != rastrilloPath { |
| 102 | t.Errorf("make release stamps %s.BuildVersion; the served version is %s.BuildVersion", stamped, rastrilloPath) |
| 103 | } |
| 104 | served := false |
| 105 | for _, imp := range goImports(t, root)[filepath.Join("cmd", "fichas", "main.go")] { |
| 106 | served = served || imp == stamped |
| 107 | } |
| 108 | if !served { |
| 109 | t.Errorf("cmd/fichas does not import %s, so -X %s.BuildVersion stamps nothing", stamped, stamped) |
| 110 | } |
| 111 | } |
| 112 | |