github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/bzr/bzr_test.go (about)

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