@@ -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" ,
@@ -106,7 +107,7 @@ func TestPluginPathsAreValid(t *testing.T) {
106
107
pluginFile : func () (* os.File , error ) {
107
108
return ioutil .TempFile (tempDir , "notkubectl-" )
108
109
},
109
- expectErr : "unable to find any kubectl plugins in your PATH" ,
110
+ expectErr : "error: unable to find any kubectl plugins in your PATH\n " ,
110
111
},
111
112
{
112
113
name : "ensure de-duplicated plugin-paths slice" ,
@@ -115,12 +116,22 @@ func TestPluginPathsAreValid(t *testing.T) {
115
116
pluginFile : func () (* os.File , error ) {
116
117
return ioutil .TempFile (tempDir , "kubectl-" )
117
118
},
119
+ expectOut : "The following compatible plugins are available:" ,
120
+ },
121
+ {
122
+ name : "ensure no errors when empty string or blank path are specified" ,
123
+ pluginPaths : []string {tempDir , "" , " " },
124
+ verifier : newFakePluginPathVerifier (),
125
+ pluginFile : func () (* os.File , error ) {
126
+ return ioutil .TempFile (tempDir , "kubectl-" )
127
+ },
128
+ expectOut : "The following compatible plugins are available:" ,
118
129
},
119
130
}
120
131
121
132
for _ , test := range tc {
122
133
t .Run (test .name , func (t * testing.T ) {
123
- ioStreams , _ , _ , errOut := genericclioptions .NewTestIOStreams ()
134
+ ioStreams , _ , out , errOut := genericclioptions .NewTestIOStreams ()
124
135
o := & PluginListOptions {
125
136
Verifier : test .verifier ,
126
137
IOStreams : ioStreams ,
@@ -145,23 +156,23 @@ func TestPluginPathsAreValid(t *testing.T) {
145
156
146
157
err := o .Run ()
147
158
if err == nil && len (test .expectErr ) > 0 {
148
- t .Fatalf ("unexpected non-error: expecting %v" , test .expectErr )
149
- }
150
- if err != nil && len (test .expectErr ) == 0 {
151
- t .Fatalf ("unexpected error: %v - %v" , err , errOut .String ())
152
- }
153
- if err == nil {
154
- return
159
+ t .Fatalf ("unexpected non-error: expected %v, but got nothing" , test .expectErr )
160
+ } else if err != nil && len (test .expectErr ) == 0 {
161
+ t .Fatalf ("unexpected error: expected nothing, but got %v" , err .Error ())
162
+ } else if err != nil && err .Error () != test .expectErr {
163
+ t .Fatalf ("unexpected error: expected %v, but got %v" , test .expectErr , err .Error ())
155
164
}
156
165
157
- allErrs := bytes .NewBuffer (errOut .Bytes ())
158
- if _ , err := allErrs .WriteString (err .Error ()); err != nil {
159
- t .Fatalf ("unexpected error: %v" , err )
166
+ if len (test .expectErrOut ) == 0 && errOut .Len () > 0 {
167
+ t .Fatalf ("unexpected error output: expected nothing, but got %v" , errOut .String ())
168
+ } else if len (test .expectErrOut ) > 0 && ! strings .Contains (errOut .String (), test .expectErrOut ) {
169
+ t .Fatalf ("unexpected error output: expected to contain %v, but got %v" , test .expectErrOut , errOut .String ())
160
170
}
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
- }
171
+
172
+ if len (test .expectOut ) == 0 && out .Len () > 0 {
173
+ t .Fatalf ("unexpected output: expected nothing, but got %v" , out .String ())
174
+ } else if len (test .expectOut ) > 0 && ! strings .Contains (out .String (), test .expectOut ) {
175
+ t .Fatalf ("unexpected output: expected to contain %v, but got %v" , test .expectOut , out .String ())
165
176
}
166
177
})
167
178
}
0 commit comments