From 1d8b1e3e0de65647cea67ab4ceb72c5a9c8e21d6 Mon Sep 17 00:00:00 2001
From: Michael Maximilien <maxim@us.ibm.com>
Date: Tue, 18 Jun 2019 13:44:22 -0700
Subject: [PATCH] Fixes issue #111 by responding with an error for unknown
 subcommands
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

```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.
---
 docs/cmd/kn_revision.md                       |  4 ++
 docs/cmd/kn_service.md                        |  4 ++
 pkg/kn/commands/revision/revision.go          |  6 ++
 .../revision/revision_describe_test.go        |  6 +-
 pkg/kn/commands/revision/revision_test.go     | 66 +++++++++++++++++++
 pkg/kn/commands/service/service.go            |  6 ++
 pkg/kn/commands/service/service_test.go       | 66 +++++++++++++++++++
 7 files changed, 155 insertions(+), 3 deletions(-)
 create mode 100644 pkg/kn/commands/revision/revision_test.go
 create mode 100644 pkg/kn/commands/service/service_test.go

diff --git a/docs/cmd/kn_revision.md b/docs/cmd/kn_revision.md
index 38468d4e69..e98e43a694 100644
--- a/docs/cmd/kn_revision.md
+++ b/docs/cmd/kn_revision.md
@@ -6,6 +6,10 @@ Revision command group
 
 Revision command group
 
+```
+kn revision [flags]
+```
+
 ### Options
 
 ```
diff --git a/docs/cmd/kn_service.md b/docs/cmd/kn_service.md
index 2823e3ae3b..e9bd3bd29b 100644
--- a/docs/cmd/kn_service.md
+++ b/docs/cmd/kn_service.md
@@ -6,6 +6,10 @@ Service command group
 
 Service command group
 
+```
+kn service [flags]
+```
+
 ### Options
 
 ```
diff --git a/pkg/kn/commands/revision/revision.go b/pkg/kn/commands/revision/revision.go
index 24519bfbe9..28b6624f2b 100644
--- a/pkg/kn/commands/revision/revision.go
+++ b/pkg/kn/commands/revision/revision.go
@@ -15,6 +15,8 @@
 package revision
 
 import (
+	"errors"
+
 	"github.com/knative/client/pkg/kn/commands"
 	"github.com/spf13/cobra"
 )
@@ -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))
diff --git a/pkg/kn/commands/revision/revision_describe_test.go b/pkg/kn/commands/revision/revision_describe_test.go
index 9c1b2af4e5..7e5e05839a 100644
--- a/pkg/kn/commands/revision/revision_describe_test.go
+++ b/pkg/kn/commands/revision/revision_describe_test.go
@@ -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("*", "*",
@@ -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")
@@ -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)
 	}
diff --git a/pkg/kn/commands/revision/revision_test.go b/pkg/kn/commands/revision/revision_test.go
new file mode 100644
index 0000000000..6a01957cc1
--- /dev/null
+++ b/pkg/kn/commands/revision/revision_test.go
@@ -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())
+	}
+}
diff --git a/pkg/kn/commands/service/service.go b/pkg/kn/commands/service/service.go
index 7a4ff8ef2a..643820619b 100644
--- a/pkg/kn/commands/service/service.go
+++ b/pkg/kn/commands/service/service.go
@@ -15,6 +15,8 @@
 package service
 
 import (
+	"errors"
+
 	"github.com/knative/client/pkg/kn/commands"
 	"github.com/spf13/cobra"
 )
@@ -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))
diff --git a/pkg/kn/commands/service/service_test.go b/pkg/kn/commands/service/service_test.go
new file mode 100644
index 0000000000..2d5913cee5
--- /dev/null
+++ b/pkg/kn/commands/service/service_test.go
@@ -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())
+	}
+}