~juju/juju-core/1.10

« back to all changes in this revision

Viewing changes to bzr/bzr_test.go

  • Committer: Gustavo Niemeyer
  • Date: 2013-04-03 21:16:56 UTC
  • mto: (1104.3.1 publish-command)
  • mto: This revision was merged to the branch mainline in revision 1116.
  • Revision ID: gustavo@niemeyer.net-20130403211656-30su6znhc9i3255w
bzr: add package to handle Bazaar branches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package bzr_test
 
2
 
 
3
import (
 
4
        . "launchpad.net/gocheck"
 
5
        "launchpad.net/juju-core/bzr"
 
6
        "os"
 
7
        "os/exec"
 
8
        "testing"
 
9
)
 
10
 
 
11
 
 
12
func Test(t *testing.T) {
 
13
        TestingT(t)
 
14
}
 
15
 
 
16
var _ = Suite(&BzrSuite{})
 
17
 
 
18
type BzrSuite struct{
 
19
        b *bzr.Branch
 
20
}
 
21
 
 
22
func (s *BzrSuite) SetUpTest(c *C) {
 
23
        b, err := bzr.New(c.MkDir())
 
24
        c.Assert(err, IsNil)
 
25
        c.Assert(b.Init(), IsNil)
 
26
        s.b = b
 
27
}
 
28
 
 
29
func (s *BzrSuite) TestNewFindsRoot(c *C) {
 
30
        err := os.Mkdir(s.b.Join("dir"), 0755)
 
31
        c.Assert(err, IsNil)
 
32
        b, err := bzr.New(s.b.Join("dir"))
 
33
        c.Assert(err, IsNil)
 
34
        c.Assert(b.Location(), Equals, s.b.Location())
 
35
}
 
36
 
 
37
func (s *BzrSuite) TestJoin(c *C) {
 
38
        b, err := bzr.New("lp:foo")
 
39
        c.Assert(err, IsNil)
 
40
        path := b.Join("baz", "bar")
 
41
        c.Assert(path, Equals, "lp:foo/baz/bar")
 
42
}
 
43
 
 
44
func (s *BzrSuite) TestErrorHandling(c *C) {
 
45
        b, err := bzr.New("/non/existent/path")
 
46
        c.Assert(err, IsNil)
 
47
        err = b.Init()
 
48
        c.Assert(err, ErrorMatches, `(?s)error running "bzr init":.*does not exist.*`)
 
49
}
 
50
 
 
51
func (s *BzrSuite) TestInit(c *C) {
 
52
        _, err := os.Stat(s.b.Join(".bzr"))
 
53
        c.Assert(err, IsNil)
 
54
}
 
55
 
 
56
func (s *BzrSuite) TestRevisionIdOnEmpty(c *C) {
 
57
        revid, err := s.b.RevisionId()
 
58
        c.Assert(err, ErrorMatches, "branch has no content")
 
59
        c.Assert(revid, Equals, "")
 
60
}
 
61
 
 
62
func (s *BzrSuite) TestCommit(c *C) {
 
63
        f, err := os.Create(s.b.Join("myfile"))
 
64
        c.Assert(err, IsNil)
 
65
        f.Close()
 
66
        err = s.b.Add("myfile")
 
67
        c.Assert(err, IsNil)
 
68
        err = s.b.Commit("my log message")
 
69
        c.Assert(err, IsNil)
 
70
 
 
71
        revid, err := s.b.RevisionId()
 
72
        c.Assert(err, IsNil)
 
73
 
 
74
        cmd := exec.Command("bzr", "log", "--long", "-v", s.b.Location())
 
75
        output, err := cmd.CombinedOutput()
 
76
        c.Assert(err, IsNil)
 
77
        c.Assert(string(output), Matches, "(?s).*revision-id: " + revid + "\n.*message:\n.*my log message\n.*added:\n.*myfile .*")
 
78
}
 
79
 
 
80
func (s *BzrSuite) TestPush(c *C) {
 
81
        b1, err := bzr.New(c.MkDir())
 
82
        c.Assert(err, IsNil)
 
83
        b2, err := bzr.New(c.MkDir())
 
84
        c.Assert(err, IsNil)
 
85
        b3, err := bzr.New(c.MkDir())
 
86
        c.Assert(err, IsNil)
 
87
        c.Assert(b1.Init(), IsNil)
 
88
        c.Assert(b2.Init(), IsNil)
 
89
        c.Assert(b3.Init(), IsNil)
 
90
 
 
91
        // Create and add b1/file to the branch.
 
92
        f, err := os.Create(b1.Join("file"))
 
93
        c.Assert(err, IsNil)
 
94
        f.Close()
 
95
        err = b1.Add("file")
 
96
        c.Assert(err, IsNil)
 
97
        err = b1.Commit("added file")
 
98
        c.Assert(err, IsNil)
 
99
 
 
100
        // Push file to b2.
 
101
        err = b1.Push(&bzr.PushAttr{Location: b2.Location()})
 
102
        c.Assert(err, IsNil)
 
103
 
 
104
        // Push location should be set to b2.
 
105
        location, err := b1.PushLocation()
 
106
        c.Assert(err, IsNil)
 
107
        c.Assert(location, Equals, b2.Location())
 
108
 
 
109
        // Now push it to b3.
 
110
        err = b1.Push(&bzr.PushAttr{Location: b3.Location()})
 
111
        c.Assert(err, IsNil)
 
112
 
 
113
        // Push location is still set to b2.
 
114
        location, err = b1.PushLocation()
 
115
        c.Assert(err, IsNil)
 
116
        c.Assert(location, Equals, b2.Location())
 
117
 
 
118
        // Push it again, this time with the remember flag set.
 
119
        err = b1.Push(&bzr.PushAttr{Location: b3.Location(), Remember: true})
 
120
        c.Assert(err, IsNil)
 
121
 
 
122
        // Now the push location has shifted to b3.
 
123
        location, err = b1.PushLocation()
 
124
        c.Assert(err, IsNil)
 
125
        c.Assert(location, Equals, b3.Location())
 
126
 
 
127
        // Both b2 and b3 should have the file.
 
128
        _, err = os.Stat(b2.Join("file"))
 
129
        c.Assert(err, IsNil)
 
130
        _, err = os.Stat(b3.Join("file"))
 
131
        c.Assert(err, IsNil)
 
132
}