-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit_test.go
135 lines (125 loc) · 3.48 KB
/
git_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package vcsstate
import (
"errors"
"reflect"
"testing"
)
func TestGuessBranch(t *testing.T) {
tests := []struct {
in []byte
revision string
wantBranch string
wantErr error
}{
{
// Issue #10.
// git ls-remote --symref https://code.googlesource.com/google-api-go-client HEAD 'refs/heads/*'
in: []byte(`fbbaff1827317122a8a0e1b24de25df8417ce87b HEAD
fbbaff1827317122a8a0e1b24de25df8417ce87b refs/heads/master
`),
revision: "fbbaff1827317122a8a0e1b24de25df8417ce87b",
wantBranch: "master",
},
}
for _, test := range tests {
branch, err := guessBranch(test.in, test.revision)
if got, want := err, test.wantErr; !reflect.DeepEqual(got, want) {
t.Errorf("got %#v, want %#v", got, want)
}
if test.wantErr != nil {
continue
}
if got, want := branch, test.wantBranch; got != want {
t.Errorf("got branch %q, want %q", got, want)
}
}
}
func TestParseGit17Remote(t *testing.T) {
tests := []struct {
in []byte
want string
wantErr error
}{
{
in: []byte(`foo https://github.com/foo/vcsstate (fetch)
foo https://github.com/foo/vcsstate (push)
origin https://github.com/shurcooL/vcsstate (fetch)
origin https://github.com/shurcooL/vcsstate (push)
somebody https://github.com/somebody/vcsstate (fetch)
somebody https://github.com/somebody/vcsstate (push)
`),
want: "https://github.com/shurcooL/vcsstate",
},
{
in: []byte(""),
wantErr: errors.New("no origin remote"),
},
// Only accept "origin" remote, even if others exist.
{
in: []byte(`fork https://github.com/foobar/vcsstate (fetch)
fork https://github.com/foobar/vcsstate (push)
`),
wantErr: errors.New("no origin remote"),
},
}
for _, test := range tests {
url, err := parseGit17Remote(test.in)
if got, want := err, test.wantErr; !reflect.DeepEqual(got, want) {
t.Errorf("got %#v, want %#v", got, want)
}
if test.wantErr != nil {
continue
}
if got, want := url, test.want; got != want {
t.Errorf("got url %q, want %q", got, want)
}
}
}
func TestParseGit17LsRemote(t *testing.T) {
tests := []struct {
in []byte
wantBranch string
wantRevision string
wantErr error
}{
{
in: []byte(`7cafcd837844e784b526369c9bce262804aebc60 HEAD
0a50dc0e5a012dbf22f1289471dc52bc0fe44e9a refs/heads/cb
7cafcd837844e784b526369c9bce262804aebc60 refs/heads/main
67253a98dcf0dd3273ea58929d7aaaa781ef6d13 refs/heads/wip
`),
wantBranch: "main",
wantRevision: "7cafcd837844e784b526369c9bce262804aebc60",
},
{
in: []byte(""),
wantErr: errors.New("empty ls-remote output"),
},
// Prefer "master" even though "datetimes" has same revision.
{
in: []byte(`f0aeabca5a127c4078abb8c8d64298b147264b55 HEAD
f0aeabca5a127c4078abb8c8d64298b147264b55 refs/heads/datetimes
f42bdee2ab503fed466739d8e8c55ae34fd9be45 refs/heads/encode-tagged-anon-structs
f0aeabca5a127c4078abb8c8d64298b147264b55 refs/heads/master
f93697607c2406ba00b57fe418f4101b4e447eb8 refs/heads/numbers
`),
wantBranch: "master",
wantRevision: "f0aeabca5a127c4078abb8c8d64298b147264b55",
},
}
for _, test := range tests {
branch, revision, err := parseGit17LsRemote(test.in)
if got, want := err, test.wantErr; !reflect.DeepEqual(got, want) {
t.Errorf("got %#v, want %#v", got, want)
}
if test.wantErr != nil {
continue
}
if got, want := branch, test.wantBranch; got != want {
t.Errorf("got branch %q, want %q", got, want)
}
if got, want := revision, test.wantRevision; got != want {
t.Errorf("got revision %q, want %q", got, want)
}
}
}