/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

  • Committer: John Arbash Meinel
  • Date: 2008-04-30 15:02:49 UTC
  • mto: This revision was merged to the branch mainline in revision 3394.
  • Revision ID: john@arbash-meinel.com-20080430150249-viu4vgzglhfu7k7y
Update some tests to pass with the branch checks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from cStringIO import StringIO
19
19
import os
20
20
 
21
 
import bzrlib
 
21
from bzrlib import (
 
22
    bzrdir,
 
23
    conflicts,
 
24
    errors,
 
25
    workingtree,
 
26
    )
22
27
from bzrlib.branch import Branch
23
 
import bzrlib.bzrdir as bzrdir
24
28
from bzrlib.bzrdir import BzrDir
25
 
from bzrlib.conflicts import *
26
 
import bzrlib.errors as errors
27
 
from bzrlib.errors import NotBranchError, NotVersionedError
28
29
from bzrlib.lockdir import LockDir
29
 
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
30
 
from bzrlib.tests import TestCaseWithTransport, TestSkipped
31
 
from bzrlib.trace import mutter
 
30
from bzrlib.mutabletree import needs_tree_write_lock
 
31
from bzrlib.symbol_versioning import zero_thirteen
 
32
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
32
33
from bzrlib.transport import get_transport
33
 
import bzrlib.workingtree as workingtree
34
 
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
35
 
                                WorkingTree)
 
34
from bzrlib.workingtree import (
 
35
    TreeEntry,
 
36
    TreeDirectory,
 
37
    TreeFile,
 
38
    TreeLink,
 
39
    )
 
40
 
36
41
 
37
42
class TestTreeDirectory(TestCaseWithTransport):
38
43
 
89
94
        """See WorkingTreeFormat.get_format_string()."""
90
95
        return "Sample tree format."
91
96
 
92
 
    def initialize(self, a_bzrdir, revision_id=None):
 
97
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
 
98
                   accelerator_tree=None, hardlink=False):
93
99
        """Sample branches cannot be created."""
94
100
        t = a_bzrdir.get_workingtree_transport(self)
95
 
        t.put('format', StringIO(self.get_format_string()))
 
101
        t.put_bytes('format', self.get_format_string())
96
102
        return 'A tree'
97
103
 
98
104
    def is_supported(self):
169
175
        t = control.get_workingtree_transport(None)
170
176
        self.assertEqualDiff('Bazaar-NG Working Tree format 3',
171
177
                             t.get('format').read())
172
 
        self.assertEqualDiff('<inventory format="5">\n'
173
 
                             '</inventory>\n',
174
 
                             t.get('inventory').read())
 
178
        self.assertEqualDiff(t.get('inventory').read(), 
 
179
                              '<inventory format="5">\n'
 
180
                              '</inventory>\n',
 
181
                             )
175
182
        self.assertEqualDiff('### bzr hashcache v5\n',
176
183
                             t.get('stat-cache').read())
177
184
        self.assertFalse(t.has('inventory.basis'))
205
212
        tree.unlock()
206
213
        self.assertEquals(our_lock.peek(), None)
207
214
 
 
215
    def test_missing_pending_merges(self):
 
216
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
217
        control.create_repository()
 
218
        control.create_branch()
 
219
        tree = workingtree.WorkingTreeFormat3().initialize(control)
 
220
        tree._control_files._transport.delete("pending-merges")
 
221
        self.assertEqual([], tree.get_parent_ids())
 
222
 
 
223
 
 
224
class TestFormat2WorkingTree(TestCaseWithTransport):
 
225
    """Tests that are specific to format 2 trees."""
 
226
 
208
227
    def create_format2_tree(self, url):
209
 
        return BzrDir.create_standalone_workingtree(url)
 
228
        return self.make_branch_and_tree(
 
229
            url, format=bzrdir.BzrDirFormat6())
210
230
 
211
 
    def test_conflicts_format2(self):
 
231
    def test_conflicts(self):
212
232
        # test backwards compatability
213
233
        tree = self.create_format2_tree('.')
214
234
        self.assertRaises(errors.UnsupportedOperation, tree.set_conflicts,
215
235
                          None)
216
236
        file('lala.BASE', 'wb').write('labase')
217
 
        expected = ContentsConflict('lala')
 
237
        expected = conflicts.ContentsConflict('lala')
218
238
        self.assertEqual(list(tree.conflicts()), [expected])
219
239
        file('lala', 'wb').write('la')
220
240
        tree.add('lala', 'lala-id')
221
 
        expected = ContentsConflict('lala', file_id='lala-id')
 
241
        expected = conflicts.ContentsConflict('lala', file_id='lala-id')
222
242
        self.assertEqual(list(tree.conflicts()), [expected])
223
243
        file('lala.THIS', 'wb').write('lathis')
224
244
        file('lala.OTHER', 'wb').write('laother')
225
245
        # When "text conflict"s happen, stem, THIS and OTHER are text
226
 
        expected = TextConflict('lala', file_id='lala-id')
 
246
        expected = conflicts.TextConflict('lala', file_id='lala-id')
227
247
        self.assertEqual(list(tree.conflicts()), [expected])
228
248
        os.unlink('lala.OTHER')
229
249
        os.mkdir('lala.OTHER')
230
 
        expected = ContentsConflict('lala', file_id='lala-id')
 
250
        expected = conflicts.ContentsConflict('lala', file_id='lala-id')
231
251
        self.assertEqual(list(tree.conflicts()), [expected])
 
252
 
 
253
 
 
254
class TestNonFormatSpecificCode(TestCaseWithTransport):
 
255
    """This class contains tests of workingtree that are not format specific."""
 
256
 
 
257
    def test_gen_file_id(self):
 
258
        file_id = self.applyDeprecated(zero_thirteen, workingtree.gen_file_id,
 
259
                                      'filename')
 
260
        self.assertStartsWith(file_id, 'filename-')
 
261
 
 
262
    def test_gen_root_id(self):
 
263
        file_id = self.applyDeprecated(zero_thirteen, workingtree.gen_root_id)
 
264
        self.assertStartsWith(file_id, 'tree_root-')
 
265
        
 
266
 
 
267
class InstrumentedTree(object):
 
268
    """A instrumented tree to check the needs_tree_write_lock decorator."""
 
269
 
 
270
    def __init__(self):
 
271
        self._locks = []
 
272
 
 
273
    def lock_tree_write(self):
 
274
        self._locks.append('t')
 
275
 
 
276
    @needs_tree_write_lock
 
277
    def method_with_tree_write_lock(self, *args, **kwargs):
 
278
        """A lock_tree_write decorated method that returns its arguments."""
 
279
        return args, kwargs
 
280
 
 
281
    @needs_tree_write_lock
 
282
    def method_that_raises(self):
 
283
        """This method causes an exception when called with parameters.
 
284
        
 
285
        This allows the decorator code to be checked - it should still call
 
286
        unlock.
 
287
        """
 
288
 
 
289
    def unlock(self):
 
290
        self._locks.append('u')
 
291
 
 
292
 
 
293
class TestInstrumentedTree(TestCase):
 
294
 
 
295
    def test_needs_tree_write_lock(self):
 
296
        """@needs_tree_write_lock should be semantically transparent."""
 
297
        tree = InstrumentedTree()
 
298
        self.assertEqual(
 
299
            'method_with_tree_write_lock',
 
300
            tree.method_with_tree_write_lock.__name__)
 
301
        self.assertEqual(
 
302
            "A lock_tree_write decorated method that returns its arguments.",
 
303
            tree.method_with_tree_write_lock.__doc__)
 
304
        args = (1, 2, 3)
 
305
        kwargs = {'a':'b'}
 
306
        result = tree.method_with_tree_write_lock(1,2,3, a='b')
 
307
        self.assertEqual((args, kwargs), result)
 
308
        self.assertEqual(['t', 'u'], tree._locks)
 
309
        self.assertRaises(TypeError, tree.method_that_raises, 'foo')
 
310
        self.assertEqual(['t', 'u', 't', 'u'], tree._locks)
 
311
 
 
312
 
 
313
class TestRevert(TestCaseWithTransport):
 
314
 
 
315
    def test_revert_conflicts_recursive(self):
 
316
        this_tree = self.make_branch_and_tree('this-tree')
 
317
        self.build_tree_contents([('this-tree/foo/',),
 
318
                                  ('this-tree/foo/bar', 'bar')])
 
319
        this_tree.add(['foo', 'foo/bar'])
 
320
        this_tree.commit('created foo/bar')
 
321
        other_tree = this_tree.bzrdir.sprout('other-tree').open_workingtree()
 
322
        self.build_tree_contents([('other-tree/foo/bar', 'baz')])
 
323
        other_tree.commit('changed bar')
 
324
        self.build_tree_contents([('this-tree/foo/bar', 'qux')])
 
325
        this_tree.commit('changed qux')
 
326
        this_tree.merge_from_branch(other_tree.branch)
 
327
        self.assertEqual(1, len(this_tree.conflicts()))
 
328
        this_tree.revert(['foo'])
 
329
        self.assertEqual(0, len(this_tree.conflicts()))
 
330
 
 
331
 
 
332
class TestAutoResolve(TestCaseWithTransport):
 
333
 
 
334
    def test_auto_resolve(self):
 
335
        base = self.make_branch_and_tree('base')
 
336
        self.build_tree_contents([('base/hello', 'Hello')])
 
337
        base.add('hello', 'hello_id')
 
338
        base.commit('Hello')
 
339
        other = base.bzrdir.sprout('other').open_workingtree()
 
340
        self.build_tree_contents([('other/hello', 'hELLO')])
 
341
        other.commit('Case switch')
 
342
        this = base.bzrdir.sprout('this').open_workingtree()
 
343
        self.failUnlessExists('this/hello')
 
344
        self.build_tree_contents([('this/hello', 'Hello World')])
 
345
        this.commit('Add World')
 
346
        this.merge_from_branch(other.branch)
 
347
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
348
                         this.conflicts())
 
349
        this.auto_resolve()
 
350
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
351
                         this.conflicts())
 
352
        self.build_tree_contents([('this/hello', '<<<<<<<')])
 
353
        this.auto_resolve()
 
354
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
355
                         this.conflicts())
 
356
        self.build_tree_contents([('this/hello', '=======')])
 
357
        this.auto_resolve()
 
358
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
359
                         this.conflicts())
 
360
        self.build_tree_contents([('this/hello', '\n>>>>>>>')])
 
361
        remaining, resolved = this.auto_resolve()
 
362
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
363
                         this.conflicts())
 
364
        self.assertEqual([], resolved)
 
365
        self.build_tree_contents([('this/hello', 'hELLO wORLD')])
 
366
        remaining, resolved = this.auto_resolve()
 
367
        self.assertEqual([], this.conflicts())
 
368
        self.assertEqual([conflicts.TextConflict('hello', None, 'hello_id')],
 
369
                         resolved)
 
370
        self.failIfExists('this/hello.BASE')
 
371
 
 
372
    def test_auto_resolve_dir(self):
 
373
        tree = self.make_branch_and_tree('tree')
 
374
        self.build_tree(['tree/hello/'])
 
375
        tree.add('hello', 'hello-id')
 
376
        file_conflict = conflicts.TextConflict('file', None, 'hello-id')
 
377
        tree.set_conflicts(conflicts.ConflictList([file_conflict]))
 
378
        tree.auto_resolve()
 
379
 
 
380
 
 
381
class TestFindTrees(TestCaseWithTransport):
 
382
 
 
383
    def test_find_trees(self):
 
384
        self.make_branch_and_tree('foo')
 
385
        self.make_branch_and_tree('foo/bar')
 
386
        # Sticking a tree inside a control dir is heinous, so let's skip it
 
387
        self.make_branch_and_tree('foo/.bzr/baz')
 
388
        self.make_branch('qux')
 
389
        trees = workingtree.WorkingTree.find_trees('.')
 
390
        self.assertEqual(2, len(list(trees)))