gunk-dev/the-valley

names_test.go

source4cbb5ec9c5dbmain's published tip history
3.9 KiB
1package location
2
3import (
4 "go/ast"
5 "go/parser"
6 "go/token"
7 "io/fs"
8 "path/filepath"
9 "reflect"
10 "strconv"
11 "strings"
12 "testing"
13)
14
15// TestOnlyLocationSpellsStateDirs fails if production Go code outside this
16// package spells a directory or lock file flow keeps state in, so every
17// path to state goes through location. Prose that mentions one, as help
18// text does, is allowed.
19func TestOnlyLocationSpellsStateDirs(t *testing.T) {
20 flowRoot, err := filepath.Abs(filepath.Join("..", ".."))
21 if err != nil {
22 t.Fatal(err)
23 }
24 self, err := filepath.Abs(".")
25 if err != nil {
26 t.Fatal(err)
27 }
28 fset := token.NewFileSet()
29 files := 0
30 err = filepath.WalkDir(flowRoot, func(path string, d fs.DirEntry, walkErr error) error {
31 if walkErr != nil {
32 return walkErr
33 }
34 if d.IsDir() {
35 if path == self || strings.HasPrefix(d.Name(), ".") {
36 return filepath.SkipDir
37 }
38 return nil
39 }
40 if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
41 return nil
42 }
43 f, err := parser.ParseFile(fset, path, nil, 0)
44 if err != nil {
45 return err
46 }
47 files++
48 for _, s := range stateDirSpellings(fset, f) {
49 rel, _ := filepath.Rel(flowRoot, path)
50 t.Errorf("%s:%d: spells %s; find the path through location", rel, s.line, s.what)
51 }
52 return nil
53 })
54 if err != nil {
55 t.Fatal(err)
56 }
57 if files == 0 {
58 t.Fatalf("found no production Go files under %s", flowRoot)
59 }
60}
61
62func TestStateDirSpellings(t *testing.T) {
63 src := `package sample
64import "path/filepath"
65func bad(home, gcd string) {
66 _ = filepath.Join(home, ".klaus", "config.json")
67 _ = filepath.Join(home, ".valley", "flow")
68 _ = filepath.Join(gcd, "klaus-continue.lock")
69 _ = ".klaus/prompt.md"
70 _ = filepath.Join(gcd, "klaus", "runs")
71 _ = filepath.Join(gcd, "valley", "flow", "runs")
72}
73func good(home string) {
74 _ = "the host config, ~/.klaus/config.json, sets it"
75 _ = "shorten .klaus/prompt.md"
76 _ = filepath.Join(home, "bin", "klaus")
77 _ = "klaus"
78}
79`
80 fset := token.NewFileSet()
81 f, err := parser.ParseFile(fset, "sample.go", src, 0)
82 if err != nil {
83 t.Fatal(err)
84 }
85 var lines []int
86 for _, s := range stateDirSpellings(fset, f) {
87 lines = append(lines, s.line)
88 }
89 if want := []int{4, 5, 6, 7, 8, 9}; !reflect.DeepEqual(lines, want) {
90 t.Errorf("found spellings on lines %v, want %v", lines, want)
91 }
92}
93
94type spelling struct {
95 line int
96 what string
97}
98
99// stateDirSpellings finds where a file spells a state directory: a string
100// literal that is one of the root or lock names or starts a relative path
101// with one, or a filepath.Join that uses "klaus" or "valley", "flow" as
102// directories.
103func stateDirSpellings(fset *token.FileSet, f *ast.File) []spelling {
104 names := map[string]bool{legacyHome: true, ".valley": true, "klaus-continue.lock": true, "continuation.lock": true}
105 var found []spelling
106 ast.Inspect(f, func(n ast.Node) bool {
107 line := func() int { return fset.Position(n.Pos()).Line }
108 switch e := n.(type) {
109 case *ast.ImportSpec:
110 return false
111 case *ast.BasicLit:
112 lit, ok := stringLit(e)
113 if !ok {
114 break
115 }
116 first, _, _ := strings.Cut(lit, "/")
117 if names[lit] || first != lit && names[first] {
118 found = append(found, spelling{line(), strconv.Quote(lit)})
119 }
120 case *ast.CallExpr:
121 sel, ok := e.Fun.(*ast.SelectorExpr)
122 if !ok || sel.Sel.Name != "Join" {
123 break
124 }
125 if pkg, ok := sel.X.(*ast.Ident); !ok || pkg.Name != "filepath" {
126 break
127 }
128 for i, arg := range e.Args {
129 lit, _ := stringLit(arg)
130 last := i == len(e.Args)-1
131 next := ""
132 if !last {
133 next, _ = stringLit(e.Args[i+1])
134 }
135 if lit == "klaus" && !last && next != "klaus" || lit == "valley" && next == "flow" {
136 found = append(found, spelling{line(), strconv.Quote(lit) + " as a directory"})
137 return false
138 }
139 }
140 }
141 return true
142 })
143 return found
144}
145
146func stringLit(e ast.Expr) (string, bool) {
147 lit, ok := e.(*ast.BasicLit)
148 if !ok || lit.Kind != token.STRING {
149 return "", false
150 }
151 s, err := strconv.Unquote(lit.Value)
152 return s, err == nil
153}