File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1
1
package cmd
2
2
3
3
import (
4
+ "io"
4
5
"io/ioutil"
5
6
"os"
6
7
"testing"
@@ -90,3 +91,30 @@ func (test *CommandTest) Teardown(t *testing.T) {
90
91
t .Fatal (err )
91
92
}
92
93
}
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
+ }
You can’t perform that action at this time.
0 commit comments