/
git_test.go
157 lines (121 loc) · 3.12 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
package git
import (
"io/ioutil"
"os"
"os/exec"
"testing"
"time"
"github.com/spf13/afero"
)
var (
testHasGit bool
timeout = 5 * time.Second
)
func init() {
// Thanks to Mitchell Hashimoto!
if _, err := exec.LookPath("git"); err == nil {
testHasGit = true
}
}
type mockLog struct{}
func (m *mockLog) Infof(format string, args ...interface{}) {}
func (m *mockLog) Errorf(format string, args ...interface{}) {}
func TestGitDryRun(t *testing.T) {
if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}
appFs = afero.NewMemMapFs()
repo, err := New(new(mockLog), true, "/tmp/ktest", "", timeout).Start()
if err != nil {
t.Errorf("failed to start git: %v", err)
}
_, err = repo.Status()
if err != nil {
t.Error(err)
}
repo.Stop()
}
// testing with real git repositories and commands
func TestGit(t *testing.T) {
if !testHasGit {
t.Log("git not found, skipping")
t.Skip()
}
dir, err := ioutil.TempDir("", "katafygio-tests")
if err != nil {
t.Fatal("failed to create a temp dir for tests")
}
defer os.RemoveAll(dir)
repo, err := New(new(mockLog), false, dir, "", timeout).Start()
if err != nil {
t.Errorf("failed to start git: %v", err)
}
changed, err := repo.Status()
if changed || err != nil {
t.Errorf("Status should return false on empty new repos (%v)", err)
}
_ = ioutil.WriteFile(dir+"/t.yaml", []byte{42}, 0600)
changed, err = repo.Status()
if !changed || err != nil {
t.Errorf("Status should return true on non committed files (%v)", err)
}
changed, err = repo.Commit()
if !changed || err != nil {
t.Errorf("Commit should notify changes and not fail (%v)", err)
}
changed, err = repo.Status()
if changed || err != nil {
t.Errorf("Status should return false after a add+commit (%v)", err)
}
changed, err = repo.Commit()
if changed || err != nil {
t.Errorf("Commit shouldn't notify changes on unchanged repos (%v)", err)
}
// re-use the previous repos for clone tests
newdir, err := ioutil.TempDir("", "katafygio-tests")
if err != nil {
t.Fatal("failed to create a temp dir for tests")
}
defer os.RemoveAll(newdir)
repo.LocalDir = newdir
repo.URL = dir
err = repo.CloneOrInit()
if err != nil {
t.Errorf("clone failed: %v", err)
}
_ = ioutil.WriteFile(newdir+"/t2.yaml", []byte{42}, 0600)
repo.commitAndPush()
changed, err = repo.Status()
if changed || err != nil {
t.Errorf("Status should return false after a add+commit+push (%v)", err)
}
repo.Stop()
// test various failure modes
_, err = repo.Start()
if err == nil {
t.Error("Start/Clone on an existing repository should fail")
}
err = repo.Git("fortzob", "42")
if err == nil {
t.Error("Git should fail with unknown subcommands")
}
if err == nil {
t.Error("clone should fail on existing repos")
}
notrepo, err := ioutil.TempDir("", "katafygio-tests")
if err != nil {
t.Fatal("failed to create a temp dir for tests")
}
defer os.RemoveAll(notrepo)
repo.LocalDir = notrepo
_, err = repo.Status()
if err == nil {
t.Error("Status should fail on a non-repos")
}
repo.commitAndPush()
_, err = repo.Commit()
if err == nil {
t.Error("Commit should fail on a non-repos")
}
}