go.undefinedlabs.com/scopeagent@v0.4.2/agent/git_test.go (about)

     1  package agent
     2  
     3  import "testing"
     4  
     5  func TestRefRegex(t *testing.T) {
     6  	tests := map[string]string{
     7  		"ref: refs/heads/git-alternative":     "refs/heads/git-alternative",
     8  		"ref:    refs/heads/git-alternative":  "refs/heads/git-alternative",
     9  		"    ref: refs/heads/git-alternative": "refs/heads/git-alternative",
    10  		"ref: a.b_c:d|e-f%g$h(i)/j\\k":        "a.b_c:d|e-f%g$h(i)/j\\k",
    11  	}
    12  	for k, v := range tests {
    13  		match := refRegex.FindStringSubmatch(k)
    14  		if match == nil {
    15  			t.Fatalf("match for '%s' is nil", k)
    16  		}
    17  		if match[1] != v {
    18  			t.Fatalf("match for '%s' is '%s', but '%s' was expected", k, match[1], v)
    19  		}
    20  	}
    21  }
    22  
    23  func TestRemoteRegex(t *testing.T) {
    24  	tests := map[string]string{
    25  		"[remote \"origin\"]":      "origin",
    26  		"[remote \"upstream\"]":    "upstream",
    27  		"[remote \"a-b-c\"]":       "a-b-c",
    28  		"[remote \"a.b.c\"]":       "a.b.c",
    29  		"[remote \"a_b_c\"]":       "a_b_c",
    30  		"[remote \"a\\b\\c\"]":     "a\\b\\c",
    31  		"[remote \"a$b$c\"]":       "a$b$c",
    32  		"[remote  \"a$b$c\"]":      "a$b$c",
    33  		"[remote  \"a/b/c\"]":      "a/b/c",
    34  		"[remote  \"a b c\"]":      "a b c",
    35  		"[remote    \"a b c\"   ]": "a b c",
    36  	}
    37  	for k, v := range tests {
    38  		match := remoteRegex.FindStringSubmatch(k)
    39  		if match == nil {
    40  			t.Fatalf("match for '%s' is nil", k)
    41  		}
    42  		if match[1] != v {
    43  			t.Fatalf("match for '%s' is '%s', but '%s' was expected", k, match[1], v)
    44  		}
    45  	}
    46  }
    47  
    48  func TestBranchRegex(t *testing.T) {
    49  	tests := map[string]string{
    50  		"[branch \"origin\"]":         "origin",
    51  		"[branch \"upstream\"]":       "upstream",
    52  		"[branch \"a-b-c\"]":          "a-b-c",
    53  		"[branch \"a.b.c\"]":          "a.b.c",
    54  		"[branch \"a_b_c\"]":          "a_b_c",
    55  		"[branch \"a\\b\\c\"]":        "a\\b\\c",
    56  		"[branch \"a$b$c\"]":          "a$b$c",
    57  		"[branch  \"a$b$c\"]":         "a$b$c",
    58  		"[branch  \"a/b/c\"]":         "a/b/c",
    59  		"[branch  \"a b c\"]":         "a b c",
    60  		"[branch      \"a b c\"    ]": "a b c",
    61  	}
    62  	for k, v := range tests {
    63  		match := branchRegex.FindStringSubmatch(k)
    64  		if match == nil {
    65  			t.Fatalf("match for '%s' is nil", k)
    66  		}
    67  		if match[1] != v {
    68  			t.Fatalf("match for '%s' is '%s', but '%s' was expected", k, match[1], v)
    69  		}
    70  	}
    71  }
    72  
    73  func TestUrlRegex(t *testing.T) {
    74  	tests := map[string]string{
    75  		"	url = git@github.com:undefinedlabs/scope-go-agent.git": "git@github.com:undefinedlabs/scope-go-agent.git",
    76  		"url = git@github.com:undefinedlabs/scope-go-agent.git":  "git@github.com:undefinedlabs/scope-go-agent.git",
    77  		"url =  git@github.com:undefinedlabs/scope-go-agent.git": "git@github.com:undefinedlabs/scope-go-agent.git",
    78  		"	url =    git@github.com:undefinedlabs/scope-go-agent.git": "git@github.com:undefinedlabs/scope-go-agent.git",
    79  		"url=git@github.com:undefinedlabs/scope-go-agent.git":    "git@github.com:undefinedlabs/scope-go-agent.git",
    80  		"url   =git@github.com:undefinedlabs/scope-go-agent.git": "git@github.com:undefinedlabs/scope-go-agent.git",
    81  		"	url = a.b_c:d|e-f%g$h(i)/j\\k": "a.b_c:d|e-f%g$h(i)/j\\k",
    82  	}
    83  	for k, v := range tests {
    84  		match := urlRegex.FindStringSubmatch(k)
    85  		if match == nil {
    86  			t.Fatalf("match for '%s' is nil", k)
    87  		}
    88  		if match[1] != v {
    89  			t.Fatalf("match for '%s' is '%s', but '%s' was expected", k, match[1], v)
    90  		}
    91  	}
    92  }
    93  
    94  func TestMergeRegex(t *testing.T) {
    95  	tests := map[string]string{
    96  		"	merge = refs/heads/no-module-package-name": "refs/heads/no-module-package-name",
    97  		"merge = refs/heads/no-module-package-name":           "refs/heads/no-module-package-name",
    98  		"merge       =     refs/heads/no-module-package-name": "refs/heads/no-module-package-name",
    99  		"	merge = a.b_c:d|e-f%g$h(i)/j\\k": "a.b_c:d|e-f%g$h(i)/j\\k",
   100  	}
   101  	for k, v := range tests {
   102  		match := mergeRegex.FindStringSubmatch(k)
   103  		if match == nil {
   104  			t.Fatalf("match for '%s' is nil", k)
   105  		}
   106  		if match[1] != v {
   107  			t.Fatalf("match for '%s' is '%s', but '%s' was expected", k, match[1], v)
   108  		}
   109  	}
   110  }
   111  
   112  var data *GitData
   113  var diff *GitDiff
   114  
   115  func BenchmarkGetGitData(b *testing.B) {
   116  	for i := 0; i < b.N; i++ {
   117  		data = getGitData()
   118  	}
   119  }
   120  
   121  func BenchmarkGetGitDiff(b *testing.B) {
   122  	for i := 0; i < b.N; i++ {
   123  		diff = getGitDiff()
   124  	}
   125  }