From 652d7c4cb7b88e38e7c6a5bbdcca73cce46bd947 Mon Sep 17 00:00:00 2001
From: bryanl <bryanliles@gmail.com>
Date: Tue, 27 Mar 2018 21:30:03 -0400
Subject: [PATCH] removing need for github access for registry tests

Signed-off-by: bryanl <bryanliles@gmail.com>
---
 actions/init.go              | 32 +++++++++++++++++++-------------
 actions/init_test.go         |  9 +++++++++
 pkg/registry/manager_test.go | 34 +++++++++++++++++++++++-----------
 3 files changed, 51 insertions(+), 24 deletions(-)

diff --git a/actions/init.go b/actions/init.go
index 88255397..b2d6e9af 100644
--- a/actions/init.go
+++ b/actions/init.go
@@ -38,6 +38,7 @@ func RunInit(fs afero.Fs, name, rootPath, k8sSpecFlag, serverURI, namespace stri
 }
 
 type appInitFn func(fs afero.Fs, name, rootPath, k8sSpecFlag, serverURI, namespace string, registries []registry.Registry) error
+type initIncubatorFn func() (registry.Registry, error)
 
 // Init creates a component namespace
 type Init struct {
@@ -48,19 +49,21 @@ type Init struct {
 	serverURI   string
 	namespace   string
 
-	appInitFn appInitFn
+	appInitFn       appInitFn
+	initIncubatorFn initIncubatorFn
 }
 
 // NewInit creates an instance of Init.
 func NewInit(fs afero.Fs, name, rootPath, k8sSpecFlag, serverURI, namespace string) (*Init, error) {
 	i := &Init{
-		fs:          fs,
-		name:        name,
-		rootPath:    rootPath,
-		k8sSpecFlag: k8sSpecFlag,
-		serverURI:   serverURI,
-		namespace:   namespace,
-		appInitFn:   appinit.Init,
+		fs:              fs,
+		name:            name,
+		rootPath:        rootPath,
+		k8sSpecFlag:     k8sSpecFlag,
+		serverURI:       serverURI,
+		namespace:       namespace,
+		appInitFn:       appinit.Init,
+		initIncubatorFn: initIncubator,
 	}
 
 	return i, nil
@@ -68,11 +71,7 @@ func NewInit(fs afero.Fs, name, rootPath, k8sSpecFlag, serverURI, namespace stri
 
 // Run runs that ns create action.
 func (i *Init) Run() error {
-	gh, err := registry.NewGitHub(&app.RegistryRefSpec{
-		Name:     "incubator",
-		Protocol: registry.ProtocolGitHub,
-		URI:      defaultIncubatorURI,
-	})
+	gh, err := i.initIncubatorFn()
 	if err != nil {
 		return err
 	}
@@ -88,5 +87,12 @@ func (i *Init) Run() error {
 		i.namespace,
 		registries,
 	)
+}
 
+func initIncubator() (registry.Registry, error) {
+	return registry.NewGitHub(&app.RegistryRefSpec{
+		Name:     "incubator",
+		Protocol: registry.ProtocolGitHub,
+		URI:      defaultIncubatorURI,
+	})
 }
diff --git a/actions/init_test.go b/actions/init_test.go
index 7474bd48..e96c913c 100644
--- a/actions/init_test.go
+++ b/actions/init_test.go
@@ -20,6 +20,7 @@ import (
 
 	amocks "github.com/ksonnet/ksonnet/metadata/app/mocks"
 	"github.com/ksonnet/ksonnet/pkg/registry"
+	rmocks "github.com/ksonnet/ksonnet/pkg/registry/mocks"
 	"github.com/spf13/afero"
 	"github.com/stretchr/testify/assert"
 	"github.com/stretchr/testify/require"
@@ -55,6 +56,14 @@ func TestInit(t *testing.T) {
 			return nil
 		}
 
+		a.initIncubatorFn = func() (registry.Registry, error) {
+			r := &rmocks.Registry{}
+			r.On("Protocol").Return("github")
+			r.On("URI").Return("github.com/ksonnet/parts/tree/master/incubator")
+			r.On("Name").Return("incubator")
+			return r, nil
+		}
+
 		err = a.Run()
 		require.NoError(t, err)
 	})
diff --git a/pkg/registry/manager_test.go b/pkg/registry/manager_test.go
index 983c08f3..8e51ec36 100644
--- a/pkg/registry/manager_test.go
+++ b/pkg/registry/manager_test.go
@@ -60,18 +60,30 @@ func Test_Package(t *testing.T) {
 }
 
 func Test_List(t *testing.T) {
-	specs := app.RegistryRefSpecs{
-		"incubator": &app.RegistryRefSpec{
-			Protocol: ProtocolGitHub,
-			URI:      "github.com/ksonnet/parts/tree/master/incubator",
-		},
-	}
+	withApp(t, func(a *mocks.App, fs afero.Fs) {
+		c := &ghmocks.GitHub{}
+		c.On("CommitSHA1", mock.Anything, github.Repo{Org: "ksonnet", Repo: "parts"}, mock.AnythingOfType("string")).
+			Return("12345", nil)
 
-	appMock := &mocks.App{}
-	appMock.On("Registries").Return(specs, nil)
+		ghcOpt := GitHubClient(c)
+		githubFactory = func(spec *app.RegistryRefSpec) (*GitHub, error) {
+			return NewGitHub(spec, ghcOpt)
+		}
 
-	registries, err := List(appMock)
-	require.NoError(t, err)
+		specs := app.RegistryRefSpecs{
+			"incubator": &app.RegistryRefSpec{
+				Protocol: ProtocolGitHub,
+				URI:      "github.com/ksonnet/parts/tree/master/incubator",
+			},
+		}
 
-	require.Len(t, registries, 1)
+		appMock := &mocks.App{}
+		appMock.On("Registries").Return(specs, nil)
+
+		registries, err := List(appMock)
+		require.NoError(t, err)
+
+		require.Len(t, registries, 1)
+
+	})
 }
-- 
GitLab