-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathgoveralls_test.go
231 lines (184 loc) · 5.57 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"net/http"
"net/http/httptest"
)
var goverallsTestBin string
func TestMain(m *testing.M) {
tmpBin, err := ioutil.TempDir("", "goveralls_")
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
// generate the test binary used by all tests
goverallsTestBin = filepath.Join(tmpBin, "bin", "goveralls")
if runtime.GOOS == "windows" {
goverallsTestBin += ".exe"
}
_, err = exec.Command("go", "build", "-o", goverallsTestBin, ".").CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
// run all tests
exitVal := m.Run()
os.RemoveAll(tmpBin)
os.Exit(exitVal)
}
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) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// 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)
// this is a standard baked response
fmt.Fprintln(w, `{"error":false,"message":"Fake message","URL":"http://fake.url"}`)
}))
}
func TestCustomJobId(t *testing.T) {
t.Parallel()
jobBodyChannel := make(chan Job, 16)
fs := fakeServerWithPayloadChannel(jobBodyChannel)
b, err := testRun("-jobid=123abc", "-package=github.com/mattn/goveralls/tester", "-endpoint", "-v", "-endpoint", fs.URL)
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) {
t.Parallel()
b, err := testRun("pkg")
if err == nil {
t.Fatal("Expected exit code 1 got 0")
}
s := strings.Split(string(b), "\n")[0]
expectedPrefix := "Usage: goveralls"
if !strings.HasPrefix(s, expectedPrefix) {
t.Fatalf("Expected %q, but got %q", expectedPrefix, s)
}
}
func TestVerboseArg(t *testing.T) {
t.Parallel()
fs := fakeServer()
t.Run("with verbose", func(t *testing.T) {
t.Parallel()
b, err := testRun("-package=github.com/mattn/goveralls/tester", "-v", "-endpoint", "-v", "-endpoint", fs.URL)
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) {
t.Parallel()
b, err := testRun("-package=github.com/mattn/goveralls/tester", "-endpoint", "-v", "-endpoint", fs.URL)
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) {
t.Parallel()
fs := fakeServer()
t.Run("with show", func(t *testing.T) {
t.Parallel()
b, err := testRun("-package=github.com/mattn/goveralls/tester/...", "-show", "-endpoint", "-show", "-endpoint", fs.URL)
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) {
t.Parallel()
fs := fakeServer()
t.Run("it should pass the test", func(t *testing.T) {
t.Parallel()
b, err := testRun("-package=github.com/mattn/goveralls/tester", "-race", "-endpoint", fs.URL)
if err != nil {
t.Fatal("Expected exit code 0 got 1", err, string(b))
}
})
}
func TestUploadSource(t *testing.T) {
t.Parallel()
t.Run("with uploadsource", func(t *testing.T) {
t.Parallel()
jobBodyChannel := make(chan Job, 16)
fs := fakeServerWithPayloadChannel(jobBodyChannel)
b, err := testRun("-uploadsource=true", "-package=github.com/mattn/goveralls/tester", "-endpoint", "-v", "-endpoint", fs.URL)
if err != nil {
t.Fatal("Expected exit code 0 got 1", err, string(b))
}
jobBody := <-jobBodyChannel
for _, sf := range jobBody.SourceFiles {
if len(sf.Source) == 0 {
t.Fatalf("expected source for %q to not be empty", sf.Name)
}
}
})
t.Run("without uploadsource", func(t *testing.T) {
t.Parallel()
jobBodyChannel := make(chan Job, 16)
fs := fakeServerWithPayloadChannel(jobBodyChannel)
b, err := testRun("-uploadsource=false", "-package=github.com/mattn/goveralls/tester", "-endpoint", "-v", "-endpoint", fs.URL)
if err != nil {
t.Fatal("Expected exit code 0 got 1", err, string(b))
}
jobBody := <-jobBodyChannel
for _, sf := range jobBody.SourceFiles {
if len(sf.Source) != 0 {
t.Fatalf("expected source for %q to be empty", sf.Name)
}
}
})
}
func testRun(args ...string) ([]byte, error) {
// always disallow the git fetch automatically used for GitHub Actions
args = append([]string{"-allowgitfetch=false"}, args...)
return exec.Command(goverallsTestBin, args...).CombinedOutput()
}