github.com/quay/claircore@v1.5.28/docs/styleguide_test.go (about) 1 package docs 2 3 import ( 4 "context" 5 "errors" 6 "io/fs" 7 "os" 8 "path/filepath" 9 "regexp" 10 "testing" 11 12 "golang.org/x/sync/errgroup" 13 ) 14 15 func TestStyleguide(t *testing.T) { 16 eg, ctx := errgroup.WithContext(context.Background()) 17 names := make(chan string) 18 findMarkdown := func(p string, dirent fs.DirEntry, err error) error { 19 switch { 20 case !errors.Is(err, nil): 21 return err 22 case dirent.IsDir(): 23 return nil 24 case filepath.Ext(p) != ".md": 25 return nil 26 } 27 select { 28 case <-ctx.Done(): 29 return fs.SkipAll 30 case names <- p: 31 } 32 return nil 33 } 34 runlint := func() error { 35 rules := []struct { 36 Name string 37 Regexp *regexp.Regexp 38 Explanation string 39 }{ 40 { 41 Name: "NoIntercap", 42 Regexp: regexp.MustCompile(`[Cc]lairCore`), 43 Explanation: `should not be inter-capped`, 44 }, 45 { 46 Name: "NoIntercap", 47 Regexp: regexp.MustCompile(`[Ll]ibVuln`), 48 Explanation: `should not be inter-capped`, 49 }, 50 { 51 Name: "NoIntercap", 52 Regexp: regexp.MustCompile(`[Ll]ibIndex`), 53 Explanation: `should not be inter-capped`, 54 }, 55 { 56 Name: "RespectPostgreSQL", 57 Regexp: regexp.MustCompile(`[Pp]ostgres([Qq][Ll])?`), 58 Explanation: `it's spelled "PostgreSQL"`, 59 }, 60 { 61 Name: "RespectClair", 62 Regexp: regexp.MustCompile(`[Cc]lair[Vv]4`), 63 Explanation: `it's spelled "Clair v4"`, 64 }, 65 } 66 for fn := range names { 67 b, err := os.ReadFile(fn) 68 if err != nil { 69 return err 70 } 71 72 for _, rule := range rules { 73 for _, idx := range rule.Regexp.FindAllIndex(b, -1) { 74 t.Errorf("%s:%d: %q\t[%s: %s]", fn, idx[0], string(b[idx[0]:idx[1]]), rule.Name, rule.Explanation) 75 } 76 } 77 78 } 79 return nil 80 } 81 eg.Go(func() error { 82 defer close(names) 83 return fs.WalkDir(os.DirFS("."), ".", findMarkdown) 84 }) 85 eg.Go(runlint) 86 87 if err := eg.Wait(); err != nil { 88 t.Error(err) 89 } 90 }