1
0
Fork 0
mirror of https://github.com/avelino/awesome-go.git synced 2024-11-20 11:16:22 -05:00
go/main_test.go

136 lines
3.4 KiB
Go
Raw Normal View History

package main
import (
2023-02-14 16:55:40 -05: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 19:14:43 -05:00
func requireNoErr(t *testing.T, err error, msg string) {
// FIXME: replace to github.com/stretchr/testify
t.Helper()
if msg == "" {
msg = "unknown error"
2023-02-14 16:55:40 -05:00
}
2023-02-14 19:14:43 -05:00
2023-02-14 16:55:40 -05:00
if err != nil {
2023-02-14 19:14:43 -05:00
t.Fatalf("Received unexpected error [%s]: %+v", msg, err)
2023-02-14 16:55:40 -05:00
}
2023-02-14 19:14:43 -05:00
}
2023-02-14 20:33:11 -05:00
func goqueryFromReadme(t *testing.T) *goquery.Document {
2023-02-14 19:14:43 -05:00
t.Helper()
2023-02-14 20:33:11 -05:00
input, err := os.ReadFile(readmePath)
2023-02-14 19:14:43 -05:00
requireNoErr(t, err, "readme file should be exists")
html, err := markdown.ToHTML(input)
requireNoErr(t, err, "markdown should be rendered to html")
2023-02-14 20:33:11 -05:00
buf := bytes.NewBuffer(html)
2023-02-14 19:14:43 -05:00
doc, err := goquery.NewDocumentFromReader(buf)
requireNoErr(t, err, "html must be valid for goquery")
return doc
2023-02-14 16:55:40 -05:00
}
func TestAlpha(t *testing.T) {
2023-02-14 19:14:43 -05:00
doc := goqueryFromReadme(t)
doc.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 15:28:54 -04:00
func TestDuplicatedLinks(t *testing.T) {
2023-02-14 19:14:43 -05:00
doc := goqueryFromReadme(t)
2015-04-23 15:28:54 -04:00
links := make(map[string]bool, 0)
2023-02-14 19:14:43 -05:00
doc.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 15:28:54 -04: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)
2023-02-14 19:26:37 -05:00
requireNoErr(t, err, "readme should be exists")
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 06:45:07 -05:00
t.Errorf("expected entry to be in form of `* [link] - description.`, got '%s'", line)
}
}
}
}
2023-02-14 18:35:13 -05:00
func TestRenderIndex(t *testing.T) {
2023-02-25 19:40:39 -05:00
requireNoErr(t, mkdirAll(outDir), "output dir should exists")
2023-02-14 18:35:13 -05:00
err := renderIndex(readmePath, outIndexFile)
2023-02-14 19:26:37 -05:00
requireNoErr(t, err, "html should be rendered")
}
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"))
}
}