github.com/annwntech/go-micro/v2@v2.9.5/runtime/local/git/git_test.go (about)

     1  package git
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type parseCase struct {
     8  	source   string
     9  	expected *Source
    10  }
    11  
    12  func TestParseSource(t *testing.T) {
    13  	cases := []parseCase{
    14  		{
    15  			source: "helloworld",
    16  			expected: &Source{
    17  				Repo:   "github.com/micro/services",
    18  				Folder: "helloworld",
    19  				Ref:    "latest",
    20  			},
    21  		},
    22  		{
    23  			source: "github.com/micro/services/helloworld",
    24  			expected: &Source{
    25  				Repo:   "github.com/micro/services",
    26  				Folder: "helloworld",
    27  				Ref:    "latest",
    28  			},
    29  		},
    30  		{
    31  			source: "github.com/micro/services/helloworld@v1.12.1",
    32  			expected: &Source{
    33  				Repo:   "github.com/micro/services",
    34  				Folder: "helloworld",
    35  				Ref:    "v1.12.1",
    36  			},
    37  		},
    38  		{
    39  			source: "github.com/micro/services/helloworld@branchname",
    40  			expected: &Source{
    41  				Repo:   "github.com/micro/services",
    42  				Folder: "helloworld",
    43  				Ref:    "branchname",
    44  			},
    45  		},
    46  		{
    47  			source: "github.com/crufter/reponame/helloworld@branchname",
    48  			expected: &Source{
    49  				Repo:   "github.com/crufter/reponame",
    50  				Folder: "helloworld",
    51  				Ref:    "branchname",
    52  			},
    53  		},
    54  	}
    55  	for i, c := range cases {
    56  		result, err := ParseSource(c.source)
    57  		if err != nil {
    58  			t.Fatalf("Failed case %v: %v", i, err)
    59  		}
    60  		if result.Folder != c.expected.Folder {
    61  			t.Fatalf("Folder does not match for '%v', expected '%v', got '%v'", i, c.expected.Folder, result.Folder)
    62  		}
    63  		if result.Repo != c.expected.Repo {
    64  			t.Fatalf("Repo address does not match for '%v', expected '%v', got '%v'", i, c.expected.Repo, result.Repo)
    65  		}
    66  		if result.Ref != c.expected.Ref {
    67  			t.Fatalf("Ref does not match for '%v', expected '%v', got '%v'", i, c.expected.Ref, result.Ref)
    68  		}
    69  	}
    70  }
    71  
    72  type localParseCase struct {
    73  	source     string
    74  	expected   *Source
    75  	workDir    string
    76  	pathExists bool
    77  }
    78  
    79  func TestLocalParseSource(t *testing.T) {
    80  	cases := []localParseCase{
    81  		{
    82  			source: ".",
    83  			expected: &Source{
    84  				Folder: "folder2",
    85  				Ref:    "latest",
    86  			},
    87  			workDir:    "/folder1/folder2",
    88  			pathExists: true,
    89  		},
    90  	}
    91  	for i, c := range cases {
    92  		result, err := ParseSourceLocal(c.workDir, c.source, func(s string) (bool, error) {
    93  			return c.pathExists, nil
    94  		})
    95  		if err != nil {
    96  			t.Fatalf("Failed case %v: %v", i, err)
    97  		}
    98  		if result.Folder != c.expected.Folder {
    99  			t.Fatalf("Folder does not match for '%v', expected '%v', got '%v'", i, c.expected.Folder, result.Folder)
   100  		}
   101  		if result.Repo != c.expected.Repo {
   102  			t.Fatalf("Repo address does not match for '%v', expected '%v', got '%v'", i, c.expected.Repo, result.Repo)
   103  		}
   104  		if result.Ref != c.expected.Ref {
   105  			t.Fatalf("Ref does not match for '%v', expected '%v', got '%v'", i, c.expected.Ref, result.Ref)
   106  		}
   107  	}
   108  }
   109  
   110  type nameCase struct {
   111  	fileContent string
   112  	expected    string
   113  }
   114  
   115  func TestServiceNameExtract(t *testing.T) {
   116  	cases := []nameCase{
   117  		{
   118  			fileContent: `func main() {
   119  			// New Service
   120  			service := micro.NewService(
   121  				micro.Name("go.micro.service.helloworld"),
   122  				micro.Version("latest"),
   123  			)`,
   124  			expected: "go.micro.service.helloworld",
   125  		},
   126  	}
   127  	for i, c := range cases {
   128  		result := extractServiceName([]byte(c.fileContent))
   129  		if result != c.expected {
   130  			t.Fatalf("Case %v, expected: %v, got: %v", i, c.expected, result)
   131  		}
   132  	}
   133  }