Skip to content

Commit

Permalink
Continued with the clean up. Also, added some TODOs and unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbdz committed Sep 6, 2024
1 parent f591e1f commit b836f1f
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 94 deletions.
46 changes: 29 additions & 17 deletions entity/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package build
import (
"errors"
"fmt"
"github.com/AmadlaOrg/hery/errtypes"
"github.com/google/uuid"
"path/filepath"
"strings"
Expand All @@ -19,10 +18,11 @@ import (

// IBuild to help with mocking and to gather metadata from remote and local sources.
type IBuild interface {
MetaFromRemote(paths storage.AbsPaths, entityUri string) (entity.Entity, error)
Meta(paths storage.AbsPaths, entityUri string) (entity.Entity, error)
metaFromLocalWithVersion(entityUri, entityVersion string) (entity.Entity, error)
metaFromRemoteWithoutVersion(entityUri string) (entity.Entity, error)
metaFromRemoteWithVersion(entityUri, entityVersion string) (entity.Entity, error)
constructOrigin(entityUri, name, version string) string
}

// SBuild struct implements the MetaBuilder interface.
Expand All @@ -39,9 +39,9 @@ var (
uuidNew = uuid.New
)

// MetaFromRemote gathers as many details about an Entity as possible from git and from the URI passed to populate the
// Meta gathers as many details about an Entity as possible from git and from the URI passed to populate the
// Entity struct. It also validates values that are passed to it.
func (s *SBuild) MetaFromRemote(paths storage.AbsPaths, entityUri string) (entity.Entity, error) {
func (s *SBuild) Meta(paths storage.AbsPaths, entityUri string) (entity.Entity, error) {
var (
entityVals = entity.Entity{
Have: false,
Expand All @@ -55,8 +55,8 @@ func (s *SBuild) MetaFromRemote(paths storage.AbsPaths, entityUri string) (entit
}

dir, err := s.Entity.FindEntityDir(paths, entityVals)
if !errors.Is(err, errtypes.NotFoundError) &&
!errors.Is(err, errtypes.MultipleFoundError) &&
if !errors.Is(err, entity.ErrorNotFound) &&
!errors.Is(err, entity.ErrorMultipleFound) &&
err != nil {
return entityVals, err
} else if err == nil {
Expand All @@ -74,7 +74,7 @@ func (s *SBuild) MetaFromRemote(paths storage.AbsPaths, entityUri string) (entit
}
}

if errors.Is(err, errtypes.NotFoundError) || entityVersion == "latest" {
if errors.Is(err, entity.ErrorNotFound) || entityVersion == "latest" {
entityVals, err = s.metaFromRemoteWithVersion(entityUri, entityVersion)
if err != nil {
return entityVals, err
Expand Down Expand Up @@ -115,6 +115,17 @@ func (s *SBuild) metaFromLocalWithVersion(entityUri, entityVersion string) (enti
return entityVals, fmt.Errorf("error extracting repo url: %v", err)
}

// TODO: Get hash
// entityVals.Hash

entityVals.Have = true
entityVals.Exist = true
entityVals.IsPseudoVersion = false
entityVals.Name = filepath.Base(entityUriWithoutVersion)
entityVals.Version = entityVersion
entityVals.Entity = entityUri
entityVals.Origin = s.constructOrigin(entityVals.Entity, entityVals.Name, entityVals.Version)

return entityVals, nil
}

Expand Down Expand Up @@ -156,11 +167,7 @@ func (s *SBuild) metaFromRemoteWithoutVersion(entityUri string) (entity.Entity,
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.Origin = s.constructOrigin(entityVals.Entity, entityVals.Name, entityVals.Version)

return entityVals, nil
}
Expand Down Expand Up @@ -210,11 +217,16 @@ func (s *SBuild) metaFromRemoteWithVersion(entityUri, entityVersion string) (ent
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.Origin = s.constructOrigin(entityVals.Entity, entityVals.Name, entityVals.Version)

return entityVals, nil
}

// constructOrigin
func (s *SBuild) constructOrigin(entityUri, name, version string) string {
return strings.Replace(
entityUri,
fmt.Sprintf("%s@%s", name, version),
"",
1)
}
31 changes: 29 additions & 2 deletions entity/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"testing"
)

func TestMetaFromRemote(t *testing.T) {
func TestMeta(t *testing.T) {
// Mocking UUID generation for consistent results
originalUUIDNew := uuidNew
defer func() { uuidNew = originalUUIDNew }() // Restore original function after test
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestMetaFromRemote(t *testing.T) {
EntityVersionValidation: mockEntityVersionVal,
}

metaFromRemote, err := mockBuilder.MetaFromRemote(test.inputPaths, test.inputEntityUri)
metaFromRemote, err := mockBuilder.Meta(test.inputPaths, test.inputEntityUri)
if test.hasError {
assert.Error(t, err)
} else {
Expand Down Expand Up @@ -641,3 +641,30 @@ func TestMetaFromRemoteWithVersion(t *testing.T) {
})
}
}

func TestConstructOrigin(t *testing.T) {
mockBuilder := SBuild{}

tests := []struct {
name string
inputEntityUri string
inputName string
inputVersion string
expected string
}{
{
name: "Valid origin",
inputEntityUri: "github.com/AmadlaOrg/Entity",
inputName: "github.com/AmadlaOrg/Entity@latest",
inputVersion: "latest",
expected: "github.com/AmadlaOrg/Entity",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := mockBuilder.constructOrigin(tt.inputEntityUri, tt.inputName, tt.inputVersion)
assert.Equal(t, tt.expected, got)
})
}
}
7 changes: 3 additions & 4 deletions entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"github.com/AmadlaOrg/hery/entity/version"
versionValidationPkg "github.com/AmadlaOrg/hery/entity/version/validation"
"github.com/AmadlaOrg/hery/errtypes"
"github.com/AmadlaOrg/hery/storage"
"os"
"path/filepath"
Expand All @@ -30,7 +29,7 @@ func (s *SEntity) FindEntityDir(paths storage.AbsPaths, entityVals Entity) (stri
// Check if the directory exists
if _, err := os.Stat(exactPath); os.IsNotExist(err) {
return "", errors.Join(
errtypes.NotFoundError,
ErrorNotFound,
fmt.Errorf("no matching directory found for exact version: %s", exactPath))
} else if err != nil {
return "", err
Expand All @@ -52,13 +51,13 @@ func (s *SEntity) FindEntityDir(paths storage.AbsPaths, entityVals Entity) (stri

if len(matches) == 0 {
return "", errors.Join(
errtypes.NotFoundError,
ErrorNotFound,
fmt.Errorf("no matching directories found for pattern: %s", pattern))
}

if len(matches) > 1 {
return "", errors.Join(
errtypes.MultipleFoundError,
ErrorMultipleFound,
fmt.Errorf("multiple matching directories found for pattern: %s", pattern))
}

Expand Down
8 changes: 8 additions & 0 deletions entity/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package entity

import "errors"

var (
ErrorNotFound = errors.New("not found")
ErrorMultipleFound = errors.New("multiple found")
)
Loading

0 comments on commit b836f1f

Please sign in to comment.