~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_workingtree.py

  • Committer: Aaron Bentley
  • Date: 2012-07-19 16:57:16 UTC
  • mto: This revision was merged to the branch mainline in revision 6540.
  • Revision ID: aaron@aaronbentley.com-20120719165716-b4iupzkb17b9l9wx
Avoid branch write lock to preserve VFS call count.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
    bzrdir,
20
20
    conflicts,
21
21
    errors,
22
 
    symbol_versioning,
23
22
    transport,
24
23
    workingtree,
25
24
    workingtree_3,
26
25
    workingtree_4,
27
26
    )
 
27
from bzrlib.lock import write_locked
28
28
from bzrlib.lockdir import LockDir
29
29
from bzrlib.mutabletree import needs_tree_write_lock
30
30
from bzrlib.tests import TestCase, TestCaseWithTransport, TestSkipped
79
79
            workingtree.format_registry.set_default(old_format)
80
80
        self.assertEqual(old_format, workingtree.format_registry.get_default())
81
81
 
 
82
    def test_from_string(self):
 
83
        self.assertIsInstance(
 
84
            SampleTreeFormat.from_string("Sample tree format."),
 
85
            SampleTreeFormat)
 
86
        self.assertRaises(AssertionError,
 
87
            SampleTreeFormat.from_string, "Different format string.")
 
88
 
82
89
    def test_get_set_default_format_by_key(self):
83
90
        old_format = workingtree.format_registry.get_default()
84
91
        # default is 6
120
127
        self.assertEqual('subdir', relpath)
121
128
 
122
129
 
123
 
class SampleTreeFormat(workingtree.WorkingTreeFormat):
 
130
class SampleTreeFormat(workingtree.WorkingTreeFormatMetaDir):
124
131
    """A sample format
125
132
 
126
133
    this format is initializable, unsupported to aid in testing the
127
134
    open and open_downlevel routines.
128
135
    """
129
136
 
130
 
    def get_format_string(self):
 
137
    @classmethod
 
138
    def get_format_string(cls):
131
139
        """See WorkingTreeFormat.get_format_string()."""
132
140
        return "Sample tree format."
133
141
 
172
180
        # is the right format object found for a working tree?
173
181
        branch = self.make_branch('branch')
174
182
        self.assertRaises(errors.NoWorkingTree,
175
 
            workingtree.WorkingTreeFormat.find_format_string, branch.bzrdir)
 
183
            workingtree.WorkingTreeFormatMetaDir.find_format_string, branch.bzrdir)
176
184
        transport = branch.bzrdir.get_workingtree_transport(None)
177
185
        transport.mkdir('.')
178
186
        transport.put_bytes("format", "some format name")
179
187
        # The format does not have to be known by Bazaar,
180
188
        # find_format_string just retrieves the name
181
189
        self.assertEquals("some format name",
182
 
            workingtree.WorkingTreeFormat.find_format_string(branch.bzrdir))
 
190
            workingtree.WorkingTreeFormatMetaDir.find_format_string(branch.bzrdir))
183
191
 
184
192
    def test_find_format(self):
185
193
        # is the right format object found for a working tree?
191
199
            dir.create_branch()
192
200
            format.initialize(dir)
193
201
            t = transport.get_transport(url)
194
 
            found_format = workingtree.WorkingTreeFormat.find_format(dir)
 
202
            found_format = workingtree.WorkingTreeFormatMetaDir.find_format(dir)
195
203
            self.assertIsInstance(found_format, format.__class__)
196
204
        check_format(workingtree_3.WorkingTreeFormat3(), "bar")
197
205
 
198
206
    def test_find_format_no_tree(self):
199
207
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
200
208
        self.assertRaises(errors.NoWorkingTree,
201
 
                          workingtree.WorkingTreeFormat.find_format,
 
209
                          workingtree.WorkingTreeFormatMetaDir.find_format,
202
210
                          dir)
203
211
 
204
212
    def test_find_format_unknown_format(self):
207
215
        dir.create_branch()
208
216
        SampleTreeFormat().initialize(dir)
209
217
        self.assertRaises(errors.UnknownFormatError,
210
 
                          workingtree.WorkingTreeFormat.find_format,
 
218
                          workingtree.WorkingTreeFormatMetaDir.find_format,
211
219
                          dir)
212
220
 
213
 
    def test_register_unregister_format(self):
214
 
        format = SampleTreeFormat()
215
 
        # make a control dir
216
 
        dir = bzrdir.BzrDirMetaFormat1().initialize('.')
217
 
        dir.create_repository()
218
 
        dir.create_branch()
219
 
        # make a branch
220
 
        format.initialize(dir)
221
 
        # register a format for it.
222
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
223
 
            workingtree.WorkingTreeFormat.register_format, format)
224
 
        self.assertTrue(format in 
225
 
            self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
226
 
                workingtree.WorkingTreeFormat.get_formats))
227
 
        # which branch.Open will refuse (not supported)
228
 
        self.assertRaises(errors.UnsupportedFormatError, workingtree.WorkingTree.open, '.')
229
 
        # but open_downlevel will work
230
 
        self.assertEqual(format.open(dir), workingtree.WorkingTree.open_downlevel('.'))
231
 
        # unregister the format
232
 
        self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
233
 
            workingtree.WorkingTreeFormat.unregister_format, format)
234
 
        self.assertFalse(format in
235
 
            self.applyDeprecated(symbol_versioning.deprecated_in((2, 4, 0)),
236
 
                workingtree.WorkingTreeFormat.get_formats))
 
221
    def test_find_format_with_features(self):
 
222
        tree = self.make_branch_and_tree('.', format='2a')
 
223
        tree.update_feature_flags({"name": "necessity"})
 
224
        found_format = workingtree.WorkingTreeFormatMetaDir.find_format(
 
225
            tree.bzrdir)
 
226
        self.assertIsInstance(found_format, workingtree.WorkingTreeFormat)
 
227
        self.assertEquals(found_format.features.get("name"), "necessity")
 
228
        self.assertRaises(errors.MissingFeature, found_format.check_support_status,
 
229
            True)
 
230
        self.addCleanup(workingtree.WorkingTreeFormatMetaDir.unregister_feature,
 
231
            "name")
 
232
        workingtree.WorkingTreeFormatMetaDir.register_feature("name")
 
233
        found_format.check_support_status(True)
237
234
 
238
235
 
239
236
class TestWorkingTreeIterEntriesByDir_wSubtrees(TestCaseWithTransport):
498
495
        self.make_branch('qux')
499
496
        trees = workingtree.WorkingTree.find_trees('.')
500
497
        self.assertEqual(2, len(list(trees)))
 
498
 
 
499
 
 
500
class TestStoredUncommitted(TestCaseWithTransport):
 
501
 
 
502
    def store_uncommitted(self):
 
503
        tree = self.make_branch_and_tree('tree')
 
504
        tree.commit('get root in there')
 
505
        self.build_tree_contents([('tree/file', 'content')])
 
506
        tree.add('file', 'file-id')
 
507
        tree.store_uncommitted()
 
508
        return tree
 
509
 
 
510
    def test_store_uncommitted(self):
 
511
        self.store_uncommitted()
 
512
        self.assertPathDoesNotExist('tree/file')
 
513
 
 
514
    def test_store_uncommitted_no_change(self):
 
515
        tree = self.make_branch_and_tree('tree')
 
516
        tree.commit('get root in there')
 
517
        tree.store_uncommitted()
 
518
        self.assertIs(None, tree.branch.get_unshelver(tree))
 
519
 
 
520
    def test_restore_uncommitted(self):
 
521
        with write_locked(self.store_uncommitted()) as tree:
 
522
            tree.restore_uncommitted()
 
523
            self.assertPathExists('tree/file')
 
524
            self.assertIs(None, tree.branch.get_unshelver(tree))
 
525
 
 
526
    def test_restore_uncommitted_none(self):
 
527
        tree = self.make_branch_and_tree('tree')
 
528
        tree.restore_uncommitted()