Skip to content

Commit ed74b7c

Browse files
authored
Merge pull request kubernetes#87633 from brianpursley/kubectl-792
Prevent error message from being displayed during plugin list when path includes empty string
2 parents 6d4e2d7 + 2d21f16 commit ed74b7c

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,14 @@ func (o *PluginListOptions) Run() error {
113113
pluginWarnings := 0
114114

115115
for _, dir := range uniquePathsList(o.PluginPaths) {
116+
if len(strings.TrimSpace(dir)) == 0 {
117+
continue
118+
}
119+
116120
files, err := ioutil.ReadDir(dir)
117121
if err != nil {
118122
if _, ok := err.(*os.PathError); ok {
119-
fmt.Fprintf(o.ErrOut, "Unable read directory %q from your PATH: %v. Skipping...", dir, err)
123+
fmt.Fprintf(o.ErrOut, "Unable read directory %q from your PATH: %v. Skipping...\n", dir, err)
120124
continue
121125
}
122126

@@ -133,7 +137,7 @@ func (o *PluginListOptions) Run() error {
133137
}
134138

135139
if isFirstFile {
136-
fmt.Fprintf(o.ErrOut, "The following compatible plugins are available:\n\n")
140+
fmt.Fprintf(o.Out, "The following compatible plugins are available:\n\n")
137141
pluginsFound = true
138142
isFirstFile = false
139143
}
@@ -165,7 +169,6 @@ func (o *PluginListOptions) Run() error {
165169
}
166170
}
167171
if len(pluginErrors) > 0 {
168-
fmt.Fprintln(o.ErrOut)
169172
errs := bytes.NewBuffer(nil)
170173
for _, e := range pluginErrors {
171174
fmt.Fprintln(errs, e)

staging/src/k8s.io/kubectl/pkg/cmd/plugin/plugin_test.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package plugin
1818

1919
import (
20-
"bytes"
2120
"fmt"
2221
"io/ioutil"
2322
"os"
@@ -98,6 +97,8 @@ func TestPluginPathsAreValid(t *testing.T) {
9897
verifier *fakePluginPathVerifier
9998
expectVerifyErrors []error
10099
expectErr string
100+
expectErrOut string
101+
expectOut string
101102
}{
102103
{
103104
name: "ensure no plugins found if no files begin with kubectl- prefix",
@@ -106,7 +107,7 @@ func TestPluginPathsAreValid(t *testing.T) {
106107
pluginFile: func() (*os.File, error) {
107108
return ioutil.TempFile(tempDir, "notkubectl-")
108109
},
109-
expectErr: "unable to find any kubectl plugins in your PATH",
110+
expectErr: "error: unable to find any kubectl plugins in your PATH\n",
110111
},
111112
{
112113
name: "ensure de-duplicated plugin-paths slice",
@@ -115,12 +116,22 @@ func TestPluginPathsAreValid(t *testing.T) {
115116
pluginFile: func() (*os.File, error) {
116117
return ioutil.TempFile(tempDir, "kubectl-")
117118
},
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:",
118129
},
119130
}
120131

121132
for _, test := range tc {
122133
t.Run(test.name, func(t *testing.T) {
123-
ioStreams, _, _, errOut := genericclioptions.NewTestIOStreams()
134+
ioStreams, _, out, errOut := genericclioptions.NewTestIOStreams()
124135
o := &PluginListOptions{
125136
Verifier: test.verifier,
126137
IOStreams: ioStreams,
@@ -145,23 +156,23 @@ func TestPluginPathsAreValid(t *testing.T) {
145156

146157
err := o.Run()
147158
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())
155164
}
156165

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())
160170
}
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())
165176
}
166177
})
167178
}

0 commit comments

Comments
 (0)