Skip to content

Commit

Permalink
Fixes issue knative#111 by responding with an error for unknown subco…
Browse files Browse the repository at this point in the history
…mmands

```bash
➜  client git:(issue111) ✗ ./kn service unknown
unknown command "unknown" for "kn service"

➜  client git:(issue111) ✗ ./kn revision
please provide a valid sub-command for "kn revision"
```

This works for both the `service` and `revision` groups.
  • Loading branch information
maximilien committed Jun 19, 2019
1 parent 8a1d412 commit 1d8b1e3
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/cmd/kn_revision.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Revision command group

Revision command group

```
kn revision [flags]
```

### Options

```
Expand Down
4 changes: 4 additions & 0 deletions docs/cmd/kn_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Service command group

Service command group

```
kn service [flags]
```

### Options

```
Expand Down
6 changes: 6 additions & 0 deletions pkg/kn/commands/revision/revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package revision

import (
"errors"

"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
)
Expand All @@ -23,6 +25,10 @@ func NewRevisionCommand(p *commands.KnParams) *cobra.Command {
revisionCmd := &cobra.Command{
Use: "revision",
Short: "Revision command group",
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error {
return errors.New("please provide a valid sub-command for \"kn revision\"")
},
}
revisionCmd.AddCommand(NewRevisionListCommand(p))
revisionCmd.AddCommand(NewRevisionDescribeCommand(p))
Expand Down
6 changes: 3 additions & 3 deletions pkg/kn/commands/revision/revision_describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"sigs.k8s.io/yaml"
)

func fakeRevision(args []string, response *v1alpha1.Revision) (action client_testing.Action, output string, err error) {
func fakeRevisionDescribe(args []string, response *v1alpha1.Revision) (action client_testing.Action, output string, err error) {
knParams := &commands.KnParams{}
cmd, fakeServing, buf := commands.CreateTestKnCommand(NewRevisionCommand(knParams), knParams)
fakeServing.AddReactor("*", "*",
Expand All @@ -46,7 +46,7 @@ func fakeRevision(args []string, response *v1alpha1.Revision) (action client_tes
}

func TestDescribeRevisionWithNoName(t *testing.T) {
_, _, err := fakeRevision([]string{"revision", "describe"}, &v1alpha1.Revision{})
_, _, err := fakeRevisionDescribe([]string{"revision", "describe"}, &v1alpha1.Revision{})
expectedError := "requires the revision name."
if err == nil || err.Error() != expectedError {
t.Fatal("expect to fail with missing revision name")
Expand All @@ -70,7 +70,7 @@ func TestDescribeRevisionYaml(t *testing.T) {
},
}

action, data, err := fakeRevision([]string{"revision", "describe", "test-rev"}, &expectedRevision)
action, data, err := fakeRevisionDescribe([]string{"revision", "describe", "test-rev"}, &expectedRevision)
if err != nil {
t.Fatal(err)
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/kn/commands/revision/revision_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2018 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package revision

import (
"strings"
"testing"

"github.com/knative/client/pkg/kn/commands"
v1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
client_testing "k8s.io/client-go/testing"
)

func fakeRevision(args []string, response *v1alpha1.ServiceList) (action client_testing.Action, output []string, err error) {
knParams := &commands.KnParams{}
cmd, fakeServing, buf := commands.CreateTestKnCommand(NewRevisionCommand(knParams), knParams)
fakeServing.AddReactor("*", "*",
func(a client_testing.Action) (bool, runtime.Object, error) {
action = a
return true, response, nil
})
cmd.SetArgs(args)
err = cmd.Execute()
if err != nil {
return
}
output = strings.Split(buf.String(), "\n")
return
}

func TestUnknownSubcommand(t *testing.T) {
_, _, err := fakeRevision([]string{"revision", "unknown"}, &v1alpha1.ServiceList{})
if err == nil {
t.Error(err)
return
}

if err.Error() != "unknown command \"unknown\" for \"kn revision\"" {
t.Errorf("Bad error message %s", err.Error())
}
}

func TestEmptySubcommand(t *testing.T) {
_, _, err := fakeRevision([]string{"revision"}, &v1alpha1.ServiceList{})
if err == nil {
t.Error(err)
return
}

if err.Error() != "please provide a valid sub-command for \"kn revision\"" {
t.Errorf("Bad error message %s", err.Error())
}
}
6 changes: 6 additions & 0 deletions pkg/kn/commands/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package service

import (
"errors"

"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
)
Expand All @@ -23,6 +25,10 @@ func NewServiceCommand(p *commands.KnParams) *cobra.Command {
serviceCmd := &cobra.Command{
Use: "service",
Short: "Service command group",
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error {
return errors.New("please provide a valid sub-command for \"kn service\"")
},
}
serviceCmd.AddCommand(NewServiceListCommand(p))
serviceCmd.AddCommand(NewServiceDescribeCommand(p))
Expand Down
66 changes: 66 additions & 0 deletions pkg/kn/commands/service/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright © 2018 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package service

import (
"strings"
"testing"

"github.com/knative/client/pkg/kn/commands"
v1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
client_testing "k8s.io/client-go/testing"
)

func fakeService(args []string, response *v1alpha1.ServiceList) (action client_testing.Action, output []string, err error) {
knParams := &commands.KnParams{}
cmd, fakeServing, buf := commands.CreateTestKnCommand(NewServiceCommand(knParams), knParams)
fakeServing.AddReactor("*", "*",
func(a client_testing.Action) (bool, runtime.Object, error) {
action = a
return true, response, nil
})
cmd.SetArgs(args)
err = cmd.Execute()
if err != nil {
return
}
output = strings.Split(buf.String(), "\n")
return
}

func TestUnknownSubcommand(t *testing.T) {
_, _, err := fakeService([]string{"service", "unknown"}, &v1alpha1.ServiceList{})
if err == nil {
t.Error(err)
return
}

if err.Error() != "unknown command \"unknown\" for \"kn service\"" {
t.Errorf("Bad error message %s", err.Error())
}
}

func TestEmptySubcommand(t *testing.T) {
_, _, err := fakeService([]string{"service"}, &v1alpha1.ServiceList{})
if err == nil {
t.Error(err)
return
}

if err.Error() != "please provide a valid sub-command for \"kn service\"" {
t.Errorf("Bad error message %s", err.Error())
}
}

0 comments on commit 1d8b1e3

Please sign in to comment.