-
Notifications
You must be signed in to change notification settings - Fork 232
/
clitest.go
232 lines (196 loc) · 6.96 KB
/
clitest.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
232
// Package clitest contains common utilities and helpers for testing the CLI
package clitest
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"github.com/CircleCI-Public/circleci-cli/api/graphql"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/ghttp"
"github.com/onsi/gomega/types"
"github.com/onsi/gomega"
)
// On Unix, we want to assert that processed exited with 255
// On Windows, it should be -1.
func ShouldFail() types.GomegaMatcher {
failureCode := 255
if runtime.GOOS == "windows" {
failureCode = -1
}
return gexec.Exit(failureCode)
}
// TempSettings contains useful settings for testing the CLI
type TempSettings struct {
Home string
TestServer *ghttp.Server
Config *TmpFile
Update *TmpFile
}
// Close should be called in an AfterEach and cleans up the temp directory and server process
func (settings *TempSettings) Close() error {
settings.TestServer.Close()
settings.Config.Close()
settings.Update.Close()
return os.RemoveAll(settings.Home)
}
// AssertConfigRereadMatches re-opens the config file and checks it's contents against the given string
func (tempSettings TempSettings) AssertConfigRereadMatches(contents string) {
file, err := os.Open(tempSettings.Config.Path)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
reread, err := ioutil.ReadAll(file)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(string(reread)).To(gomega.ContainSubstring(contents))
}
// WithTempSettings should be called in a BeforeEach and returns a new TempSettings with everything setup for you
func WithTempSettings() *TempSettings {
var err error
tempSettings := &TempSettings{}
tempSettings.Home, err = ioutil.TempDir("", "circleci-cli-test-")
gomega.Expect(err).ToNot(gomega.HaveOccurred())
settingsPath := filepath.Join(tempSettings.Home, ".circleci")
gomega.Expect(os.Mkdir(settingsPath, 0700)).To(gomega.Succeed())
tempSettings.Config = OpenTmpFile(settingsPath, "cli.yml")
tempSettings.Update = OpenTmpFile(settingsPath, "update_check.yml")
tempSettings.TestServer = ghttp.NewServer()
return tempSettings
}
// NewFakeClient returns a new *client.Client with the TestServer set and the provided endpoint, token.
func (tempSettings *TempSettings) NewFakeClient(endpoint, token string) *graphql.Client {
return graphql.NewClient(http.DefaultClient, tempSettings.TestServer.URL(), endpoint, token, false)
}
// MockRequestResponse is a helpful type for mocking HTTP handlers.
type MockRequestResponse struct {
Request string
Status int
Response string
ErrorResponse string
}
func (tempSettings *TempSettings) AppendRESTPostHandler(combineHandlers ...MockRequestResponse) {
for _, handler := range combineHandlers {
responseBody := handler.Response
if handler.ErrorResponse != "" {
responseBody = handler.ErrorResponse
}
tempSettings.TestServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/api/v2/context"),
ghttp.VerifyContentType("application/json"),
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = req.Body.Close()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(handler.Request).Should(gomega.MatchJSON(body), "JSON Mismatch")
},
ghttp.RespondWith(handler.Status, responseBody),
),
)
}
}
// AppendPostHandler stubs out the provided MockRequestResponse.
// When authToken is an empty string no token validation is performed.
func (tempSettings *TempSettings) AppendPostHandler(authToken string, combineHandlers ...MockRequestResponse) {
for _, handler := range combineHandlers {
responseBody := `{ "data": ` + handler.Response + `}`
if handler.ErrorResponse != "" {
responseBody = fmt.Sprintf("{ \"data\": %s, \"errors\": %s}", handler.Response, handler.ErrorResponse)
}
if authToken == "" {
tempSettings.TestServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/graphql-unstable"),
ghttp.VerifyContentType("application/json; charset=utf-8"),
// From Gomegas ghttp.VerifyJson to avoid the
// VerifyContentType("application/json") check
// that fails with "application/json; charset=utf-8"
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = req.Body.Close()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(handler.Request).Should(gomega.MatchJSON(body), "JSON Mismatch")
},
ghttp.RespondWith(handler.Status, responseBody),
),
)
} else {
tempSettings.TestServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("POST", "/graphql-unstable"),
ghttp.VerifyHeader(http.Header{
"Authorization": []string{authToken},
}),
ghttp.VerifyContentType("application/json; charset=utf-8"),
// From Gomegas ghttp.VerifyJson to avoid the
// VerifyContentType("application/json") check
// that fails with "application/json; charset=utf-8"
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
err = req.Body.Close()
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(body).Should(gomega.MatchJSON(handler.Request), "JSON Mismatch")
},
ghttp.RespondWith(handler.Status, responseBody),
),
)
}
}
}
// TmpFile wraps a temporary file on disk for utility.
type TmpFile struct {
RootDir string
Path string
File *os.File
}
func (tempFile *TmpFile) Close() error {
return tempFile.File.Close()
}
// Write will write the given contents to the file on disk and close it.
func (f TmpFile) Write(contents []byte) {
_, err := f.File.Write(contents)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(f.File.Close()).To(gomega.Succeed())
}
// OpenTmpFile will create a new temporary file in the provided directory with a name of the given path.
func OpenTmpFile(directory string, path string) *TmpFile {
var (
config = &TmpFile{}
err error
)
config.RootDir = directory
config.Path = filepath.Join(directory, path)
err = os.MkdirAll(filepath.Dir(config.Path), 0700)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
var file *os.File
file, err = os.OpenFile(
config.Path,
os.O_RDWR|os.O_CREATE,
0600,
)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
config.File = file
return config
}
// WithCapturedOutput will call the provided function and capture any output to stdout which is returned as a string.
func WithCapturedOutput(f func()) string {
r, w, err := os.Pipe()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
stdout := os.Stdout
os.Stdout = w
defer func() {
os.Stdout = stdout
}()
f()
err = w.Close()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
var buf bytes.Buffer
_, err = io.Copy(&buf, r)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
return buf.String()
}