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
|
// ParseSourceLocal detects and handles local pathes too
|
||||||
// workdir should be used only from the CLI @todo better interface for this function
|
// workdir should be used only from the CLI @todo better interface for this function.
|
||||||
func ParseSourceLocal(workDir, source string) (*Source, error) {
|
// 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
|
var localFullPath string
|
||||||
if len(workDir) > 0 {
|
if len(workDir) > 0 {
|
||||||
localFullPath = filepath.Join(workDir, source)
|
localFullPath = filepath.Join(workDir, source)
|
||||||
} else {
|
} else {
|
||||||
localFullPath = source
|
localFullPath = source
|
||||||
}
|
}
|
||||||
if exists, err := pathExists(localFullPath); err == nil && exists {
|
if exists, err := pexists(localFullPath); err == nil && exists {
|
||||||
localRepoRoot, err := GetRepoRoot(localFullPath)
|
localRepoRoot, err := GetRepoRoot(localFullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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{
|
return &Source{
|
||||||
Local: true,
|
Local: true,
|
||||||
Folder: folder,
|
Folder: folder,
|
||||||
|
@ -5,14 +5,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type parseCase struct {
|
type parseCase struct {
|
||||||
url string
|
source string
|
||||||
expected *Source
|
expected *Source
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseSource(t *testing.T) {
|
func TestParseSource(t *testing.T) {
|
||||||
cases := []parseCase{
|
cases := []parseCase{
|
||||||
{
|
{
|
||||||
url: "helloworld",
|
source: "helloworld",
|
||||||
expected: &Source{
|
expected: &Source{
|
||||||
Repo: "github.com/micro/services",
|
Repo: "github.com/micro/services",
|
||||||
Folder: "helloworld",
|
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{
|
expected: &Source{
|
||||||
Repo: "github.com/micro/services",
|
Repo: "github.com/micro/services",
|
||||||
Folder: "helloworld",
|
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{
|
expected: &Source{
|
||||||
Repo: "github.com/micro/services",
|
Repo: "github.com/micro/services",
|
||||||
Folder: "helloworld",
|
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{
|
expected: &Source{
|
||||||
Repo: "github.com/micro/services",
|
Repo: "github.com/micro/services",
|
||||||
Folder: "helloworld",
|
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{
|
expected: &Source{
|
||||||
Repo: "github.com/crufter/reponame",
|
Repo: "github.com/crufter/reponame",
|
||||||
Folder: "helloworld",
|
Folder: "helloworld",
|
||||||
@ -53,7 +53,45 @@ func TestParseSource(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for i, c := range cases {
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("Failed case %v: %v", i, err)
|
t.Fatalf("Failed case %v: %v", i, err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user