github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/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 "io/ioutil" 8 "os" 9 "os/exec" 10 "path/filepath" 11 stdtesting "testing" 12 13 gc "launchpad.net/gocheck" 14 15 "github.com/juju/juju/bzr" 16 "github.com/juju/juju/testing" 17 ) 18 19 func Test(t *stdtesting.T) { 20 gc.TestingT(t) 21 } 22 23 var _ = gc.Suite(&BzrSuite{}) 24 25 type BzrSuite struct { 26 testing.BaseSuite 27 b *bzr.Branch 28 } 29 30 const bzr_config = `[DEFAULT] 31 email = testing <test@example.com> 32 ` 33 34 func (s *BzrSuite) SetUpTest(c *gc.C) { 35 s.BaseSuite.SetUpTest(c) 36 bzrdir := c.MkDir() 37 s.PatchEnvironment("BZR_HOME", bzrdir) 38 err := os.Mkdir(filepath.Join(bzrdir, ".bazaar"), 0755) 39 c.Assert(err, gc.IsNil) 40 err = ioutil.WriteFile( 41 filepath.Join(bzrdir, ".bazaar", "bazaar.conf"), 42 []byte(bzr_config), 0644) 43 c.Assert(err, gc.IsNil) 44 s.b = bzr.New(c.MkDir()) 45 c.Assert(s.b.Init(), gc.IsNil) 46 } 47 48 func (s *BzrSuite) TestNewFindsRoot(c *gc.C) { 49 err := os.Mkdir(s.b.Join("dir"), 0755) 50 c.Assert(err, gc.IsNil) 51 b := bzr.New(s.b.Join("dir")) 52 // When bzr has to search for the root, it will expand any symlinks it 53 // found along the way. 54 path, err := filepath.EvalSymlinks(s.b.Location()) 55 c.Assert(err, gc.IsNil) 56 c.Assert(b.Location(), gc.Equals, path) 57 } 58 59 func (s *BzrSuite) TestJoin(c *gc.C) { 60 path := bzr.New("lp:foo").Join("baz", "bar") 61 c.Assert(path, gc.Equals, "lp:foo/baz/bar") 62 } 63 64 func (s *BzrSuite) TestErrorHandling(c *gc.C) { 65 err := bzr.New("/non/existent/path").Init() 66 c.Assert(err, gc.ErrorMatches, `(?s)error running "bzr init":.*does not exist.*`) 67 } 68 69 func (s *BzrSuite) TestInit(c *gc.C) { 70 _, err := os.Stat(s.b.Join(".bzr")) 71 c.Assert(err, gc.IsNil) 72 } 73 74 func (s *BzrSuite) TestRevisionIdOnEmpty(c *gc.C) { 75 revid, err := s.b.RevisionId() 76 c.Assert(err, gc.ErrorMatches, "branch has no content") 77 c.Assert(revid, gc.Equals, "") 78 } 79 80 func (s *BzrSuite) TestCommit(c *gc.C) { 81 f, err := os.Create(s.b.Join("myfile")) 82 c.Assert(err, gc.IsNil) 83 f.Close() 84 err = s.b.Add("myfile") 85 c.Assert(err, gc.IsNil) 86 err = s.b.Commit("my log message") 87 c.Assert(err, gc.IsNil) 88 89 revid, err := s.b.RevisionId() 90 c.Assert(err, gc.IsNil) 91 92 cmd := exec.Command("bzr", "log", "--long", "--show-ids", "-v", s.b.Location()) 93 output, err := cmd.CombinedOutput() 94 c.Assert(err, gc.IsNil) 95 c.Assert(string(output), gc.Matches, "(?s).*revision-id: "+revid+"\n.*message:\n.*my log message\n.*added:\n.*myfile .*") 96 } 97 98 func (s *BzrSuite) TestPush(c *gc.C) { 99 b1 := bzr.New(c.MkDir()) 100 b2 := bzr.New(c.MkDir()) 101 b3 := bzr.New(c.MkDir()) 102 c.Assert(b1.Init(), gc.IsNil) 103 c.Assert(b2.Init(), gc.IsNil) 104 c.Assert(b3.Init(), gc.IsNil) 105 106 // Create and add b1/file to the branch. 107 f, err := os.Create(b1.Join("file")) 108 c.Assert(err, gc.IsNil) 109 f.Close() 110 err = b1.Add("file") 111 c.Assert(err, gc.IsNil) 112 err = b1.Commit("added file") 113 c.Assert(err, gc.IsNil) 114 115 // Push file to b2. 116 err = b1.Push(&bzr.PushAttr{Location: b2.Location()}) 117 c.Assert(err, gc.IsNil) 118 119 // Push location should be set to b2. 120 location, err := b1.PushLocation() 121 c.Assert(err, gc.IsNil) 122 c.Assert(location, gc.Equals, b2.Location()) 123 124 // Now push it to b3. 125 err = b1.Push(&bzr.PushAttr{Location: b3.Location()}) 126 c.Assert(err, gc.IsNil) 127 128 // Push location is still set to b2. 129 location, err = b1.PushLocation() 130 c.Assert(err, gc.IsNil) 131 c.Assert(location, gc.Equals, b2.Location()) 132 133 // Push it again, this time with the remember flag set. 134 err = b1.Push(&bzr.PushAttr{Location: b3.Location(), Remember: true}) 135 c.Assert(err, gc.IsNil) 136 137 // Now the push location has shifted to b3. 138 location, err = b1.PushLocation() 139 c.Assert(err, gc.IsNil) 140 c.Assert(location, gc.Equals, b3.Location()) 141 142 // Both b2 and b3 should have the file. 143 _, err = os.Stat(b2.Join("file")) 144 c.Assert(err, gc.IsNil) 145 _, err = os.Stat(b3.Join("file")) 146 c.Assert(err, gc.IsNil) 147 } 148 149 func (s *BzrSuite) TestCheckClean(c *gc.C) { 150 err := s.b.CheckClean() 151 c.Assert(err, gc.IsNil) 152 153 // Create and add b1/file to the branch. 154 f, err := os.Create(s.b.Join("file")) 155 c.Assert(err, gc.IsNil) 156 f.Close() 157 158 err = s.b.CheckClean() 159 c.Assert(err, gc.ErrorMatches, `branch is not clean \(bzr status\)`) 160 }