-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoveralls_test.go
More file actions
158 lines (140 loc) · 4.26 KB
/
Copy pathgoveralls_test.go
File metadata and controls
158 lines (140 loc) · 4.26 KB
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
package main
import (
"fmt"
"io/ioutil"
"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 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 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/smithoss/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 verbosed go test output in stdout", string(b))
}
})
t.Run("without verbose", func(t *testing.T) {
cmd := exec.Command("goveralls", "-package=github.com/smithoss/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 verbosed 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/smithoss/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/smithoss/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/smithoss/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 dones'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/smithoss/goveralls/testergo-runewidth")
b := runCmd(t, "goveralls", "-package=github.com/smithoss/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 failured")
}
}
*/
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/smithoss/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
}