| 1 | package lint |
| 2 | |
| 3 | import ( |
| 4 | "go/parser" |
| 5 | "go/token" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // preMoveRastrillo is rastrillo's module path before it moved to |
| 14 | // amadan.net/rastrillo/rastrillo. Nothing after v0.23.0 was released |
| 15 | // under it, so code importing it gets no rastrillo fix, and its own |
| 16 | // checks cannot tell: Docs zoomed iOS on every tap of every field for a |
| 17 | // month after rastrillo fixed it (polish audit r3, item 1). |
| 18 | const preMoveRastrillo = "github.com/carlosframework/rastrillo" |
| 19 | |
| 20 | // preMoveAllowed is the one file that may still import it, the one |
| 21 | // package it may import, and why: rastrillo/native's server takes the |
| 22 | // pre-move path's *sessions.Sessions. Delete the entry when |
| 23 | // native/server moves. |
| 24 | var preMoveAllowed = map[string]string{ |
| 25 | "internal/docs/native.go": preMoveRastrillo + "/sessions", |
| 26 | } |
| 27 | |
| 28 | // TestNoPreMoveRastrillo fails on any import of rastrillo's pre-move |
| 29 | // module path outside preMoveAllowed. |
| 30 | func TestNoPreMoveRastrillo(t *testing.T) { |
| 31 | root := repoRoot(t) |
| 32 | fset := token.NewFileSet() |
| 33 | err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | if d.IsDir() { |
| 38 | if path != root && skipDir(d.Name()) { |
| 39 | return filepath.SkipDir |
| 40 | } |
| 41 | return nil |
| 42 | } |
| 43 | if !strings.HasSuffix(path, ".go") { |
| 44 | return nil |
| 45 | } |
| 46 | f, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly) |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | rel, err := filepath.Rel(root, path) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | rel = filepath.ToSlash(rel) |
| 55 | for _, imp := range f.Imports { |
| 56 | p, err := strconv.Unquote(imp.Path.Value) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | if (p == preMoveRastrillo || strings.HasPrefix(p, preMoveRastrillo+"/")) && preMoveAllowed[rel] != p { |
| 61 | t.Errorf("%s: imports %q, rastrillo's frozen pre-move path; use amadan.net/rastrillo/rastrillo", rel, p) |
| 62 | } |
| 63 | } |
| 64 | return nil |
| 65 | }) |
| 66 | if err != nil { |
| 67 | t.Fatalf("lint: walk %s: %v", root, err) |
| 68 | } |
| 69 | } |
| 70 | |