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.*[\.\!]`)
)
func helpGetReadmeHTML() []byte {
input, err := os.ReadFile(readmePath)
if err != nil {
panic(err)
func requireNoErr(t *testing.T, err error, msg string) {
// FIXME: replace to github.com/stretchr/testify
t.Helper()
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)
if err != nil {
panic(err)
}
requireNoErr(t, err, "markdown should be rendered to html")
return html
}
func helpBuildQuery() *goquery.Document {
buf := bytes.NewBuffer(helpGetReadmeHTML())
query, err := goquery.NewDocumentFromReader(buf)
if err != nil {
panic(err)
}
return query
func goqueryFromReadme(t *testing.T) *goquery.Document {
t.Helper()
buf := bytes.NewBuffer(getReadmeHTML(t))
doc, err := goquery.NewDocumentFromReader(buf)
requireNoErr(t, err, "html must be valid for goquery")
return doc
}
func TestAlpha(t *testing.T) {
query := helpBuildQuery()
query.Find("body > ul").Each(func(i int, s *goquery.Selection) {
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
@ -53,9 +67,9 @@ func TestAlpha(t *testing.T) {
}
func TestDuplicatedLinks(t *testing.T) {
query := helpBuildQuery()
doc := goqueryFromReadme(t)
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) {
href, ok := s.Attr("href")
if !ok {

View File

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