/brz/remove-bazaar

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