package memoriatest import ( "go/parser" "go/token" "io/fs" "os" "path/filepath" "strings" "testing" ) // preMoveRastrillo is rastrillo's module path before it moved to // amadan.net/rastrillo/rastrillo (v0.25.0). Nothing after v0.23.0 was // released under it, so code importing it gets no rastrillo fix, and // its own vendored-asset check cannot tell. That is how Memoria kept // zooming iOS on every tap for a month after rastrillo fixed it (polish // audit r3, item 1). const preMoveRastrillo = "github.com/carlosframework/rastrillo" // preMoveAllowed is the one file that may still import it, and why: // rastrillo/native's server takes the old path's *sessions.Sessions. // Delete the entry when native/server moves. var preMoveAllowed = map[string]string{ "internal/memoria/native.go": preMoveRastrillo + "/sessions", } // TestNoPreMoveRastrillo fails on any import of rastrillo's old module // path outside preMoveAllowed, anywhere in the repository's Go. func TestNoPreMoveRastrillo(t *testing.T) { root, err := filepath.Abs(filepath.Join("..", "..")) if err != nil { t.Fatal(err) } fset := token.NewFileSet() seen := 0 err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } if d.IsDir() { switch d.Name() { case ".git", "node_modules", "native", "releases": return filepath.SkipDir } return nil } if !strings.HasSuffix(path, ".go") { return nil } src, err := os.ReadFile(path) if err != nil { return err } f, err := parser.ParseFile(fset, path, src, parser.ImportsOnly) if err != nil { return nil // a file that does not parse is the compiler's problem } seen++ rel, _ := filepath.Rel(root, path) rel = filepath.ToSlash(rel) for _, im := range f.Imports { imp := strings.Trim(im.Path.Value, `"`) if (imp == preMoveRastrillo || strings.HasPrefix(imp, preMoveRastrillo+"/")) && preMoveAllowed[rel] != imp { t.Errorf("%s: imports %q, rastrillo's frozen pre-move path; use amadan.net/rastrillo/rastrillo", rel, imp) } } return nil }) if err != nil { t.Fatal(err) } // The walk found the app, or it would pass by reading nothing. if seen < 50 { t.Fatalf("walked only %d Go files from %s; the walk is not reaching the repository", seen, root) } }