~rogpeppe/juju-core/azure

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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package bzr_test

import (
	"os"
	"os/exec"
	"testing"

	. "launchpad.net/gocheck"

	"launchpad.net/juju-core/bzr"
)

func Test(t *testing.T) {
	TestingT(t)
}

var _ = Suite(&BzrSuite{})

type BzrSuite struct {
	b *bzr.Branch
}

func (s *BzrSuite) SetUpTest(c *C) {
	s.b = bzr.New(c.MkDir())
	c.Assert(s.b.Init(), IsNil)
}

func (s *BzrSuite) TestNewFindsRoot(c *C) {
	err := os.Mkdir(s.b.Join("dir"), 0755)
	c.Assert(err, IsNil)
	b := bzr.New(s.b.Join("dir"))
	c.Assert(b.Location(), Equals, s.b.Location())
}

func (s *BzrSuite) TestJoin(c *C) {
	path := bzr.New("lp:foo").Join("baz", "bar")
	c.Assert(path, Equals, "lp:foo/baz/bar")
}

func (s *BzrSuite) TestErrorHandling(c *C) {
	err := bzr.New("/non/existent/path").Init()
	c.Assert(err, ErrorMatches, `(?s)error running "bzr init":.*does not exist.*`)
}

func (s *BzrSuite) TestInit(c *C) {
	_, err := os.Stat(s.b.Join(".bzr"))
	c.Assert(err, IsNil)
}

func (s *BzrSuite) TestRevisionIdOnEmpty(c *C) {
	revid, err := s.b.RevisionId()
	c.Assert(err, ErrorMatches, "branch has no content")
	c.Assert(revid, Equals, "")
}

func (s *BzrSuite) TestCommit(c *C) {
	f, err := os.Create(s.b.Join("myfile"))
	c.Assert(err, IsNil)
	f.Close()
	err = s.b.Add("myfile")
	c.Assert(err, IsNil)
	err = s.b.Commit("my log message")
	c.Assert(err, IsNil)

	revid, err := s.b.RevisionId()
	c.Assert(err, IsNil)

	cmd := exec.Command("bzr", "log", "--long", "--show-ids", "-v", s.b.Location())
	output, err := cmd.CombinedOutput()
	c.Assert(err, IsNil)
	c.Assert(string(output), Matches, "(?s).*revision-id: "+revid+"\n.*message:\n.*my log message\n.*added:\n.*myfile .*")
}

func (s *BzrSuite) TestPush(c *C) {
	b1 := bzr.New(c.MkDir())
	b2 := bzr.New(c.MkDir())
	b3 := bzr.New(c.MkDir())
	c.Assert(b1.Init(), IsNil)
	c.Assert(b2.Init(), IsNil)
	c.Assert(b3.Init(), IsNil)

	// Create and add b1/file to the branch.
	f, err := os.Create(b1.Join("file"))
	c.Assert(err, IsNil)
	f.Close()
	err = b1.Add("file")
	c.Assert(err, IsNil)
	err = b1.Commit("added file")
	c.Assert(err, IsNil)

	// Push file to b2.
	err = b1.Push(&bzr.PushAttr{Location: b2.Location()})
	c.Assert(err, IsNil)

	// Push location should be set to b2.
	location, err := b1.PushLocation()
	c.Assert(err, IsNil)
	c.Assert(location, Equals, b2.Location())

	// Now push it to b3.
	err = b1.Push(&bzr.PushAttr{Location: b3.Location()})
	c.Assert(err, IsNil)

	// Push location is still set to b2.
	location, err = b1.PushLocation()
	c.Assert(err, IsNil)
	c.Assert(location, Equals, 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, IsNil)

	// Now the push location has shifted to b3.
	location, err = b1.PushLocation()
	c.Assert(err, IsNil)
	c.Assert(location, Equals, b3.Location())

	// Both b2 and b3 should have the file.
	_, err = os.Stat(b2.Join("file"))
	c.Assert(err, IsNil)
	_, err = os.Stat(b3.Join("file"))
	c.Assert(err, IsNil)
}

func (s *BzrSuite) TestCheckClean(c *C) {
	err := s.b.CheckClean()
	c.Assert(err, IsNil)

	// Create and add b1/file to the branch.
	f, err := os.Create(s.b.Join("file"))
	c.Assert(err, IsNil)
	f.Close()

	err = s.b.CheckClean()
	c.Assert(err, ErrorMatches, `branch is not clean \(bzr status\)`)
}