-
Notifications
You must be signed in to change notification settings - Fork 316
/
cherrypick_test.go
140 lines (114 loc) · 2.75 KB
/
cherrypick_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
package git
import (
"io/ioutil"
"testing"
)
func checkout(t *testing.T, repo *Repository, commit *Commit) {
tree, err := commit.Tree()
if err != nil {
t.Fatal(err)
}
err = repo.CheckoutTree(tree, &CheckoutOptions{Strategy: CheckoutSafe})
if err != nil {
t.Fatal(err)
}
err = repo.SetHeadDetached(commit.Id())
if err != nil {
t.Fatal(err)
}
}
const content = "Herro, Worrd!"
func readReadme(t *testing.T, repo *Repository) string {
bytes, err := ioutil.ReadFile(pathInRepo(repo, "README"))
if err != nil {
t.Fatal(err)
}
return string(bytes)
}
func TestCherrypick(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
c1, _ := seedTestRepo(t, repo)
c2, _ := updateReadme(t, repo, content)
commit1, err := repo.LookupCommit(c1)
if err != nil {
t.Fatal(err)
}
commit2, err := repo.LookupCommit(c2)
if err != nil {
t.Fatal(err)
}
checkout(t, repo, commit1)
if readReadme(t, repo) == content {
t.Fatalf("README has wrong content after checking out initial commit")
}
opts, err := DefaultCherrypickOptions()
if err != nil {
t.Fatal(err)
}
err = repo.Cherrypick(commit2, opts)
if err != nil {
t.Fatal(err)
}
if readReadme(t, repo) != content {
t.Fatalf("README has wrong contents after cherry-picking")
}
state := repo.State()
if state != RepositoryStateCherrypick {
t.Fatal("Incorrect repository state: ", state)
}
err = repo.StateCleanup()
if err != nil {
t.Fatal(err)
}
state = repo.State()
if state != RepositoryStateNone {
t.Fatal("Incorrect repository state: ", state)
}
}
func TestCherrypickCommit(t *testing.T) {
t.Parallel()
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
c1, _ := seedTestRepo(t, repo)
c2, _ := updateReadme(t, repo, content)
commit1, err := repo.LookupCommit(c1)
if err != nil {
t.Fatal(err)
}
commit2, err := repo.LookupCommit(c2)
if err != nil {
t.Fatal(err)
}
checkout(t, repo, commit1)
if got := readReadme(t, repo); got == content {
t.Fatalf("README = %q, want %q", got, content)
}
opts, err := DefaultCherrypickOptions()
if err != nil {
t.Fatal(err)
}
idx, err := repo.CherrypickCommit(commit2, commit1, opts)
if err != nil {
t.Fatal(err)
}
defer idx.Free()
// The file is only updated in the index, not in the working directory.
if got := readReadme(t, repo); got == content {
t.Errorf("README = %q, want %q", got, content)
}
if got := repo.State(); got != RepositoryStateNone {
t.Errorf("repo.State() = %v, want %v", got, RepositoryStateCherrypick)
}
if got := idx.EntryCount(); got != 1 {
t.Fatalf("idx.EntryCount() = %v, want %v", got, 1)
}
entry, err := idx.EntryByIndex(0)
if err != nil {
t.Fatal(err)
}
if entry.Path != "README" {
t.Errorf("entry.Path = %v, want %v", entry.Path, "README")
}
}