Skip to content

Commit f60c0af

Browse files
committed
Ignore empty or blank string in path when listing plugins
1 parent a71586f commit f60c0af

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

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

Lines changed: 6 additions & 2 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
}

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

Lines changed: 25 additions & 12 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",
@@ -107,6 +108,7 @@ func TestPluginPathsAreValid(t *testing.T) {
107108
return ioutil.TempFile(tempDir, "notkubectl-")
108109
},
109110
expectErr: "unable to find any kubectl plugins in your PATH",
111+
expectErrOut: "\n",
110112
},
111113
{
112114
name: "ensure de-duplicated plugin-paths slice",
@@ -115,18 +117,30 @@ func TestPluginPathsAreValid(t *testing.T) {
115117
pluginFile: func() (*os.File, error) {
116118
return ioutil.TempFile(tempDir, "kubectl-")
117119
},
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:",
118130
},
119131
}
120132

121133
for _, test := range tc {
122134
t.Run(test.name, func(t *testing.T) {
123-
ioStreams, _, _, errOut := genericclioptions.NewTestIOStreams()
135+
ioStreams, _, out, errOut := genericclioptions.NewTestIOStreams()
124136
o := &PluginListOptions{
125137
Verifier: test.verifier,
126138
IOStreams: ioStreams,
127139

128140
PluginPaths: test.pluginPaths,
129141
}
142+
o.Out = out
143+
o.ErrOut = errOut
130144

131145
// create files
132146
if test.pluginFile != nil {
@@ -150,18 +164,17 @@ func TestPluginPathsAreValid(t *testing.T) {
150164
if err != nil && len(test.expectErr) == 0 {
151165
t.Fatalf("unexpected error: %v - %v", err, errOut.String())
152166
}
153-
if err == nil {
154-
return
155-
}
156167

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())
160172
}
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())
165178
}
166179
})
167180
}

0 commit comments

Comments
 (0)