mark help functions as helpers

This commit is contained in:
Kirill Zhuravlev 2023-02-15 01:14:43 +01:00 committed by Avelino
parent 045416c17d
commit f2fb99dcca
No known key found for this signature in database
GPG Key ID: B345B4D52E98180A
2 changed files with 36 additions and 25 deletions

View File

@ -18,30 +18,44 @@ var (
reLinkWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S.*[\.\!]`) reLinkWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S.*[\.\!]`)
) )
func helpGetReadmeHTML() []byte { func requireNoErr(t *testing.T, err error, msg string) {
input, err := os.ReadFile(readmePath) // FIXME: replace to github.com/stretchr/testify
if err != nil { t.Helper()
panic(err)
if msg == "" {
msg = "unknown error"
} }
if err != nil {
t.Fatalf("Received unexpected error [%s]: %+v", msg, err)
}
}
func getReadmeHTML(t *testing.T) []byte {
t.Helper()
input, err := os.ReadFile(readmePath + "asdasd")
requireNoErr(t, err, "readme file should be exists")
html, err := markdown.ToHTML(input) html, err := markdown.ToHTML(input)
if err != nil { requireNoErr(t, err, "markdown should be rendered to html")
panic(err)
}
return html return html
} }
func helpBuildQuery() *goquery.Document { func goqueryFromReadme(t *testing.T) *goquery.Document {
buf := bytes.NewBuffer(helpGetReadmeHTML()) t.Helper()
query, err := goquery.NewDocumentFromReader(buf)
if err != nil { buf := bytes.NewBuffer(getReadmeHTML(t))
panic(err) doc, err := goquery.NewDocumentFromReader(buf)
} requireNoErr(t, err, "html must be valid for goquery")
return query
return doc
} }
func TestAlpha(t *testing.T) { func TestAlpha(t *testing.T) {
query := helpBuildQuery() doc := goqueryFromReadme(t)
query.Find("body > ul").Each(func(i int, s *goquery.Selection) { doc.Find("body > ul").Each(func(i int, s *goquery.Selection) {
if i != 0 { if i != 0 {
// skip content menu // skip content menu
// TODO: the sub items (with 3 hash marks `###`) are staying in // TODO: the sub items (with 3 hash marks `###`) are staying in
@ -53,9 +67,9 @@ func TestAlpha(t *testing.T) {
} }
func TestDuplicatedLinks(t *testing.T) { func TestDuplicatedLinks(t *testing.T) {
query := helpBuildQuery() doc := goqueryFromReadme(t)
links := make(map[string]bool, 0) links := make(map[string]bool, 0)
query.Find("body li > a:first-child").Each(func(_ int, s *goquery.Selection) { doc.Find("body li > a:first-child").Each(func(_ int, s *goquery.Selection) {
t.Run(s.Text(), func(t *testing.T) { t.Run(s.Text(), func(t *testing.T) {
href, ok := s.Attr("href") href, ok := s.Attr("href")
if !ok { if !ok {

View File

@ -214,8 +214,9 @@ func testCommitAge(toRun bool, href string, client *http.Client, staleRepos *[]s
} }
return false return false
} }
func testStaleRepository() {
query := helpBuildQuery() func TestStaleRepository(t *testing.T) {
doc := goqueryFromReadme(t)
var staleRepos []string var staleRepos []string
addressedRepositories := make(map[string]bool) addressedRepositories := make(map[string]bool)
oauth := os.Getenv("OAUTH_TOKEN") oauth := os.Getenv("OAUTH_TOKEN")
@ -234,7 +235,7 @@ func testStaleRepository() {
log.Println("Failed to get existing issues. Exiting...") log.Println("Failed to get existing issues. Exiting...")
return return
} }
query.Find("body li > a:first-child").EachWithBreak(func(_ int, s *goquery.Selection) bool { doc.Find("body li > a:first-child").EachWithBreak(func(_ int, s *goquery.Selection) bool {
href, ok := s.Attr("href") href, ok := s.Attr("href")
if !ok { if !ok {
log.Println("expected to have href") log.Println("expected to have href")
@ -263,7 +264,3 @@ func testStaleRepository() {
}) })
createIssue(staleRepos, client) createIssue(staleRepos, client)
} }
func TestStaleRepository(t *testing.T) {
testStaleRepository()
}