-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo_test.go
122 lines (103 loc) · 3.08 KB
/
repo_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
package gerrittest
import (
"os"
"path/filepath"
"strings"
"github.com/opalmer/dockertest"
. "gopkg.in/check.v1"
)
type RepoTest struct {
repos []*Repository
}
var _ = Suite(&RepoTest{})
func (s *RepoTest) TearDownTest(c *C) {
for _, repo := range s.repos {
c.Assert(repo.Destroy(), IsNil)
}
s.repos = []*Repository{}
}
func (s *RepoTest) newRepository(c *C) *Repository {
cfg := NewConfig()
repo, err := NewRepository(cfg)
c.Assert(err, IsNil)
s.repos = append(s.repos, repo)
return repo
}
func (s *RepoTest) TestNewRepository_InitCalled(c *C) {
repo := s.newRepository(c)
stat, err := os.Stat(filepath.Join(repo.Root, ".git"))
c.Assert(err, IsNil)
c.Assert(stat.IsDir(), Equals, true)
}
func (s *RepoTest) TestNewRepository_InstallsCommitHook(c *C) {
repo := s.newRepository(c)
stat, err := os.Stat(filepath.Join(repo.Root, ".git", "hooks", "commit-msg"))
c.Assert(err, IsNil)
c.Assert(stat.Mode(), Equals, os.FileMode(0700))
}
func (s *RepoTest) TestNewRepository_ConfiguresRepo(c *C) {
repo := s.newRepository(c)
cfg := NewConfig()
for key, value := range cfg.GitConfig {
stdout, _, err := repo.Git([]string{"config", "--local", "--get", key})
c.Assert(err, IsNil)
c.Assert(strings.Contains(stdout, value), Equals, true)
}
}
func (s *RepoTest) TestAmend_NoCommits(c *C) {
repo := s.newRepository(c)
c.Assert(repo.Amend(), ErrorMatches, ErrNoCommits.Error())
}
func (s *RepoTest) TestCommitAndAmend(c *C) {
repo := s.newRepository(c)
c.Assert(repo.Commit("hello"), IsNil)
c.Assert(repo.Amend(), IsNil)
}
func (s *RepoTest) TestGetRemote_ErrRemoteDoesNotExist(c *C) {
repo := s.newRepository(c)
_, err := repo.GetRemote("test")
c.Assert(err, ErrorMatches, ErrRemoteDoesNotExist.Error())
}
func (s *RepoTest) TestAddRemote_DoesNotExist(c *C) {
repo := s.newRepository(c)
c.Assert(repo.AddRemote("test", "http://localhost/"), IsNil)
remote, err := repo.GetRemote("test")
c.Assert(err, IsNil)
c.Assert(remote, Equals, "http://localhost/")
}
func (s *RepoTest) TestAddRemote_ErrRemoteDiffers(c *C) {
repo := s.newRepository(c)
c.Assert(repo.AddRemote("test", "http://localhost/"), IsNil)
c.Assert(repo.AddRemote("test", "http://localhost2/"), ErrorMatches, ErrRemoteDiffers.Error())
}
func (s *RepoTest) TestAddOriginFromContainer(c *C) {
repo := s.newRepository(c)
container := &Container{
SSH: &dockertest.Port{
Address: "0.0.0.0",
Public: 2222,
},
}
c.Assert(repo.AddOriginFromContainer(container, "foo"), IsNil)
remote, err := repo.GetRemote("origin")
c.Assert(err, IsNil)
c.Assert(remote, Equals, "ssh://[email protected]:2222/foo")
}
func (s *RepoTest) TestChangeID_ErrNoCommits(c *C) {
repo := s.newRepository(c)
_, err := repo.ChangeID()
c.Assert(err, ErrorMatches, ErrNoCommits.Error())
}
func (s *RepoTest) TestChangeID(c *C) {
repo := s.newRepository(c)
c.Assert(repo.Commit("foo"), IsNil)
change, err := repo.ChangeID()
c.Assert(err, IsNil)
c.Assert(len(change), Not(Equals), 0)
}
func (s *RepoTest) TestDestroy(c *C) {
repo := s.newRepository(c)
c.Assert(repo.Destroy(), IsNil)
_, err := os.Stat(repo.Root)
c.Assert(os.IsNotExist(err), Equals, true)
}