Runtime name should be base folder outside repos (#1598)
This commit is contained in:
parent
46d09ec2bc
commit
fccab8ad27
@ -274,20 +274,35 @@ func ParseSource(source string) (*Source, error) {
|
||||
}
|
||||
|
||||
// ParseSourceLocal detects and handles local pathes too
|
||||
// workdir should be used only from the CLI @todo better interface for this function
|
||||
func ParseSourceLocal(workDir, source string) (*Source, error) {
|
||||
// workdir should be used only from the CLI @todo better interface for this function.
|
||||
// PathExistsFunc exists only for testing purposes, to make the function side effect free.
|
||||
func ParseSourceLocal(workDir, source string, pathExistsFunc ...func(path string) (bool, error)) (*Source, error) {
|
||||
var pexists func(string) (bool, error)
|
||||
if len(pathExistsFunc) == 0 {
|
||||
pexists = pathExists
|
||||
} else {
|
||||
pexists = pathExistsFunc[0]
|
||||
}
|
||||
var localFullPath string
|
||||
if len(workDir) > 0 {
|
||||
localFullPath = filepath.Join(workDir, source)
|
||||
} else {
|
||||
localFullPath = source
|
||||
}
|
||||
if exists, err := pathExists(localFullPath); err == nil && exists {
|
||||
if exists, err := pexists(localFullPath); err == nil && exists {
|
||||
localRepoRoot, err := GetRepoRoot(localFullPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
folder := strings.ReplaceAll(localFullPath, localRepoRoot+string(filepath.Separator), "")
|
||||
var folder string
|
||||
// If the local repo root is a top level folder, we are not in a git repo.
|
||||
// In this case, we should take the last folder as folder name.
|
||||
if localRepoRoot == "" {
|
||||
folder = filepath.Base(localFullPath)
|
||||
} else {
|
||||
folder = strings.ReplaceAll(localFullPath, localRepoRoot+string(filepath.Separator), "")
|
||||
}
|
||||
|
||||
return &Source{
|
||||
Local: true,
|
||||
Folder: folder,
|
||||
|
@ -5,14 +5,14 @@ import (
|
||||
)
|
||||
|
||||
type parseCase struct {
|
||||
url string
|
||||
source string
|
||||
expected *Source
|
||||
}
|
||||
|
||||
func TestParseSource(t *testing.T) {
|
||||
cases := []parseCase{
|
||||
{
|
||||
url: "helloworld",
|
||||
source: "helloworld",
|
||||
expected: &Source{
|
||||
Repo: "github.com/micro/services",
|
||||
Folder: "helloworld",
|
||||
@ -20,7 +20,7 @@ func TestParseSource(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "github.com/micro/services/helloworld",
|
||||
source: "github.com/micro/services/helloworld",
|
||||
expected: &Source{
|
||||
Repo: "github.com/micro/services",
|
||||
Folder: "helloworld",
|
||||
@ -28,7 +28,7 @@ func TestParseSource(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "github.com/micro/services/helloworld@v1.12.1",
|
||||
source: "github.com/micro/services/helloworld@v1.12.1",
|
||||
expected: &Source{
|
||||
Repo: "github.com/micro/services",
|
||||
Folder: "helloworld",
|
||||
@ -36,7 +36,7 @@ func TestParseSource(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "github.com/micro/services/helloworld@branchname",
|
||||
source: "github.com/micro/services/helloworld@branchname",
|
||||
expected: &Source{
|
||||
Repo: "github.com/micro/services",
|
||||
Folder: "helloworld",
|
||||
@ -44,7 +44,7 @@ func TestParseSource(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
url: "github.com/crufter/reponame/helloworld@branchname",
|
||||
source: "github.com/crufter/reponame/helloworld@branchname",
|
||||
expected: &Source{
|
||||
Repo: "github.com/crufter/reponame",
|
||||
Folder: "helloworld",
|
||||
@ -53,7 +53,45 @@ func TestParseSource(t *testing.T) {
|
||||
},
|
||||
}
|
||||
for i, c := range cases {
|
||||
result, err := ParseSource(c.url)
|
||||
result, err := ParseSource(c.source)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed case %v: %v", i, err)
|
||||
}
|
||||
if result.Folder != c.expected.Folder {
|
||||
t.Fatalf("Folder does not match for '%v', expected '%v', got '%v'", i, c.expected.Folder, result.Folder)
|
||||
}
|
||||
if result.Repo != c.expected.Repo {
|
||||
t.Fatalf("Repo address does not match for '%v', expected '%v', got '%v'", i, c.expected.Repo, result.Repo)
|
||||
}
|
||||
if result.Ref != c.expected.Ref {
|
||||
t.Fatalf("Ref does not match for '%v', expected '%v', got '%v'", i, c.expected.Ref, result.Ref)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type localParseCase struct {
|
||||
source string
|
||||
expected *Source
|
||||
workDir string
|
||||
pathExists bool
|
||||
}
|
||||
|
||||
func TestLocalParseSource(t *testing.T) {
|
||||
cases := []localParseCase{
|
||||
{
|
||||
source: ".",
|
||||
expected: &Source{
|
||||
Folder: "folder2",
|
||||
Ref: "latest",
|
||||
},
|
||||
workDir: "/folder1/folder2",
|
||||
pathExists: true,
|
||||
},
|
||||
}
|
||||
for i, c := range cases {
|
||||
result, err := ParseSourceLocal(c.workDir, c.source, func(s string) (bool, error) {
|
||||
return c.pathExists, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed case %v: %v", i, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user