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