Debian Codegraph @

cmd/gen_test.go

raw
1 package cmd
2
3 import (
4 "go/ast"
5 "go/token"
6 "os"
7 "path/filepath"
8 "reflect"
9 "testing"
10
11 godoc "go/doc"
12
13 "github.com/miniscruff/changie/then"
14 )
15
16 func TestFilePrependerCreatesFrontmatter(t *testing.T) {
17 prepender := filePrepender("my_command.go")
18 then.Contains(t, `title: "my command"`, prepender)
19 then.Contains(t, `description:`, prepender)
20 }
21
22 func TestLinkHandlerLinksToOtherPages(t *testing.T) {
23 link := linkHandler("My_Command.go")
24 then.Equals(t, "my_command.md", link)
25 }
26
27 func TestCanGetFiles(t *testing.T) {
28 // This could be useful but for now it always updates our schema
29 t.SkipNow()
30
31 // move our wd to project root instead of cmd dir
32 wd, _ := os.Getwd()
33 t.Chdir(filepath.Join(wd, ".."))
34
35 cliDocsPath := filepath.Join("docs", "cli")
36 configDocsPath := filepath.Join("docs", "config", "index.md")
37
38 // ignore any bad error trying to remove the file, it may not exist and that is ok
39 _ = os.Remove(configDocsPath)
40
41 // create cli docs directory if it does not already exist
42 err := os.MkdirAll(cliDocsPath, os.ModePerm)
43 then.Nil(t, err)
44
45 cliDocsFiles, err := os.ReadDir(cliDocsPath)
46 then.Nil(t, err)
47
48 // remove all our generated cli docs before running gen
49 for _, fp := range cliDocsFiles {
50 if fp.Name() == "_index.md" || fp.Name() == ".gitignore" {
51 continue
52 }
53
54 err = os.Remove(filepath.Join(cliDocsPath, fp.Name()))
55 then.Nil(t, err)
56 }
57
58 // run the gen command
59 cmd := RootCmd()
60 cmd.SetArgs([]string{"gen"})
61 then.Nil(t, cmd.Execute())
62
63 // test a few files exist
64 then.FileExists(t, cliDocsPath, "changie.md")
65 then.FileExists(t, cliDocsPath, "changie_init.md")
66 then.FileExists(t, cliDocsPath, "changie_batch.md")
67 then.FileExists(t, cliDocsPath, "changie_merge.md")
68 then.FileExists(t, configDocsPath)
69 }
70
71 func TestErrorGenBadWriteTypesWriter(t *testing.T) {
72 w := then.NewErrWriter()
73 parent := TypeProps{
74 Name: "Parent",
75 }
76
77 w.Raised(t, writeType(w, parent))
78 }
79
80 func TestErrorGenBadWriteTypesWriterWithFields(t *testing.T) {
81 w := then.NewErrWriter()
82 parent := TypeProps{
83 Name: "Config",
84 Fields: []FieldProps{{
85 Key: "Anything",
86 }},
87 }
88
89 w.Raised(t, writeType(w, parent))
90 }
91
92 func TestErrorGenWriteFieldBadWriter(t *testing.T) {
93 w := then.NewErrWriter()
94 parent := TypeProps{
95 Name: "Parent",
96 }
97 field := FieldProps{
98 Key: "Key",
99 }
100
101 w.Raised(t, writeField(w, parent, field))
102 }
103
104 func TestPanicBuildFieldBadFieldType(t *testing.T) {
105 corePackages := make(CoreTypes)
106 queue := make([]string, 0)
107 field := &ast.Field{
108 Names: []*ast.Ident{
109 {Name: "MyVar", NamePos: 1},
110 },
111 Doc: &ast.CommentGroup{
112 List: []*ast.Comment{
113 {Text: "This is our var docs"},
114 },
115 },
116 Type: &ast.Ellipsis{}, // this is an unknown type for our system
117 }
118
119 fset := token.NewFileSet()
120 _ = fset.AddFile("file.go", -1, 1000)
121
122 then.Panic(t, func() {
123 _ = buildField(fset, field, corePackages, &queue)
124 })
125 }
126
127 func TestBuildType(t *testing.T) {
128 for _, tc := range []struct {
129 name string
130 docType *godoc.Type
131 output TypeProps
132 expectedQueue []string
133 }{
134 {
135 name: "ExampleAndMethod",
136 docType: &godoc.Type{
137 Name: "MyType",
138 Doc: `Type docs
139 example: yaml
140 here is an example`,
141 Methods: []*godoc.Func{{
142 Name: "MyFuncWithExample",
143 Decl: &ast.FuncDecl{
144 Type: &ast.FuncType{
145 Func: 250,
146 },
147 },
148 Doc: `Some func description
149 example: yaml
150 has example`,
151 }},
152 Decl: &ast.GenDecl{
153 TokPos: 1,
154 Specs: []ast.Spec{},
155 },
156 },
157 output: TypeProps{
158 Name: "MyType",
159 Doc: "Type docs\n",
160 File: "a.go",
161 Line: 1,
162 ExampleLang: "yaml",
163 ExampleContent: "here is an example",
164 Fields: []FieldProps{
165 {
166 Name: "MyFuncWithExample",
167 Key: "myfuncwithexample",
168 File: "c.go",
169 Line: 1,
170 TypeName: "",
171 Doc: "Some func description\n",
172 ExampleLang: "yaml",
173 ExampleContent: "has example",
174 TemplateType: "",
175 IsCustomType: false,
176 Required: false,
177 Slice: false,
178 },
179 },
180 },
181 },
182 {
183 name: "TypeWithMultipleSpecs",
184 docType: &godoc.Type{
185 Name: "MyType",
186 Doc: "Type docs",
187 Methods: []*godoc.Func{},
188 Decl: &ast.GenDecl{
189 TokPos: 1,
190 Specs: []ast.Spec{
191 &ast.TypeSpec{
192 Name: &ast.Ident{
193 Name: "RandomInterface",
194 NamePos: 10,
195 },
196 Type: &ast.InterfaceType{},
197 },
198 &ast.TypeSpec{
199 Name: &ast.Ident{
200 Name: "ParentType",
201 NamePos: 30,
202 },
203 Type: &ast.StructType{
204 Struct: 150,
205 Fields: &ast.FieldList{
206 List: []*ast.Field{
207 {
208 Names: []*ast.Ident{
209 {Name: "OtherVar", NamePos: 250},
210 },
211 Doc: &ast.CommentGroup{
212 List: []*ast.Comment{
213 {Text: "othervar docs"},
214 },
215 },
216 Type: &ast.Ident{
217 Name: "string",
218 },
219 },
220 {
221 Names: []*ast.Ident{
222 {Name: "MyVar", NamePos: 350},
223 },
224 Doc: &ast.CommentGroup{
225 List: []*ast.Comment{
226 {Text: "myvar docs"},
227 },
228 },
229 Type: &ast.Ident{
230 Name: "int",
231 },
232 },
233 },
234 },
235 },
236 },
237 },
238 },
239 },
240 output: TypeProps{
241 Name: "MyType",
242 Doc: "Type docs",
243 File: "a.go",
244 Line: 1,
245 ExampleLang: "",
246 ExampleContent: "",
247 Fields: []FieldProps{
248 {
249 Name: "MyVar",
250 Key: "MyVar",
251 TypeName: "int",
252 Doc: "myvar docs\n",
253 File: "d.go",
254 Line: 1,
255 ExampleLang: "",
256 ExampleContent: "",
257 TemplateType: "",
258 IsCustomType: false,
259 Required: false,
260 Slice: false,
261 },
262 {
263 Name: "OtherVar",
264 Key: "OtherVar",
265 TypeName: "string",
266 File: "c.go",
267 Line: 1,
268 Doc: "othervar docs\n",
269 ExampleLang: "",
270 ExampleContent: "",
271 TemplateType: "",
272 IsCustomType: false,
273 Required: false,
274 Slice: false,
275 },
276 },
277 },
278 expectedQueue: []string{"string", "int"},
279 },
280 } {
281 t.Run(tc.name, func(t *testing.T) {
282 corePackages := make(CoreTypes)
283 // include one package so we can check is custom type
284 corePackages["MyOptionalType"] = nil
285 queue := make([]string, 0)
286 fset := token.NewFileSet()
287 _ = fset.AddFile("a.go", -1, 100)
288 _ = fset.AddFile("b.go", -1, 100)
289 _ = fset.AddFile("c.go", -1, 100)
290 _ = fset.AddFile("d.go", -1, 100)
291
292 props := buildType(fset, tc.docType, corePackages, &queue)
293 // annoying hack to deeply compare output
294 then.True(t, reflect.DeepEqual(tc.output, props))
295 then.SliceEquals(t, tc.expectedQueue, queue)
296 })
297 }
298 }
299
300 func TestBuildMethod(t *testing.T) {
301 for _, tc := range []struct {
302 name string
303 method *godoc.Func
304 output FieldProps
305 }{
306 {
307 name: "Method",
308 method: &godoc.Func{
309 Name: "MyFunc",
310 Decl: &ast.FuncDecl{
311 Type: &ast.FuncType{
312 Func: 1,
313 },
314 },
315 Doc: "Func docs",
316 },
317 output: FieldProps{
318 Name: "MyFunc",
319 Key: "myfunc",
320 File: "file.go",
321 Line: 1,
322 TypeName: "",
323 Doc: "Func docs",
324 ExampleLang: "",
325 ExampleContent: "",
326 TemplateType: "",
327 IsCustomType: false,
328 Required: false,
329 Slice: false,
330 },
331 },
332 {
333 name: "MethodWithExample",
334 method: &godoc.Func{
335 Name: "MyFuncWithExample",
336 Decl: &ast.FuncDecl{
337 Type: &ast.FuncType{
338 Func: 1,
339 },
340 },
341 Doc: `Some func description
342 example: yaml
343 doSomething: '{{.Body}}'
344 `,
345 },
346 output: FieldProps{
347 Name: "MyFuncWithExample",
348 Key: "myfuncwithexample",
349 TypeName: "",
350 File: "file.go",
351 Line: 1,
352 Doc: "Some func description\n",
353 ExampleLang: "yaml",
354 ExampleContent: "doSomething: '{{.Body}}'",
355 TemplateType: "",
356 IsCustomType: false,
357 Required: false,
358 Slice: false,
359 },
360 },
361 } {
362 t.Run(tc.name, func(t *testing.T) {
363 fset := token.NewFileSet()
364 _ = fset.AddFile("file.go", -1, 1000)
365 props := buildMethod(fset, tc.method)
366 then.Equals(t, tc.output, props)
367 })
368 }
369 }
370
371 func TestGenBuildField(t *testing.T) {
372 for _, tc := range []struct {
373 name string
374 field *ast.Field
375 output FieldProps
376 expectedQueue []string
377 }{
378 {
379 name: "Identifier",
380 field: &ast.Field{
381 Names: []*ast.Ident{
382 {Name: "MyVar", NamePos: 1},
383 },
384 Doc: &ast.CommentGroup{
385 List: []*ast.Comment{
386 {Text: "This is our var docs"},
387 },
388 },
389 Type: &ast.Ident{
390 Name: "MyType",
391 },
392 },
393 output: FieldProps{
394 Name: "MyVar",
395 Key: "MyVar",
396 TypeName: "MyType",
397 File: "file.go",
398 Line: 1,
399 Doc: "This is our var docs\n",
400 ExampleLang: "",
401 ExampleContent: "",
402 TemplateType: "",
403 IsCustomType: false,
404 Required: false,
405 Slice: false,
406 },
407 expectedQueue: []string{"MyType"},
408 },
409 {
410 name: "RequiredSliceWithExample",
411 field: &ast.Field{
412 Names: []*ast.Ident{
413 {Name: "MySliceVar", NamePos: 1},
414 },
415 Doc: &ast.CommentGroup{
416 List: []*ast.Comment{
417 {Text: "This is a slice with an example"},
418 {Text: "example: yaml"},
419 {Text: "mySliceVar: [1, 2, 3]"},
420 },
421 },
422 Type: &ast.ArrayType{
423 Elt: &ast.Ident{Name: "MySliceType"},
424 },
425 Tag: &ast.BasicLit{Value: `required:"true" yaml:"mySliceVar"`},
426 },
427 output: FieldProps{
428 Name: "MySliceVar",
429 Key: "mySliceVar",
430 TypeName: "MySliceType",
431 File: "file.go",
432 Line: 1,
433 Doc: "This is a slice with an example\n",
434 ExampleLang: "yaml",
435 ExampleContent: "mySliceVar: [1, 2, 3]",
436 TemplateType: "",
437 IsCustomType: false,
438 Required: true,
439 Slice: true,
440 },
441 expectedQueue: []string{"MySliceType"},
442 },
443 {
444 name: "PointerWithTemplateType",
445 field: &ast.Field{
446 Names: []*ast.Ident{
447 {Name: "Optional", NamePos: 1},
448 },
449 Type: &ast.StarExpr{
450 X: &ast.Ident{Name: "MyOptionalType"},
451 },
452 Tag: &ast.BasicLit{Value: `yaml:"" templateType:"CustomData"`},
453 },
454 output: FieldProps{
455 Name: "Optional",
456 Key: "optional",
457 TypeName: "MyOptionalType",
458 File: "file.go",
459 Line: 1,
460 Doc: "",
461 ExampleLang: "",
462 ExampleContent: "",
463 TemplateType: "CustomData",
464 IsCustomType: true,
465 Required: false,
466 Slice: false,
467 },
468 expectedQueue: []string{"MyOptionalType", "CustomData"},
469 },
470 {
471 name: "SelectorExpressions",
472 field: &ast.Field{
473 Names: []*ast.Ident{
474 {Name: "MyVarName", NamePos: 1},
475 },
476 Type: &ast.SelectorExpr{
477 Sel: &ast.Ident{Name: "MySelectorType"},
478 },
479 },
480 output: FieldProps{
481 Name: "MyVarName",
482 Key: "MyVarName",
483 TypeName: "MySelectorType",
484 File: "file.go",
485 Line: 1,
486 Doc: "",
487 ExampleLang: "",
488 ExampleContent: "",
489 TemplateType: "",
490 IsCustomType: false,
491 Required: false,
492 Slice: false,
493 },
494 expectedQueue: []string{"MySelectorType"},
495 },
496 {
497 name: "MapType",
498 field: &ast.Field{
499 Names: []*ast.Ident{
500 {Name: "MyMap", NamePos: 1},
501 },
502 Type: &ast.MapType{
503 Key: &ast.Ident{Name: "string"},
504 Value: &ast.Ident{Name: "string"},
505 },
506 },
507 output: FieldProps{
508 Name: "MyMap",
509 Key: "MyMap",
510 File: "file.go",
511 Line: 1,
512 TypeName: "",
513 MapKeyTypeName: "string",
514 MapValueTypeName: "string",
515 Doc: "",
516 ExampleLang: "",
517 ExampleContent: "",
518 TemplateType: "",
519 IsCustomType: false,
520 Required: false,
521 Slice: false,
522 },
523 expectedQueue: []string{"string", "string"},
524 },
525 } {
526 t.Run(tc.name, func(t *testing.T) {
527 corePackages := make(CoreTypes)
528 // include one package so we can check is custom type
529 corePackages["MyOptionalType"] = nil
530 queue := make([]string, 0)
531 fset := token.NewFileSet()
532 _ = fset.AddFile("file.go", -1, 1000)
533
534 props := buildField(fset, tc.field, corePackages, &queue)
535 then.Equals(t, tc.output, props)
536 then.SliceEquals(t, tc.expectedQueue, queue)
537 })
538 }
539 }