/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.15 by John Arbash Meinel
Merge bzr.dev 5597 to resolve NEWS, aka bzr-2.3.txt
1
# Copyright (C) 2005-2011 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
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1399.1.12 by Robert Collins
add new test script
17
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
18
from bzrlib import (
19
    bzrdir,
20
    conflicts,
21
    errors,
6543.2.2 by Martin Packman
Deprecate WorkingTree.auto_resolve
22
    symbol_versioning,
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
23
    transport,
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
24
    workingtree,
5816.5.1 by Jelmer Vernooij
Move WorkingTree3 to bzrlib.workingtree_3.
25
    workingtree_3,
5816.5.7 by Jelmer Vernooij
Fix more imports.
26
    workingtree_4,
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
27
    )
6538.1.11 by Aaron Bentley
Switch to much simpler implementation of restore_uncommitted.
28
from bzrlib.lock import write_locked
1553.5.74 by Martin Pool
Convert WorkingTree format3 to use LockDirs
29
from bzrlib.lockdir import LockDir
1986.1.8 by Robert Collins
Update to bzr.dev, which involves adding lock_tree_write to MutableTree and MemoryTree.
30
from bzrlib.mutabletree import needs_tree_write_lock
1997.1.1 by Robert Collins
Add WorkingTree.lock_tree_write.
31
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
32
from bzrlib.workingtree import (
33
    TreeEntry,
34
    TreeDirectory,
35
    TreeFile,
36
    TreeLink,
37
    )
1399.1.12 by Robert Collins
add new test script
38
2255.2.121 by John Arbash Meinel
split out the WorkingTreeFormat4 tests into a separate test file
39
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
40
class TestTreeDirectory(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
41
42
    def test_kind_character(self):
43
        self.assertEqual(TreeDirectory().kind_character(), '/')
44
45
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
46
class TestTreeEntry(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
47
48
    def test_kind_character(self):
49
        self.assertEqual(TreeEntry().kind_character(), '???')
50
51
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
52
class TestTreeFile(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
53
54
    def test_kind_character(self):
55
        self.assertEqual(TreeFile().kind_character(), '')
56
57
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
58
class TestTreeLink(TestCaseWithTransport):
1399.1.12 by Robert Collins
add new test script
59
60
    def test_kind_character(self):
61
        self.assertEqual(TreeLink().kind_character(), '')
62
63
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
64
class TestDefaultFormat(TestCaseWithTransport):
65
66
    def test_get_set_default_format(self):
5662.3.3 by Jelmer Vernooij
add tests
67
        old_format = workingtree.format_registry.get_default()
5816.5.2 by Jelmer Vernooij
Fix comment - default wt format is 6.
68
        # default is 6
5816.5.7 by Jelmer Vernooij
Fix more imports.
69
        self.assertTrue(isinstance(old_format, workingtree_4.WorkingTreeFormat6))
5662.3.3 by Jelmer Vernooij
add tests
70
        workingtree.format_registry.set_default(SampleTreeFormat())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
71
        try:
72
            # the default branch format is used by the meta dir format
73
            # which is not the default bzrdir format at this point
74
            dir = bzrdir.BzrDirMetaFormat1().initialize('.')
75
            dir.create_repository()
76
            dir.create_branch()
77
            result = dir.create_workingtree()
78
            self.assertEqual(result, 'A tree')
79
        finally:
5662.3.3 by Jelmer Vernooij
add tests
80
            workingtree.format_registry.set_default(old_format)
81
        self.assertEqual(old_format, workingtree.format_registry.get_default())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
82
6349.2.4 by Jelmer Vernooij
Add test for WorkingTreeFormat.from_string.
83
    def test_from_string(self):
84
        self.assertIsInstance(
85
            SampleTreeFormat.from_string("Sample tree format."),
86
            SampleTreeFormat)
6213.1.54 by Jelmer Vernooij
Fix tests.
87
        self.assertRaises(AssertionError,
6349.2.4 by Jelmer Vernooij
Add test for WorkingTreeFormat.from_string.
88
            SampleTreeFormat.from_string, "Different format string.")
89
5816.2.1 by Jelmer Vernooij
Allow lazily setting default for working trees.
90
    def test_get_set_default_format_by_key(self):
91
        old_format = workingtree.format_registry.get_default()
5816.5.7 by Jelmer Vernooij
Fix more imports.
92
        # default is 6
5816.2.1 by Jelmer Vernooij
Allow lazily setting default for working trees.
93
        format = SampleTreeFormat()
94
        workingtree.format_registry.register(format)
95
        self.addCleanup(workingtree.format_registry.remove, format)
5816.5.7 by Jelmer Vernooij
Fix more imports.
96
        self.assertTrue(isinstance(old_format, workingtree_4.WorkingTreeFormat6))
5816.2.1 by Jelmer Vernooij
Allow lazily setting default for working trees.
97
        workingtree.format_registry.set_default_key(format.get_format_string())
98
        try:
99
            # the default branch format is used by the meta dir format
100
            # which is not the default bzrdir format at this point
101
            dir = bzrdir.BzrDirMetaFormat1().initialize('.')
102
            dir.create_repository()
103
            dir.create_branch()
104
            result = dir.create_workingtree()
105
            self.assertEqual(result, 'A tree')
106
        finally:
107
            workingtree.format_registry.set_default_key(
108
                old_format.get_format_string())
109
        self.assertEqual(old_format, workingtree.format_registry.get_default())
110
3753.1.1 by John Arbash Meinel
Add some simple direct tests for WT.open and WT.open_containing.
111
    def test_open(self):
112
        tree = self.make_branch_and_tree('.')
3753.1.2 by John Arbash Meinel
Switch to using the class attribute, rather than the instance
113
        open_direct = workingtree.WorkingTree.open('.')
3753.1.1 by John Arbash Meinel
Add some simple direct tests for WT.open and WT.open_containing.
114
        self.assertEqual(tree.basedir, open_direct.basedir)
3753.1.2 by John Arbash Meinel
Switch to using the class attribute, rather than the instance
115
        open_no_args = workingtree.WorkingTree.open()
3753.1.1 by John Arbash Meinel
Add some simple direct tests for WT.open and WT.open_containing.
116
        self.assertEqual(tree.basedir, open_no_args.basedir)
117
118
    def test_open_containing(self):
119
        tree = self.make_branch_and_tree('.')
3753.1.2 by John Arbash Meinel
Switch to using the class attribute, rather than the instance
120
        open_direct, relpath = workingtree.WorkingTree.open_containing('.')
3753.1.1 by John Arbash Meinel
Add some simple direct tests for WT.open and WT.open_containing.
121
        self.assertEqual(tree.basedir, open_direct.basedir)
122
        self.assertEqual('', relpath)
3753.1.2 by John Arbash Meinel
Switch to using the class attribute, rather than the instance
123
        open_no_args, relpath = workingtree.WorkingTree.open_containing()
3753.1.1 by John Arbash Meinel
Add some simple direct tests for WT.open and WT.open_containing.
124
        self.assertEqual(tree.basedir, open_no_args.basedir)
125
        self.assertEqual('', relpath)
3753.1.2 by John Arbash Meinel
Switch to using the class attribute, rather than the instance
126
        open_subdir, relpath = workingtree.WorkingTree.open_containing('subdir')
3753.1.1 by John Arbash Meinel
Add some simple direct tests for WT.open and WT.open_containing.
127
        self.assertEqual(tree.basedir, open_subdir.basedir)
128
        self.assertEqual('subdir', relpath)
129
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
130
6349.2.4 by Jelmer Vernooij
Add test for WorkingTreeFormat.from_string.
131
class SampleTreeFormat(workingtree.WorkingTreeFormatMetaDir):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
132
    """A sample format
133
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
134
    this format is initializable, unsupported to aid in testing the
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
135
    open and open_downlevel routines.
136
    """
137
6349.2.4 by Jelmer Vernooij
Add test for WorkingTreeFormat.from_string.
138
    @classmethod
139
    def get_format_string(cls):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
140
        """See WorkingTreeFormat.get_format_string()."""
141
        return "Sample tree format."
142
3123.5.3 by Aaron Bentley
Get tests passing with accelerator_tree
143
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
3136.1.5 by Aaron Bentley
Fix sample workingtree format
144
                   accelerator_tree=None, hardlink=False):
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
145
        """Sample branches cannot be created."""
146
        t = a_bzrdir.get_workingtree_transport(self)
1955.3.13 by John Arbash Meinel
Run the full test suite, and fix up any deprecation warnings.
147
        t.put_bytes('format', self.get_format_string())
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
148
        return 'A tree'
149
150
    def is_supported(self):
151
        return False
152
153
    def open(self, transport, _found=False):
154
        return "opened tree."
155
156
5642.2.4 by Jelmer Vernooij
add tests.
157
class SampleExtraTreeFormat(workingtree.WorkingTreeFormat):
158
    """A sample format that does not support use in a metadir.
159
160
    """
161
162
    def get_format_string(self):
163
        # Not usable in a metadir, so no format string
164
        return None
165
166
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
167
                   accelerator_tree=None, hardlink=False):
168
        raise NotImplementedError(self.initialize)
169
170
    def is_supported(self):
171
        return False
172
173
    def open(self, transport, _found=False):
174
        raise NotImplementedError(self.open)
175
176
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
177
class TestWorkingTreeFormat(TestCaseWithTransport):
178
    """Tests for the WorkingTreeFormat facility."""
179
5816.3.3 by Jelmer Vernooij
Add tariff test to make sure working tree isn't opened by 'bzr serve'
180
    def test_find_format_string(self):
181
        # is the right format object found for a working tree?
182
        branch = self.make_branch('branch')
183
        self.assertRaises(errors.NoWorkingTree,
6349.2.2 by Jelmer Vernooij
Fix remaining tests.
184
            workingtree.WorkingTreeFormatMetaDir.find_format_string, branch.bzrdir)
5816.3.3 by Jelmer Vernooij
Add tariff test to make sure working tree isn't opened by 'bzr serve'
185
        transport = branch.bzrdir.get_workingtree_transport(None)
186
        transport.mkdir('.')
187
        transport.put_bytes("format", "some format name")
188
        # The format does not have to be known by Bazaar,
189
        # find_format_string just retrieves the name
190
        self.assertEquals("some format name",
6349.2.2 by Jelmer Vernooij
Fix remaining tests.
191
            workingtree.WorkingTreeFormatMetaDir.find_format_string(branch.bzrdir))
5816.3.3 by Jelmer Vernooij
Add tariff test to make sure working tree isn't opened by 'bzr serve'
192
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
193
    def test_find_format(self):
194
        # is the right format object found for a working tree?
195
        # create a branch with a few known format objects.
196
        self.build_tree(["foo/", "bar/"])
197
        def check_format(format, url):
198
            dir = format._matchingbzrdir.initialize(url)
199
            dir.create_repository()
200
            dir.create_branch()
201
            format.initialize(dir)
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
202
            t = transport.get_transport(url)
6349.2.2 by Jelmer Vernooij
Fix remaining tests.
203
            found_format = workingtree.WorkingTreeFormatMetaDir.find_format(dir)
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
204
            self.assertIsInstance(found_format, format.__class__)
5816.5.1 by Jelmer Vernooij
Move WorkingTree3 to bzrlib.workingtree_3.
205
        check_format(workingtree_3.WorkingTreeFormat3(), "bar")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
206
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
207
    def test_find_format_no_tree(self):
208
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
209
        self.assertRaises(errors.NoWorkingTree,
6349.2.2 by Jelmer Vernooij
Fix remaining tests.
210
                          workingtree.WorkingTreeFormatMetaDir.find_format,
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
211
                          dir)
212
213
    def test_find_format_unknown_format(self):
214
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
215
        dir.create_repository()
216
        dir.create_branch()
217
        SampleTreeFormat().initialize(dir)
218
        self.assertRaises(errors.UnknownFormatError,
6349.2.2 by Jelmer Vernooij
Fix remaining tests.
219
                          workingtree.WorkingTreeFormatMetaDir.find_format,
1534.4.46 by Robert Collins
Nearly complete .bzr/checkout splitout.
220
                          dir)
221
6213.1.36 by Jelmer Vernooij
Add news entry, test for working tree features.
222
    def test_find_format_with_features(self):
223
        tree = self.make_branch_and_tree('.', format='2a')
6213.1.58 by Jelmer Vernooij
Use update_feature_flags everywhere.
224
        tree.update_feature_flags({"name": "necessity"})
6213.1.36 by Jelmer Vernooij
Add news entry, test for working tree features.
225
        found_format = workingtree.WorkingTreeFormatMetaDir.find_format(
226
            tree.bzrdir)
227
        self.assertIsInstance(found_format, workingtree.WorkingTreeFormat)
228
        self.assertEquals(found_format.features.get("name"), "necessity")
229
        self.assertRaises(errors.MissingFeature, found_format.check_support_status,
230
            True)
231
        self.addCleanup(workingtree.WorkingTreeFormatMetaDir.unregister_feature,
232
            "name")
233
        workingtree.WorkingTreeFormatMetaDir.register_feature("name")
234
        found_format.check_support_status(True)
235
5662.3.3 by Jelmer Vernooij
add tests
236
5786.1.5 by John Arbash Meinel
Move the logic about InventoryDirectory => TreeReference into iter_entries
237
class TestWorkingTreeIterEntriesByDir_wSubtrees(TestCaseWithTransport):
238
239
    def make_simple_tree(self):
240
        tree = self.make_branch_and_tree('tree', format='development-subtree')
241
        self.build_tree(['tree/a/', 'tree/a/b/', 'tree/a/b/c'])
242
        tree.set_root_id('root-id')
243
        tree.add(['a', 'a/b', 'a/b/c'], ['a-id', 'b-id', 'c-id'])
244
        tree.commit('initial')
245
        return tree
246
247
    def test_just_directory(self):
248
        tree = self.make_simple_tree()
249
        self.assertEqual([('directory', 'root-id'),
250
                          ('directory', 'a-id'),
251
                          ('directory', 'b-id'),
252
                          ('file', 'c-id')],
253
                         [(ie.kind, ie.file_id)
254
                          for path, ie in tree.iter_entries_by_dir()])
255
        subtree = self.make_branch_and_tree('tree/a/b')
256
        self.assertEqual([('tree-reference', 'b-id')],
257
                         [(ie.kind, ie.file_id)
258
                          for path, ie in tree.iter_entries_by_dir(['b-id'])])
259
260
    def test_direct_subtree(self):
261
        tree = self.make_simple_tree()
262
        subtree = self.make_branch_and_tree('tree/a/b')
263
        self.assertEqual([('directory', 'root-id'),
264
                          ('directory', 'a-id'),
265
                          ('tree-reference', 'b-id')],
266
                         [(ie.kind, ie.file_id)
267
                          for path, ie in tree.iter_entries_by_dir()])
268
269
    def test_indirect_subtree(self):
270
        tree = self.make_simple_tree()
271
        subtree = self.make_branch_and_tree('tree/a')
272
        self.assertEqual([('directory', 'root-id'),
273
                          ('tree-reference', 'a-id')],
274
                         [(ie.kind, ie.file_id)
275
                          for path, ie in tree.iter_entries_by_dir()])
276
277
5662.3.3 by Jelmer Vernooij
add tests
278
class TestWorkingTreeFormatRegistry(TestCase):
279
280
    def setUp(self):
281
        super(TestWorkingTreeFormatRegistry, self).setUp()
282
        self.registry = workingtree.WorkingTreeFormatRegistry()
283
284
    def test_register_unregister_format(self):
285
        format = SampleTreeFormat()
286
        self.registry.register(format)
287
        self.assertEquals(format, self.registry.get("Sample tree format."))
288
        self.registry.remove(format)
289
        self.assertRaises(KeyError, self.registry.get, "Sample tree format.")
290
291
    def test_get_all(self):
292
        format = SampleTreeFormat()
293
        self.assertEquals([], self.registry._get_all())
294
        self.registry.register(format)
295
        self.assertEquals([format], self.registry._get_all())
296
297
    def test_register_extra(self):
5642.2.4 by Jelmer Vernooij
add tests.
298
        format = SampleExtraTreeFormat()
5662.3.3 by Jelmer Vernooij
add tests
299
        self.assertEquals([], self.registry._get_all())
300
        self.registry.register_extra(format)
301
        self.assertEquals([format], self.registry._get_all())
302
303
    def test_register_extra_lazy(self):
304
        self.assertEquals([], self.registry._get_all())
305
        self.registry.register_extra_lazy("bzrlib.tests.test_workingtree",
306
            "SampleExtraTreeFormat")
307
        formats = self.registry._get_all()
308
        self.assertEquals(1, len(formats))
309
        self.assertIsInstance(formats[0], SampleExtraTreeFormat)
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
310
311
312
class TestWorkingTreeFormat3(TestCaseWithTransport):
313
    """Tests specific to WorkingTreeFormat3."""
314
315
    def test_disk_layout(self):
316
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
317
        control.create_repository()
318
        control.create_branch()
5816.5.1 by Jelmer Vernooij
Move WorkingTree3 to bzrlib.workingtree_3.
319
        tree = workingtree_3.WorkingTreeFormat3().initialize(control)
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
320
        # we want:
321
        # format 'Bazaar-NG Working Tree format 3'
322
        # inventory = blank inventory
323
        # pending-merges = ''
324
        # stat-cache = ??
325
        # no inventory.basis yet
326
        t = control.get_workingtree_transport(None)
1553.5.81 by Martin Pool
Revert change to WorkingTreeFormat3 format string; too many things want it the old way
327
        self.assertEqualDiff('Bazaar-NG Working Tree format 3',
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
328
                             t.get('format').read())
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
329
        self.assertEqualDiff(t.get('inventory').read(),
2100.3.12 by Aaron Bentley
Stop generating unique roots for WorkingTree3
330
                              '<inventory format="5">\n'
1731.1.33 by Aaron Bentley
Revert no-special-root changes
331
                              '</inventory>\n',
332
                             )
1534.4.51 by Robert Collins
Test the disk layout of format3 working trees.
333
        self.assertEqualDiff('### bzr hashcache v5\n',
334
                             t.get('stat-cache').read())
335
        self.assertFalse(t.has('inventory.basis'))
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
336
        # no last-revision file means 'None' or 'NULLREVISION'
337
        self.assertFalse(t.has('last-revision'))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
338
        # TODO RBC 20060210 do a commit, check the inventory.basis is created
1508.1.19 by Robert Collins
Give format3 working trees their own last-revision marker.
339
        # correctly and last-revision file becomes present.
1553.5.74 by Martin Pool
Convert WorkingTree format3 to use LockDirs
340
341
    def test_uses_lockdir(self):
342
        """WorkingTreeFormat3 uses its own LockDir:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
343
1553.5.74 by Martin Pool
Convert WorkingTree format3 to use LockDirs
344
            - lock is a directory
345
            - when the WorkingTree is locked, LockDir can see that
346
        """
347
        t = self.get_transport()
348
        url = self.get_url()
349
        dir = bzrdir.BzrDirMetaFormat1().initialize(url)
350
        repo = dir.create_repository()
351
        branch = dir.create_branch()
1558.10.1 by Aaron Bentley
Handle lockdirs over NFS properly
352
        try:
5816.5.1 by Jelmer Vernooij
Move WorkingTree3 to bzrlib.workingtree_3.
353
            tree = workingtree_3.WorkingTreeFormat3().initialize(dir)
1558.10.1 by Aaron Bentley
Handle lockdirs over NFS properly
354
        except errors.NotLocalUrl:
355
            raise TestSkipped('Not a local URL')
1553.5.74 by Martin Pool
Convert WorkingTree format3 to use LockDirs
356
        self.assertIsDirectory('.bzr', t)
357
        self.assertIsDirectory('.bzr/checkout', t)
358
        self.assertIsDirectory('.bzr/checkout/lock', t)
359
        our_lock = LockDir(t, '.bzr/checkout/lock')
360
        self.assertEquals(our_lock.peek(), None)
1553.5.75 by Martin Pool
Additional WorkingTree LockDir test
361
        tree.lock_write()
362
        self.assertTrue(our_lock.peek())
363
        tree.unlock()
364
        self.assertEquals(our_lock.peek(), None)
1534.10.6 by Aaron Bentley
Conflict serialization working for WorkingTree3
365
1815.2.2 by Jelmer Vernooij
Move missing_pending_merges test to WorkingTreeFormat3-specific tests.
366
    def test_missing_pending_merges(self):
367
        control = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
368
        control.create_repository()
369
        control.create_branch()
5816.5.1 by Jelmer Vernooij
Move WorkingTree3 to bzrlib.workingtree_3.
370
        tree = workingtree_3.WorkingTreeFormat3().initialize(control)
3407.2.14 by Martin Pool
Remove more cases of getting transport via control_files
371
        tree._transport.delete("pending-merges")
1908.6.11 by Robert Collins
Remove usage of tree.pending_merges().
372
        self.assertEqual([], tree.get_parent_ids())
1815.2.2 by Jelmer Vernooij
Move missing_pending_merges test to WorkingTreeFormat3-specific tests.
373
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
374
1997.1.1 by Robert Collins
Add WorkingTree.lock_tree_write.
375
class InstrumentedTree(object):
376
    """A instrumented tree to check the needs_tree_write_lock decorator."""
377
378
    def __init__(self):
379
        self._locks = []
380
381
    def lock_tree_write(self):
382
        self._locks.append('t')
383
384
    @needs_tree_write_lock
385
    def method_with_tree_write_lock(self, *args, **kwargs):
386
        """A lock_tree_write decorated method that returns its arguments."""
387
        return args, kwargs
388
389
    @needs_tree_write_lock
390
    def method_that_raises(self):
391
        """This method causes an exception when called with parameters.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
392
1997.1.1 by Robert Collins
Add WorkingTree.lock_tree_write.
393
        This allows the decorator code to be checked - it should still call
394
        unlock.
395
        """
396
397
    def unlock(self):
398
        self._locks.append('u')
399
400
401
class TestInstrumentedTree(TestCase):
402
403
    def test_needs_tree_write_lock(self):
404
        """@needs_tree_write_lock should be semantically transparent."""
405
        tree = InstrumentedTree()
406
        self.assertEqual(
407
            'method_with_tree_write_lock',
408
            tree.method_with_tree_write_lock.__name__)
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
409
        self.assertDocstring(
1997.1.1 by Robert Collins
Add WorkingTree.lock_tree_write.
410
            "A lock_tree_write decorated method that returns its arguments.",
5131.2.1 by Martin
Permit bzrlib to run under python -OO by explictly assigning to __doc__ for user-visible docstrings
411
            tree.method_with_tree_write_lock)
1997.1.1 by Robert Collins
Add WorkingTree.lock_tree_write.
412
        args = (1, 2, 3)
413
        kwargs = {'a':'b'}
414
        result = tree.method_with_tree_write_lock(1,2,3, a='b')
415
        self.assertEqual((args, kwargs), result)
416
        self.assertEqual(['t', 'u'], tree._locks)
417
        self.assertRaises(TypeError, tree.method_that_raises, 'foo')
418
        self.assertEqual(['t', 'u', 't', 'u'], tree._locks)
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
419
420
3017.2.1 by Aaron Bentley
Revert now resolves conflicts recursively (#102739)
421
class TestRevert(TestCaseWithTransport):
422
423
    def test_revert_conflicts_recursive(self):
424
        this_tree = self.make_branch_and_tree('this-tree')
425
        self.build_tree_contents([('this-tree/foo/',),
426
                                  ('this-tree/foo/bar', 'bar')])
427
        this_tree.add(['foo', 'foo/bar'])
428
        this_tree.commit('created foo/bar')
429
        other_tree = this_tree.bzrdir.sprout('other-tree').open_workingtree()
430
        self.build_tree_contents([('other-tree/foo/bar', 'baz')])
431
        other_tree.commit('changed bar')
432
        self.build_tree_contents([('this-tree/foo/bar', 'qux')])
433
        this_tree.commit('changed qux')
434
        this_tree.merge_from_branch(other_tree.branch)
435
        self.assertEqual(1, len(this_tree.conflicts()))
436
        this_tree.revert(['foo'])
437
        self.assertEqual(0, len(this_tree.conflicts()))
438
439
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
440
class TestAutoResolve(TestCaseWithTransport):
441
6543.2.2 by Martin Packman
Deprecate WorkingTree.auto_resolve
442
    def _auto_resolve(self, tree):
443
        """Call auto_resolve on tree expecting deprecation"""
444
        return self.applyDeprecated(
445
            symbol_versioning.deprecated_in((2, 6, 0)),
446
            tree.auto_resolve,)
447
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
448
    def test_auto_resolve(self):
449
        base = self.make_branch_and_tree('base')
450
        self.build_tree_contents([('base/hello', 'Hello')])
451
        base.add('hello', 'hello_id')
452
        base.commit('Hello')
453
        other = base.bzrdir.sprout('other').open_workingtree()
454
        self.build_tree_contents([('other/hello', 'hELLO')])
455
        other.commit('Case switch')
456
        this = base.bzrdir.sprout('this').open_workingtree()
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
457
        self.assertPathExists('this/hello')
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
458
        self.build_tree_contents([('this/hello', 'Hello World')])
459
        this.commit('Add World')
460
        this.merge_from_branch(other.branch)
5050.51.3 by Vincent Ladeuil
Fix test failing on pqm.
461
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
462
                         this.conflicts())
6543.2.2 by Martin Packman
Deprecate WorkingTree.auto_resolve
463
        self._auto_resolve(this)
5050.51.3 by Vincent Ladeuil
Fix test failing on pqm.
464
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
465
                         this.conflicts())
466
        self.build_tree_contents([('this/hello', '<<<<<<<')])
6543.2.2 by Martin Packman
Deprecate WorkingTree.auto_resolve
467
        self._auto_resolve(this)
5050.51.3 by Vincent Ladeuil
Fix test failing on pqm.
468
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
469
                         this.conflicts())
470
        self.build_tree_contents([('this/hello', '=======')])
6543.2.2 by Martin Packman
Deprecate WorkingTree.auto_resolve
471
        self._auto_resolve(this)
5050.51.3 by Vincent Ladeuil
Fix test failing on pqm.
472
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
473
                         this.conflicts())
474
        self.build_tree_contents([('this/hello', '\n>>>>>>>')])
6543.2.2 by Martin Packman
Deprecate WorkingTree.auto_resolve
475
        remaining, resolved = self._auto_resolve(this)
5050.51.3 by Vincent Ladeuil
Fix test failing on pqm.
476
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
477
                         this.conflicts())
478
        self.assertEqual([], resolved)
479
        self.build_tree_contents([('this/hello', 'hELLO wORLD')])
6543.2.2 by Martin Packman
Deprecate WorkingTree.auto_resolve
480
        remaining, resolved = self._auto_resolve(this)
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
481
        self.assertEqual([], this.conflicts())
5050.51.3 by Vincent Ladeuil
Fix test failing on pqm.
482
        self.assertEqual([conflicts.TextConflict('hello', 'hello_id')],
2120.7.2 by Aaron Bentley
Move autoresolve functionality to workingtree
483
                         resolved)
5784.1.3 by Martin Pool
Switch away from using failUnlessExists and failIfExists
484
        self.assertPathDoesNotExist('this/hello.BASE')
2120.7.3 by Aaron Bentley
Update resolve command to automatically mark conflicts as resolved
485
486
    def test_auto_resolve_dir(self):
487
        tree = self.make_branch_and_tree('tree')
488
        self.build_tree(['tree/hello/'])
489
        tree.add('hello', 'hello-id')
5050.51.3 by Vincent Ladeuil
Fix test failing on pqm.
490
        file_conflict = conflicts.TextConflict('file', 'hello-id')
2120.7.3 by Aaron Bentley
Update resolve command to automatically mark conflicts as resolved
491
        tree.set_conflicts(conflicts.ConflictList([file_conflict]))
6543.2.2 by Martin Packman
Deprecate WorkingTree.auto_resolve
492
        self._auto_resolve(tree)
3140.1.4 by Aaron Bentley
Add WorkingTree.find_trees
493
494
495
class TestFindTrees(TestCaseWithTransport):
496
497
    def test_find_trees(self):
498
        self.make_branch_and_tree('foo')
499
        self.make_branch_and_tree('foo/bar')
500
        # Sticking a tree inside a control dir is heinous, so let's skip it
501
        self.make_branch_and_tree('foo/.bzr/baz')
502
        self.make_branch('qux')
503
        trees = workingtree.WorkingTree.find_trees('.')
504
        self.assertEqual(2, len(list(trees)))
6538.1.5 by Aaron Bentley
Implement WorkingTree.store_uncommitted.
505
506
507
class TestStoredUncommitted(TestCaseWithTransport):
508
509
    def store_uncommitted(self):
510
        tree = self.make_branch_and_tree('tree')
511
        tree.commit('get root in there')
512
        self.build_tree_contents([('tree/file', 'content')])
513
        tree.add('file', 'file-id')
514
        tree.store_uncommitted()
515
        return tree
516
517
    def test_store_uncommitted(self):
518
        self.store_uncommitted()
519
        self.assertPathDoesNotExist('tree/file')
520
521
    def test_store_uncommitted_no_change(self):
522
        tree = self.make_branch_and_tree('tree')
523
        tree.commit('get root in there')
524
        tree.store_uncommitted()
6538.1.24 by Aaron Bentley
Eliminate get_stored_uncommitted from API.
525
        self.assertIs(None, tree.branch.get_unshelver(tree))
6538.1.10 by Aaron Bentley
Implement WorkingTree.get_uncommitted_data
526
6538.1.11 by Aaron Bentley
Switch to much simpler implementation of restore_uncommitted.
527
    def test_restore_uncommitted(self):
528
        with write_locked(self.store_uncommitted()) as tree:
529
            tree.restore_uncommitted()
530
            self.assertPathExists('tree/file')
6538.1.24 by Aaron Bentley
Eliminate get_stored_uncommitted from API.
531
            self.assertIs(None, tree.branch.get_unshelver(tree))
6538.1.11 by Aaron Bentley
Switch to much simpler implementation of restore_uncommitted.
532
533
    def test_restore_uncommitted_none(self):
534
        tree = self.make_branch_and_tree('tree')
535
        tree.restore_uncommitted()