|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "io/ioutil" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/PuerkitoBio/goquery" |
| 12 | + "github.com/gomarkdown/markdown" |
| 13 | + "github.com/gomarkdown/markdown/parser" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + regexContainLink = regexp.MustCompile(`\* \[.*\]\(.*\)`) |
| 18 | + regexWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S.*[\.\!\?]`) |
| 19 | +) |
| 20 | + |
| 21 | +func TestDuplicatedContent(t *testing.T) { |
| 22 | + html := readMarkdown() |
| 23 | + doc, err := goquery.NewDocumentFromReader(html) |
| 24 | + if err != nil { |
| 25 | + panic(err) |
| 26 | + } |
| 27 | + list := make(map[string]bool, 0) |
| 28 | + doc.Find("ul>li a").Each(func(i int, s *goquery.Selection) { |
| 29 | + t.Run(s.Text(), func(t *testing.T) { |
| 30 | + href, exist := s.Attr("href") |
| 31 | + if !exist { |
| 32 | + t.Error("Expected to have a href") |
| 33 | + } |
| 34 | + if list[href] { |
| 35 | + t.Fatalf("Duplicated link %s", href) |
| 36 | + } |
| 37 | + list[href] = true |
| 38 | + }) |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +func TestLinkFormat(t *testing.T) { |
| 43 | + content, err := ioutil.ReadFile("README.md") |
| 44 | + if err != nil { |
| 45 | + panic(err) |
| 46 | + } |
| 47 | + list := strings.Split(string(content), "\n") |
| 48 | + for _, item := range list { |
| 49 | + containLink := regexContainLink.MatchString(item) |
| 50 | + if containLink { |
| 51 | + isCorrect := regexWithDescription.MatchString(item) |
| 52 | + if !isCorrect { |
| 53 | + t.Errorf("Expected the correct usage for new links (`* [repo name](repo url) - repo description`) on %s", item) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func readMarkdown() io.Reader { |
| 60 | + content, err := ioutil.ReadFile("README.md") |
| 61 | + if err != nil { |
| 62 | + panic(err) |
| 63 | + } |
| 64 | + |
| 65 | + extensions := parser.CommonExtensions | parser.AutoHeadingIDs |
| 66 | + parser := parser.NewWithExtensions(extensions) |
| 67 | + |
| 68 | + md := []byte(content) |
| 69 | + html := markdown.ToHTML(md, parser, nil) |
| 70 | + r := bytes.NewReader(html) |
| 71 | + return r |
| 72 | +} |
0 commit comments