-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathgit_test.go
186 lines (153 loc) · 4.52 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package git
import (
"fmt"
"os"
"strings"
"testing"
"github.com/github/hub/v2/fixtures"
"github.com/github/hub/v2/internal/assert"
)
func TestGitDir(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
gitDir, _ := Dir()
assert.T(t, strings.Contains(gitDir, ".git"))
}
func TestGitEditor(t *testing.T) {
repo := fixtures.SetupTestRepo()
editor := os.Getenv("GIT_EDITOR")
if err := os.Unsetenv("GIT_EDITOR"); err != nil {
t.Fatal(err)
}
defer func() {
repo.TearDown()
if err := os.Setenv("GIT_EDITOR", editor); err != nil {
t.Fatal(err)
}
os.Unsetenv("FOO")
os.Unsetenv("BAR")
}()
os.Setenv("FOO", "hello")
os.Setenv("BAR", "happy world")
SetGlobalConfig("core.editor", `$FOO "${BAR}"`)
gitEditor, err := Editor()
assert.Equal(t, nil, err)
assert.Equal(t, `hello "happy world"`, gitEditor)
}
func TestGitLog(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
log, err := Log("08f4b7b6513dffc6245857e497cfd6101dc47818", "9b5a719a3d76ac9dc2fa635d9b1f34fd73994c06")
assert.Equal(t, nil, err)
assert.NotEqual(t, "", log)
}
func TestGitRef(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
ref := "08f4b7b6513dffc6245857e497cfd6101dc47818"
gitRef, err := Ref(ref)
assert.Equal(t, nil, err)
assert.Equal(t, ref, gitRef)
}
func TestGitRefList(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
refList, err := RefList("08f4b7b6513dffc6245857e497cfd6101dc47818", "9b5a719a3d76ac9dc2fa635d9b1f34fd73994c06")
assert.Equal(t, nil, err)
assert.Equal(t, 1, len(refList))
assert.Equal(t, "9b5a719a3d76ac9dc2fa635d9b1f34fd73994c06", refList[0])
}
func TestGitShow(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
output, err := Show("9b5a719a3d76ac9dc2fa635d9b1f34fd73994c06")
assert.Equal(t, nil, err)
assert.Equal(t, "First comment\n\nMore comment", output)
}
func TestGitConfig(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
_, err := GlobalConfig("hub.test")
assert.NotEqual(t, nil, err)
SetGlobalConfig("hub.test", "1")
v, err := GlobalConfig("hub.test")
assert.Equal(t, nil, err)
assert.Equal(t, "1", v)
SetGlobalConfig("hub.test", "")
v, err = GlobalConfig("hub.test")
assert.Equal(t, nil, err)
assert.Equal(t, "", v)
}
func TestRemotes(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
type remote struct {
name string
url string
pushURL string
}
testCases := map[string]remote{
"testremote1": {
"testremote1",
"https://example.com/test1/project1.git",
"no_push",
},
"testremote2": {
"testremote2",
"user@example.com:test2/project2.git",
"http://example.com/project.git",
},
"testremote3": {
"testremote3",
"https://example.com/test1/project2.git",
"",
},
}
for _, tc := range testCases {
repo.AddRemote(tc.name, tc.url, tc.pushURL)
}
remotes, err := Remotes()
assert.Equal(t, nil, err)
// In addition to the remotes we added to the repo, repo will
// also have an additional remote "origin". So add it to the
// expected cases to test.
wantCases := map[string]struct{}{
fmt.Sprintf("origin %s (fetch)", repo.Remote): {},
fmt.Sprintf("origin %s (push)", repo.Remote): {},
"testremote1 https://example.com/test1/project1.git (fetch)": {},
"testremote1 no_push (push)": {},
"testremote2 user@example.com:test2/project2.git (fetch)": {},
"testremote2 http://example.com/project.git (push)": {},
"testremote3 https://example.com/test1/project2.git (fetch)": {},
"testremote3 https://example.com/test1/project2.git (push)": {},
}
assert.Equal(t, len(remotes), len(wantCases))
for _, got := range remotes {
if _, ok := wantCases[got]; !ok {
t.Errorf("Unexpected remote: %s", got)
}
}
}
func TestCommentChar(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
char, err := CommentChar("")
assert.Equal(t, nil, err)
assert.Equal(t, "#", char)
SetGlobalConfig("core.commentchar", ";")
char, err = CommentChar("")
assert.Equal(t, nil, err)
assert.Equal(t, ";", char)
SetGlobalConfig("core.commentchar", "auto")
char, err = CommentChar("")
assert.Equal(t, nil, err)
assert.Equal(t, "#", char)
char, err = CommentChar("hello\n#nice\nworld")
assert.Equal(t, nil, err)
assert.Equal(t, ";", char)
char, err = CommentChar("hello\n#nice\n;world")
assert.Equal(t, nil, err)
assert.Equal(t, "@", char)
_, err = CommentChar("#\n;\n@\n!\n$\n%\n^\n&\n|\n:")
assert.Equal(t, "unable to select a comment character that is not used in the current message", err.Error())
}