@@ -17,7 +17,6 @@ limitations under the License.
17
17
package plugin
18
18
19
19
import (
20
- "bytes"
21
20
"fmt"
22
21
"io/ioutil"
23
22
"os"
@@ -98,6 +97,8 @@ func TestPluginPathsAreValid(t *testing.T) {
98
97
verifier * fakePluginPathVerifier
99
98
expectVerifyErrors []error
100
99
expectErr string
100
+ expectErrOut string
101
+ expectOut string
101
102
}{
102
103
{
103
104
name : "ensure no plugins found if no files begin with kubectl- prefix" ,
@@ -107,6 +108,7 @@ func TestPluginPathsAreValid(t *testing.T) {
107
108
return ioutil .TempFile (tempDir , "notkubectl-" )
108
109
},
109
110
expectErr : "unable to find any kubectl plugins in your PATH" ,
111
+ expectErrOut : "\n " ,
110
112
},
111
113
{
112
114
name : "ensure de-duplicated plugin-paths slice" ,
@@ -115,18 +117,30 @@ func TestPluginPathsAreValid(t *testing.T) {
115
117
pluginFile : func () (* os.File , error ) {
116
118
return ioutil .TempFile (tempDir , "kubectl-" )
117
119
},
120
+ expectOut : "The following compatible plugins are available:" ,
121
+ },
122
+ {
123
+ name : "ensure no errors when empty string or blank path are specified" ,
124
+ pluginPaths : []string {tempDir , "" , " " },
125
+ verifier : newFakePluginPathVerifier (),
126
+ pluginFile : func () (* os.File , error ) {
127
+ return ioutil .TempFile (tempDir , "kubectl-" )
128
+ },
129
+ expectOut : "The following compatible plugins are available:" ,
118
130
},
119
131
}
120
132
121
133
for _ , test := range tc {
122
134
t .Run (test .name , func (t * testing.T ) {
123
- ioStreams , _ , _ , errOut := genericclioptions .NewTestIOStreams ()
135
+ ioStreams , _ , out , errOut := genericclioptions .NewTestIOStreams ()
124
136
o := & PluginListOptions {
125
137
Verifier : test .verifier ,
126
138
IOStreams : ioStreams ,
127
139
128
140
PluginPaths : test .pluginPaths ,
129
141
}
142
+ o .Out = out
143
+ o .ErrOut = errOut
130
144
131
145
// create files
132
146
if test .pluginFile != nil {
@@ -150,18 +164,17 @@ func TestPluginPathsAreValid(t *testing.T) {
150
164
if err != nil && len (test .expectErr ) == 0 {
151
165
t .Fatalf ("unexpected error: %v - %v" , err , errOut .String ())
152
166
}
153
- if err == nil {
154
- return
155
- }
156
167
157
- allErrs := bytes .NewBuffer (errOut .Bytes ())
158
- if _ , err := allErrs .WriteString (err .Error ()); err != nil {
159
- t .Fatalf ("unexpected error: %v" , err )
168
+ if len (test .expectErrOut ) == 0 && errOut .Len () > 0 {
169
+ t .Fatalf ("unexpected error output: expected nothing, but got %v" , errOut .String ())
170
+ } else if len (test .expectErrOut ) > 0 && ! strings .Contains (errOut .String (), test .expectErrOut ) {
171
+ t .Fatalf ("unexpected error output: expected to contain %v, but got %v" , test .expectErrOut , errOut .String ())
160
172
}
161
- if len (test .expectErr ) > 0 {
162
- if ! strings .Contains (allErrs .String (), test .expectErr ) {
163
- t .Fatalf ("unexpected error: expected %v, but got %v" , test .expectErr , allErrs .String ())
164
- }
173
+
174
+ if len (test .expectOut ) == 0 && out .Len () > 0 {
175
+ t .Fatalf ("unexpected output: expected nothing, but got %v" , out .String ())
176
+ } else if len (test .expectOut ) > 0 && ! strings .Contains (out .String (), test .expectOut ) {
177
+ t .Fatalf ("unexpected output: expected to contain %v, but got %v" , test .expectOut , out .String ())
165
178
}
166
179
})
167
180
}
0 commit comments