Repository: openshift-pipelines/pipelines-as-code
Pull request #168 (2021-06-29 20:14:46)

More testing of commands
Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>



Patch:
diff --git a/pkg/cmd/repository/create.go b/pkg/cmd/repository/create.go
index dfeefd74..1012e7bb 100644
--- a/pkg/cmd/repository/create.go
+++ b/pkg/cmd/repository/create.go
@@ -34,7 +34,7 @@ type CreateOptions struct {
 	AssumeYes bool
 }
 
-func CreateCommand(p cli.Params) *cobra.Command {
+func CreateCommand(p cli.Params, ioStreams *ui.IOStreams) *cobra.Command {
 	createOpts := CreateOptions{}
 	cmd := &cobra.Command{
 		Use:     "create",
@@ -42,8 +42,7 @@ func CreateCommand(p cli.Params) *cobra.Command {
 		Short:   "Create  a repository",
 		RunE: func(cmd *cobra.Command, args []string) error {
 			var err error
-
-			createOpts.IOStreams = ui.NewIOStreams()
+			createOpts.IOStreams = ioStreams
 			createOpts.CLIOpts, err = flags.NewCliOptions(cmd)
 			if err != nil {
 				return err
diff --git a/pkg/cmd/repository/describe.go b/pkg/cmd/repository/describe.go
index cd37e881..450701ec 100644
--- a/pkg/cmd/repository/describe.go
+++ b/pkg/cmd/repository/describe.go
@@ -117,10 +117,10 @@ func askRepo(ctx context.Context, cs *cli.Clients, opts *flags.CliOpts, namespac
 		}
 	}
 
-	return nil, fmt.Errorf("cannot match repository???")
+	return nil, fmt.Errorf("cannot match repository")
 }
 
-func DescribeCommand(p cli.Params) *cobra.Command {
+func DescribeCommand(p cli.Params, streams *ui.IOStreams) *cobra.Command {
 	cmd := &cobra.Command{
 		Use:     "describe",
 		Aliases: []string{"desc"},
@@ -151,11 +151,11 @@ func DescribeCommand(p cli.Params) *cobra.Command {
 			if err != nil {
 				return err
 			}
-			ioStreams := ui.NewIOStreams()
-			ioStreams.SetColorEnabled(!opts.NoColoring)
-			return describe(ctx, cs, clock, opts, ioStreams, p.GetNamespace(), repoName)
+			streams.SetColorEnabled(!opts.NoColoring)
+			return describe(ctx, cs, clock, opts, streams, p.GetNamespace(), repoName)
 		},
 	}
+	flags.AddPacCliOptions(cmd)
 	cmd.Flags().StringP(
 		namespaceFlag, "n", "", "If present, the namespace scope for this CLI request")
 
diff --git a/pkg/cmd/repository/list.go b/pkg/cmd/repository/list.go
index a8ace677..b0c15b60 100644
--- a/pkg/cmd/repository/list.go
+++ b/pkg/cmd/repository/list.go
@@ -21,7 +21,7 @@ var (
 	namespaceFlag     = "namespace"
 )
 
-func ListCommand(p cli.Params) *cobra.Command {
+func ListCommand(p cli.Params, ioStreams *ui.IOStreams) *cobra.Command {
 	var noheaders, allNamespaces bool
 	var selectors string
 
@@ -30,7 +30,6 @@ func ListCommand(p cli.Params) *cobra.Command {
 		Aliases: []string{"ls"},
 		Short:   "List repositories",
 		RunE: func(cmd *cobra.Command, args []string) error {
-			ioStreams := ui.NewIOStreams()
 			opts, err := flags.NewCliOptions(cmd)
 			if err != nil {
 				return err
@@ -56,6 +55,7 @@ func ListCommand(p cli.Params) *cobra.Command {
 		},
 	}
 
+	flags.AddPacCliOptions(cmd)
 	cmd.PersistentFlags().BoolVarP(&allNamespaces, allNamespacesFlag, "A", false, "If present, "+
 		"list the repository across all namespaces. Namespace in current context is ignored even if specified with"+
 		" --namespace.")
diff --git a/pkg/cmd/repository/root.go b/pkg/cmd/repository/root.go
index c5249461..4c0b9d4c 100644
--- a/pkg/cmd/repository/root.go
+++ b/pkg/cmd/repository/root.go
@@ -2,6 +2,7 @@ package repository
 
 import (
 	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/cli/ui"
 	"github.com/spf13/cobra"
 )
 
@@ -13,10 +14,11 @@ func Root(p cli.Params) *cobra.Command {
 		Long:         `Manage Pipeline as Code repositories`,
 		SilenceUsage: true,
 	}
+	ioStreams := ui.NewIOStreams()
 
-	cmd.AddCommand(ListCommand(p))
-	cmd.AddCommand(DescribeCommand(p))
-	cmd.AddCommand(CreateCommand(p))
+	cmd.AddCommand(ListCommand(p, ioStreams))
+	cmd.AddCommand(DescribeCommand(p, ioStreams))
+	cmd.AddCommand(CreateCommand(p, ioStreams))
 
 	return cmd
 }


Test patch:
diff --git a/pkg/cmd/repository/root_test.go b/pkg/cmd/repository/root_test.go
index b161a29e..08a53444 100644
--- a/pkg/cmd/repository/root_test.go
+++ b/pkg/cmd/repository/root_test.go
@@ -1,12 +1,28 @@
 package repository
 
 import (
+	"bytes"
+	"fmt"
 	"strings"
 	"testing"
+	"time"
 
+	"github.com/google/go-github/v35/github"
+	"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/ui"
 	tcli "github.com/openshift-pipelines/pipelines-as-code/pkg/test/cli"
+	testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
+	"github.com/openshift-pipelines/pipelines-as-code/pkg/test/params"
+	"github.com/spf13/cobra"
 	"gotest.tools/v3/assert"
+	"gotest.tools/v3/golden"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	knativeapis "knative.dev/pkg/apis"
+	"knative.dev/pkg/apis/duck/v1beta1"
+	rtesting "knative.dev/pkg/reconciler/testing"
 )
 
 func TestRoot(t *testing.T) {
@@ -14,3 +30,148 @@ func TestRoot(t *testing.T) {
 	assert.NilError(t, err)
 	assert.Assert(t, strings.Contains(s, "repository, repo, repsitories"))
 }
+
+func TestCommands(t *testing.T) {
+	tests := []struct {
+		name    string
+		command func(p cli.Params, ioStreams *ui.IOStreams) *cobra.Command
+		want    *cobra.Command
+	}{
+		{
+			name:    "List",
+			command: ListCommand,
+		},
+		{
+			name:    "Describe",
+			command: DescribeCommand,
+		},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			cw := clockwork.NewFakeClock()
+			const (
+				nsName   = "ns"
+				repoName = "repo1"
+			)
+			statuses := []v1alpha1.RepositoryRunStatus{
+				{
+					Status: v1beta1.Status{
+						Conditions: []knativeapis.Condition{
+							{
+								Reason: "Success",
+							},
+						},
+					},
+					PipelineRunName: "pipelinerun1",
+					StartTime:       &metav1.Time{Time: cw.Now().Add(-16 * time.Minute)},
+					CompletionTime:  &metav1.Time{Time: cw.Now().Add(-15 * time.Minute)},
+					SHA:             github.String("SHA"),
+					Title:           github.String("A title"),
+				},
+			}
+			repositories := []*v1alpha1.Repository{
+				{
+					ObjectMeta: metav1.ObjectMeta{
+						Name:      repoName,
+						Namespace: nsName,
+					},
+					Spec: v1alpha1.RepositorySpec{
+						Namespace: nsName,
+						URL:       "https://anurl.com/repo/owner",
+						Branch:    "branch",
+						EventType: "pull_request",
+					},
+					Status: statuses,
+				},
+			}
+			tdata := testclient.Data{
+				Namespaces: []*corev1.Namespace{
+					{
+						ObjectMeta: metav1.ObjectMeta{
+							Name: nsName,
+						},
+					},
+				},
+				Repositories: repositories,
+			}
+			ctx, _ := rtesting.SetupFakeContext(t)
+			stdata, _ := testclient.SeedTestData(t, ctx, tdata)
+			cs := &cli.Clients{
+				PipelineAsCode: stdata.PipelineAsCode,
+			}
+			buf := new(bytes.Buffer)
+			ioStream := &ui.IOStreams{Out: buf, ErrOut: buf}
+			cmd := tt.command(params.FakeParams{
+				Fakeclients: cs,
+			}, ioStream)
+			cmd.SetOut(buf)
+			_, err := tcli.ExecuteCommand(cmd, "--no-color", "-n", nsName, repoName)
+			assert.NilError(t, err)
+
+			golden.Assert(t, buf.String(), strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-"))
+		})
+	}
+}
+
+func TESSS(t *testing.T) {
+	cw := clockwork.NewFakeClock()
+	const (
+		nsName   = "ns"
+		repoName = "repo1"
+	)
+	statuses := []v1alpha1.RepositoryRunStatus{
+		{
+			Status: v1beta1.Status{
+				Conditions: []knativeapis.Condition{
+					{
+						Reason: "Success",
+					},
+				},
+			},
+			PipelineRunName: "pipelinerun1",
+			StartTime:       &metav1.Time{Time: cw.Now().Add(-16 * time.Minute)},
+			CompletionTime:  &metav1.Time{Time: cw.Now().Add(-15 * time.Minute)},
+			SHA:             github.String("SHA"),
+			Title:           github.String("A title"),
+		},
+	}
+	repositories := []*v1alpha1.Repository{
+		{
+			ObjectMeta: metav1.ObjectMeta{
+				Name:      repoName,
+				Namespace: nsName,
+			},
+			Spec: v1alpha1.RepositorySpec{
+				Namespace: nsName,
+				URL:       "https://anurl.com/repo/owner",
+				Branch:    "branch",
+				EventType: "pull_request",
+			},
+			Status: statuses,
+		},
+	}
+	tdata := testclient.Data{
+		Namespaces: []*corev1.Namespace{
+			{
+				ObjectMeta: metav1.ObjectMeta{
+					Name: nsName,
+				},
+			},
+		},
+		Repositories: repositories,
+	}
+	ctx, _ := rtesting.SetupFakeContext(t)
+	stdata, _ := testclient.SeedTestData(t, ctx, tdata)
+	cs := &cli.Clients{
+		PipelineAsCode: stdata.PipelineAsCode,
+	}
+	buf := new(bytes.Buffer)
+	cmd := ListCommand(params.FakeParams{
+		Fakeclients: cs,
+	}, &ui.IOStreams{Out: buf, ErrOut: buf})
+	cmd.SetOut(buf)
+	_, err := tcli.ExecuteCommand(cmd, "-n", nsName, repoName)
+	assert.NilError(t, err)
+
+	golden.Assert(t, buf.String(), strings.ReplaceAll(fmt.Sprintf("%s.golden", t.Name()), "/", "-"))
+}
diff --git a/pkg/cmd/repository/testdata/TestCommands-Describe.golden b/pkg/cmd/repository/testdata/TestCommands-Describe.golden
new file mode 100644
index 00000000..718c1a76
--- /dev/null
+++ b/pkg/cmd/repository/testdata/TestCommands-Describe.golden
@@ -0,0 +1,15 @@
+Name:            repo1
+Namespace:       ns
+URL:             https://anurl.com/repo/owner
+Event Type:      PullRequest
+Target Branch:   branch
+
+Last Run:
+
+PipelineRun:    pipelinerun1
+Status:         Success
+Commit:         https://anurl.com/repo/owner/commit/SHA
+Commit Title:   A title
+StartTime:      37 years ago
+Duration:       1 minute
+
diff --git a/pkg/cmd/repository/testdata/TestCommands-List.golden b/pkg/cmd/repository/testdata/TestCommands-List.golden
new file mode 100644
index 00000000..3955d98b
--- /dev/null
+++ b/pkg/cmd/repository/testdata/TestCommands-List.golden
@@ -0,0 +1,2 @@
+NAME    AGE            OWNER/REPOSITORY   SHA   EVENT-TYPE     STATUS
+repo1   37 years ago   repo/owner         SHA   pull_request   Success