awesome-go/main_test.go

129 lines
3.0 KiB
Go
Raw Normal View History

package main
import (
2023-02-14 21:55:40 +00:00
"bytes"
"github.com/avelino/awesome-go/pkg/markdown"
"os"
"regexp"
"sort"
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
)
var (
reContainsLink = regexp.MustCompile(`\* \[.*\]\(.*\)`)
reOnlyLink = regexp.MustCompile(`\* \[.*\]\([^()]*\)$`)
reLinkWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S.*[\.\!]`)
)
2023-02-14 21:55:40 +00:00
func helpGetReadmeHTML() []byte {
input, err := os.ReadFile(readmePath)
if err != nil {
panic(err)
}
html, err := markdown.ToHTML(input)
if err != nil {
panic(err)
}
return html
}
func helpBuildQuery() *goquery.Document {
buf := bytes.NewBuffer(helpGetReadmeHTML())
query, err := goquery.NewDocumentFromReader(buf)
if err != nil {
panic(err)
}
return query
}
func TestAlpha(t *testing.T) {
2023-02-14 21:55:40 +00:00
query := helpBuildQuery()
query.Find("body > ul").Each(func(i int, s *goquery.Selection) {
if i != 0 {
// skip content menu
// TODO: the sub items (with 3 hash marks `###`) are staying in
// the main list, not respecting the hierarchy and making it
// impossible to test the alphabetical order
testList(t, s)
}
})
}
2015-04-23 19:28:54 +00:00
func TestDuplicatedLinks(t *testing.T) {
2023-02-14 21:55:40 +00:00
query := helpBuildQuery()
2015-04-23 19:28:54 +00:00
links := make(map[string]bool, 0)
query.Find("body li > a:first-child").Each(func(_ int, s *goquery.Selection) {
t.Run(s.Text(), func(t *testing.T) {
href, ok := s.Attr("href")
if !ok {
t.Error("expected to have href")
}
if links[href] {
t.Fatalf("duplicated link '%s'", href)
}
links[href] = true
})
2015-04-23 19:28:54 +00:00
})
}
// Test if an entry has description, it must be separated from link with ` - `
func TestSeparator(t *testing.T) {
var matched, containsLink, noDescription bool
input, err := os.ReadFile(readmePath)
if err != nil {
panic(err)
}
lines := strings.Split(string(input), "\n")
for _, line := range lines {
line = strings.Trim(line, " ")
containsLink = reContainsLink.MatchString(line)
if containsLink {
noDescription = reOnlyLink.MatchString(line)
if noDescription {
continue
}
matched = reLinkWithDescription.MatchString(line)
if !matched {
End description with dot (#2283) * End description with dot Make sure a description always ends with a dot. * update (#1) * Add me to maintainers list (#2238) * Http clients (#2237) * Remove gorequest This does not really meet the required quality https://goreportcard.com/report/github.com/parnurzeal/gorequest * Add HTTP-Clients category and sling * Add grequests * I suck at pressing ctrl+f * Add Hoverfly API simulation tool (#2214) * Update README.md (#2236) * Add did (#2235) * Add errorx (#2231) * Add errorx * fix errorx description * Remove Florest, no longer exist (#2239) * Add go-zero-width (#2242) * Deprecate Go-AWS-Auth (#2243) * Add huego. Updated README (#2241) * Add Octillery (#2244) * Remove github.com/GeertJohan/yubigo (#2247) * Remove github.com/rcrowley/go-metric (#2246) * Add a link to fastcache (#2249) * Added iso601 (#2250) * Remove mgutz/dat (#2251) * No PR reviews * last Commits on Mar 28, 2017 * Add mole (#2210) * Deprecate github.com/FiloSottile/gvt (#2256) * Deprecate github.com/sevki/graphql (#2257) * Deprecate github.com/gonum/matrix (#2258) * Deprecate github.com/fatih/structs (#2259) * Deprecate github.com/toby3d/go-telegraph (#2260) * Deprecate github.com/mitchellh/goamz (#2261) * Deprecate github.com/franela/goreq (#2262) * Deprecate github.com/facebookgo/httpcontrol (#2263) * Deprecate github.com/markbates/validate (#2264) * Deprecate github.com/codehack/go-relax (#2265) * Deprecate github.com/volatile/core (#2266) * Deprecate github.com/imdario/medeina (#2267) * Deprecate github.com/daryl/zeus (#2268) * Deprecate github.com/mvdan/interfacer (#2269) * Deprecate github.com/GoClipse/goclipse (#2270) * Add gonum (#2273) * Add gonum * add period * Update validate (#2271) * Update validate package * accidentally removed a word, add it again * Update go-telegraph (#2272) * Remove https://github.com/bmizerany/pat (#2252) * Commits on Aug 15, 2017 * PR No reviews * fix project link (#2278) * Add llir/llvm (#2279) * Koazee (#2248) Please check if what you want to add to `awesome-go` list meets [quality standards](https://github.com/avelino/awesome-go/blob/master/CONTRIBUTING.md#quality-standard) before sending pull request. Thanks! **Please provide package links to:** - github.com repo: https://github.com/wesovilabs/koazee - godoc.org: https://godoc.org/github.com/wesovilabs/koazee - goreportcard.com: https://goreportcard.com/report/github.com/wesovilabs/koazee - coverage service link ([cover.run](https://cover.run/), [gocover](http://gocover.io/), [coveralls](https://coveralls.io/) etc.), example: `[![cover.run](https://cover.run/go/github.com/user/repository.svg?style=flat&tag=golang-1.10)](https://cover.run/go?tag=golang-1.10&repo=github.com%2Fuser%2Frepository)` [![codecov](https://codecov.io/gh/wesovilabs/koazee/branch/master/graph/badge.svg)](https://codecov.io/gh/wesovilabs/koazee) Very good coverage **Note**: that new categories can be added only when there are 3 packages or more. **Make sure that you've checked the boxes below before you submit PR:** - [x] I have added my package in alphabetical order. - [x] I have an appropriate description with correct grammar. - [x] I know that this package was not listed before. - [x] I have added godoc link to the repo and to my pull request. - [x] I have added coverage service link to the repo and to my pull request. - [x] I have added goreportcard link to the repo and to my pull request. - [x] I have read [Contribution guidelines](https://github.com/avelino/awesome-go/blob/master/CONTRIBUTING.md#contribution-guidelines), [maintainers note](https://github.com/avelino/awesome-go/blob/master/CONTRIBUTING.md#maintainers) and [Quality standard](https://github.com/avelino/awesome-go/blob/master/CONTRIBUTING.md#quality-standard). Thanks for your PR, you're awesome! :+1: * Allow exclamation mark * updated error message * add various dots * follow-up (#2) * Revert "update (#1)" (#3) This reverts commit 3e410e38c4e2e1c828aec65346a55379e79e0a0f. * Missed some dots
2018-12-14 11:45:07 +00:00
t.Errorf("expected entry to be in form of `* [link] - description.`, got '%s'", line)
}
}
}
}
2023-02-14 23:35:13 +00:00
func TestRenderIndex(t *testing.T) {
err := renderIndex(readmePath, outIndexFile)
if err != nil {
t.Errorf("html generate error '%s'", err.Error())
}
}
func testList(t *testing.T, list *goquery.Selection) {
list.Find("ul").Each(func(_ int, items *goquery.Selection) {
testList(t, items)
items.RemoveFiltered("ul")
})
t.Run(list.Prev().Text(), func(t *testing.T) {
checkAlphabeticOrder(t, list)
})
}
func checkAlphabeticOrder(t *testing.T, s *goquery.Selection) {
items := s.Find("li > a:first-child").Map(func(_ int, li *goquery.Selection) string {
return strings.ToLower(li.Text())
})
sorted := make([]string, len(items))
copy(sorted, items)
sort.Strings(sorted)
for k, item := range items {
if item != sorted[k] {
t.Errorf("expected '%s' but actual is '%s'", sorted[k], item)
}
}
if t.Failed() {
t.Logf("expected order is:\n%s", strings.Join(sorted, "\n"))
}
}