Skip to content

Commit

Permalink
Added more unit tests and started cleaning up the code... for Meta re…
Browse files Browse the repository at this point in the history
…mote
  • Loading branch information
jnbdz committed Aug 27, 2024
1 parent 21d7596 commit cda3178
Show file tree
Hide file tree
Showing 6 changed files with 486 additions and 156 deletions.
162 changes: 96 additions & 66 deletions entity/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,78 +42,17 @@ func (b *Builder) MetaFromRemote(paths storage.AbsPaths, entityUri string) (enti
return entityVals, errors.New("invalid entity url")
}

var entityVersion string
//var entityVersion string
if strings.Contains(entityUri, "@") {
entityVersion, err := b.EntityVersion.Extract(entityUri)
if err != nil {
return entityVals, fmt.Errorf("error extracting version: %v", err)
}

entityUriWithoutVersion := url.TrimVersion(entityUri, entityVersion)
entityVals.RepoUrl, err = url.ExtractRepoUrl(entityUriWithoutVersion)
entityVals, err := b.metaFromRemoteWithVersion(entityUri)
if err != nil {
return entityVals, fmt.Errorf("error extracting repo url: %v", err)
return entityVals, err
}

entityVersionList, err := b.EntityVersion.List(entityVals.RepoUrl)
if err != nil {
return entityVals, fmt.Errorf("error listing versions: %v", err)
}

// TODO: If no tags (version) found then use pseudo version
if entityVersion == "latest" {
entityVersion, err = b.EntityVersion.Latest(entityVersionList)
if err != nil {
return entityVals, fmt.Errorf("error finding latest version: %v", err)
}
} else if !b.EntityVersionValidation.Format(entityVersion) {
return entityVals, fmt.Errorf("invalid entity version: %v", entityVersion)
} else if !b.EntityVersionValidation.Exists(entityVersion, entityVersionList) {
return entityVals, fmt.Errorf("invalid entity version: %v", entityVersion)
}

entityVals.Name = filepath.Base(entityUriWithoutVersion)
entityVals.Version = entityVersion
entityVals.Entity = entityUri
entityVals.Origin = strings.Replace(
entityVals.Entity,
fmt.Sprintf("%s@%s", entityVals.Name, entityVals.Version),
"",
1)
entityVals.IsPseudoVersion = false
} else {
repoUrl, err := url.ExtractRepoUrl(entityUri)
entityVals, err := b.metaFromRemoteWithoutVersion(entityUri)
if err != nil {
return entityVals, fmt.Errorf("error extracting repo url: %v", err)
}
entityVals.RepoUrl = repoUrl

entityVersionList, err := b.EntityVersion.List(entityVals.RepoUrl)
if err != nil {
return entityVals, fmt.Errorf("error listing versions: %v", err)
}

if len(entityVersionList) == 0 {
entityVersion, err = b.EntityVersion.GeneratePseudo(entityVals.RepoUrl)
if err != nil {
return entityVals, err
}
} else {
entityVersion, err = b.EntityVersion.Latest(entityVersionList)
if err != nil {
return entityVals, err
}
return entityVals, err
}

entityVals.Name = filepath.Base(entityUri)
entityVals.Version = entityVersion
entityVals.Entity = fmt.Sprintf("%s@%s", entityUri, entityVersion)
entityVals.Origin = strings.Replace(
entityVals.Entity,
fmt.Sprintf("%s@%s", entityVals.Name, entityVals.Version),
"",
1)
entityVals.IsPseudoVersion = true
}

entityVals.AbsPath = filepath.Join(paths.Entities, entityVals.Entity)
Expand All @@ -134,3 +73,94 @@ func (b *Builder) MetaFromRemote(paths storage.AbsPaths, entityUri string) (enti

return entityVals, nil
}

func (b *Builder) metaFromRemoteWithoutVersion(entityUri string) (entity.Entity, error) {
var entityVals = entity.Entity{
Have: false,
Exist: false,
}
var entityVersion string

repoUrl, err := url.ExtractRepoUrl(entityUri)
if err != nil {
return entityVals, fmt.Errorf("error extracting repo url: %v", err)
}
entityVals.RepoUrl = repoUrl

entityVersionList, err := b.EntityVersion.List(entityVals.RepoUrl)
if err != nil {
return entityVals, fmt.Errorf("error listing versions: %v", err)
}

if len(entityVersionList) == 0 {
entityVersion, err = b.EntityVersion.GeneratePseudo(entityVals.RepoUrl)
if err != nil {
return entityVals, err
}
} else {
entityVersion, err = b.EntityVersion.Latest(entityVersionList)
if err != nil {
return entityVals, err
}
}

entityVals.Name = filepath.Base(entityUri)
entityVals.Version = entityVersion
entityVals.Entity = fmt.Sprintf("%s@%s", entityUri, entityVersion)
entityVals.Origin = strings.Replace(
entityVals.Entity,
fmt.Sprintf("%s@%s", entityVals.Name, entityVals.Version),
"",
1)
entityVals.IsPseudoVersion = true

return entityVals, nil
}

func (b *Builder) metaFromRemoteWithVersion(entityUri string) (entity.Entity, error) {
var entityVals = entity.Entity{
Have: false,
Exist: false,
}
var entityVersion string

entityVersion, err := b.EntityVersion.Extract(entityUri)
if err != nil {
return entityVals, fmt.Errorf("error extracting version: %v", err)
}

entityUriWithoutVersion := url.TrimVersion(entityUri, entityVersion)
entityVals.RepoUrl, err = url.ExtractRepoUrl(entityUriWithoutVersion)
if err != nil {
return entityVals, fmt.Errorf("error extracting repo url: %v", err)
}

entityVersionList, err := b.EntityVersion.List(entityVals.RepoUrl)
if err != nil {
return entityVals, fmt.Errorf("error listing versions: %v", err)
}

// TODO: If no tags (version) found then use pseudo version
if entityVersion == "latest" {
entityVersion, err = b.EntityVersion.Latest(entityVersionList)
if err != nil {
return entityVals, fmt.Errorf("error finding latest version: %v", err)
}
} else if !b.EntityVersionValidation.Format(entityVersion) {
return entityVals, fmt.Errorf("invalid entity version: %v", entityVersion)
} else if !b.EntityVersionValidation.Exists(entityVersion, entityVersionList) {
return entityVals, fmt.Errorf("invalid entity version: %v", entityVersion)
}

entityVals.Name = filepath.Base(entityUriWithoutVersion)
entityVals.Version = entityVersion
entityVals.Entity = entityUri
entityVals.Origin = strings.Replace(
entityVals.Entity,
fmt.Sprintf("%s@%s", entityVals.Name, entityVals.Version),
"",
1)
entityVals.IsPseudoVersion = false

return entityVals, nil
}
34 changes: 34 additions & 0 deletions entity/build/build_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
package build

import (
"github.com/AmadlaOrg/hery/entity"
"reflect"
"testing"
)

func TestMetaFromRemoteWithoutVersion(t *testing.T) {
entityBuildService := NewEntityBuildService()

tests := []struct {
name string
entityUri string
expectEntity entity.Entity
expectError error
}{
{
name: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
entity, err := entityBuildService.metaFromRemoteWithoutVersion(tt.entityUri)
if err != nil {
return
}

if !reflect.DeepEqual(entity, tt.expectEntity) {
t.Errorf("got %v, want %v", entity, tt.expectEntity)
}
})
}
}
14 changes: 14 additions & 0 deletions entity/build/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package build

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewEntityBuildService(t *testing.T) {
t.Run("should return a new instance of Builder", func(t *testing.T) {
service := NewEntityBuildService()
assert.NotNil(t, service)
assert.IsType(t, &Builder{}, service)
})
}
14 changes: 14 additions & 0 deletions entity/version/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package version

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestNewEntityVersionService(t *testing.T) {
t.Run("should return a new instance of Entity Version", func(t *testing.T) {
entityVersionService := NewEntityVersionService()
assert.NotNil(t, entityVersionService)
assert.IsType(t, &Service{}, entityVersionService)
})
}
2 changes: 1 addition & 1 deletion entity/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Manager interface {

// Service struct implements the Manager interface.
type Service struct {
GitRemote *remote.GitRemote
GitRemote remote.RepoRemoteManager
}

// Extract extracts the version from a go get URI.
Expand Down
Loading

0 comments on commit cda3178

Please sign in to comment.