/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
1
# (C) 2005, 2006 Canonical Ltd
905 by Martin Pool
- merge aaron's append_multiple.patch
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
17
"""Tests for branch implementations - tests a branch format."""
18
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
19
import os
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
20
import sys
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
21
1534.4.2 by Robert Collins
Introduce BranchFormats - factoring out intialisation of Branches.
22
import bzrlib.branch as branch
23
from bzrlib.branch import Branch, needs_read_lock, needs_write_lock
1393.1.5 by Martin Pool
- move copy_branch into bzrlib.clone
24
from bzrlib.clone import copy_branch
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
25
from bzrlib.commit import commit
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
26
import bzrlib.errors as errors
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
27
from bzrlib.errors import (NoSuchRevision,
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
28
                           NoSuchFile,
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
29
                           UninitializableFormat,
30
                           NotBranchError,
31
                           )
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
32
import bzrlib.gpg
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
33
from bzrlib.osutils import getcwd
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
34
from bzrlib.tests import TestCase, TestCaseInTempDir, TestSkipped
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
35
from bzrlib.tests.HTTPTestUtil import TestCaseWithWebserver
1260 by Martin Pool
- some updates for fetch/update function
36
from bzrlib.trace import mutter
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
37
import bzrlib.transactions as transactions
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
38
from bzrlib.transport import get_transport
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
39
from bzrlib.revision import NULL_REVISION
1092.2.25 by Robert Collins
support ghosts in commits
40
1185.16.56 by Martin Pool
doc
41
# TODO: Make a branch using basis branch, and check that it 
42
# doesn't request any files that could have been avoided, by 
43
# hooking into the Transport.
44
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
45
46
class TestCaseWithBranch(TestCaseInTempDir):
47
48
    def setUp(self):
49
        super(TestCaseWithBranch, self).setUp()
50
        self.branch = None
51
52
    def get_branch(self):
53
        if self.branch is None:
54
            self.branch = self.make_branch('.')
55
        return self.branch
56
57
    def make_branch(self, relpath):
58
        try:
59
            return self.branch_format.initialize(relpath)
60
        except UninitializableFormat:
61
            raise TestSkipped("Format %s is not initializable.")
62
63
64
class TestBranch(TestCaseWithBranch):
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
65
1102 by Martin Pool
- merge test refactoring from robertc
66
    def test_append_revisions(self):
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
67
        """Test appending more than one revision"""
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
68
        br = self.get_branch()
905 by Martin Pool
- merge aaron's append_multiple.patch
69
        br.append_revision("rev1")
70
        self.assertEquals(br.revision_history(), ["rev1",])
71
        br.append_revision("rev2", "rev3")
72
        self.assertEquals(br.revision_history(), ["rev1", "rev2", "rev3"])
1110 by Martin Pool
- merge aaron's merge improvements:
73
1260 by Martin Pool
- some updates for fetch/update function
74
    def test_fetch_revisions(self):
75
        """Test fetch-revision operation."""
76
        from bzrlib.fetch import Fetcher
77
        os.mkdir('b1')
78
        os.mkdir('b2')
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
79
        b1 = self.make_branch('b1')
80
        b2 = self.make_branch('b2')
1185.31.35 by John Arbash Meinel
Couple small fixes, all tests pass on cygwin.
81
        file('b1/foo', 'w').write('hello')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
82
        b1.working_tree().add(['foo'], ['foo-id'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
83
        b1.working_tree().commit('lala!', rev_id='revision-1', allow_pointless=False)
1260 by Martin Pool
- some updates for fetch/update function
84
85
        mutter('start fetch')
86
        f = Fetcher(from_branch=b1, to_branch=b2)
87
        eq = self.assertEquals
88
        eq(f.count_copied, 1)
89
        eq(f.last_revision, 'revision-1')
90
91
        rev = b2.get_revision('revision-1')
92
        tree = b2.revision_tree('revision-1')
93
        eq(tree.get_file_text('foo-id'), 'hello')
94
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
95
    def test_revision_tree(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
96
        b1 = self.get_branch()
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
97
        b1.working_tree().commit('lala!', rev_id='revision-1', allow_pointless=True)
1185.12.98 by Aaron Bentley
Support for forcing merges of unrelated trees
98
        tree = b1.revision_tree('revision-1')
99
        tree = b1.revision_tree(None)
100
        self.assertEqual(len(tree.list_files()), 0)
101
        tree = b1.revision_tree(NULL_REVISION)
102
        self.assertEqual(len(tree.list_files()), 0)
103
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
104
    def get_unbalanced_branch_pair(self):
105
        """Return two branches, a and b, with one file in a."""
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
106
        os.mkdir('a')
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
107
        br_a = self.make_branch('a')
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
108
        file('a/b', 'wb').write('b')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
109
        br_a.working_tree().add('b')
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
110
        commit(br_a, "silly commit", rev_id='A')
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
111
        os.mkdir('b')
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
112
        br_b = self.make_branch('b')
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
113
        return br_a, br_b
114
115
    def get_balanced_branch_pair(self):
116
        """Returns br_a, br_b as with one commit in a, and b has a's stores."""
117
        br_a, br_b = self.get_unbalanced_branch_pair()
118
        br_a.push_stores(br_b)
119
        return br_a, br_b
120
121
    def test_push_stores(self):
122
        """Copy the stores from one branch to another"""
123
        br_a, br_b = self.get_unbalanced_branch_pair()
124
        # ensure the revision is missing.
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
125
        self.assertRaises(NoSuchRevision, br_b.get_revision, 
126
                          br_a.revision_history()[0])
1391 by Robert Collins
merge from integration
127
        br_a.push_stores(br_b)
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
128
        # check that b now has all the data from a's first commit.
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
129
        rev = br_b.get_revision(br_a.revision_history()[0])
130
        tree = br_b.revision_tree(br_a.revision_history()[0])
131
        for file_id in tree:
132
            if tree.inventory[file_id].kind == "file":
133
                tree.get_file(file_id).read()
134
        return br_a, br_b
135
136
    def test_copy_branch(self):
137
        """Copy the stores from one branch to another"""
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
138
        br_a, br_b = self.get_balanced_branch_pair()
1185.10.1 by Aaron Bentley
Added --basis option to bzr branch
139
        commit(br_b, "silly commit")
140
        os.mkdir('c')
141
        br_c = copy_branch(br_a, 'c', basis_branch=br_b)
142
        self.assertEqual(br_a.revision_history(), br_c.revision_history())
1393.1.23 by Martin Pool
- fix cloning of part of a branch
143
144
    def test_copy_partial(self):
145
        """Copy only part of the history of a branch."""
146
        self.build_tree(['a/', 'a/one'])
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
147
        br_a = self.make_branch('a')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
148
        br_a.working_tree().add(['one'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
149
        br_a.working_tree().commit('commit one', rev_id='u@d-1')
1393.1.23 by Martin Pool
- fix cloning of part of a branch
150
        self.build_tree(['a/two'])
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
151
        br_a.working_tree().add(['two'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
152
        br_a.working_tree().commit('commit two', rev_id='u@d-2')
1393.1.23 by Martin Pool
- fix cloning of part of a branch
153
        br_b = copy_branch(br_a, 'b', revision='u@d-1')
154
        self.assertEqual(br_b.last_revision(), 'u@d-1')
155
        self.assertTrue(os.path.exists('b/one'))
156
        self.assertFalse(os.path.exists('b/two'))
157
        
1092.2.25 by Robert Collins
support ghosts in commits
158
    def test_record_initial_ghost_merge(self):
159
        """A pending merge with no revision present is still a merge."""
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
160
        branch = self.get_branch()
1457.1.15 by Robert Collins
Move add_pending_merge to WorkingTree.
161
        branch.working_tree().add_pending_merge('non:existent@rev--ision--0--2')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
162
        branch.working_tree().commit('pretend to merge nonexistent-revision', rev_id='first')
1092.2.25 by Robert Collins
support ghosts in commits
163
        rev = branch.get_revision(branch.last_revision())
164
        self.assertEqual(len(rev.parent_ids), 1)
165
        # parent_sha1s is not populated now, WTF. rbc 20051003
166
        self.assertEqual(len(rev.parent_sha1s), 0)
167
        self.assertEqual(rev.parent_ids[0], 'non:existent@rev--ision--0--2')
168
1185.12.90 by Aaron Bentley
Fixed InvalidRevisionID handling in Branch.get_revision_xml
169
    def test_bad_revision(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
170
        self.assertRaises(errors.InvalidRevisionId, self.get_branch().get_revision, None)
1185.12.90 by Aaron Bentley
Fixed InvalidRevisionID handling in Branch.get_revision_xml
171
1092.2.28 by Robert Collins
reenable test of fetching a branch with ghosts
172
# TODO 20051003 RBC:
1092.2.25 by Robert Collins
support ghosts in commits
173
# compare the gpg-to-sign info for a commit with a ghost and 
174
#     an identical tree without a ghost
175
# fetch missing should rewrite the TOC of weaves to list newly available parents.
1393.1.9 by Martin Pool
- tidy up test assertion
176
        
1092.2.27 by Robert Collins
reenable pending merge tests in testbranch.py
177
    def test_pending_merges(self):
178
        """Tracking pending-merged revisions."""
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
179
        b = self.get_branch()
1457.1.14 by Robert Collins
Move pending_merges() to WorkingTree.
180
        wt = b.working_tree()
181
        self.assertEquals(wt.pending_merges(), [])
1457.1.15 by Robert Collins
Move add_pending_merge to WorkingTree.
182
        wt.add_pending_merge('foo@azkhazan-123123-abcabc')
183
        self.assertEquals(wt.pending_merges(), ['foo@azkhazan-123123-abcabc'])
184
        wt.add_pending_merge('foo@azkhazan-123123-abcabc')
185
        self.assertEquals(wt.pending_merges(), ['foo@azkhazan-123123-abcabc'])
186
        wt.add_pending_merge('wibble@fofof--20050401--1928390812')
1457.1.14 by Robert Collins
Move pending_merges() to WorkingTree.
187
        self.assertEquals(wt.pending_merges(),
1092.2.27 by Robert Collins
reenable pending merge tests in testbranch.py
188
                          ['foo@azkhazan-123123-abcabc',
189
                           'wibble@fofof--20050401--1928390812'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
190
        b.working_tree().commit("commit from base with two merges")
1092.2.27 by Robert Collins
reenable pending merge tests in testbranch.py
191
        rev = b.get_revision(b.revision_history()[0])
192
        self.assertEquals(len(rev.parent_ids), 2)
193
        self.assertEquals(rev.parent_ids[0],
194
                          'foo@azkhazan-123123-abcabc')
195
        self.assertEquals(rev.parent_ids[1],
196
                           'wibble@fofof--20050401--1928390812')
197
        # list should be cleared when we do a commit
1457.1.14 by Robert Collins
Move pending_merges() to WorkingTree.
198
        self.assertEquals(wt.pending_merges(), [])
1425 by Robert Collins
merge from Aaron - unbreaks open_containing and the fetch progress bar
199
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
200
    def test_sign_existing_revision(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
201
        branch = self.get_branch()
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
202
        branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
203
        from bzrlib.testament import Testament
204
        branch.sign_revision('A', bzrlib.gpg.LoopbackGPGStrategy(None))
205
        self.assertEqual(Testament.from_revision(branch, 'A').as_short_text(),
206
                         branch.revision_store.get('A', 'sig').read())
207
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
208
    def test_store_signature(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
209
        branch = self.get_branch()
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
210
        branch.store_revision_signature(bzrlib.gpg.LoopbackGPGStrategy(None),
211
                                        'FOO', 'A')
212
        self.assertEqual('FOO', branch.revision_store.get('A', 'sig').read())
213
1469 by Robert Collins
Change Transport.* to work with URL's.
214
    def test__relcontrolfilename(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
215
        self.assertEqual('.bzr/%25', self.get_branch()._rel_controlfilename('%'))
1469 by Robert Collins
Change Transport.* to work with URL's.
216
        
217
    def test__relcontrolfilename_empty(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
218
        self.assertEqual('.bzr', self.get_branch()._rel_controlfilename(''))
1469 by Robert Collins
Change Transport.* to work with URL's.
219
1185.35.11 by Aaron Bentley
Added support for branch nicks
220
    def test_nicks(self):
221
        """Branch nicknames"""
222
        os.mkdir('bzr.dev')
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
223
        branch = self.make_branch('bzr.dev')
1185.35.11 by Aaron Bentley
Added support for branch nicks
224
        self.assertEqual(branch.nick, 'bzr.dev')
225
        os.rename('bzr.dev', 'bzr.ab')
226
        branch = Branch.open('bzr.ab')
227
        self.assertEqual(branch.nick, 'bzr.ab')
228
        branch.nick = "Aaron's branch"
1185.35.12 by Aaron Bentley
Got writes of existing tree configs working.
229
        branch.nick = "Aaron's branch"
1185.35.11 by Aaron Bentley
Added support for branch nicks
230
        self.failUnless(os.path.exists(branch.controlfilename("branch.conf")))
231
        self.assertEqual(branch.nick, "Aaron's branch")
232
        os.rename('bzr.ab', 'integration')
233
        branch = Branch.open('integration')
234
        self.assertEqual(branch.nick, "Aaron's branch")
1185.35.12 by Aaron Bentley
Got writes of existing tree configs working.
235
        branch.nick = u"\u1234"
236
        self.assertEqual(branch.nick, u"\u1234")
1185.35.11 by Aaron Bentley
Added support for branch nicks
237
1185.35.15 by Aaron Bentley
Added branch nicks to revisions
238
    def test_commit_nicks(self):
239
        """Nicknames are committed to the revision"""
240
        os.mkdir('bzr.dev')
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
241
        branch = self.get_branch()
1185.35.15 by Aaron Bentley
Added branch nicks to revisions
242
        branch.nick = "My happy branch"
1185.33.27 by Martin Pool
[merge] much integrated work from robert and john
243
        branch.working_tree().commit('My commit respect da nick.')
1185.35.15 by Aaron Bentley
Added branch nicks to revisions
244
        committed = branch.get_revision(branch.last_revision())
245
        self.assertEqual(committed.properties["branch-nick"], 
246
                         "My happy branch")
247
1425 by Robert Collins
merge from Aaron - unbreaks open_containing and the fetch progress bar
248
1185.12.18 by Aaron Bentley
Fixed error handling when NotBranch on HTTP
249
class TestRemote(TestCaseWithWebserver):
1425 by Robert Collins
merge from Aaron - unbreaks open_containing and the fetch progress bar
250
1185.12.18 by Aaron Bentley
Fixed error handling when NotBranch on HTTP
251
    def test_open_containing(self):
252
        self.assertRaises(NotBranchError, Branch.open_containing,
253
                          self.get_remote_url(''))
254
        self.assertRaises(NotBranchError, Branch.open_containing,
255
                          self.get_remote_url('g/p/q'))
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
256
        try:
257
            branch = self.branch_format.initialize('.')
258
        except UninitializableFormat:
259
            raise TestSkipped("Format %s is not initializable.")
1442.1.64 by Robert Collins
Branch.open_containing now returns a tuple (Branch, relative-path).
260
        branch, relpath = Branch.open_containing(self.get_remote_url(''))
261
        self.assertEqual('', relpath)
262
        branch, relpath = Branch.open_containing(self.get_remote_url('g/p/q'))
263
        self.assertEqual('g/p/q', relpath)
1185.12.18 by Aaron Bentley
Fixed error handling when NotBranch on HTTP
264
        
1110 by Martin Pool
- merge aaron's merge improvements:
265
# TODO: rewrite this as a regular unittest, without relying on the displayed output        
266
#         >>> from bzrlib.commit import commit
267
#         >>> bzrlib.trace.silent = True
268
#         >>> br1 = ScratchBranch(files=['foo', 'bar'])
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
269
#         >>> br1.working_tree().add('foo')
270
#         >>> br1.working_tree().add('bar')
1110 by Martin Pool
- merge aaron's merge improvements:
271
#         >>> commit(br1, "lala!", rev_id="REVISION-ID-1", verbose=False)
272
#         >>> br2 = ScratchBranch()
273
#         >>> br2.update_revisions(br1)
274
#         Added 2 texts.
275
#         Added 1 inventories.
276
#         Added 1 revisions.
277
#         >>> br2.revision_history()
278
#         [u'REVISION-ID-1']
279
#         >>> br2.update_revisions(br1)
280
#         Added 0 revisions.
281
#         >>> br1.text_store.total_size() == br2.text_store.total_size()
282
#         True
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
283
284
class InstrumentedTransaction(object):
285
286
    def finish(self):
287
        self.calls.append('finish')
288
289
    def __init__(self):
290
        self.calls = []
291
292
1442.1.63 by Robert Collins
Remove self.lock_*...finally: self.unlock() dead chickens from branch.py.
293
class TestDecorator(object):
294
295
    def __init__(self):
296
        self._calls = []
297
298
    def lock_read(self):
299
        self._calls.append('lr')
300
301
    def lock_write(self):
302
        self._calls.append('lw')
303
304
    def unlock(self):
305
        self._calls.append('ul')
306
307
    @needs_read_lock
308
    def do_with_read(self):
309
        return 1
310
311
    @needs_read_lock
312
    def except_with_read(self):
313
        raise RuntimeError
314
315
    @needs_write_lock
316
    def do_with_write(self):
317
        return 2
318
319
    @needs_write_lock
320
    def except_with_write(self):
321
        raise RuntimeError
322
323
324
class TestDecorators(TestCase):
325
326
    def test_needs_read_lock(self):
327
        branch = TestDecorator()
328
        self.assertEqual(1, branch.do_with_read())
329
        self.assertEqual(['lr', 'ul'], branch._calls)
330
331
    def test_excepts_in_read_lock(self):
332
        branch = TestDecorator()
333
        self.assertRaises(RuntimeError, branch.except_with_read)
334
        self.assertEqual(['lr', 'ul'], branch._calls)
335
336
    def test_needs_write_lock(self):
337
        branch = TestDecorator()
338
        self.assertEqual(2, branch.do_with_write())
339
        self.assertEqual(['lw', 'ul'], branch._calls)
340
341
    def test_excepts_in_write_lock(self):
342
        branch = TestDecorator()
343
        self.assertRaises(RuntimeError, branch.except_with_write)
344
        self.assertEqual(['lw', 'ul'], branch._calls)
345
346
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
347
class TestBranchTransaction(TestCaseWithBranch):
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
348
349
    def setUp(self):
350
        super(TestBranchTransaction, self).setUp()
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
351
        self.branch = None
352
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
353
    def test_default_get_transaction(self):
354
        """branch.get_transaction on a new branch should give a PassThrough."""
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
355
        self.failUnless(isinstance(self.get_branch().get_transaction(),
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
356
                                   transactions.PassThroughTransaction))
357
358
    def test__set_new_transaction(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
359
        self.get_branch()._set_transaction(transactions.ReadOnlyTransaction())
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
360
361
    def test__set_over_existing_transaction_raises(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
362
        self.get_branch()._set_transaction(transactions.ReadOnlyTransaction())
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
363
        self.assertRaises(errors.LockError,
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
364
                          self.get_branch()._set_transaction,
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
365
                          transactions.ReadOnlyTransaction())
366
367
    def test_finish_no_transaction_raises(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
368
        self.assertRaises(errors.LockError, self.get_branch()._finish_transaction)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
369
370
    def test_finish_readonly_transaction_works(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
371
        self.get_branch()._set_transaction(transactions.ReadOnlyTransaction())
372
        self.get_branch()._finish_transaction()
373
        self.assertEqual(None, self.get_branch()._transaction)
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
374
375
    def test_unlock_calls_finish(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
376
        self.get_branch().lock_read()
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
377
        transaction = InstrumentedTransaction()
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
378
        self.get_branch()._transaction = transaction
379
        self.get_branch().unlock()
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
380
        self.assertEqual(['finish'], transaction.calls)
381
382
    def test_lock_read_acquires_ro_transaction(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
383
        self.get_branch().lock_read()
384
        self.failUnless(isinstance(self.get_branch().get_transaction(),
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
385
                                   transactions.ReadOnlyTransaction))
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
386
        self.get_branch().unlock()
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
387
        
388
    def test_lock_write_acquires_passthrough_transaction(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
389
        self.get_branch().lock_write()
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
390
        # cannot use get_transaction as its magic
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
391
        self.failUnless(isinstance(self.get_branch()._transaction,
1417.1.6 by Robert Collins
introduce transactions for grouping actions done to and with branches
392
                                   transactions.PassThroughTransaction))
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
393
        self.get_branch().unlock()
394
395
396
class TestBranchPushLocations(TestCaseWithBranch):
397
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
398
    def test_get_push_location_unset(self):
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
399
        self.assertEqual(None, self.get_branch().get_push_location())
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
400
401
    def test_get_push_location_exact(self):
1185.31.43 by John Arbash Meinel
Reintroduced ensure_config_dir_exists() for sftp
402
        from bzrlib.config import (branches_config_filename,
403
                                   ensure_config_dir_exists)
404
        ensure_config_dir_exists()
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
405
        fn = branches_config_filename()
406
        print >> open(fn, 'wt'), ("[%s]\n"
407
                                  "push_location=foo" %
408
                                  getcwd())
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
409
        self.assertEqual("foo", self.get_branch().get_push_location())
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
410
411
    def test_set_push_location(self):
1185.31.43 by John Arbash Meinel
Reintroduced ensure_config_dir_exists() for sftp
412
        from bzrlib.config import (branches_config_filename,
413
                                   ensure_config_dir_exists)
414
        ensure_config_dir_exists()
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
415
        fn = branches_config_filename()
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
416
        self.get_branch().set_push_location('foo')
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
417
        self.assertFileEqual("[%s]\n"
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
418
                             "push_location = foo" % getcwd(),
419
                             fn)
1490 by Robert Collins
Implement a 'bzr push' command, with saved locations; update diff to return 1.
420
421
    # TODO RBC 20051029 test getting a push location from a branch in a 
422
    # recursive section - that is, it appends the branch name.
1534.4.1 by Robert Collins
Allow parameterisation of the branch initialisation for bzrlib.
423
424
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
425
class TestFormat(TestCaseWithBranch):
426
    """Tests for the format itself."""
427
428
    def test_format_initialize_find_open(self):
429
        # loopback test to check the current format initializes to itself.
430
        made_branch = self.make_branch('.')
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
431
        self.failUnless(isinstance(made_branch, branch.Branch))
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
432
        t = get_transport('.')
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
433
        self.assertEqual(self.branch_format,
1534.4.4 by Robert Collins
Make BzrBranchFormat.find_format take a transport not a url for efficiency.
434
                         branch.BzrBranchFormat.find_format(t))
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
435
        opened_branch = self.branch_format.open(t)
1534.4.3 by Robert Collins
Implement BranchTestProviderAdapter, so tests now run across all branch formats.
436
        self.assertEqual(made_branch._branch_format, opened_branch._branch_format)
1534.4.5 by Robert Collins
Turn branch format.open into a factory.
437
        self.failUnless(isinstance(opened_branch, branch.Branch))
438
439
    def test_open_not_branch(self):
440
        self.assertRaises(NoSuchFile,
441
                          self.branch_format.open,
442
                          get_transport('.'))