Repository: openshift-pipelines/pipelines-as-code
Pull request #163 (2021-06-29 12:10:39)

Move completion to pkg/cmd and add test
Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>



Patch:
diff --git a/cmd/tkn-pac/main.go b/cmd/tkn-pac/main.go
index 1c93039b..028e9d08 100644
--- a/cmd/tkn-pac/main.go
+++ b/cmd/tkn-pac/main.go
@@ -4,7 +4,7 @@ import (
 	"os"
 
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
-	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/completion"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/completion"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/repository"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/resolve"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/flags"
diff --git a/pkg/cli/completion/cmd.go b/pkg/cmd/completion/cmd.go
similarity index 90%
rename from pkg/cli/completion/cmd.go
rename to pkg/cmd/completion/cmd.go
index 98c2aa7c..a5c207b8 100644
--- a/pkg/cli/completion/cmd.go
+++ b/pkg/cmd/completion/cmd.go
@@ -15,8 +15,6 @@
 package completion
 
 import (
-	"os"
-
 	"github.com/spf13/cobra"
 )
 
@@ -78,13 +76,13 @@ func Command() *cobra.Command {
 		RunE: func(cmd *cobra.Command, args []string) error {
 			switch args[0] {
 			case "bash":
-				_ = cmd.Root().GenBashCompletion(os.Stdout)
+				_ = cmd.Root().GenBashCompletion(cmd.OutOrStdout())
 			case "zsh":
-				_ = cmd.Root().GenZshCompletion(os.Stdout)
+				_ = cmd.Root().GenZshCompletion(cmd.OutOrStdout())
 			case "fish":
-				_ = cmd.Root().GenFishCompletion(os.Stdout, true)
+				_ = cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true)
 			case "powershell":
-				_ = cmd.Root().GenPowerShellCompletion(os.Stdout)
+				_ = cmd.Root().GenPowerShellCompletion(cmd.OutOrStdout())
 			}
 
 			return nil
diff --git a/pkg/cli/completion/completion.go b/pkg/cmd/completion/completion.go
similarity index 100%
rename from pkg/cli/completion/completion.go
rename to pkg/cmd/completion/completion.go
diff --git a/pkg/cmd/repository/describe.go b/pkg/cmd/repository/describe.go
index 88b69028..cd37e881 100644
--- a/pkg/cmd/repository/describe.go
+++ b/pkg/cmd/repository/describe.go
@@ -11,8 +11,8 @@ import (
 	"github.com/jonboulle/clockwork"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
-	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/completion"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/ui"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/completion"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/flags"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/pipelineascode"
 	"github.com/spf13/cobra"
diff --git a/pkg/cmd/repository/list.go b/pkg/cmd/repository/list.go
index e0beda58..8c4cbe7e 100644
--- a/pkg/cmd/repository/list.go
+++ b/pkg/cmd/repository/list.go
@@ -6,8 +6,8 @@ import (
 	"text/tabwriter"
 
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
-	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/completion"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/ui"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/cmd/completion"
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/flags"
 	"github.com/spf13/cobra"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"


Test patch:
diff --git a/pkg/cmd/completion/cmd_test.go b/pkg/cmd/completion/cmd_test.go
new file mode 100644
index 00000000..dd27e149
--- /dev/null
+++ b/pkg/cmd/completion/cmd_test.go
@@ -0,0 +1,36 @@
+package completion
+
+import (
+	"strings"
+	"testing"
+
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/test/cli"
+	"gotest.tools/v3/assert"
+)
+
+func TestCommand(t *testing.T) {
+	tests := []struct {
+		name string
+	}{
+		{
+			name: "zsh",
+		},
+		{
+			name: "bash",
+		},
+		{
+			name: "fish",
+		},
+		{
+			name: "powershell",
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name+" completion", func(t *testing.T) {
+			got, err := cli.ExecuteCommand(Command(), tt.name)
+			assert.NilError(t, err)
+
+			assert.Assert(t, strings.Contains(got, tt.name+" completion"))
+		})
+	}
+}
diff --git a/pkg/test/cli/cobra.go b/pkg/test/cli/cobra.go
new file mode 100644
index 00000000..c176f3b9
--- /dev/null
+++ b/pkg/test/cli/cobra.go
@@ -0,0 +1,27 @@
+package cli
+
+import (
+	"bytes"
+
+	"github.com/spf13/cobra"
+)
+
+// ExecuteCommand executes the root command passing the args and returns
+// the output as a string and error
+func ExecuteCommand(root *cobra.Command, args ...string) (string, error) {
+	_, output, err := ExecuteCommandC(root, args...)
+	return output, err
+}
+
+// ExecuteCommandC executes the root command passing the args and returns
+// the root command, output as a string and error if any
+func ExecuteCommandC(c *cobra.Command, args ...string) (*cobra.Command, string, error) {
+	buf := new(bytes.Buffer)
+	c.SetOutput(buf)
+	c.SetArgs(args)
+	c.SilenceUsage = true
+
+	root, err := c.ExecuteC()
+
+	return root, buf.String(), err
+}