-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
170 lines (152 loc) · 3.98 KB
/
client_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
package fixhub
import (
"crypto/sha1"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"github.com/google/go-github/github"
)
func TestBasic(t *testing.T) {
c, cleanup := newFakeClient(t)
defer cleanup()
ps, err := c.Check("master")
if err != nil {
t.Fatalf("Check: %v", err)
}
if len(ps) < 4 {
t.Fatalf("Didn't find enough problems")
}
if got, want := ps[0].Text, "This file needs formatting with gofmt."; got != want {
t.Errorf("ps[0].Text = %q, want %q", ps[0].Text, want)
}
if got, want := ps[1].File, "p1.go"; got != want {
t.Errorf("Problem found in %q, want %q", got, want)
}
if got, want := ps[1].Line, 1; got != want {
t.Errorf("Problem found at line %d, want %d", got, want)
}
if !strings.Contains(ps[3].Text, "expected '}'") {
t.Errorf("ps[3].Text=%q, want it to mention missing closing brace", ps[3].Text)
}
if got, want := ps[3].Line, 3; got != want {
t.Errorf("Problem found at line %d, want %d", got, want)
}
// p3.go test: govet
if !strings.Contains(ps[4].Text, "printf verb") {
t.Errorf("ps[4].Text=%q, want it to mention %q", ps[4].Text, "printf verb")
}
if got, want := ps[4].Line, 6; got != want {
t.Errorf("Problem found at line %d, want %d", got, want)
}
}
func newFakeClient(t *testing.T) (client *Client, cleanup func()) {
const owner, proj = "faker", "proj"
f, err := newFakeGitHub(filepath.Join("testdata", owner, proj))
if err != nil {
t.Fatalf("newFakeGitHub: %v", err)
}
c, err := NewClient(owner, proj, "")
if err != nil {
t.Fatalf("NewClient: %v", err)
}
srv := httptest.NewServer(f)
c.gc.BaseURL, err = url.Parse(srv.URL + "/gh/")
if err != nil {
srv.Close()
t.Fatalf("Bad httptest address %q: %v", srv.URL, err)
}
return c, srv.Close
}
type fakeGitHub struct {
baseDir string
master string // SHA-1
files map[string]string // path -> SHA-1
blobs map[string][]byte // SHA-1 -> content
}
func newFakeGitHub(baseDir string) (*fakeGitHub, error) {
f := &fakeGitHub{
baseDir: baseDir,
master: "deadbeef",
files: make(map[string]string),
blobs: make(map[string][]byte),
}
err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.Mode().IsRegular() {
return nil
}
rel, _ := filepath.Rel(baseDir, path) // can't fail
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
sha1 := fmt.Sprintf("%02x", sha1.Sum(data))
f.files[rel] = sha1
f.blobs[sha1] = data
return nil
})
return f, err
}
func (f *fakeGitHub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "GET only", http.StatusMethodNotAllowed)
return
}
path := strings.TrimPrefix(r.URL.Path, "/gh/repos/faker/proj")
if path == r.URL.Path {
// didn't have prefix
http.Error(w, "bad path", http.StatusForbidden)
return
}
switch path {
case "/commits/master":
writeJSON(w, &github.RepositoryCommit{SHA: &f.master})
return
case "/git/trees/" + f.master: // assume "?recursive=1":
t := &github.Tree{SHA: &f.master}
for path, sha1 := range f.files {
t.Entries = append(t.Entries, github.TreeEntry{
SHA: github.String(sha1),
Path: github.String(path),
Size: github.Int(len(f.blobs[sha1])),
})
}
writeJSON(w, t)
return
}
if sha1 := strings.TrimPrefix(path, "/git/blobs/"); sha1 != path {
data := f.blobs[sha1]
if data == nil {
http.Error(w, "no such blob "+sha1, 404)
return
}
writeJSON(w, &github.Blob{
Content: github.String(base64.StdEncoding.EncodeToString(data)),
Encoding: github.String("base64"),
Size: github.Int(len(data)),
})
return
}
log.Printf("r: %v", r)
w.WriteHeader(http.StatusTeapot)
}
func writeJSON(w http.ResponseWriter, obj interface{}) {
b, err := json.Marshal(obj)
if err != nil {
http.Error(w, fmt.Sprintf("internal JSON error: %v", err), 500)
return
}
w.Header().Set("Content-Type", "application/vnd.github.v3+json")
w.Write(b)
}