forked from made-in-bangladesh/made-in-bangladesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_test.go
206 lines (174 loc) · 4.77 KB
/
repo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package made_in_bangladesh
import (
"bytes"
"io/ioutil"
"regexp"
"sort"
"strings"
"testing"
"github.com/PuerkitoBio/goquery"
"github.com/octokit/go-octokit/octokit"
"github.com/russross/blackfriday"
"os"
)
// Following test file acknowledged thankfully `avelino/awesome-go` for the tests
var query = startQuery()
func TestDuplicate(t *testing.T) {
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
})
})
sections := make(map[string]struct{}, 0)
query.Find("body > ul > li").Each(func(_ int, s *goquery.Selection) {
section := strings.Fields(strings.TrimSpace(s.Text()))
if len(section) == 0 {
t.Fatal("no section header found")
}
if _, found := sections[section[0]]; found {
t.Fatalf("duplicated section '%s'", section[0])
}
sections[section[0]] = struct{}{}
})
}
func TestSorted(t *testing.T) {
var sections []string
query.Find("body > ul > li").Each(func(_ int, s *goquery.Selection) {
section := strings.Fields(strings.TrimSpace(s.Text()))
if len(section) == 0 {
t.Fatal("no section header found")
}
sections = append(sections, section[0])
})
checkSorted(t, sections)
query.Find("body > ul").Each(func(_ int, s *goquery.Selection) {
testList(t, s)
})
}
// 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 := ioutil.ReadFile("./README.md")
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 {
t.Errorf("expected entry to be in form of `* [link] - description`, got '%s'", line)
}
}
}
}
const (
requiredStarCount = 10
)
func TestStarCount(t *testing.T) {
var cl = octokit.NewClient(nil)
if val := os.Getenv("X_GITHUB_AUTH_TOKEN"); len(val) > 0 {
cl = octokit.NewClient(&octokit.TokenAuth{
AccessToken: val,
})
}
var matched, containsLink, noDescription bool
input, err := ioutil.ReadFile("./README.md")
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 {
link := strings.TrimSpace(line[strings.Index(line, "(")+1 : strings.Index(line, ")")])
if strings.HasPrefix(link, "https://github.com") {
// Only support github for now.
parts := strings.Split(link[19:], "/")
repo, res := cl.Repositories().One(&octokit.RepositoryURL, octokit.M{
"owner": parts[0],
"repo": parts[1],
})
if res.Err != nil {
panic(res.Err)
}
if repo.StargazersCount < requiredStarCount {
t.Fatal("repository didn't meet expected star count")
}
}
}
}
}
}
func testList(t *testing.T, list *goquery.Selection) {
list.Find("ul").Each(func(_ int, items *goquery.Selection) {
testList(t, items)
items.RemoveFiltered("ul")
})
category := list.Prev().Text()
t.Run(category, 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())
})
checkSorted(t, items)
}
func checkSorted(t *testing.T, items []string) {
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"))
}
}
var (
reContainsLink = regexp.MustCompile(`\* \[.*\]\(.*\)`)
reOnlyLink = regexp.MustCompile(`\* \[.*\]\(.*\)$`)
reLinkWithDescription = regexp.MustCompile(`\* \[.*\]\(.*\) - \S`)
)
func readme() []byte {
input, err := ioutil.ReadFile("./README.md")
if err != nil {
panic(err)
}
html := append([]byte("<body>"), blackfriday.MarkdownCommon(input)...)
html = append(html, []byte("</body>")...)
return html
}
func startQuery() *goquery.Document {
buf := bytes.NewBuffer(readme())
query, err := goquery.NewDocumentFromReader(buf)
if err != nil {
panic(err)
}
return query
}