gunk-dev/the-valley

go-dependencies_test.go

source6a7605f70901main's published tip history
9.1 KiB
1package main
2
3import (
4 "os"
5 "path/filepath"
6 "slices"
7 "strings"
8 "testing"
9)
10
11// The tree every case starts from: two local modules, a importing b, and
12// flow/ importing a third-party module and b.
13var base = map[string]string{
14 "a/go.mod": aMod("../b", ""),
15 "a/a.go": "package a\n\nimport (\n\t\"fmt\"\n\t\"the-valley/b/sub\"\n)\n",
16 "b/go.mod": "module the-valley/b\n\ngo 1.24\n",
17 "b/b.go": "package b\n",
18 "b/sub/sub.go": "package sub\n\nimport \"strings\"\n",
19}
20
21var flow = map[string]string{
22 "flow/go.mod": "module the-valley/flow\n\ngo 1.24\n\nrequire (\n\tgithub.com/x/y v1.0.0\n\tthe-valley/b v0.0.0\n)\n\nreplace the-valley/b => ../b\n",
23 "flow/flow.go": "package flow\n\nimport (\n\t\"github.com/x/y\"\n\t\"the-valley/b\"\n)\n",
24}
25
26// aMod is a's go.mod, with the-valley/b replaced by replacement and more
27// directives after its own.
28func aMod(replacement, directives string) string {
29 return "module the-valley/a\n\ngo 1.24\n\nrequire the-valley/b v0.0.0\n\nreplace the-valley/b => " +
30 replacement + "\n\n" + directives
31}
32
33const thirdParty = "package a\n\nimport \"github.com/x/y\"\n"
34
35// A refusal as the cases name it: the file, and the import or directive.
36type want struct{ file, subject string }
37
38func TestCheck(t *testing.T) {
39 for _, c := range []struct {
40 name string
41 noFlow bool
42 files map[string]string // added to base and flow; "-> x" is a symlink to x
43 want []want
44 }{
45 {name: "standard and local imports, and third-party ones in flow/"},
46 {name: "no flow/", noFlow: true},
47 {
48 name: "a third-party module with its go.sum in flow/",
49 files: map[string]string{"flow/go.sum": "github.com/x/y v1.0.0 h1:AAAA=\ngithub.com/x/y v1.0.0/go.mod h1:BBBB=\n"},
50 },
51 {
52 name: "a path element with a leading dot",
53 files: map[string]string{"a/dot.go": "package a\n\nimport \"the-valley/b/.sub\"\n"},
54 },
55 {
56 name: "a standard package of another target",
57 files: map[string]string{"a/js.go": "//go:build js && wasm\n\npackage a\n\nimport \"syscall/js\"\n"},
58 },
59 {
60 name: "a third-party import",
61 files: map[string]string{"a/c.go": thirdParty},
62 want: []want{{"a/c.go", `"github.com/x/y"`}},
63 },
64 {
65 name: "a third-party import behind a build constraint",
66 files: map[string]string{"a/c.go": "//go:build ignore\n\n" + thirdParty},
67 want: []want{{"a/c.go", `"github.com/x/y"`}},
68 },
69 {
70 name: "a third-party import in a test file",
71 files: map[string]string{"a/c_test.go": thirdParty},
72 want: []want{{"a/c_test.go", `"github.com/x/y"`}},
73 },
74 {
75 name: "an import from flow/",
76 files: map[string]string{"a/c.go": "package a\n\nimport \"the-valley/flow/sub\"\n"},
77 want: []want{{"a/c.go", `"the-valley/flow/sub"`}},
78 },
79 {
80 name: "flow/ declaring another path, and an import of that path",
81 files: map[string]string{
82 "flow/go.mod": "module the-valley/c\n\ngo 1.24\n",
83 "a/c.go": "package a\n\nimport \"the-valley/c\"\n",
84 },
85 want: []want{{"a/c.go", `"the-valley/c"`}, {"flow/go.mod", "module the-valley/c"}},
86 },
87 {
88 name: "a package pattern and a reserved name",
89 files: map[string]string{"a/c.go": "package a\n\nimport (\n\t\"en...g/json\"\n\t\"std\"\n\t\"work\"\n)\n"},
90 want: []want{{"a/c.go", `"en...g/json"`}, {"a/c.go", `"std"`}, {"a/c.go", `"work"`}},
91 },
92 {
93 name: "a malformed Go file",
94 files: map[string]string{"a/bad.go": "package a\n\nimport (\n"},
95 want: []want{{"a/bad.go", ""}},
96 },
97 {
98 name: "a second module declaring the-valley/flow",
99 files: map[string]string{"c/go.mod": "module the-valley/flow\n\ngo 1.24\n"},
100 want: []want{{"c/go.mod", "module the-valley/flow"}},
101 },
102 {
103 name: "a require of flow/",
104 files: map[string]string{"a/go.mod": aMod("../b", "require the-valley/flow v0.0.0\n")},
105 want: []want{{"a/go.mod", "require the-valley/flow"}},
106 },
107 {
108 name: "a tool from an external module",
109 files: map[string]string{"a/go.mod": aMod("../b", "tool github.com/x/y/cmd/z\n")},
110 want: []want{{"a/go.mod", "tool github.com/x/y/cmd/z"}},
111 },
112 {
113 name: "a require of an external module",
114 files: map[string]string{"a/go.mod": aMod("../b", "require github.com/x/y v1.0.0\n")},
115 want: []want{{"a/go.mod", "require github.com/x/y"}},
116 },
117 {
118 name: "a replacement outside the repository",
119 files: map[string]string{
120 "a/go.mod": aMod("../../outside/b", ""),
121 "../outside/b/go.mod": "module the-valley/b\n\ngo 1.24\n",
122 },
123 want: []want{{"a/go.mod", "replace the-valley/b => ../../outside/b"}},
124 },
125 {
126 name: "a replacement by an absolute path",
127 files: map[string]string{"a/go.mod": aMod("/nonexistent/b", "")},
128 want: []want{{"a/go.mod", "replace the-valley/b => /nonexistent/b"}},
129 },
130 {
131 name: "a replacement into flow/",
132 files: map[string]string{"a/go.mod": aMod("../flow", "")},
133 want: []want{{"a/go.mod", "replace the-valley/b => ../flow"}},
134 },
135 {
136 name: "a replacement through a symlink into flow/",
137 files: map[string]string{
138 "a/go.mod": aMod("../f", ""),
139 "f": "-> flow/sub",
140 "flow/sub/sub.go": "package sub\n",
141 },
142 want: []want{{"a/go.mod", "replace the-valley/b => ../f"}},
143 },
144 {
145 name: "an external module replaced with a local directory",
146 files: map[string]string{
147 "a/go.mod": aMod("../b", "require github.com/x/y v1.0.0\n\nreplace github.com/x/y => ../b\n"),
148 "a/c.go": thirdParty,
149 },
150 want: []want{
151 {"a/c.go", `"github.com/x/y"`},
152 {"a/go.mod", "replace github.com/x/y => ../b"},
153 {"a/go.mod", "require github.com/x/y"},
154 },
155 },
156 {
157 name: "a local module replaced with an external one",
158 files: map[string]string{"a/go.mod": aMod("example.org/b v1.0.0", "")},
159 want: []want{{"a/go.mod", "replace the-valley/b => example.org/b v1.0.0"}},
160 },
161 {
162 name: "a local module declaring an external path",
163 files: map[string]string{"b/go.mod": "module example.org/b\n\ngo 1.24\n"},
164 want: []want{
165 {"a/a.go", `"the-valley/b/sub"`},
166 {"a/go.mod", "replace the-valley/b => ../b"},
167 {"a/go.mod", "require the-valley/b"},
168 {"b/go.mod", "module example.org/b"},
169 },
170 },
171 {
172 name: "a nested module with a third-party import",
173 files: map[string]string{
174 "a/inner/go.mod": "module the-valley/a/inner\n\ngo 1.24\n",
175 "a/inner/inner.go": "package inner\n\nimport \"github.com/x/y\"\n",
176 },
177 want: []want{{"a/inner/inner.go", `"github.com/x/y"`}},
178 },
179 {
180 name: "a symlinked file from flow/",
181 files: map[string]string{"a/f.go": "-> ../flow/flow.go"},
182 want: []want{{"a/f.go", `"github.com/x/y"`}},
183 },
184 {
185 name: "a dangling symlinked Go file",
186 files: map[string]string{"a/gone.go": "-> missing.go"},
187 want: []want{{"a/gone.go", "missing.go"}},
188 },
189 {
190 name: "a Go file's name linked to a file of another name",
191 files: map[string]string{"a/notes.go": "-> ../notes.txt", "notes.txt": thirdParty},
192 want: []want{{"a/notes.go", "../notes.txt"}},
193 },
194 {
195 name: "a symlink leaving the tree",
196 files: map[string]string{"a/out": "-> ../../outside", "../outside/out.go": thirdParty},
197 want: []want{{"a/out", "../../outside"}},
198 },
199 {
200 name: "a symlink loop",
201 files: map[string]string{"a/loop": "-> .."},
202 want: []want{{"a/loop", ""}},
203 },
204 {
205 name: "a workspace",
206 files: map[string]string{"go.work": "go 1.24\n\nuse ./a\n"},
207 want: []want{{"go.work", ""}},
208 },
209 {
210 name: "a vendor directory",
211 files: map[string]string{"a/vendor/modules.txt": "# github.com/x/y v1.0.0\n"},
212 want: []want{{"a/vendor", ""}},
213 },
214 {
215 name: "a vendor symlink into flow/",
216 files: map[string]string{"a/vendor": "-> ../flow"},
217 want: []want{{"a/vendor", ""}},
218 },
219 {
220 name: "a go.mod the go command cannot parse",
221 files: map[string]string{"a/go.mod": "module the-valley/a\n\nrequire (\n"},
222 want: []want{{"a/go.mod", ""}},
223 },
224 } {
225 t.Run(c.name, func(t *testing.T) {
226 layers := []map[string]string{base, flow, c.files}
227 if c.noFlow {
228 layers = []map[string]string{base, c.files}
229 }
230 refusals, _, _, err := check(fixture(t, layers...))
231 if err != nil {
232 t.Fatal(err)
233 }
234 var got []want
235 for _, r := range refusals {
236 if r.File == "" || r.Reason == "" {
237 t.Errorf("a refusal names no file or no reason: %+v", r)
238 }
239 got = append(got, want{r.File, r.Subject})
240 }
241 if !slices.Equal(got, c.want) {
242 t.Errorf("refused %v, want %v; refusals:\n%v", got, c.want, refusals)
243 }
244 })
245 }
246}
247
248// fixture writes the files of each layer, later layers replacing earlier
249// ones, under a fresh root, and returns the root. A path may climb out of
250// the root, and a value "-> target" writes a symlink to target.
251func fixture(t *testing.T, layers ...map[string]string) string {
252 t.Helper()
253 root := filepath.Join(t.TempDir(), "repo")
254 files := map[string]string{}
255 for _, l := range layers {
256 for name, content := range l {
257 files[name] = content
258 }
259 }
260 for name, content := range files {
261 p := filepath.Join(root, filepath.FromSlash(name))
262 if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
263 t.Fatal(err)
264 }
265 var err error
266 if target, ok := strings.CutPrefix(content, "-> "); ok {
267 err = os.Symlink(target, p)
268 } else {
269 err = os.WriteFile(p, []byte(content), 0o644)
270 }
271 if err != nil {
272 t.Fatal(err)
273 }
274 }
275 return root
276}