github.com/quay/claircore@v1.5.28/docs/listing_test.go (about) 1 package docs 2 3 import ( 4 "bufio" 5 "io/fs" 6 "os" 7 "path" 8 "regexp" 9 "sort" 10 "testing" 11 12 "github.com/google/go-cmp/cmp" 13 ) 14 15 // TestListing fails if the SUMMARY.md falls out of sync with the markdown files 16 // in this directory. 17 func TestListing(t *testing.T) { 18 // Check that this is the docs test. 19 // These files are copied into the "book" directory, so when left around in 20 // a work tree, test will run there as well. 21 if _, err := os.Stat("index.html"); err == nil { 22 t.Skip("skip listing check in compiled docs") 23 } 24 25 linkline, err := regexp.Compile(`\s*- \[.+\]\((.+)\)`) 26 if err != nil { 27 t.Fatal(err) 28 } 29 f, err := os.Open("SUMMARY.md") 30 if err != nil { 31 t.Fatal(err) 32 } 33 defer f.Close() 34 35 linked := []string{"SUMMARY.md"} 36 s := bufio.NewScanner(f) 37 for s.Scan() { 38 ms := linkline.FindSubmatch(s.Bytes()) 39 switch { 40 case ms == nil, len(ms) == 1: 41 continue 42 case len(ms) == 2: 43 linked = append(linked, path.Clean(string(ms[1]))) 44 } 45 } 46 if err := s.Err(); err != nil { 47 t.Error(err) 48 } 49 sort.Strings(linked) 50 51 var files []string 52 err = fs.WalkDir(os.DirFS("."), ".", func(p string, d fs.DirEntry, err error) error { 53 switch { 54 case err != nil: 55 return err 56 case d.IsDir(): 57 return nil 58 case path.Ext(d.Name()) != ".md": 59 return nil 60 } 61 files = append(files, p) 62 return nil 63 }) 64 if err != nil { 65 t.Error(err) 66 } 67 sort.Strings(files) 68 69 if !cmp.Equal(linked, files) { 70 t.Error(cmp.Diff(linked, files)) 71 } 72 }