/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1399.1.12 by Robert Collins
add new test script
1
# (C) 2005 Canonical Ltd
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
1508.1.3 by Robert Collins
Do not consider urls to be relative paths within working trees.
23
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.
24
from bzrlib.tests import TestCaseWithTransport
1399.1.12 by Robert Collins
add new test script
25
from bzrlib.trace import mutter
1185.31.49 by John Arbash Meinel
Some corrections using the new osutils.rename. **ALL TESTS PASS**
26
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.
27
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
28
                                WorkingTree)
1399.1.12 by Robert Collins
add new test script
29
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
30
class TestTreeDirectory(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
31
32
    def test_kind_character(self):
33
        self.assertEqual(TreeDirectory().kind_character(), '/')
34
35
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
36
class TestTreeEntry(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
37
38
    def test_kind_character(self):
39
        self.assertEqual(TreeEntry().kind_character(), '???')
40
41
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
42
class TestTreeFile(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
43
44
    def test_kind_character(self):
45
        self.assertEqual(TreeFile().kind_character(), '')
46
47
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
48
class TestTreeLink(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
49
50
    def test_kind_character(self):
51
        self.assertEqual(TreeLink().kind_character(), '')
52
53
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
54
class TestWorkingTree(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
55
56
    def test_listfiles(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
57
        tree = WorkingTree.create_standalone('.')
1399.1.12 by Robert Collins
add new test script
58
        os.mkdir('dir')
59
        print >> open('file', 'w'), "content"
1185.31.49 by John Arbash Meinel
Some corrections using the new osutils.rename. **ALL TESTS PASS**
60
        if has_symlinks():
61
            os.symlink('target', 'symlink')
1399.1.12 by Robert Collins
add new test script
62
        files = list(tree.list_files())
63
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
64
        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**
65
        if has_symlinks():
66
            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.
67
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
68
    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.
69
        branch = WorkingTree.create_standalone('.').branch
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
70
        wt, relpath = WorkingTree.open_containing()
71
        self.assertEqual('', relpath)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
72
        self.assertEqual(wt.basedir + '/', branch.base)
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
73
        wt, relpath = WorkingTree.open_containing(u'.')
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
74
        self.assertEqual('', relpath)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
75
        self.assertEqual(wt.basedir + '/', branch.base)
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
76
        wt, relpath = WorkingTree.open_containing('./foo')
77
        self.assertEqual('foo', relpath)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
78
        self.assertEqual(wt.basedir + '/', branch.base)
1508.1.3 by Robert Collins
Do not consider urls to be relative paths within working trees.
79
        # paths that are urls are just plain wrong for working trees.
80
        self.assertRaises(NotBranchError,
81
                          WorkingTree.open_containing, 
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
82
                          'file:///' + getcwd())
1508.1.1 by Robert Collins
Provide a open_containing for WorkingTree.
83
1457.1.1 by Robert Collins
rather than getting the branch inventory, WorkingTree can use the whole Branch, or make its own.
84
    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.
85
        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.
86
        tree = WorkingTree(branch.base, branch)
87
        self.assertEqual(branch, tree.branch)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
88
        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.
89
    
90
    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.
91
        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.
92
        tree = WorkingTree(branch.base)
93
        self.assertEqual(branch.base, tree.branch.base)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
94
        self.assertEqual(branch.base, tree.basedir + '/')
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
95
96
    def test_basic_relpath(self):
97
        # 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.
98
        tree = WorkingTree.create_standalone('.')
1457.1.3 by Robert Collins
make Branch.relpath delegate to the working tree.
99
        self.assertEqual('child',
1185.31.39 by John Arbash Meinel
Replacing os.getcwdu() with osutils.getcwd(),
100
                         tree.relpath(pathjoin(getcwd(), 'child')))
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
101
102
    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.
103
        tree = WorkingTree.create_standalone('.')
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
104
        tree.lock_read()
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
105
        self.assertEqual('r', tree.branch.peek_lock_mode())
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
106
        tree.unlock()
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
107
        self.assertEqual(None, tree.branch.peek_lock_mode())
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
108
        tree.lock_write()
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
109
        self.assertEqual('w', tree.branch.peek_lock_mode())
1442.1.65 by Robert Collins
Branch.remove has been moved to WorkingTree.
110
        tree.unlock()
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
111
        self.assertEqual(None, tree.branch.peek_lock_mode())
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
112
 
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
113
    def get_pullable_trees(self):
1442.1.67 by Robert Collins
Factor out the guts of 'pull' from the command into WorkingTree.pull().
114
        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.
115
        tree = WorkingTree.create_standalone('from')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
116
        tree.add('file')
117
        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.
118
        tree_b = WorkingTree.create_standalone('to')
119
        return tree, tree_b
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
120
 
121
    def test_pull(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
122
        tree_a, tree_b = self.get_pullable_trees()
123
        tree_b.pull(tree_a.branch)
1534.4.28 by Robert Collins
first cut at merge from integration.
124
        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.
125
        self.assertEqual(['A'], tree_b.branch.revision_history())
1442.1.68 by Robert Collins
'bzr pull' now accepts '--clobber'.
126
1185.12.92 by Aaron Bentley
Fixed pull help, renamed clobber to overwrite
127
    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.
128
        tree_a, tree_b = self.get_pullable_trees()
129
        tree_b.commit('foo', rev_id='B')
130
        self.assertEqual(['B'], tree_b.branch.revision_history())
131
        tree_b.pull(tree_a.branch, overwrite=True)
1534.4.28 by Robert Collins
first cut at merge from integration.
132
        self.failUnless(tree_b.branch.repository.has_revision('A'))
133
        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.
134
        self.assertEqual(['A'], tree_b.branch.revision_history())
1501 by Robert Collins
Move revert from Branch to WorkingTree.
135
136
    def test_revert(self):
137
        """Test selected-file revert"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
138
        tree = WorkingTree.create_standalone('.')
1501 by Robert Collins
Move revert from Branch to WorkingTree.
139
140
        self.build_tree(['hello.txt'])
141
        file('hello.txt', 'w').write('initial hello')
142
143
        self.assertRaises(NotVersionedError,
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
144
                          tree.revert, ['hello.txt'])
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
145
        tree.add(['hello.txt'])
146
        tree.commit('create initial hello.txt')
1501 by Robert Collins
Move revert from Branch to WorkingTree.
147
148
        self.check_file_contents('hello.txt', 'initial hello')
149
        file('hello.txt', 'w').write('new hello')
150
        self.check_file_contents('hello.txt', 'new hello')
151
152
        # revert file modified since last revision
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
153
        tree.revert(['hello.txt'])
1501 by Robert Collins
Move revert from Branch to WorkingTree.
154
        self.check_file_contents('hello.txt', 'initial hello')
155
        self.check_file_contents('hello.txt~', 'new hello')
156
1457.1.8 by Robert Collins
Replace the WorkingTree.revert method algorithm with a call to merge_inner.
157
        # reverting again does not clobber the backup
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
158
        tree.revert(['hello.txt'])
1501 by Robert Collins
Move revert from Branch to WorkingTree.
159
        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.
160
        self.check_file_contents('hello.txt~', 'new hello')
1508.1.6 by Robert Collins
Move Branch.unknowns() to WorkingTree.
161
162
    def test_unknowns(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
163
        tree = WorkingTree.create_standalone('.')
1508.1.6 by Robert Collins
Move Branch.unknowns() to WorkingTree.
164
        self.build_tree(['hello.txt',
165
                         'hello.txt~'])
166
        self.assertEquals(list(tree.unknowns()),
167
                          ['hello.txt'])
168
1185.60.6 by Aaron Bentley
Fixed hashcache
169
    def test_hashcache(self):
170
        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.
171
        tree = WorkingTree.create_standalone('.')
1185.60.6 by Aaron Bentley
Fixed hashcache
172
        self.build_tree(['hello.txt',
173
                         'hello.txt~'])
174
        tree.add('hello.txt')
175
        pause()
176
        sha = tree.get_file_sha1(tree.path2id('hello.txt'))
177
        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.
178
        tree2 = WorkingTree('.', tree.branch)
1185.60.6 by Aaron Bentley
Fixed hashcache
179
        sha2 = tree2.get_file_sha1(tree2.path2id('hello.txt'))
180
        self.assertEqual(0, tree2._hashcache.miss_count)
181
        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.
182
183
    def test_checkout(self):
184
        # at this point as we dont have checkout versions, checkout simply
185
        # populates the required files for a working tree at the dir.
186
        self.build_tree(['branch/'])
187
        b = Branch.create('branch')
188
        t = WorkingTree.create(b, 'tree')
189
        # as we are moving the ownership to working tree, we will check here
190
        # that its split out correctly
191
        self.failIfExists('branch/.bzr/inventory')
192
        self.failIfExists('branch/.bzr/pending-merges')
193
        sio = StringIO()
194
        bzrlib.xml5.serializer_v5.write_inventory(bzrlib.inventory.Inventory(),
195
                                                  sio)
196
        self.assertFileEqual(sio.getvalue(), 'tree/.bzr/inventory')
197
        self.assertFileEqual('', 'tree/.bzr/pending-merges')
198
199
    def test_initialize(self):
200
        # initialize should create a working tree and branch in an existing dir
201
        t = WorkingTree.create_standalone('.')
202
        b = Branch.open('.')
203
        self.assertEqual(t.branch.base, b.base)
204
        t2 = WorkingTree('.')
205
        self.assertEqual(t.basedir, t2.basedir)
206
        self.assertEqual(b.base, t2.branch.base)
207
        # TODO maybe we should check the branch format? not sure if its
208
        # appropriate here.
209
210
    def test_rename_dirs(self):
211
        """Test renaming directories and the files within them."""
212
        wt = self.make_branch_and_tree('.')
213
        b = wt.branch
214
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
215
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
216
217
        wt.commit('create initial state')
218
219
        revid = b.revision_history()[0]
220
        self.log('first revision_id is {%s}' % revid)
221
        
1534.4.28 by Robert Collins
first cut at merge from integration.
222
        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.
223
        self.log('contents of inventory: %r' % inv.entries())
224
225
        self.check_inventory_shape(inv,
226
                                   ['dir', 'dir/sub', 'dir/sub/file'])
227
228
        wt.rename_one('dir', 'newdir')
229
230
        self.check_inventory_shape(wt.read_working_inventory(),
231
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
232
233
        wt.rename_one('newdir/sub', 'newdir/newsub')
234
        self.check_inventory_shape(wt.read_working_inventory(),
235
                                   ['newdir', 'newdir/newsub',
236
                                    'newdir/newsub/file'])
237
238
    def test_add_in_unversioned(self):
239
        """Try to add a file in an unversioned directory.
240
241
        "bzr add" adds the parent as necessary, but simple working tree add
242
        doesn't do that.
243
        """
244
        from bzrlib.errors import NotVersionedError
245
        wt = self.make_branch_and_tree('.')
246
        self.build_tree(['foo/',
247
                         'foo/hello'])
248
        self.assertRaises(NotVersionedError,
249
                          wt.add,
250
                          'foo/hello')
251
252
    def test_remove_verbose(self):
253
        #FIXME the remove api should not print or otherwise depend on the
254
        # text UI - RBC 20060124
255
        wt = self.make_branch_and_tree('.')
256
        self.build_tree(['hello'])
257
        wt.add(['hello'])
258
        wt.commit(message='add hello')
259
        stdout = StringIO()
260
        stderr = StringIO()
261
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
262
                                                     wt.remove,
263
                                                     ['hello'],
264
                                                     verbose=True))
265
        self.assertEqual('?       hello\n', stdout.getvalue())
266
        self.assertEqual('', stderr.getvalue())