Skip to content

Commit

Permalink
Move DetectName to method in alizer package (#6093)
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy authored Sep 6, 2022
1 parent 588472e commit 3add393
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 14 deletions.
16 changes: 8 additions & 8 deletions pkg/alizer/alizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand All @@ -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")
}
Expand Down Expand Up @@ -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,
}
}
6 changes: 5 additions & 1 deletion pkg/alizer/alizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/alizer/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import (

type Client interface {
DetectFramework(path string) (recognizer.DevFileType, api.Registry, error)
DetectName(path string) (string, error)
}
15 changes: 15 additions & 0 deletions pkg/alizer/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/init/backend/alizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/init/backend/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/init/backend/interactive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3add393

Please sign in to comment.