forked from mattn/goveralls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoveralls_test.go
205 lines (179 loc) · 5.54 KB
/
goveralls_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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"net/http"
"net/http/httptest"
)
func fakeServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"error":false,"message":"Fake message","URL":"http://fake.url"}`)
}))
}
func fakeServerWithPayloadChannel(payload chan Job) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// this is a standard baked response
fmt.Fprintln(w, `{"error":false,"message":"Fake message","URL":"http://fake.url"}`)
body, err := ioutil.ReadAll(r.Body)
// query params are used for the body payload
vals, err := url.ParseQuery(string(body))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
var job Job
err = json.Unmarshal([]byte(vals["json"][0]), &job)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
payload <- job
w.WriteHeader(http.StatusOK)
}))
}
func TestUsage(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
cmd := exec.Command("goveralls", "-h")
b, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("Expected exit code 1 bot 0")
}
s := strings.Split(string(b), "\n")[0]
if !strings.HasPrefix(s, "Usage: goveralls ") {
t.Fatalf("Expected %v, but %v", "Usage: ", s)
}
}
func TestCustomJobId(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
jobBodyChannel := make(chan Job, 8096)
fs := fakeServerWithPayloadChannel(jobBodyChannel)
cmd := exec.Command("goveralls", "-jobid=123abc", "-package=github.com/mattn/goveralls/tester", "-endpoint")
cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal("Expected exit code 0 got 1", err, string(b))
}
jobBody := <-jobBodyChannel
if jobBody.ServiceJobID != "123abc" {
t.Fatalf("Expected job id of 123abc, but was %s", jobBody.ServiceJobID)
}
}
func TestInvalidArg(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
cmd := exec.Command("goveralls", "pkg")
b, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("Expected exit code 1 got 0")
}
s := strings.Split(string(b), "\n")[0]
if !strings.HasPrefix(s, "Usage: goveralls ") {
t.Fatalf("Expected %v, but %v", "Usage: ", s)
}
}
func TestVerboseArg(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
fs := fakeServer()
t.Run("with verbose", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-v", "-endpoint")
cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal("Expected exit code 0 got 1", err, string(b))
}
if !strings.Contains(string(b), "--- PASS") {
t.Error("Expected to have verbose go test output in stdout", string(b))
}
})
t.Run("without verbose", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-endpoint")
cmd.Args = append(cmd.Args, "-v", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal("Expected exit code 0 got 1", err, string(b))
}
if strings.Contains(string(b), "--- PASS") {
t.Error("Expected to haven't verbose go test output in stdout", string(b))
}
})
}
func TestShowArg(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
fs := fakeServer()
t.Run("with show", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester/...", "-show", "-endpoint")
cmd.Args = append(cmd.Args, "-show", "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal("Expected exit code 0 got 1", err, string(b))
}
expected := `goveralls: github.com/mattn/goveralls/tester
Fake message
http://fake.url
`
if string(b) != expected {
t.Error("Unexpected output for -show:", string(b))
}
})
}
func TestRaceArg(t *testing.T) {
tmp := prepareTest(t)
defer os.RemoveAll(tmp)
fs := fakeServer()
t.Run("it should pass the test", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/mattn/goveralls/tester", "-race")
cmd.Args = append(cmd.Args, "-endpoint", fs.URL)
b, err := cmd.CombinedOutput()
if err != nil {
t.Fatal("Expected exit code 0 got 1", err, string(b))
}
})
}
/* FIXME: currently this doesn't work because the command goveralls will run
* another session for this session.
func TestGoveralls(t *testing.T) {
wd, _ := os.Getwd()
tmp := prepareTest(t)
os.Chdir(tmp)
defer func() {
os.Chdir(wd)
os.RemoveAll(tmp)
}()
runCmd(t, "go", "get", "github.com/mattn/goveralls/testergo-runewidth")
b := runCmd(t, "goveralls", "-package=github.com/mattn/goveralls/tester")
lines := strings.Split(strings.TrimSpace(string(b)), "\n")
s := lines[len(lines)-1]
if s != "Succeeded" {
t.Fatalf("Expected test of tester are succeeded, but failed")
}
}
*/
func prepareTest(t *testing.T) (tmpPath string) {
tmp, err := ioutil.TempDir("", "goveralls")
if err != nil {
t.Fatal("prepareTest:", err)
}
runCmd(t, "go", "build", "-o", filepath.Join(tmp, "bin", "goveralls"), "github.com/mattn/goveralls")
os.Setenv("PATH", filepath.Join(tmp, "bin")+string(filepath.ListSeparator)+os.Getenv("PATH"))
os.MkdirAll(filepath.Join(tmp, "src"), 0755)
return tmp
}
func runCmd(t *testing.T, cmd string, args ...string) []byte {
b, err := exec.Command(cmd, args...).CombinedOutput()
if err != nil {
t.Fatalf("Expected %v, but %v: %v", nil, err, string(b))
}
return b
}