Skip to content

Commit 0f908fa

Browse files
committed
add test to fetch URL and more validation for generate-webhook-events script
1 parent 8533d78 commit 0f908fa

File tree

2 files changed

+37
-21
lines changed

2 files changed

+37
-21
lines changed

scripts/generate-webhook-events/main.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import (
2222
"golang.org/x/net/html"
2323
)
2424

25-
var theURL = "https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows"
25+
const theURL = "https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows"
26+
2627
var dbg = log.New(io.Discard, "", log.LstdFlags)
2728

2829
// Parse the activity types of each webhook event. The keys of the map are names of the webhook events
@@ -140,16 +141,16 @@ func hasClass(n *html.Node, want string) bool {
140141
return false
141142
}
142143

143-
func walkNodes(n *html.Node, visit func(*html.Node)) {
144+
func walk(n *html.Node, visit func(*html.Node)) {
144145
visit(n)
145146
for c := n.FirstChild; c != nil; c = c.NextSibling {
146-
walkNodes(c, visit)
147+
walk(c, visit)
147148
}
148149
}
149150

150151
func text(n *html.Node) string {
151152
var b strings.Builder
152-
walkNodes(n, func(n *html.Node) {
153+
walk(n, func(n *html.Node) {
153154
if n.Type == html.TextNode {
154155
b.WriteString(n.Data)
155156
}
@@ -166,7 +167,7 @@ func eventNameOfHeading(h *html.Node) string {
166167
return name
167168
}
168169

169-
func elementChildren(n *html.Node, tag string) []*html.Node {
170+
func children(n *html.Node, tag string) []*html.Node {
170171
var nodes []*html.Node
171172
for c := n.FirstChild; c != nil; c = c.NextSibling {
172173
if c.Type == html.ElementNode && c.Data == tag {
@@ -176,7 +177,7 @@ func elementChildren(n *html.Node, tag string) []*html.Node {
176177
return nodes
177178
}
178179

179-
func firstElementChildByTag(n *html.Node, tag string) *html.Node {
180+
func firstChildByTag(n *html.Node, tag string) *html.Node {
180181
for c := n.FirstChild; c != nil; c = c.NextSibling {
181182
if c.Type == html.ElementNode && c.Data == tag {
182183
return c
@@ -192,38 +193,38 @@ func parseTable(hook string, table *html.Node) ([]string, error) {
192193
return nil, fmt.Errorf(`expected table[aria-labelledby] to be %q, got %q`, hook, label)
193194
}
194195

195-
thead := firstElementChildByTag(table, "thead")
196+
thead := firstChildByTag(table, "thead")
196197
if thead == nil {
197198
return nil, errors.New("missing thead element")
198199
}
199-
tr := firstElementChildByTag(thead, "tr")
200+
tr := firstChildByTag(thead, "tr")
200201
if tr == nil {
201202
return nil, errors.New("missing header row in thead")
202203
}
203-
headers := elementChildren(tr, "th")
204+
headers := children(tr, "th")
204205
if len(headers) < 2 {
205206
return nil, fmt.Errorf("expected at least 2 header columns, got %d", len(headers))
206207
}
207208
h0 := text(headers[0])
208209
h1 := text(headers[1])
209210
if h0 != "Webhook event payload" {
210-
return nil, fmt.Errorf(`expected first header to be %q, got %q`, "Webhook event payload", h0)
211+
return nil, fmt.Errorf(`expected first header to be "Webhook event payload", got %q`, h0)
211212
}
212213
if h1 != "Activity types" {
213-
return nil, fmt.Errorf(`expected second header to be %q, got %q`, "Activity types", h1)
214+
return nil, fmt.Errorf(`expected second header to be "Activity types", got %q`, h1)
214215
}
215216
dbg.Println(` Found table header for "Webhook event payload"`)
216217

217-
tbody := firstElementChildByTag(table, "tbody")
218+
tbody := firstChildByTag(table, "tbody")
218219
if tbody == nil {
219220
return nil, errors.New("missing tbody element")
220221
}
221-
row := firstElementChildByTag(tbody, "tr")
222+
row := firstChildByTag(tbody, "tr")
222223
if row == nil {
223224
return nil, errors.New("missing first data row in tbody")
224225
}
225226
dbg.Println(" Found the first table row")
226-
cells := elementChildren(row, "td")
227+
cells := children(row, "td")
227228
if len(cells) < 2 {
228229
return nil, fmt.Errorf("expected at least 2 data columns, got %d", len(cells))
229230
}
@@ -252,7 +253,7 @@ func parseTable(hook string, table *html.Node) ([]string, error) {
252253

253254
func code(n *html.Node) []string {
254255
var texts []string
255-
walkNodes(n, func(n *html.Node) {
256+
walk(n, func(n *html.Node) {
256257
if n.Type == html.ElementNode && n.Data == "code" {
257258
t := text(n)
258259
if t != "" {
@@ -360,26 +361,25 @@ func run(args []string, stdout, dbgout io.Writer, srcURL string) error {
360361
out = stdout
361362
dst = "stdout"
362363
} else {
363-
n := args[len(args)-1]
364-
f, err := os.Create(n)
364+
dst = args[len(args)-1]
365+
f, err := os.Create(dst)
365366
if err != nil {
366367
return err
367368
}
368369
defer f.Close()
369370
out = f
370-
dst = n
371371
}
372372

373-
m, err := parse(src)
373+
p, err := parse(src)
374374
if err != nil {
375375
return err
376376
}
377377

378-
if err := write(m, out); err != nil {
378+
if err := write(p, out); err != nil {
379379
return err
380380
}
381381

382-
dbg.Println("Wrote output to", dst)
382+
dbg.Println("Wrote the output to", dst)
383383
dbg.Println("Done generate-webhook-events script successfully")
384384

385385
return nil

scripts/generate-webhook-events/main_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package main
22

33
import (
44
"errors"
5+
"go/parser"
6+
"go/token"
57
"io"
68
"os"
79
"path/filepath"
@@ -28,6 +30,10 @@ func TestWriteStdoutOK(t *testing.T) {
2830
if diff := cmp.Diff(want, have); diff != "" {
2931
t.Fatal(diff)
3032
}
33+
34+
if _, err := parser.ParseFile(token.NewFileSet(), "", have, parser.AllErrors); err != nil {
35+
t.Fatalf("Input is not valid as Go. Error: %s\nSource: %s", err, have)
36+
}
3137
}
3238

3339
type errWriter struct{}
@@ -94,3 +100,13 @@ func TestParseError(t *testing.T) {
94100
})
95101
}
96102
}
103+
104+
func TestFetchURL(t *testing.T) {
105+
b, err := fetch("https://github.com")
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
if len(b) == 0 {
110+
t.Fatal("Fetched source is empty")
111+
}
112+
}

0 commit comments

Comments
 (0)