~bzr-pqm/bzr/bzr.dev

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