ResourceNames() should prohibit unnamed arguments

resource.Builder should prohibit empty resource names (the error is from
the wrong place) so that commands that work on multiple resources but
not resource types can properly limit errors.
This commit is contained in:
Clayton Coleman 2016-08-16 17:51:08 -04:00
parent 1b0bc9421f
commit 994e9e3c62
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
2 changed files with 26 additions and 0 deletions

View File

@ -212,6 +212,10 @@ func (b *Builder) ResourceNames(resource string, names ...string) *Builder {
b.resourceTuples = append(b.resourceTuples, tuple)
continue
}
if len(resource) == 0 {
b.errs = append(b.errs, fmt.Errorf("the argument %q must be RESOURCE/NAME", name))
continue
}
// Use the given default type to create a resource tuple
b.resourceTuples = append(b.resourceTuples, resourceTuple{Resource: resource, Name: name})

View File

@ -626,6 +626,28 @@ func TestResourceNames(t *testing.T) {
}
}
func TestResourceNamesWithoutResource(t *testing.T) {
pods, svc := testData()
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{
"/namespaces/test/pods/foo": runtime.EncodeOrDie(testapi.Default.Codec(), &pods.Items[0]),
"/namespaces/test/services/baz": runtime.EncodeOrDie(testapi.Default.Codec(), &svc.Items[0]),
}), testapi.Default.Codec()).
NamespaceParam("test")
test := &testVisitor{}
if b.Do().Err() == nil {
t.Errorf("unexpected non-error")
}
b.ResourceNames("", "foo", "services/baz")
err := b.Do().Visit(test.Handle)
if err == nil || !strings.Contains(err.Error(), "must be RESOURCE/NAME") {
t.Fatalf("unexpected response: %v", err)
}
}
func TestResourceByNameWithoutRequireObject(t *testing.T) {
b := NewBuilder(testapi.Default.RESTMapper(), api.Scheme, fakeClientWith("", t, map[string]string{}), testapi.Default.Codec()).
NamespaceParam("test")