Skip to content

Commit acc3a6e

Browse files
author
Katrina Owen
authored
Merge pull request #785 from exercism/silence-noisy-output
Silence noisy output in cmd tests
2 parents 42e56dc + 133e403 commit acc3a6e

File tree

8 files changed

+98
-129
lines changed

8 files changed

+98
-129
lines changed

cmd/cmd.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,25 @@ package cmd
33
import (
44
"fmt"
55

6+
"io"
7+
68
"github.com/exercism/cli/config"
79
"github.com/spf13/viper"
810
)
911

12+
var (
13+
// BinaryName is the name of the app.
14+
// By default this is exercism, but people
15+
// are free to name this however they want.
16+
// The usage examples and help strings should reflect
17+
// the actual name of the binary.
18+
BinaryName string
19+
// Out is used to write to information.
20+
Out io.Writer
21+
// Err is used to write errors.
22+
Err io.Writer
23+
)
24+
1025
const msgWelcomePleaseConfigure = `
1126
1227
Welcome to Exercism!

cmd/cmd_test.go

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

33
import (
4+
"io"
45
"io/ioutil"
56
"os"
67
"testing"
@@ -90,3 +91,30 @@ func (test *CommandTest) Teardown(t *testing.T) {
9091
t.Fatal(err)
9192
}
9293
}
94+
95+
// capturedOutput lets us more easily redirect streams in the tests.
96+
type capturedOutput struct {
97+
oldOut, oldErr, newOut, newErr io.Writer
98+
}
99+
100+
// newCapturedOutput creates a new value to override the streams.
101+
func newCapturedOutput() capturedOutput {
102+
return capturedOutput{
103+
oldOut: Out,
104+
oldErr: Err,
105+
newOut: ioutil.Discard,
106+
newErr: ioutil.Discard,
107+
}
108+
}
109+
110+
// override sets the package variables to the fake streams.
111+
func (co capturedOutput) override() {
112+
Out = co.newOut
113+
Err = co.newErr
114+
}
115+
116+
// reset puts back the original streams for the commands to write to.
117+
func (co capturedOutput) reset() {
118+
Out = co.oldOut
119+
Err = co.oldErr
120+
}

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/root.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"io"
65
"os"
76
"runtime"
87

@@ -13,21 +12,6 @@ import (
1312
"github.com/spf13/cobra"
1413
)
1514

16-
var (
17-
// BinaryName is the name of the app.
18-
// By default this is exercism, but people
19-
// are free to name this however they want.
20-
// The usage examples and help strings should reflect
21-
// the actual name of the binary.
22-
BinaryName string
23-
// Out is used to write to information.
24-
Out io.Writer
25-
// Err is used to write errors.
26-
Err io.Writer
27-
// In is used to provide mocked test input (i.e. for prompts).
28-
In io.Reader
29-
)
30-
3115
// RootCmd represents the base command when called without any subcommands.
3216
var RootCmd = &cobra.Command{
3317
Use: BinaryName,
@@ -59,7 +43,6 @@ func init() {
5943
config.SetDefaultDirName(BinaryName)
6044
Out = os.Stdout
6145
Err = os.Stderr
62-
In = os.Stdin
6346
api.UserAgent = fmt.Sprintf("github.com/exercism/cli v%s (%s/%s)", Version, runtime.GOOS, runtime.GOARCH)
6447
RootCmd.PersistentFlags().BoolP("verbose", "v", false, "verbose output")
6548
RootCmd.PersistentFlags().IntP("timeout", "", 0, "override the default HTTP timeout (seconds)")

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)

0 commit comments

Comments
 (0)