-
Notifications
You must be signed in to change notification settings - Fork 5
/
godoc_test.go
145 lines (134 loc) · 2.73 KB
/
godoc_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
package crawler
import (
"bufio"
"net/http"
"net/url"
"os/exec"
"reflect"
"strings"
"sync"
"testing"
"time"
"gopkg.in/inconshreveable/log15.v2"
)
type godocController struct {
NopController
t *testing.T
pkg map[string]int
sync.Mutex
}
func (c *godocController) Handle(r *Response, ch chan<- *url.URL) {
if strings.HasPrefix(r.URL.Path, "/pkg/") {
depth := r.Context().Depth()
pkg := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/pkg/"), "/")
if pkg != "" {
if depth != 2 && !strings.Contains(pkg, "internal") {
c.t.Errorf("depth of %q: expect: 2, got %d", r.URL, depth)
return
}
c.Lock()
c.pkg[pkg]++
c.Unlock()
} else { // http://localhost:6060/pkg/
if depth != 1 {
c.t.Errorf("depth of %q: expect: 1, got %d", r.URL, depth)
return
}
}
}
err := ExtractHref(r.NewURL, r.Body, ch)
if err != nil {
c.t.Error(err)
}
}
func (c *godocController) Accept(_ *Response, u *url.URL) bool {
return u.Host == "localhost:34567" && strings.HasPrefix(u.Path, "/pkg/")
}
func TestGodoc(t *testing.T) {
godoc := exec.Command("godoc", "-http", ":34567")
godoc.Env = []string{}
if err := godoc.Start(); err != nil {
t.Fatal(err)
}
defer godoc.Process.Kill()
cmd := exec.Command("go", "list", "std")
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
defer stdout.Close()
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
pkg := map[string]int{}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
pkg[scanner.Text()]++
}
if err := scanner.Err(); err != nil {
t.Fatal(err)
}
var cnt int
for {
if _, err := http.Get("http://localhost:34567"); err == nil {
break
}
time.Sleep(time.Second)
if cnt++; cnt > 5 {
t.Fatal("godoc timeout")
}
}
g := &godocController{
t: t,
pkg: make(map[string]int),
}
cw := New(&Config{
Controller: g,
})
cw.Logger().SetHandler(log15.StdoutHandler)
if err := cw.Crawl("http://localhost:34567"); err != nil {
t.Fatal(err)
}
cw.Wait()
// remove internal and vendor packages
f := func(m map[string]int) {
for k := range m {
if strings.Contains(k, "internal") ||
strings.Contains(k, "vendor") {
delete(m, k)
}
}
}
f(pkg)
f(g.pkg)
for _, s := range []string{
"container",
"index",
"runtime/msan",
"text",
"database",
"compress",
"archive",
"go",
"builtin",
"debug",
} {
// if _, ok := g.pkg[s]; !ok {
// t.Errorf("expect %q in crawled packages", s)
// }
delete(g.pkg, s)
}
if !reflect.DeepEqual(pkg, g.pkg) {
t.Errorf("packages: expect\n%v,\ngot\n%v\n", pkg, g.pkg)
for k := range pkg {
if _, ok := g.pkg[k]; !ok {
t.Log("+go list std:", k)
}
}
for k := range g.pkg {
if _, ok := pkg[k]; !ok {
t.Log("+crawled packages:", k)
}
}
}
}