/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
1
# (C) 2005,2006 Canonical Ltd
1399.1.12 by Robert Collins
add new test script
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
18
from cStringIO import StringIO
1399.1.12 by Robert Collins
add new test script
19
import os
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
20
21
import bzrlib
1399.1.12 by Robert Collins
add new test script
22
from bzrlib.branch import Branch
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
23
from bzrlib.bzrdir import BzrDir
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
24
import bzrlib.errors as errors
1508.1.3 by Robert Collins
Do not consider urls to be relative paths within working trees.
25
from bzrlib.errors import NotBranchError, NotVersionedError
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
26
from bzrlib.tests import TestCaseWithTransport
1399.1.12 by Robert Collins
add new test script
27
from bzrlib.trace import mutter
1185.31.49 by John Arbash Meinel
Some corrections using the new osutils.rename. **ALL TESTS PASS**
28
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
29
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
30
                                WorkingTree)
1399.1.12 by Robert Collins
add new test script
31
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
32
class TestTreeDirectory(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
33
34
    def test_kind_character(self):
35
        self.assertEqual(TreeDirectory().kind_character(), '/')
36
37
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
38
class TestTreeEntry(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
39
40
    def test_kind_character(self):
41
        self.assertEqual(TreeEntry().kind_character(), '???')
42
43
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
44
class TestTreeFile(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
45
46
    def test_kind_character(self):
47
        self.assertEqual(TreeFile().kind_character(), '')
48
49
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
50
class TestTreeLink(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
51
52
    def test_kind_character(self):
53
        self.assertEqual(TreeLink().kind_character(), '')
54
55
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
56
class TestWorkingTree(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
57
58
    def test_listfiles(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
59
        tree = WorkingTree.create_standalone('.')
1399.1.12 by Robert Collins
add new test script
60
        os.mkdir('dir')
61
        print >> open('file', 'w'), "content"
1185.31.49 by John Arbash Meinel
Some corrections using the new osutils.rename. **ALL TESTS PASS**
62
        if has_symlinks():
63
            os.symlink('target', 'symlink')
1399.1.12 by Robert Collins
add new test script
64
        files = list(tree.list_files())
65
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
66
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
1185.31.49 by John Arbash Meinel
Some corrections using the new osutils.rename. **ALL TESTS PASS**
67
        if has_symlinks():
68
            self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
69
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
70
    def test_open_containing(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
71
        branch = WorkingTree.create_standalone('.').branch
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
72
        wt, relpath = WorkingTree.open_containing()
73
        self.assertEqual('', relpath)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
74
        self.assertEqual(wt.basedir + '/', branch.base)
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
75
        wt, relpath = WorkingTree.open_containing(u'.')
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
76
        self.assertEqual('', relpath)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
77
        self.assertEqual(wt.basedir + '/', branch.base)
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
78
        wt, relpath = WorkingTree.open_containing('./foo')
79
        self.assertEqual('foo', relpath)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
80
        self.assertEqual(wt.basedir + '/', branch.base)
1508.1.3 by Robert Collins
Do not consider urls to be relative paths within working trees.
81
        # paths that are urls are just plain wrong for working trees.
82
        self.assertRaises(NotBranchError,
83
                          WorkingTree.open_containing, 
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
84
                          'file:///' + getcwd())
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
85
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
86
    def test_construct_with_branch(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
87
        branch = WorkingTree.create_standalone('.').branch
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
88
        tree = WorkingTree(branch.base, branch)
89
        self.assertEqual(branch, tree.branch)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
90
        self.assertEqual(branch.base, tree.basedir + '/')
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
91
    
92
    def test_construct_without_branch(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
93
        branch = WorkingTree.create_standalone('.').branch
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
94
        tree = WorkingTree(branch.base)
95
        self.assertEqual(branch.base, tree.branch.base)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
96
        self.assertEqual(branch.base, tree.basedir + '/')
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
97
98
    def test_basic_relpath(self):
99
        # for comprehensive relpath tests, see whitebox.py.
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
100
        tree = WorkingTree.create_standalone('.')
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
101
        self.assertEqual('child',
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
102
                         tree.relpath(pathjoin(getcwd(), 'child')))
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
103
104
    def test_lock_locks_branch(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
105
        tree = WorkingTree.create_standalone('.')
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
106
        tree.lock_read()
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
107
        self.assertEqual('r', tree.branch.peek_lock_mode())
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
108
        tree.unlock()
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
109
        self.assertEqual(None, tree.branch.peek_lock_mode())
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
110
        tree.lock_write()
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
111
        self.assertEqual('w', tree.branch.peek_lock_mode())
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
112
        tree.unlock()
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
113
        self.assertEqual(None, tree.branch.peek_lock_mode())
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
114
 
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
115
    def get_pullable_trees(self):
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
116
        self.build_tree(['from/', 'from/file', 'to/'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
117
        tree = WorkingTree.create_standalone('from')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
118
        tree.add('file')
119
        tree.commit('foo', rev_id='A')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
120
        tree_b = WorkingTree.create_standalone('to')
121
        return tree, tree_b
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
122
 
123
    def test_pull(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
124
        tree_a, tree_b = self.get_pullable_trees()
125
        tree_b.pull(tree_a.branch)
1534.4.28 by Robert Collins
first cut at merge from integration.
126
        self.failUnless(tree_b.branch.repository.has_revision('A'))
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
127
        self.assertEqual(['A'], tree_b.branch.revision_history())
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
128
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
129
    def test_pull_overwrites(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
130
        tree_a, tree_b = self.get_pullable_trees()
131
        tree_b.commit('foo', rev_id='B')
132
        self.assertEqual(['B'], tree_b.branch.revision_history())
133
        tree_b.pull(tree_a.branch, overwrite=True)
1534.4.28 by Robert Collins
first cut at merge from integration.
134
        self.failUnless(tree_b.branch.repository.has_revision('A'))
135
        self.failUnless(tree_b.branch.repository.has_revision('B'))
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
136
        self.assertEqual(['A'], tree_b.branch.revision_history())
1501 by Robert Collins
Move revert from Branch to WorkingTree.
137
138
    def test_revert(self):
139
        """Test selected-file revert"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
140
        tree = WorkingTree.create_standalone('.')
1501 by Robert Collins
Move revert from Branch to WorkingTree.
141
142
        self.build_tree(['hello.txt'])
143
        file('hello.txt', 'w').write('initial hello')
144
145
        self.assertRaises(NotVersionedError,
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
146
                          tree.revert, ['hello.txt'])
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
147
        tree.add(['hello.txt'])
148
        tree.commit('create initial hello.txt')
1501 by Robert Collins
Move revert from Branch to WorkingTree.
149
150
        self.check_file_contents('hello.txt', 'initial hello')
151
        file('hello.txt', 'w').write('new hello')
152
        self.check_file_contents('hello.txt', 'new hello')
153
154
        # revert file modified since last revision
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
155
        tree.revert(['hello.txt'])
1501 by Robert Collins
Move revert from Branch to WorkingTree.
156
        self.check_file_contents('hello.txt', 'initial hello')
157
        self.check_file_contents('hello.txt~', 'new hello')
158
1457.1.8 by Robert Collins
Replace the WorkingTree.revert method algorithm with a call to merge_inner.
159
        # reverting again does not clobber the backup
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
160
        tree.revert(['hello.txt'])
1501 by Robert Collins
Move revert from Branch to WorkingTree.
161
        self.check_file_contents('hello.txt', 'initial hello')
1457.1.8 by Robert Collins
Replace the WorkingTree.revert method algorithm with a call to merge_inner.
162
        self.check_file_contents('hello.txt~', 'new hello')
1508.1.6 by Robert Collins
Move Branch.unknowns() to WorkingTree.
163
164
    def test_unknowns(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
165
        tree = WorkingTree.create_standalone('.')
1508.1.6 by Robert Collins
Move Branch.unknowns() to WorkingTree.
166
        self.build_tree(['hello.txt',
167
                         'hello.txt~'])
168
        self.assertEquals(list(tree.unknowns()),
169
                          ['hello.txt'])
170
1185.60.6 by Aaron Bentley
Fixed hashcache
171
    def test_hashcache(self):
172
        from bzrlib.tests.test_hashcache import pause
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
173
        tree = WorkingTree.create_standalone('.')
1185.60.6 by Aaron Bentley
Fixed hashcache
174
        self.build_tree(['hello.txt',
175
                         'hello.txt~'])
176
        tree.add('hello.txt')
177
        pause()
178
        sha = tree.get_file_sha1(tree.path2id('hello.txt'))
179
        self.assertEqual(1, tree._hashcache.miss_count)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
180
        tree2 = WorkingTree('.', tree.branch)
1185.60.6 by Aaron Bentley
Fixed hashcache
181
        sha2 = tree2.get_file_sha1(tree2.path2id('hello.txt'))
182
        self.assertEqual(0, tree2._hashcache.miss_count)
183
        self.assertEqual(1, tree2._hashcache.hit_count)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
184
185
    def test_checkout(self):
186
        # at this point as we dont have checkout versions, checkout simply
187
        # populates the required files for a working tree at the dir.
1534.4.41 by Robert Collins
Branch now uses BzrDir reasonably sanely.
188
        b = BzrDir.create_branch_and_repo('branch')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
189
        t = WorkingTree.create(b, 'tree')
190
        # as we are moving the ownership to working tree, we will check here
191
        # that its split out correctly
192
        self.failIfExists('branch/.bzr/inventory')
193
        self.failIfExists('branch/.bzr/pending-merges')
194
        sio = StringIO()
195
        bzrlib.xml5.serializer_v5.write_inventory(bzrlib.inventory.Inventory(),
196
                                                  sio)
197
        self.assertFileEqual(sio.getvalue(), 'tree/.bzr/inventory')
198
        self.assertFileEqual('', 'tree/.bzr/pending-merges')
199
200
    def test_initialize(self):
201
        # initialize should create a working tree and branch in an existing dir
202
        t = WorkingTree.create_standalone('.')
203
        b = Branch.open('.')
204
        self.assertEqual(t.branch.base, b.base)
205
        t2 = WorkingTree('.')
206
        self.assertEqual(t.basedir, t2.basedir)
207
        self.assertEqual(b.base, t2.branch.base)
208
        # TODO maybe we should check the branch format? not sure if its
209
        # appropriate here.
210
211
    def test_rename_dirs(self):
212
        """Test renaming directories and the files within them."""
213
        wt = self.make_branch_and_tree('.')
214
        b = wt.branch
215
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
216
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
217
218
        wt.commit('create initial state')
219
220
        revid = b.revision_history()[0]
221
        self.log('first revision_id is {%s}' % revid)
222
        
1534.4.28 by Robert Collins
first cut at merge from integration.
223
        inv = b.repository.get_revision_inventory(revid)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
224
        self.log('contents of inventory: %r' % inv.entries())
225
226
        self.check_inventory_shape(inv,
227
                                   ['dir', 'dir/sub', 'dir/sub/file'])
228
229
        wt.rename_one('dir', 'newdir')
230
231
        self.check_inventory_shape(wt.read_working_inventory(),
232
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
233
234
        wt.rename_one('newdir/sub', 'newdir/newsub')
235
        self.check_inventory_shape(wt.read_working_inventory(),
236
                                   ['newdir', 'newdir/newsub',
237
                                    'newdir/newsub/file'])
238
239
    def test_add_in_unversioned(self):
240
        """Try to add a file in an unversioned directory.
241
242
        "bzr add" adds the parent as necessary, but simple working tree add
243
        doesn't do that.
244
        """
245
        from bzrlib.errors import NotVersionedError
246
        wt = self.make_branch_and_tree('.')
247
        self.build_tree(['foo/',
248
                         'foo/hello'])
249
        self.assertRaises(NotVersionedError,
250
                          wt.add,
251
                          'foo/hello')
252
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
253
    def test_add_missing(self):
254
        # adding a msising file -> NoSuchFile
255
        wt = self.make_branch_and_tree('.')
256
        self.assertRaises(errors.NoSuchFile, wt.add, 'fpp')
257
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
258
    def test_remove_verbose(self):
259
        #FIXME the remove api should not print or otherwise depend on the
260
        # text UI - RBC 20060124
261
        wt = self.make_branch_and_tree('.')
262
        self.build_tree(['hello'])
263
        wt.add(['hello'])
264
        wt.commit(message='add hello')
265
        stdout = StringIO()
266
        stderr = StringIO()
267
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
268
                                                     wt.remove,
269
                                                     ['hello'],
270
                                                     verbose=True))
271
        self.assertEqual('?       hello\n', stdout.getvalue())
272
        self.assertEqual('', stderr.getvalue())
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
273
274
    def test_clone_trivial(self):
275
        wt = self.make_branch_and_tree('source')
276
        cloned = wt.clone('target')
277
        self.assertEqual(cloned.last_revision(), wt.last_revision())
278
279
    def test_last_revision(self):
280
        wt = self.make_branch_and_tree('source')
281
        self.assertEqual(None, wt.last_revision())
282
        wt.commit('A', allow_pointless=True, rev_id='A')
283
        self.assertEqual('A', wt.last_revision())
284
285
    def test_set_last_revision(self):
286
        wt = self.make_branch_and_tree('source')
287
        self.assertEqual(None, wt.last_revision())
288
        # cannot set the last revision to one not in the branch
289
        self.assertRaises(errors.NoSuchRevision, wt.set_last_revision, 'A')
290
        wt.commit('A', allow_pointless=True, rev_id='A')
291
        self.assertEqual('A', wt.last_revision())
292
        # None is aways in the branch
293
        wt.set_last_revision(None)
294
        self.assertEqual(None, wt.last_revision())
295
        # and now we can set it to 'A'
296
        # because the current format mutates the branch to set it,
297
        # we need to alter the branch to let this pass.
298
        wt.branch.set_revision_history(['A', 'B'])
299
        wt.set_last_revision('A')
300
        self.assertEqual('A', wt.last_revision())
301
302
    def test_clone_and_commit_preserves_last_revision(self):
303
        wt = self.make_branch_and_tree('source')
304
        cloned = wt.clone('target')
305
        wt.commit('A', allow_pointless=True, rev_id='A')
306
        self.assertNotEqual(cloned.last_revision(), wt.last_revision())
307
        
308
    def test_basis_tree_returns_last_revision(self):
309
        wt = self.make_branch_and_tree('.')
310
        self.build_tree(['foo'])
311
        wt.add('foo', 'foo-id')
312
        wt.commit('A', rev_id='A')
313
        wt.rename_one('foo', 'bar')
314
        wt.commit('B', rev_id='B')
315
        wt.set_last_revision('B')
316
        tree = wt.basis_tree()
317
        self.failUnless(tree.has_filename('bar'))
318
        wt.set_last_revision('A')
319
        tree = wt.basis_tree()
320
        self.failUnless(tree.has_filename('foo'))