2020-04-23 16:30:43 +02:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type parseCase struct {
|
2020-04-30 18:20:51 +02:00
|
|
|
source string
|
2020-04-23 16:30:43 +02:00
|
|
|
expected *Source
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseSource(t *testing.T) {
|
|
|
|
cases := []parseCase{
|
|
|
|
{
|
2020-04-30 18:20:51 +02:00
|
|
|
source: "helloworld",
|
2020-04-23 16:30:43 +02:00
|
|
|
expected: &Source{
|
|
|
|
Repo: "github.com/micro/services",
|
|
|
|
Folder: "helloworld",
|
|
|
|
Ref: "latest",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-04-30 18:20:51 +02:00
|
|
|
source: "github.com/micro/services/helloworld",
|
2020-04-23 16:30:43 +02:00
|
|
|
expected: &Source{
|
|
|
|
Repo: "github.com/micro/services",
|
|
|
|
Folder: "helloworld",
|
|
|
|
Ref: "latest",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-04-30 18:20:51 +02:00
|
|
|
source: "github.com/micro/services/helloworld@v1.12.1",
|
2020-04-23 16:30:43 +02:00
|
|
|
expected: &Source{
|
|
|
|
Repo: "github.com/micro/services",
|
|
|
|
Folder: "helloworld",
|
|
|
|
Ref: "v1.12.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-04-30 18:20:51 +02:00
|
|
|
source: "github.com/micro/services/helloworld@branchname",
|
2020-04-23 16:30:43 +02:00
|
|
|
expected: &Source{
|
|
|
|
Repo: "github.com/micro/services",
|
|
|
|
Folder: "helloworld",
|
|
|
|
Ref: "branchname",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-04-30 18:20:51 +02:00
|
|
|
source: "github.com/crufter/reponame/helloworld@branchname",
|
2020-04-23 16:30:43 +02:00
|
|
|
expected: &Source{
|
|
|
|
Repo: "github.com/crufter/reponame",
|
|
|
|
Folder: "helloworld",
|
|
|
|
Ref: "branchname",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, c := range cases {
|
2020-04-30 18:20:51 +02:00
|
|
|
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
|
|
|
|
})
|
2020-04-23 16:30:43 +02:00
|
|
|
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 nameCase struct {
|
|
|
|
fileContent string
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceNameExtract(t *testing.T) {
|
|
|
|
cases := []nameCase{
|
|
|
|
{
|
|
|
|
fileContent: `func main() {
|
|
|
|
// New Service
|
|
|
|
service := micro.NewService(
|
|
|
|
micro.Name("go.micro.service.helloworld"),
|
|
|
|
micro.Version("latest"),
|
|
|
|
)`,
|
|
|
|
expected: "go.micro.service.helloworld",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, c := range cases {
|
|
|
|
result := extractServiceName([]byte(c.fileContent))
|
|
|
|
if result != c.expected {
|
|
|
|
t.Fatalf("Case %v, expected: %v, got: %v", i, c.expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|