forked from Masterminds/vcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_test.go
191 lines (163 loc) · 4.72 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
package vcs
import (
"io/ioutil"
//"log"
"os"
"testing"
)
// Canary test to ensure GitRepo implements the Repo interface.
var _ Repo = &GitRepo{}
// To verify git is working we perform intergration testing
// with a known git service.
func TestGit(t *testing.T) {
tempDir, err := ioutil.TempDir("", "go-vcs-git-tests")
if err != nil {
t.Error(err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Error(err)
}
}()
repo, err := NewGitRepo("https://github.com/Masterminds/VCSTestRepo", tempDir+"/VCSTestRepo")
if err != nil {
t.Error(err)
}
if repo.Vcs() != Git {
t.Error("Git is detecting the wrong type")
}
// Check the basic getters.
if repo.Remote() != "https://github.com/Masterminds/VCSTestRepo" {
t.Error("Remote not set properly")
}
if repo.LocalPath() != tempDir+"/VCSTestRepo" {
t.Error("Local disk location not set properly")
}
//Logger = log.New(os.Stdout, "", log.LstdFlags)
// Do an initial clone.
err = repo.Get()
if err != nil {
t.Errorf("Unable to clone Git repo. Err was %s", err)
}
// Verify Git repo is a Git repo
if repo.CheckLocal() == false {
t.Error("Problem checking out repo or Git CheckLocal is not working")
}
// Test internal lookup mechanism used outside of Git specific functionality.
ltype, err := DetectVcsFromFS(tempDir + "/VCSTestRepo")
if err != nil {
t.Error("detectVcsFromFS unable to Git repo")
}
if ltype != Git {
t.Errorf("detectVcsFromFS detected %s instead of Git type", ltype)
}
// Test NewRepo on existing checkout. This should simply provide a working
// instance without error based on looking at the local directory.
nrepo, nrerr := NewRepo("https://github.com/Masterminds/VCSTestRepo", tempDir+"/VCSTestRepo")
if nrerr != nil {
t.Error(nrerr)
}
// Verify the right oject is returned. It will check the local repo type.
if nrepo.CheckLocal() == false {
t.Error("Wrong version returned from NewRepo")
}
// Perform an update.
err = repo.Update()
if err != nil {
t.Error(err)
}
// Set the version using the short hash.
err = repo.UpdateVersion("806b07b")
if err != nil {
t.Errorf("Unable to update Git repo version. Err was %s", err)
}
// Once a ref has been checked out the repo is in a detached head state.
// Trying to pull in an update in this state will cause an error. Update
// should cleanly handle this. Pulling on a branch (tested elsewhere) and
// skipping that here.
err = repo.Update()
if err != nil {
t.Error(err)
}
// Use Version to verify we are on the right version.
v, err := repo.Version()
if v != "806b07b08faa21cfbdae93027904f80174679402" {
t.Error("Error checking checked out Git version")
}
if err != nil {
t.Error(err)
}
// Use Date to verify we are on the right commit.
d, err := repo.Date()
if d.Format(longForm) != "2015-07-29 09:46:39 -0400" {
t.Error("Error checking checked out Git commit date")
}
if err != nil {
t.Error(err)
}
// Verify that we can set the version something other than short hash
err = repo.UpdateVersion("master")
if err != nil {
t.Errorf("Unable to update Git repo version. Err was %s", err)
}
err = repo.UpdateVersion("806b07b08faa21cfbdae93027904f80174679402")
if err != nil {
t.Errorf("Unable to update Git repo version. Err was %s", err)
}
v, err = repo.Version()
if v != "806b07b08faa21cfbdae93027904f80174679402" {
t.Error("Error checking checked out Git version")
}
if err != nil {
t.Error(err)
}
tags, err := repo.Tags()
if err != nil {
t.Error(err)
}
if tags[0] != "1.0.0" {
t.Error("Git tags is not reporting the correct version")
}
branches, err := repo.Branches()
if err != nil {
t.Error(err)
}
// The branches should be HEAD, master, and test.
if branches[2] != "test" {
t.Error("Git is incorrectly returning branches")
}
if repo.IsReference("1.0.0") != true {
t.Error("Git is reporting a reference is not one")
}
if repo.IsReference("foo") == true {
t.Error("Git is reporting a non-existant reference is one")
}
if repo.IsDirty() == true {
t.Error("Git incorrectly reporting dirty")
}
}
func TestGitCheckLocal(t *testing.T) {
// Verify repo.CheckLocal fails for non-Git directories.
// TestGit is already checking on a valid repo
tempDir, err := ioutil.TempDir("", "go-vcs-git-tests")
if err != nil {
t.Error(err)
}
defer func() {
err = os.RemoveAll(tempDir)
if err != nil {
t.Error(err)
}
}()
repo, _ := NewGitRepo("", tempDir)
if repo.CheckLocal() == true {
t.Error("Git CheckLocal does not identify non-Git location")
}
// Test NewRepo when there's no local. This should simply provide a working
// instance without error based on looking at the remote localtion.
_, nrerr := NewRepo("https://github.com/Masterminds/VCSTestRepo", tempDir+"/VCSTestRepo")
if nrerr != nil {
t.Error(nrerr)
}
}