-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_test.go
193 lines (145 loc) · 4.23 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
package git
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
func TestGitSuite(t *testing.T) {
suite.Run(t, new(GitSuite))
}
type GitSuite struct {
suite.Suite
user *User
}
func (s *GitSuite) SetupSuite() {
s.user = s.newUser()
}
func (s *GitSuite) newUser() *User {
return &User{Name: "John Doe",
Email: "[email protected]",
HomeFolder: "/home/john-doe/"}
}
// newTestRepository creates a git repository using the systems `git` used for testing
func (s *GitSuite) newTestRepository(name string) *Repository {
dir, err := ioutil.TempDir("", name)
require.NoError(s.T(), err)
dotgit := filepath.Join(dir, ".git")
gitExec(s, dotgit, dir, "init")
repo := &Repository{Path: dir}
// create a master branch with a single file and a single commit
err = repo.Load()
require.NoError(s.T(), err)
w, err := repo.root.Worktree()
require.NoError(s.T(), err)
o := &git.CheckoutOptions{}
o.Branch = plumbing.ReferenceName("refs/heads/master")
o.Create = true
err = w.Checkout(o)
require.NoError(s.T(), err)
err = ioutil.WriteFile(filepath.Join(repo.Path, "empty"), []byte(""), 0600)
require.NoError(s.T(), err)
_, err = w.Add("empty")
require.NoError(s.T(), err)
_, err = w.Commit("add empty", &git.CommitOptions{
Author: &object.Signature{
Name: s.user.Name,
Email: s.user.Email,
When: time.Now(),
},
})
require.NoError(s.T(), err)
// create an second branch named `test` based on `master`
// adding a second file and commit
w, err = repo.root.Worktree()
require.NoError(s.T(), err)
o = &git.CheckoutOptions{}
o.Branch = plumbing.ReferenceName("refs/heads/test")
o.Create = true
err = w.Checkout(o)
require.NoError(s.T(), err)
err = ioutil.WriteFile(filepath.Join(repo.Path, "empty2"), []byte(""), 0600)
require.NoError(s.T(), err)
_, err = w.Add("empty2")
require.NoError(s.T(), err)
_, err = w.Commit("add empty2", &git.CommitOptions{
Author: &object.Signature{
Name: s.user.Name,
Email: s.user.Email,
When: time.Now(),
},
})
require.NoError(s.T(), err)
// checkout master
o = &git.CheckoutOptions{}
o.Branch = plumbing.ReferenceName("refs/heads/master")
o.Create = false
err = w.Checkout(o)
require.NoError(s.T(), err)
return repo
}
func (s *GitSuite) TestCreateBranch() {
gpass := s.newTestRepository("gpass-test")
defer os.RemoveAll(gpass.Path)
master := "master"
n := "testbranch"
ref := fmt.Sprintf("refs/heads/%s", n)
err := gpass.Load()
require.NoError(s.T(), err)
err = gpass.CreateBranch(master, n)
require.NoError(s.T(), err)
gpassRef, err := gpass.root.Head()
s.Equal(string(gpassRef.Name()), ref)
}
func (s *GitSuite) TestCreateOrphanBranch() {
gpass := s.newTestRepository("gpass-test")
defer os.RemoveAll(gpass.Path)
n := "orphanbranch"
ref := fmt.Sprintf("refs/heads/%s", n)
err := gpass.Load()
require.NoError(s.T(), err)
err = gpass.CreateOrphanBranch(s.user, n)
require.NoError(s.T(), err)
_, err = gpass.root.Reference(plumbing.ReferenceName(ref), false)
require.NoError(s.T(), err)
}
func (s *GitSuite) TestCheckoutBranch() {
gpass := s.newTestRepository("gpass-test")
defer os.RemoveAll(gpass.Path)
n := "test"
ref := fmt.Sprintf("refs/heads/%s", n)
err := gpass.Load()
require.NoError(s.T(), err)
err = gpass.CheckoutBranch(n)
require.NoError(s.T(), err)
gpassRef, err := gpass.root.Reference(plumbing.ReferenceName(ref), false)
require.NoError(s.T(), err)
headRef, err := gpass.root.Head()
require.NoError(s.T(), err)
s.Equal(gpassRef, headRef)
}
// TODO: should be removed once creating git repositories with go-git is added to this package
// creates an unnecessary dependency for `git` to exist on the system
func gitExec(s *GitSuite, dir string, worktree string, command ...string) {
cmd := exec.Command("git",
append([]string{"--git-dir", dir, "--work-tree", worktree},
command...)...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
err = fmt.Errorf(fmt.Sprint(err) + ": " + stderr.String())
}
require.NoError(s.T(), err)
}