-
Notifications
You must be signed in to change notification settings - Fork 40
/
git_test.go
136 lines (108 loc) · 3.09 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
package main
import (
"io/ioutil"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/require"
)
func TestGitExists(t *testing.T) {
_, err := exec.LookPath("git")
require.Nil(t, err, "error: %+v", err)
}
func TestCommit_failsOnEmptyRepo(t *testing.T) {
repo := newRepo(t)
defer os.RemoveAll(repo.dir)
_, err := repo.Commit()
require.NotNil(t, err)
}
func TestCommit(t *testing.T) {
repo := newRepo(t)
defer os.RemoveAll(repo.dir)
mkCommit(t, repo, "commit 1")
c1, err := repo.Commit()
require.Nil(t, err)
require.Regexp(t, "^[0-9a-f]{4,15}$", c1)
mkCommit(t, repo, "commit 2")
c2, err := repo.Commit()
require.Nil(t, err)
require.Regexp(t, "^[0-9a-f]{4,15}$", c2)
// commit hash changed
require.NotEqual(t, c1, c2)
}
func TestState(t *testing.T) {
repo := newRepo(t)
defer os.RemoveAll(repo.dir)
s1, err := repo.State()
require.Nil(t, err)
require.EqualValues(t, "clean", s1)
f, err := ioutil.TempFile(repo.dir, "") // contaminate
require.Nil(t, err, "failed to create test file")
f.Close()
s2, err := repo.State()
require.Nil(t, err)
require.EqualValues(t, "dirty", s2)
require.Nil(t, os.Remove(f.Name()), "failed to rm test file")
s3, err := repo.State()
require.Nil(t, err)
require.EqualValues(t, "clean", s3)
}
func TestBranch(t *testing.T) {
repo := newRepo(t)
defer os.RemoveAll(repo.dir)
// default branch "master"
mkCommit(t, repo, "commit 1")
mkCommit(t, repo, "commit 2")
require.EqualValues(t, "master", repo.Branch())
// move into detached state: "HEAD"
_, err := repo.exec("checkout", "HEAD~1")
require.Nil(t, err)
require.EqualValues(t, "HEAD", repo.Branch())
// checkout into another branch
_, err = repo.exec("checkout", "-b", "foo")
require.Nil(t, err)
require.EqualValues(t, "foo", repo.Branch())
}
func TestSummary(t *testing.T) {
repo := newRepo(t)
defer os.RemoveAll(repo.dir)
// no tags yet, should be just short commit number
mkCommit(t, repo, "commit 1")
s, err := repo.Summary()
require.Nil(t, err)
require.Regexp(t, "^[0-9a-f]{4,15}$", s)
// if commit is a tag, tag is returned
_, err = repo.exec("tag", "v1.0.0")
require.Nil(t, err)
s, err = repo.Summary()
require.Nil(t, err)
require.EqualValues(t, "v1.0.0", s)
// add 3 more commits, it should be in format v1.0.0-2-*
mkCommit(t, repo, "commit 2")
mkCommit(t, repo, "commit 3")
s, err = repo.Summary()
require.Nil(t, err)
require.Regexp(t, "^v1.0.0-2-.*$", s)
// add a dirty file
f, err := ioutil.TempFile(repo.dir, "") // contaminate
require.Nil(t, err, "failed to create test file")
f.Close()
_, err = repo.exec("add", f.Name())
require.Nil(t, err)
s, err = repo.Summary()
require.Nil(t, err)
require.Regexp(t, ".*-dirty$", s)
}
// Test utilities
func newRepo(t *testing.T) git {
dir, err := ioutil.TempDir("", "gitrepo")
require.Nil(t, err, "failed to create test dir")
repo := git{dir}
_, err = repo.exec("init", "-q", dir)
require.Nil(t, err, "failed to initialize git repo")
return repo
}
func mkCommit(t *testing.T, repo git, msg string) {
_, err := repo.exec("commit", "--allow-empty", "--message", msg)
require.Nil(t, err, "failed to commit: %+v", err)
}