From f2cd220e50b2f76e908895a5067a1e8b93862eb1 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Mon, 5 Sep 2022 12:24:52 +0200 Subject: [PATCH] Move DetectName to method in alizer package --- pkg/alizer/alizer.go | 16 ++++++++-------- pkg/alizer/alizer_test.go | 6 +++++- pkg/alizer/interface.go | 1 + pkg/alizer/mock.go | 15 +++++++++++++++ pkg/component/component.go | 3 ++- pkg/init/backend/alizer.go | 2 +- pkg/init/backend/interactive.go | 6 ++++-- pkg/init/backend/interactive_test.go | 12 ++++++++++++ pkg/init/init.go | 2 +- 9 files changed, 49 insertions(+), 14 deletions(-) diff --git a/pkg/alizer/alizer.go b/pkg/alizer/alizer.go index 540a71d9aee..c02c8ff3623 100644 --- a/pkg/alizer/alizer.go +++ b/pkg/alizer/alizer.go @@ -47,13 +47,6 @@ func (o *Alizer) DetectFramework(path string) (recognizer.DevFileType, api.Regis return types[typ], components.Items[typ].Registry, nil } -func GetDevfileLocationFromDetection(typ recognizer.DevFileType, registry api.Registry) *api.DevfileLocation { - return &api.DevfileLocation{ - Devfile: typ.Name, - DevfileRegistry: registry.Name, - } -} - // DetectName retrieves the name of the project (if available) // If source code is detected: // 1. Detect the name (pom.xml for java, package.json for nodejs, etc.) @@ -69,7 +62,7 @@ func GetDevfileLocationFromDetection(typ recognizer.DevFileType, registry api.Re // components, err := recognizer.DetectComponents("./") // In order to detect the name, the name will first try to find out the name based on the program (pom.xml, etc.) but then if not, it will use the dir name. -func DetectName(path string) (string, error) { +func (o *Alizer) DetectName(path string) (string, error) { if path == "" { return "", fmt.Errorf("path is empty") } @@ -120,3 +113,10 @@ func DetectName(path string) (string, error) { return name, nil } + +func GetDevfileLocationFromDetection(typ recognizer.DevFileType, registry api.Registry) *api.DevfileLocation { + return &api.DevfileLocation{ + Devfile: typ.Name, + DevfileRegistry: registry.Name, + } +} diff --git a/pkg/alizer/alizer_test.go b/pkg/alizer/alizer_test.go index c07375ec756..f5dc59e3af1 100644 --- a/pkg/alizer/alizer_test.go +++ b/pkg/alizer/alizer_test.go @@ -180,7 +180,11 @@ func TestDetectName(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - name, err := DetectName(tt.args.path) + ctrl := gomock.NewController(t) + registryClient := registry.NewMockClient(ctrl) + alizerClient := NewAlizerClient(registryClient) + + name, err := alizerClient.DetectName(tt.args.path) if !tt.wantErr == (err != nil) { t.Errorf("unexpected error %v, wantErr %v", err, tt.wantErr) diff --git a/pkg/alizer/interface.go b/pkg/alizer/interface.go index 94e55168b13..5a92ce93eb5 100644 --- a/pkg/alizer/interface.go +++ b/pkg/alizer/interface.go @@ -7,4 +7,5 @@ import ( type Client interface { DetectFramework(path string) (recognizer.DevFileType, api.Registry, error) + DetectName(path string) (string, error) } diff --git a/pkg/alizer/mock.go b/pkg/alizer/mock.go index 3c49dc8505b..83107cd6e5e 100644 --- a/pkg/alizer/mock.go +++ b/pkg/alizer/mock.go @@ -50,3 +50,18 @@ func (mr *MockClientMockRecorder) DetectFramework(path interface{}) *gomock.Call mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DetectFramework", reflect.TypeOf((*MockClient)(nil).DetectFramework), path) } + +// DetectName mocks base method. +func (m *MockClient) DetectName(path string) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DetectName", path) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DetectName indicates an expected call of DetectName. +func (mr *MockClientMockRecorder) DetectName(path interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DetectName", reflect.TypeOf((*MockClient)(nil).DetectName), path) +} diff --git a/pkg/component/component.go b/pkg/component/component.go index 0291480764d..f6df6a71be4 100644 --- a/pkg/component/component.go +++ b/pkg/component/component.go @@ -57,7 +57,8 @@ func GatherName(contextDir string, devfileObj *parser.DevfileObj) (string, error // Use Alizer if Devfile has no (optional) metadata.name field. // We need to pass in the Devfile base directory (not the path to the devfile.yaml). // Name returned by alizer.DetectName is expected to be already sanitized. - return alizer.DetectName(filepath.Dir(devfileObj.Ctx.GetAbsPath())) + alizerClient := alizer.Alizer{} // TODO(feloy) fix with DI + return alizerClient.DetectName(filepath.Dir(devfileObj.Ctx.GetAbsPath())) } } else { // Fallback to the context dir name diff --git a/pkg/init/backend/alizer.go b/pkg/init/backend/alizer.go index 2633cc35d4c..e4c96c529b3 100644 --- a/pkg/init/backend/alizer.go +++ b/pkg/init/backend/alizer.go @@ -58,7 +58,7 @@ func (o *AlizerBackend) PersonalizeName(devfile parser.DevfileObj, flags map[str if path == "" { return "", fmt.Errorf("cannot determine the absolute path of the directory") } - return alizer.DetectName(path) + return o.alizerClient.DetectName(path) } func (o *AlizerBackend) PersonalizeDevfileConfig(devfile parser.DevfileObj) (parser.DevfileObj, error) { diff --git a/pkg/init/backend/interactive.go b/pkg/init/backend/interactive.go index e53359e45a6..c1cc59e8c68 100644 --- a/pkg/init/backend/interactive.go +++ b/pkg/init/backend/interactive.go @@ -29,14 +29,16 @@ const ( type InteractiveBackend struct { askerClient asker.Asker registryClient registry.Client + alizerClient alizer.Client } var _ InitBackend = (*InteractiveBackend)(nil) -func NewInteractiveBackend(askerClient asker.Asker, registryClient registry.Client) *InteractiveBackend { +func NewInteractiveBackend(askerClient asker.Asker, registryClient registry.Client, alizerClient alizer.Client) *InteractiveBackend { return &InteractiveBackend{ askerClient: askerClient, registryClient: registryClient, + alizerClient: alizerClient, } } @@ -123,7 +125,7 @@ func (o *InteractiveBackend) PersonalizeName(devfile parser.DevfileObj, flags ma baseDir := filepath.Dir(path) // Detect the name - name, err := alizer.DetectName(baseDir) + name, err := o.alizerClient.DetectName(baseDir) if err != nil { return "", fmt.Errorf("detecting name using alizer: %w", err) } diff --git a/pkg/init/backend/interactive_test.go b/pkg/init/backend/interactive_test.go index 02877635998..db677bb19ff 100644 --- a/pkg/init/backend/interactive_test.go +++ b/pkg/init/backend/interactive_test.go @@ -6,6 +6,7 @@ import ( "github.com/golang/mock/gomock" + "github.com/redhat-developer/odo/pkg/alizer" "github.com/redhat-developer/odo/pkg/api" "github.com/redhat-developer/odo/pkg/init/asker" "github.com/redhat-developer/odo/pkg/registry" @@ -203,6 +204,7 @@ func TestInteractiveBackend_PersonalizeName(t *testing.T) { type fields struct { asker func(ctrl *gomock.Controller) asker.Asker registryClient registry.Client + alizer func(ctrl *gomock.Controller) alizer.Client } type args struct { devfile func(fs filesystem.Filesystem) parser.DevfileObj @@ -223,6 +225,11 @@ func TestInteractiveBackend_PersonalizeName(t *testing.T) { client.EXPECT().AskName(gomock.Any()).Return("aname", nil) return client }, + alizer: func(ctrl *gomock.Controller) alizer.Client { + client := alizer.NewMockClient(ctrl) + client.EXPECT().DetectName(gomock.Any()).Return("name1", nil) + return client + }, }, args: args{ devfile: func(fs filesystem.Filesystem) parser.DevfileObj { @@ -248,9 +255,14 @@ func TestInteractiveBackend_PersonalizeName(t *testing.T) { if tt.fields.asker != nil { askerClient = tt.fields.asker(ctrl) } + var alizerClient alizer.Client + if tt.fields.alizer != nil { + alizerClient = tt.fields.alizer(ctrl) + } o := &InteractiveBackend{ askerClient: askerClient, registryClient: tt.fields.registryClient, + alizerClient: alizerClient, } fs := filesystem.NewFakeFs() newName, err := o.PersonalizeName(tt.args.devfile(fs), tt.args.flags) diff --git a/pkg/init/init.go b/pkg/init/init.go index 8ba2cd0d239..0e97367cb1a 100644 --- a/pkg/init/init.go +++ b/pkg/init/init.go @@ -44,7 +44,7 @@ func NewInitClient(fsys filesystem.Filesystem, preferenceClient preference.Clien askerClient := asker.NewSurveyAsker() return &InitClient{ flagsBackend: backend.NewFlagsBackend(preferenceClient), - interactiveBackend: backend.NewInteractiveBackend(askerClient, registryClient), + interactiveBackend: backend.NewInteractiveBackend(askerClient, registryClient, alizerClient), alizerBackend: backend.NewAlizerBackend(askerClient, alizerClient), fsys: fsys, preferenceClient: preferenceClient,