~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

Move working tree initialisation out from  Branch.initialize, deprecated Branch.initialize to Branch.create.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with this program; if not, write to the Free Software
16
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
17
 
 
18
from cStringIO import StringIO
18
19
import os
 
20
 
 
21
import bzrlib
19
22
from bzrlib.branch import Branch
20
23
from bzrlib.errors import NotBranchError, NotVersionedError
21
 
from bzrlib.tests import TestCaseInTempDir
 
24
from bzrlib.tests import TestCaseWithTransport
22
25
from bzrlib.trace import mutter
23
26
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
24
27
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
25
28
                                WorkingTree)
26
29
 
27
 
class TestTreeDirectory(TestCaseInTempDir):
 
30
class TestTreeDirectory(TestCaseWithTransport):
28
31
 
29
32
    def test_kind_character(self):
30
33
        self.assertEqual(TreeDirectory().kind_character(), '/')
31
34
 
32
35
 
33
 
class TestTreeEntry(TestCaseInTempDir):
 
36
class TestTreeEntry(TestCaseWithTransport):
34
37
 
35
38
    def test_kind_character(self):
36
39
        self.assertEqual(TreeEntry().kind_character(), '???')
37
40
 
38
41
 
39
 
class TestTreeFile(TestCaseInTempDir):
 
42
class TestTreeFile(TestCaseWithTransport):
40
43
 
41
44
    def test_kind_character(self):
42
45
        self.assertEqual(TreeFile().kind_character(), '')
43
46
 
44
47
 
45
 
class TestTreeLink(TestCaseInTempDir):
 
48
class TestTreeLink(TestCaseWithTransport):
46
49
 
47
50
    def test_kind_character(self):
48
51
        self.assertEqual(TreeLink().kind_character(), '')
49
52
 
50
53
 
51
 
class TestWorkingTree(TestCaseInTempDir):
 
54
class TestWorkingTree(TestCaseWithTransport):
52
55
 
53
56
    def test_listfiles(self):
54
 
        branch = Branch.initialize(u'.')
 
57
        tree = WorkingTree.create_standalone('.')
55
58
        os.mkdir('dir')
56
59
        print >> open('file', 'w'), "content"
57
60
        if has_symlinks():
58
61
            os.symlink('target', 'symlink')
59
 
        tree = branch.working_tree()
60
62
        files = list(tree.list_files())
61
63
        self.assertEqual(files[0], ('dir', '?', 'directory', None, TreeDirectory()))
62
64
        self.assertEqual(files[1], ('file', '?', 'file', None, TreeFile()))
64
66
            self.assertEqual(files[2], ('symlink', '?', 'symlink', None, TreeLink()))
65
67
 
66
68
    def test_open_containing(self):
67
 
        branch = Branch.initialize(u'.')
 
69
        branch = WorkingTree.create_standalone('.').branch
68
70
        wt, relpath = WorkingTree.open_containing()
69
71
        self.assertEqual('', relpath)
70
72
        self.assertEqual(wt.basedir + '/', branch.base)
80
82
                          'file:///' + getcwd())
81
83
 
82
84
    def test_construct_with_branch(self):
83
 
        branch = Branch.initialize(u'.')
 
85
        branch = WorkingTree.create_standalone('.').branch
84
86
        tree = WorkingTree(branch.base, branch)
85
87
        self.assertEqual(branch, tree.branch)
86
88
        self.assertEqual(branch.base, tree.basedir + '/')
87
89
    
88
90
    def test_construct_without_branch(self):
89
 
        branch = Branch.initialize(u'.')
 
91
        branch = WorkingTree.create_standalone('.').branch
90
92
        tree = WorkingTree(branch.base)
91
93
        self.assertEqual(branch.base, tree.branch.base)
92
94
        self.assertEqual(branch.base, tree.basedir + '/')
93
95
 
94
96
    def test_basic_relpath(self):
95
97
        # for comprehensive relpath tests, see whitebox.py.
96
 
        branch = Branch.initialize(u'.')
97
 
        tree = WorkingTree(branch.base)
 
98
        tree = WorkingTree.create_standalone('.')
98
99
        self.assertEqual('child',
99
100
                         tree.relpath(pathjoin(getcwd(), 'child')))
100
101
 
101
102
    def test_lock_locks_branch(self):
102
 
        branch = Branch.initialize(u'.')
103
 
        tree = WorkingTree(branch.base)
 
103
        tree = WorkingTree.create_standalone('.')
104
104
        tree.lock_read()
105
105
        self.assertEqual(1, tree.branch._lock_count)
106
106
        self.assertEqual('r', tree.branch._lock_mode)
112
112
        tree.unlock()
113
113
        self.assertEqual(None, tree.branch._lock_count)
114
114
 
115
 
    def get_pullable_branches(self):
 
115
    def get_pullable_trees(self):
116
116
        self.build_tree(['from/', 'from/file', 'to/'])
117
 
        br_a = Branch.initialize('from')
118
 
        tree = br_a.working_tree()
 
117
        tree = WorkingTree.create_standalone('from')
119
118
        tree.add('file')
120
119
        tree.commit('foo', rev_id='A')
121
 
        br_b = Branch.initialize('to')
122
 
        return br_a, br_b
 
120
        tree_b = WorkingTree.create_standalone('to')
 
121
        return tree, tree_b
123
122
 
124
123
    def test_pull(self):
125
 
        br_a, br_b = self.get_pullable_branches()
126
 
        br_b.working_tree().pull(br_a)
127
 
        self.failUnless(br_b.has_revision('A'))
128
 
        self.assertEqual(['A'], br_b.revision_history())
 
124
        tree_a, tree_b = self.get_pullable_trees()
 
125
        tree_b.pull(tree_a.branch)
 
126
        self.failUnless(tree_b.branch.has_revision('A'))
 
127
        self.assertEqual(['A'], tree_b.branch.revision_history())
129
128
 
130
129
    def test_pull_overwrites(self):
131
 
        br_a, br_b = self.get_pullable_branches()
132
 
        br_b.working_tree().commit('foo', rev_id='B')
133
 
        self.assertEqual(['B'], br_b.revision_history())
134
 
        br_b.working_tree().pull(br_a, overwrite=True)
135
 
        self.failUnless(br_b.has_revision('A'))
136
 
        self.failUnless(br_b.has_revision('B'))
137
 
        self.assertEqual(['A'], br_b.revision_history())
 
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)
 
134
        self.failUnless(tree_b.branch.has_revision('A'))
 
135
        self.failUnless(tree_b.branch.has_revision('B'))
 
136
        self.assertEqual(['A'], tree_b.branch.revision_history())
138
137
 
139
138
    def test_revert(self):
140
139
        """Test selected-file revert"""
141
 
        b = Branch.initialize(u'.')
 
140
        tree = WorkingTree.create_standalone('.')
142
141
 
143
142
        self.build_tree(['hello.txt'])
144
143
        file('hello.txt', 'w').write('initial hello')
145
144
 
146
145
        self.assertRaises(NotVersionedError,
147
 
                          b.working_tree().revert, ['hello.txt'])
148
 
        tree = WorkingTree(b.base, b)
 
146
                          tree.revert, ['hello.txt'])
149
147
        tree.add(['hello.txt'])
150
148
        tree.commit('create initial hello.txt')
151
149
 
164
162
        self.check_file_contents('hello.txt~', 'new hello')
165
163
 
166
164
    def test_unknowns(self):
167
 
        b = Branch.initialize(u'.')
168
 
        tree = WorkingTree(u'.', b)
 
165
        tree = WorkingTree.create_standalone('.')
169
166
        self.build_tree(['hello.txt',
170
167
                         'hello.txt~'])
171
168
        self.assertEquals(list(tree.unknowns()),
173
170
 
174
171
    def test_hashcache(self):
175
172
        from bzrlib.tests.test_hashcache import pause
176
 
        b = Branch.initialize(u'.')
177
 
        tree = WorkingTree(u'.', b)
 
173
        tree = WorkingTree.create_standalone('.')
178
174
        self.build_tree(['hello.txt',
179
175
                         'hello.txt~'])
180
176
        tree.add('hello.txt')
181
177
        pause()
182
178
        sha = tree.get_file_sha1(tree.path2id('hello.txt'))
183
179
        self.assertEqual(1, tree._hashcache.miss_count)
184
 
        tree2 = WorkingTree(u'.', b)
 
180
        tree2 = WorkingTree('.', tree.branch)
185
181
        sha2 = tree2.get_file_sha1(tree2.path2id('hello.txt'))
186
182
        self.assertEqual(0, tree2._hashcache.miss_count)
187
183
        self.assertEqual(1, tree2._hashcache.hit_count)
 
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.
 
188
        self.build_tree(['branch/'])
 
189
        b = Branch.create('branch')
 
190
        t = WorkingTree.create(b, 'tree')
 
191
        # as we are moving the ownership to working tree, we will check here
 
192
        # that its split out correctly
 
193
        self.failIfExists('branch/.bzr/inventory')
 
194
        self.failIfExists('branch/.bzr/pending-merges')
 
195
        sio = StringIO()
 
196
        bzrlib.xml5.serializer_v5.write_inventory(bzrlib.inventory.Inventory(),
 
197
                                                  sio)
 
198
        self.assertFileEqual(sio.getvalue(), 'tree/.bzr/inventory')
 
199
        self.assertFileEqual('', 'tree/.bzr/pending-merges')
 
200
 
 
201
    def test_initialize(self):
 
202
        # initialize should create a working tree and branch in an existing dir
 
203
        t = WorkingTree.create_standalone('.')
 
204
        b = Branch.open('.')
 
205
        self.assertEqual(t.branch.base, b.base)
 
206
        t2 = WorkingTree('.')
 
207
        self.assertEqual(t.basedir, t2.basedir)
 
208
        self.assertEqual(b.base, t2.branch.base)
 
209
        # TODO maybe we should check the branch format? not sure if its
 
210
        # appropriate here.
 
211
 
 
212
    def test_rename_dirs(self):
 
213
        """Test renaming directories and the files within them."""
 
214
        wt = self.make_branch_and_tree('.')
 
215
        b = wt.branch
 
216
        self.build_tree(['dir/', 'dir/sub/', 'dir/sub/file'])
 
217
        wt.add(['dir', 'dir/sub', 'dir/sub/file'])
 
218
 
 
219
        wt.commit('create initial state')
 
220
 
 
221
        revid = b.revision_history()[0]
 
222
        self.log('first revision_id is {%s}' % revid)
 
223
        
 
224
        inv = b.get_revision_inventory(revid)
 
225
        self.log('contents of inventory: %r' % inv.entries())
 
226
 
 
227
        self.check_inventory_shape(inv,
 
228
                                   ['dir', 'dir/sub', 'dir/sub/file'])
 
229
 
 
230
        wt.rename_one('dir', 'newdir')
 
231
 
 
232
        self.check_inventory_shape(wt.read_working_inventory(),
 
233
                                   ['newdir', 'newdir/sub', 'newdir/sub/file'])
 
234
 
 
235
        wt.rename_one('newdir/sub', 'newdir/newsub')
 
236
        self.check_inventory_shape(wt.read_working_inventory(),
 
237
                                   ['newdir', 'newdir/newsub',
 
238
                                    'newdir/newsub/file'])
 
239
 
 
240
    def test_add_in_unversioned(self):
 
241
        """Try to add a file in an unversioned directory.
 
242
 
 
243
        "bzr add" adds the parent as necessary, but simple working tree add
 
244
        doesn't do that.
 
245
        """
 
246
        from bzrlib.errors import NotVersionedError
 
247
        wt = self.make_branch_and_tree('.')
 
248
        self.build_tree(['foo/',
 
249
                         'foo/hello'])
 
250
        self.assertRaises(NotVersionedError,
 
251
                          wt.add,
 
252
                          'foo/hello')
 
253
 
 
254
    def test_remove_verbose(self):
 
255
        #FIXME the remove api should not print or otherwise depend on the
 
256
        # text UI - RBC 20060124
 
257
        wt = self.make_branch_and_tree('.')
 
258
        self.build_tree(['hello'])
 
259
        wt.add(['hello'])
 
260
        wt.commit(message='add hello')
 
261
        stdout = StringIO()
 
262
        stderr = StringIO()
 
263
        self.assertEqual(None, self.apply_redirected(None, stdout, stderr,
 
264
                                                     wt.remove,
 
265
                                                     ['hello'],
 
266
                                                     verbose=True))
 
267
        self.assertEqual('?       hello\n', stdout.getvalue())
 
268
        self.assertEqual('', stderr.getvalue())