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
|
// Copyright 2014 Canonical Ltd.
// Copyright 2014 Cloudbase Solutions SRL
// Licensed under the LGPLv3, see LICENCE file for details.
package bzr_test
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
stdtesting "testing"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/utils/bzr"
)
func Test(t *stdtesting.T) {
gc.TestingT(t)
}
var _ = gc.Suite(&BzrSuite{})
type BzrSuite struct {
testing.CleanupSuite
b *bzr.Branch
}
const bzr_config = `[DEFAULT]
email = testing <test@example.com>
`
func (s *BzrSuite) SetUpTest(c *gc.C) {
s.CleanupSuite.SetUpTest(c)
bzrdir := c.MkDir()
s.PatchEnvironment("BZR_HOME", bzrdir)
err := os.MkdirAll(filepath.Join(bzrdir, bzrHome), 0755)
c.Assert(err, jc.ErrorIsNil)
err = ioutil.WriteFile(
filepath.Join(bzrdir, bzrHome, "bazaar.conf"),
[]byte(bzr_config), 0644)
c.Assert(err, jc.ErrorIsNil)
s.b = bzr.New(c.MkDir())
c.Assert(s.b.Init(), gc.IsNil)
}
func (s *BzrSuite) TestNewFindsRoot(c *gc.C) {
err := os.Mkdir(s.b.Join("dir"), 0755)
c.Assert(err, jc.ErrorIsNil)
b := bzr.New(s.b.Join("dir"))
// When bzr has to search for the root, it will expand any symlinks it
// found along the way.
path, err := filepath.EvalSymlinks(s.b.Location())
c.Assert(err, jc.ErrorIsNil)
c.Assert(b.Location(), jc.SamePath, path)
}
func (s *BzrSuite) TestJoin(c *gc.C) {
path := bzr.New("lp:foo").Join("baz", "bar")
c.Assert(path, gc.Equals, "lp:foo/baz/bar")
}
func (s *BzrSuite) TestErrorHandling(c *gc.C) {
err := bzr.New("/non/existent/path").Init()
c.Assert(err, gc.ErrorMatches, `(?s)error running "bzr init":.*does not exist.*`)
}
func (s *BzrSuite) TestInit(c *gc.C) {
_, err := os.Stat(s.b.Join(".bzr"))
c.Assert(err, jc.ErrorIsNil)
}
func (s *BzrSuite) TestRevisionIdOnEmpty(c *gc.C) {
revid, err := s.b.RevisionId()
c.Assert(err, gc.ErrorMatches, "branch has no content")
c.Assert(revid, gc.Equals, "")
}
func (s *BzrSuite) TestCommit(c *gc.C) {
f, err := os.Create(s.b.Join("myfile"))
c.Assert(err, jc.ErrorIsNil)
f.Close()
err = s.b.Add("myfile")
c.Assert(err, jc.ErrorIsNil)
err = s.b.Commit("my log message")
c.Assert(err, jc.ErrorIsNil)
revid, err := s.b.RevisionId()
c.Assert(err, jc.ErrorIsNil)
cmd := exec.Command("bzr", "log", "--long", "--show-ids", "-v", s.b.Location())
output, err := cmd.CombinedOutput()
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(output), gc.Matches, "(?s).*revision-id: "+revid+"\n.*message:\n.*my log message\n.*added:\n.*myfile .*")
}
func (s *BzrSuite) TestPush(c *gc.C) {
b1 := bzr.New(c.MkDir())
b2 := bzr.New(c.MkDir())
b3 := bzr.New(c.MkDir())
c.Assert(b1.Init(), gc.IsNil)
c.Assert(b2.Init(), gc.IsNil)
c.Assert(b3.Init(), gc.IsNil)
// Create and add b1/file to the branch.
f, err := os.Create(b1.Join("file"))
c.Assert(err, jc.ErrorIsNil)
f.Close()
err = b1.Add("file")
c.Assert(err, jc.ErrorIsNil)
err = b1.Commit("added file")
c.Assert(err, jc.ErrorIsNil)
// Push file to b2.
err = b1.Push(&bzr.PushAttr{Location: b2.Location()})
c.Assert(err, jc.ErrorIsNil)
// Push location should be set to b2.
location, err := b1.PushLocation()
c.Assert(err, jc.ErrorIsNil)
c.Assert(location, jc.SamePath, b2.Location())
// Now push it to b3.
err = b1.Push(&bzr.PushAttr{Location: b3.Location()})
c.Assert(err, jc.ErrorIsNil)
// Push location is still set to b2.
location, err = b1.PushLocation()
c.Assert(err, jc.ErrorIsNil)
c.Assert(location, jc.SamePath, b2.Location())
// Push it again, this time with the remember flag set.
err = b1.Push(&bzr.PushAttr{Location: b3.Location(), Remember: true})
c.Assert(err, jc.ErrorIsNil)
// Now the push location has shifted to b3.
location, err = b1.PushLocation()
c.Assert(err, jc.ErrorIsNil)
c.Assert(location, jc.SamePath, b3.Location())
// Both b2 and b3 should have the file.
_, err = os.Stat(b2.Join("file"))
c.Assert(err, jc.ErrorIsNil)
_, err = os.Stat(b3.Join("file"))
c.Assert(err, jc.ErrorIsNil)
}
func (s *BzrSuite) TestCheckClean(c *gc.C) {
err := s.b.CheckClean()
c.Assert(err, jc.ErrorIsNil)
// Create and add b1/file to the branch.
f, err := os.Create(s.b.Join("file"))
c.Assert(err, jc.ErrorIsNil)
f.Close()
err = s.b.CheckClean()
c.Assert(err, gc.ErrorMatches, `branch is not clean \(bzr status\)`)
}
|