-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_test.go
242 lines (181 loc) · 5.31 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"testing"
"strings"
)
func TestFetch(t *testing.T) {
mockRunner := NewMockRunner("")
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
git.Fetch(repo)
if mockRunner.folder != "/test/" {
t.Errorf("Folder should be /test/")
}
if mockRunner.command!= "git" {
t.Errorf("Command should be git")
}
if len(mockRunner.args) != 2 {
t.Errorf("Args size should be 2")
}
if mockRunner.args[0] != "fetch" {
t.Errorf("Args 0 should be fetch")
}
if mockRunner.args[1] != "--all" {
t.Errorf("Args 1 should be --all")
}
}
func TestClone(t *testing.T) {
// TODO
}
func TestStatus(t *testing.T) {
mockRunner := NewMockRunner("")
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
git.Status(repo)
if mockRunner.folder != "/test/" {
t.Errorf("Folder should be /test/")
}
if mockRunner.command!= "git" {
t.Errorf("Command should be git")
}
if len(mockRunner.args) != 2 {
t.Errorf("Args size should be 2")
}
if mockRunner.args[0] != "status" {
t.Errorf("Args 0 should be status")
}
if mockRunner.args[1] != "-unormal" {
t.Errorf("Args 1 should be --all")
}
}
func TestNotGitRepoStatus(t *testing.T) {
output := "fatal: Not a git repository (or any of the parent directories): .git"
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if status != "error" {
t.Errorf("Should be error status")
}
}
func TestRemoteSyncStatus(t *testing.T) {
output := `On branch develop
Your branch is up to date with 'origin/develop'.
nothing to commit, working tree clean`
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if !strings.HasPrefix(status, SYNC) {
t.Errorf("Should be sync status")
}
}
func TestRemoteBehindStatus(t *testing.T) {
output := `On branch develop
Your branch is behind 'origin/develop' by 10 commits, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean`
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if !strings.HasPrefix(status, BEHIND) {
t.Errorf("Should be behind status")
}
}
func TestRemoteAheadStatus(t *testing.T) {
output := `On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean`
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if !strings.HasPrefix(status, AHEAD) {
t.Errorf("Should be ahead status")
}
}
func TestRemoteDivergedStatus(t *testing.T) {
output := `On branch chore/test
Your branch and 'origin/chore/test' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean`
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if !strings.HasPrefix(status, DIVERGED) {
t.Errorf("Should be diverged status")
}
}
func TestRemoteNoRemoteStatus(t *testing.T) {
output := `On branch test
nothing to commit, working tree clean`
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if !strings.HasPrefix(status, NO_REMOTE) {
t.Errorf("Should be no remote status")
}
}
func TestLocalSyncStatus(t *testing.T) {
output := `On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working tree clean`
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if !strings.HasSuffix(status, SYNC) {
t.Errorf("Should be sync status")
}
}
func TestLocalUntrackedStatus(t *testing.T) {
output := `On branch develop
Your branch is up-to-date with 'origin/develop'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
untracked-file
nothing added to commit but untracked files present (use "git add" to track)`
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if !strings.HasSuffix(status, UNTRACKED) {
t.Errorf("Should be untracked status")
}
}
func TestLocalChangedStatus(t *testing.T) {
output := `On branch develop
Your branch is up-to-date with 'origin/develop'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")`
mockRunner := NewMockRunner(output)
git := NewCustomGit(mockRunner)
repo := &Repo{Path: "/test/"}
status := git.Status(repo)
if !strings.HasSuffix(status, CHANGED) {
t.Errorf("Should be changed status")
}
}
func NewMockRunner(output string) *MockRunner {
return &MockRunner{output: output}
}
func (runner *MockRunner) Run(folder string, command string, args []string) (string, error) {
runner.folder = folder
runner.command = command
runner.args = args
return runner.output, nil
}
type MockRunner struct {
folder string
command string
args []string
output string
}