-
Notifications
You must be signed in to change notification settings - Fork 316
/
stash_test.go
198 lines (155 loc) · 4.56 KB
/
stash_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
package git
import (
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"runtime"
"testing"
"time"
)
func TestStash(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
prepareStashRepo(t, repo)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "[email protected]",
When: time.Now(),
}
stash1, err := repo.Stashes.Save(sig, "First stash", StashDefault)
checkFatal(t, err)
_, err = repo.LookupCommit(stash1)
checkFatal(t, err)
b, err := ioutil.ReadFile(pathInRepo(repo, "README"))
checkFatal(t, err)
if string(b) == "Update README goes to stash\n" {
t.Errorf("README still contains the uncommitted changes")
}
if !fileExistsInRepo(repo, "untracked.txt") {
t.Errorf("untracked.txt doesn't exist in the repo; should be untracked")
}
// Apply: default
opts, err := DefaultStashApplyOptions()
checkFatal(t, err)
err = repo.Stashes.Apply(0, opts)
checkFatal(t, err)
b, err = ioutil.ReadFile(pathInRepo(repo, "README"))
checkFatal(t, err)
if string(b) != "Update README goes to stash\n" {
t.Errorf("README changes aren't here")
}
// Apply: no stash for the given index
err = repo.Stashes.Apply(1, opts)
if !IsErrorCode(err, ErrorCodeNotFound) {
t.Errorf("expecting GIT_ENOTFOUND error code %d, got %v", ErrorCodeNotFound, err)
}
// Apply: callback stopped
opts.ProgressCallback = func(progress StashApplyProgress) error {
if progress == StashApplyProgressCheckoutModified {
return fmt.Errorf("Stop")
}
return nil
}
err = repo.Stashes.Apply(0, opts)
if err.Error() != "Stop" {
t.Errorf("expecting error 'Stop', got %v", err)
}
// Create second stash with ignored files
os.MkdirAll(pathInRepo(repo, "tmp"), os.ModeDir|os.ModePerm)
err = ioutil.WriteFile(pathInRepo(repo, "tmp/ignored.txt"), []byte("Ignore me\n"), 0644)
checkFatal(t, err)
stash2, err := repo.Stashes.Save(sig, "Second stash", StashIncludeIgnored)
checkFatal(t, err)
if fileExistsInRepo(repo, "tmp/ignored.txt") {
t.Errorf("tmp/ignored.txt should not exist anymore in the work dir")
}
// Stash foreach
expected := []stash{
{0, "On master: Second stash", stash2.String()},
{1, "On master: First stash", stash1.String()},
}
checkStashes(t, repo, expected)
// Stash pop
opts, _ = DefaultStashApplyOptions()
err = repo.Stashes.Pop(1, opts)
checkFatal(t, err)
b, err = ioutil.ReadFile(pathInRepo(repo, "README"))
checkFatal(t, err)
if string(b) != "Update README goes to stash\n" {
t.Errorf("README changes aren't here")
}
expected = []stash{
{0, "On master: Second stash", stash2.String()},
}
checkStashes(t, repo, expected)
// Stash drop
err = repo.Stashes.Drop(0)
checkFatal(t, err)
expected = []stash{}
checkStashes(t, repo, expected)
}
type stash struct {
index int
msg string
id string
}
func checkStashes(t *testing.T, repo *Repository, expected []stash) {
var actual []stash
repo.Stashes.Foreach(func(index int, msg string, id *Oid) error {
stash := stash{index, msg, id.String()}
if len(expected) > len(actual) {
if s := expected[len(actual)]; s.id == "" {
stash.id = "" // don't check id
}
}
actual = append(actual, stash)
return nil
})
if len(expected) > 0 && !reflect.DeepEqual(expected, actual) {
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
if !ok {
t.Fatalf("Unable to get caller")
}
t.Errorf("%v:%v: expecting %#v\ngot %#v", path.Base(file), line, expected, actual)
}
}
func prepareStashRepo(t *testing.T, repo *Repository) {
seedTestRepo(t, repo)
err := ioutil.WriteFile(pathInRepo(repo, ".gitignore"), []byte("tmp\n"), 0644)
checkFatal(t, err)
sig := &Signature{
Name: "Rand Om Hacker",
Email: "[email protected]",
When: time.Now(),
}
idx, err := repo.Index()
checkFatal(t, err)
err = idx.AddByPath(".gitignore")
checkFatal(t, err)
treeID, err := idx.WriteTree()
checkFatal(t, err)
err = idx.Write()
checkFatal(t, err)
currentBranch, err := repo.Head()
checkFatal(t, err)
currentTip, err := repo.LookupCommit(currentBranch.Target())
checkFatal(t, err)
message := "Add .gitignore\n"
tree, err := repo.LookupTree(treeID)
checkFatal(t, err)
_, err = repo.CreateCommit("HEAD", sig, sig, message, tree, currentTip)
checkFatal(t, err)
err = ioutil.WriteFile(pathInRepo(repo, "README"), []byte("Update README goes to stash\n"), 0644)
checkFatal(t, err)
err = ioutil.WriteFile(pathInRepo(repo, "untracked.txt"), []byte("Hello, World\n"), 0644)
checkFatal(t, err)
}
func fileExistsInRepo(repo *Repository, name string) bool {
if _, err := os.Stat(pathInRepo(repo, name)); err != nil {
return false
}
return true
}