Skip to content

Commit 133e403

Browse files
author
Katrina Owen
committed
Replace individual overrides for streams in tests
The tests were not consistent in how they were overriding STDOUT and STDERR. This caused several of the tests to print output to the terminal during a test run if you ran only the tests for the cmd package.
1 parent b055290 commit 133e403

File tree

5 files changed

+55
-112
lines changed

5 files changed

+55
-112
lines changed

cmd/configure_test.go

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ func TestBareConfigure(t *testing.T) {
3838
}
3939

4040
func TestConfigureShow(t *testing.T) {
41-
oldErr := Err
42-
defer func() {
43-
Err = oldErr
44-
}()
45-
46-
Err = &bytes.Buffer{}
41+
co := newCapturedOutput()
42+
co.newErr = &bytes.Buffer{}
43+
co.override()
44+
defer co.reset()
4745

4846
flags := pflag.NewFlagSet("fake", pflag.PanicOnError)
4947
setupConfigureFlags(flags)
@@ -82,6 +80,10 @@ func TestConfigureShow(t *testing.T) {
8280
}
8381

8482
func TestConfigureToken(t *testing.T) {
83+
co := newCapturedOutput()
84+
co.override()
85+
defer co.reset()
86+
8587
testCases := []struct {
8688
desc string
8789
configured string
@@ -142,12 +144,6 @@ func TestConfigureToken(t *testing.T) {
142144
ts := httptest.NewServer(endpoint)
143145
defer ts.Close()
144146

145-
oldOut := Out
146-
Out = ioutil.Discard
147-
defer func() {
148-
Out = oldOut
149-
}()
150-
151147
for _, tc := range testCases {
152148
flags := pflag.NewFlagSet("fake", pflag.PanicOnError)
153149
setupConfigureFlags(flags)
@@ -173,6 +169,10 @@ func TestConfigureToken(t *testing.T) {
173169
}
174170

175171
func TestConfigureAPIBaseURL(t *testing.T) {
172+
co := newCapturedOutput()
173+
co.override()
174+
defer co.reset()
175+
176176
endpoint := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
177177
if r.URL.Path == "/ping" {
178178
w.WriteHeader(http.StatusNotFound)
@@ -225,12 +225,6 @@ func TestConfigureAPIBaseURL(t *testing.T) {
225225
},
226226
}
227227

228-
oldOut := Out
229-
Out = ioutil.Discard
230-
defer func() {
231-
Out = oldOut
232-
}()
233-
234228
for _, tc := range testCases {
235229
flags := pflag.NewFlagSet("fake", pflag.PanicOnError)
236230
setupConfigureFlags(flags)
@@ -256,11 +250,9 @@ func TestConfigureAPIBaseURL(t *testing.T) {
256250
}
257251

258252
func TestConfigureWorkspace(t *testing.T) {
259-
oldErr := Err
260-
Err = ioutil.Discard
261-
defer func() {
262-
Err = oldErr
263-
}()
253+
co := newCapturedOutput()
254+
co.override()
255+
defer co.reset()
264256

265257
testCases := []struct {
266258
desc string
@@ -342,14 +334,9 @@ func TestConfigureWorkspace(t *testing.T) {
342334
}
343335

344336
func TestConfigureDefaultWorkspaceWithoutClobbering(t *testing.T) {
345-
oldOut := Out
346-
oldErr := Err
347-
Out = ioutil.Discard
348-
Err = ioutil.Discard
349-
defer func() {
350-
Out = oldOut
351-
Err = oldErr
352-
}()
337+
co := newCapturedOutput()
338+
co.override()
339+
defer co.reset()
353340

354341
// Stub server to always be 200 OK
355342
endpoint := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
@@ -387,14 +374,9 @@ func TestConfigureDefaultWorkspaceWithoutClobbering(t *testing.T) {
387374
}
388375

389376
func TestConfigureExplicitWorkspaceWithoutClobberingNonDirectory(t *testing.T) {
390-
oldOut := Out
391-
oldErr := Err
392-
Out = ioutil.Discard
393-
Err = ioutil.Discard
394-
defer func() {
395-
Out = oldOut
396-
Err = oldErr
397-
}()
377+
co := newCapturedOutput()
378+
co.override()
379+
defer co.reset()
398380

399381
tmpDir, err := ioutil.TempDir("", "no-clobber")
400382
defer os.RemoveAll(tmpDir)

cmd/download_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,9 @@ func TestDownloadWithoutFlags(t *testing.T) {
7878
}
7979

8080
func TestDownload(t *testing.T) {
81-
oldOut := Out
82-
oldErr := Err
83-
Out = ioutil.Discard
84-
Err = ioutil.Discard
85-
defer func() {
86-
Out = oldOut
87-
Err = oldErr
88-
}()
81+
co := newCapturedOutput()
82+
co.override()
83+
defer co.reset()
8984

9085
testCases := []struct {
9186
requester bool

cmd/submit_symlink_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@ import (
1515
)
1616

1717
func TestSubmitFilesInSymlinkedPath(t *testing.T) {
18-
oldOut := Out
19-
oldErr := Err
20-
Out = ioutil.Discard
21-
Err = ioutil.Discard
22-
defer func() {
23-
Out = oldOut
24-
Err = oldErr
25-
}()
18+
co := newCapturedOutput()
19+
co.override()
20+
defer co.reset()
2621

2722
// The fake endpoint will populate this when it receives the call from the command.
2823
submittedFiles := map[string]string{}

cmd/submit_test.go

Lines changed: 25 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,10 @@ func TestSubmitFilesAndDir(t *testing.T) {
142142
}
143143

144144
func TestSubmitFiles(t *testing.T) {
145-
oldOut := Out
146-
oldErr := Err
147-
Out = ioutil.Discard
148-
Err = ioutil.Discard
149-
defer func() {
150-
Out = oldOut
151-
Err = oldErr
152-
}()
145+
co := newCapturedOutput()
146+
co.override()
147+
defer co.reset()
148+
153149
// The fake endpoint will populate this when it receives the call from the command.
154150
submittedFiles := map[string]string{}
155151
ts := fakeSubmitServer(t, submittedFiles)
@@ -201,11 +197,10 @@ func TestSubmitFiles(t *testing.T) {
201197
}
202198

203199
func TestLegacyMetadataMigration(t *testing.T) {
204-
oldErr := Err
205-
defer func() {
206-
Err = oldErr
207-
}()
208-
Err = &bytes.Buffer{}
200+
co := newCapturedOutput()
201+
co.newErr = &bytes.Buffer{}
202+
co.override()
203+
defer co.reset()
209204

210205
submittedFiles := map[string]string{}
211206
ts := fakeSubmitServer(t, submittedFiles)
@@ -265,14 +260,9 @@ func TestLegacyMetadataMigration(t *testing.T) {
265260
}
266261

267262
func TestSubmitWithEmptyFile(t *testing.T) {
268-
oldOut := Out
269-
oldErr := Err
270-
Out = ioutil.Discard
271-
Err = ioutil.Discard
272-
defer func() {
273-
Out = oldOut
274-
Err = oldErr
275-
}()
263+
co := newCapturedOutput()
264+
co.override()
265+
defer co.reset()
276266

277267
// The fake endpoint will populate this when it receives the call from the command.
278268
submittedFiles := map[string]string{}
@@ -311,14 +301,9 @@ func TestSubmitWithEmptyFile(t *testing.T) {
311301
}
312302

313303
func TestSubmitWithEnormousFile(t *testing.T) {
314-
oldOut := Out
315-
oldErr := Err
316-
Out = ioutil.Discard
317-
Err = ioutil.Discard
318-
defer func() {
319-
Out = oldOut
320-
Err = oldErr
321-
}()
304+
co := newCapturedOutput()
305+
co.override()
306+
defer co.reset()
322307

323308
// The fake endpoint will populate this when it receives the call from the command.
324309
submittedFiles := map[string]string{}
@@ -358,14 +343,10 @@ func TestSubmitWithEnormousFile(t *testing.T) {
358343
}
359344

360345
func TestSubmitFilesForTeamExercise(t *testing.T) {
361-
oldOut := Out
362-
oldErr := Err
363-
Out = ioutil.Discard
364-
Err = ioutil.Discard
365-
defer func() {
366-
Out = oldOut
367-
Err = oldErr
368-
}()
346+
co := newCapturedOutput()
347+
co.override()
348+
defer co.reset()
349+
369350
// The fake endpoint will populate this when it receives the call from the command.
370351
submittedFiles := map[string]string{}
371352
ts := fakeSubmitServer(t, submittedFiles)
@@ -409,14 +390,9 @@ func TestSubmitFilesForTeamExercise(t *testing.T) {
409390
}
410391

411392
func TestSubmitOnlyEmptyFile(t *testing.T) {
412-
oldOut := Out
413-
oldErr := Err
414-
Out = ioutil.Discard
415-
Err = ioutil.Discard
416-
defer func() {
417-
Out = oldOut
418-
Err = oldErr
419-
}()
393+
co := newCapturedOutput()
394+
co.override()
395+
defer co.reset()
420396

421397
tmpDir, err := ioutil.TempDir("", "just-an-empty-file")
422398
defer os.RemoveAll(tmpDir)
@@ -512,14 +488,10 @@ func fakeSubmitServer(t *testing.T, submittedFiles map[string]string) *httptest.
512488
}
513489

514490
func TestSubmitRelativePath(t *testing.T) {
515-
oldOut := Out
516-
oldErr := Err
517-
Out = ioutil.Discard
518-
Err = ioutil.Discard
519-
defer func() {
520-
Out = oldOut
521-
Err = oldErr
522-
}()
491+
co := newCapturedOutput()
492+
co.override()
493+
defer co.reset()
494+
523495
// The fake endpoint will populate this when it receives the call from the command.
524496
submittedFiles := map[string]string{}
525497
ts := fakeSubmitServer(t, submittedFiles)

cmd/upgrade_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"io/ioutil"
54
"testing"
65

76
"github.com/stretchr/testify/assert"
@@ -22,9 +21,9 @@ func (fc *fakeCLI) Upgrade() error {
2221
}
2322

2423
func TestUpgrade(t *testing.T) {
25-
oldOut := Out
26-
Out = ioutil.Discard
27-
defer func() { Out = oldOut }()
24+
co := newCapturedOutput()
25+
co.override()
26+
defer co.reset()
2827

2928
testCases := []struct {
3029
desc string

0 commit comments

Comments
 (0)