forked from tiborvass/gh-patch-parser
/
format_test.go
127 lines (105 loc) · 3.05 KB
/
format_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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/crosbymichael/octokat"
)
var (
mux *http.ServeMux
client *octokat.Client
server *httptest.Server
)
func setup() {
mux = http.NewServeMux()
server = httptest.NewServer(mux)
client = octokat.NewClient()
client.BaseURL = server.URL
}
func tearDown() {
server.Close()
}
func TestValidFormat(t *testing.T) {
cases := map[string]bool{
"valid_format.go": true,
"invalid_format.go": false,
}
for filePath, valid := range cases {
files := stubPullRequestFiles(filePath)
goFmtd, _, err := validFormat("fixtures/format_fixtures.git", files)
if err != nil {
t.Fatal(err)
}
if goFmtd != valid {
t.Fatalf("Expected %v, but was %v for %s\n", valid, goFmtd, filePath)
}
}
}
func TestPublishInvalidFormat(t *testing.T) {
setup()
defer tearDown()
mux.HandleFunc("/repos/docker/docker/issues/1/comments", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
fmt.Fprint(w, "[]")
case "POST":
body, _ := ioutil.ReadAll(r.Body)
if !strings.Contains(string(body), "gofmt -s -w") {
t.Fatalf("Expected add the gofmt comment. The body was:\n\t%s\n", r.Method)
}
fmt.Fprint(w, `{"id": 1}`)
default:
t.Fatalf("Expected request with POST or GET but was %s\n", r.Method)
}
})
stubStatusRequest(t, "e2e8ed82baa31d5d1624f3b79dc53af8a04cc563", "failure")
files := stubPullRequestFiles("invalid_format.go")
err := validateFormat(client, getRepoWithOwner("docker", "docker"), "e2e8ed82baa31d5d1624f3b79dc53af8a04cc563", "fixtures/format_fixtures.git", "1", files)
if err != nil {
t.Fatal(err)
}
}
func TestPublishValidFormat(t *testing.T) {
setup()
defer tearDown()
mux.HandleFunc("/repos/docker/docker/issues/1/comments", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
fmt.Fprint(w, "[]")
case "DELETE":
w.WriteHeader(204)
default:
t.Fatalf("Expected request with DELETE or GET but was %s\n", r.Method)
}
})
stubStatusRequest(t, "e2e8ed82baa31d5d1624f3b79dc53af8a04cc563", "success")
files := stubPullRequestFiles("valid_format.go")
err := validateFormat(client, getRepoWithOwner("docker", "docker"), "e2e8ed82baa31d5d1624f3b79dc53af8a04cc563", "fixtures/format_fixtures.git", "1", files)
if err != nil {
t.Fatal(err)
}
}
func stubStatusRequest(t *testing.T, sha, placeholder string) {
urlPath := fmt.Sprintf("/repos/docker/docker/statuses/%s", "e2e8ed82baa31d5d1624f3b79dc53af8a04cc563")
mux.HandleFunc(urlPath, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Fatalf("Expected request with POST but was %s\n", r.Method)
}
body, _ := ioutil.ReadAll(r.Body)
if !strings.Contains(string(body), placeholder) {
t.Fatalf("Expected request to be a %s. The body was:\n\t%s\n", placeholder, body)
}
fmt.Fprint(w, `{"id": 1}`)
})
}
func stubPullRequestFiles(name string) []*octokat.PullRequestFile {
return []*octokat.PullRequestFile{
{
FileName: name,
Sha: "e2e8ed82baa31d5d1624f3b79dc53af8a04cc563",
},
}
}