-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone_test.go
171 lines (127 loc) · 4.93 KB
/
clone_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
package git_test
import (
"os"
"testing"
git "github.com/purpleclay/gitz"
"github.com/purpleclay/gitz/gittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClone(t *testing.T) {
log := "(main, origin/main) chore: testing if a git clone works"
gittest.InitRepository(t, gittest.WithLog(log))
// Grab the remote for cloning later
remote := gittest.Remote(t)
// Clone the existing repository into a new temporary directory
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
client, _ := git.NewClient()
_, err := client.Clone(remote)
require.NoError(t, err)
require.NoError(t, os.Chdir(gittest.ClonedRepositoryName))
assert.Equal(t, gittest.LastCommit(t).Message, "chore: testing if a git clone works")
}
func TestCloneWithDirectory(t *testing.T) {
gittest.InitRepository(t)
// Grab the remote for cloning later
remote := gittest.Remote(t)
// Clone the existing repository into a new temporary directory
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
client, _ := git.NewClient()
_, err := client.Clone(remote, git.WithDirectory("cloned-repo"))
require.NoError(t, err)
assert.NoDirExists(t, gittest.ClonedRepositoryName)
assert.DirExists(t, "cloned-repo")
}
func TestCloneWithDepth(t *testing.T) {
log := `(main, origin/main) chore: testing clone depth line 2
chore: testing clone depth line 1`
gittest.InitRepository(t, gittest.WithLog(log))
// Grab the remote for cloning later
remote := gittest.Remote(t)
// Clone the existing repository into a new temporary directory
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
client, _ := git.NewClient()
_, err := client.Clone(remote, git.WithDepth(1))
require.NoError(t, err)
require.NoError(t, os.Chdir(gittest.ClonedRepositoryName))
localLog := gittest.Log(t)
require.Len(t, localLog, 1)
assert.Equal(t, "chore: testing clone depth line 2", localLog[0].Message)
}
func TestCloneWithDepthLessThanOne(t *testing.T) {
log := `(main, origin/main) chore: testing clone depth line 2
chore: testing clone depth line 1`
gittest.InitRepository(t, gittest.WithLog(log))
// Grab the remote for cloning later
remote := gittest.Remote(t)
// Clone the existing repository into a new temporary directory
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
client, _ := git.NewClient()
_, err := client.Clone(remote, git.WithDepth(0))
require.NoError(t, err)
require.NoError(t, os.Chdir(gittest.ClonedRepositoryName))
localLog := gittest.Log(t)
assert.Len(t, localLog, 3)
}
func TestCloneWithCheckoutRefForBranch(t *testing.T) {
log := "(main, origin/main, origin/branch-cloning) chore: test branch is cloned"
gittest.InitRepository(t, gittest.WithLog(log))
// Grab the remote for cloning later
remote := gittest.Remote(t)
// Clone the existing repository into a new temporary directory
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
client, _ := git.NewClient()
_, err := client.Clone(remote, git.WithCheckoutRef("branch-cloning"))
require.NoError(t, err)
require.NoError(t, os.Chdir(gittest.ClonedRepositoryName))
assert.Equal(t, "branch-cloning", gittest.ShowBranch(t))
}
func TestCloneWithCheckoutRefForTag(t *testing.T) {
log := `(main, origin/main) chore: shouldn't see this commit
(tag: clone-tag) chore: test this tag is cloned`
gittest.InitRepository(t, gittest.WithLog(log))
// Grab the remote for cloning later
remote := gittest.Remote(t)
// Clone the existing repository into a new temporary directory
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
client, _ := git.NewClient()
_, err := client.Clone(remote, git.WithCheckoutRef("clone-tag"))
require.NoError(t, err)
require.NoError(t, os.Chdir(gittest.ClonedRepositoryName))
assert.Equal(t, "chore: test this tag is cloned", gittest.LastCommit(t).Message)
}
func TestCloneWithCheckoutRefEmptyString(t *testing.T) {
log := `(main, origin/main) chore: shouldn't see this commit
(tag: clone-tag) chore: test this tag is cloned`
gittest.InitRepository(t, gittest.WithLog(log))
// Grab the remote for cloning later
remote := gittest.Remote(t)
// Clone the existing repository into a new temporary directory
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
client, _ := git.NewClient()
_, err := client.Clone(remote, git.WithCheckoutRef(" "))
require.NoError(t, err)
require.NoError(t, os.Chdir(gittest.ClonedRepositoryName))
assert.Equal(t, "chore: shouldn't see this commit", gittest.LastCommit(t).Message)
}
func TestCloneWithNoTags(t *testing.T) {
log := "(main, origin/main) chore: test no tags are cloned"
gittest.InitRepository(t, gittest.WithLog(log))
// Grab the remote for cloning later
remote := gittest.Remote(t)
// Clone the existing repository into a new temporary directory
dir := t.TempDir()
require.NoError(t, os.Chdir(dir))
client, _ := git.NewClient()
_, err := client.Clone(remote, git.WithNoTags())
require.NoError(t, err)
require.NoError(t, os.Chdir(gittest.ClonedRepositoryName))
assert.Empty(t, gittest.Tags(t))
}