~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_dpush.py

Start testing and implementing --strict for dpush.

* bzrlib/tests/blackbox/test_dpush.py:
(load_tests, TestDpushStrictMixin, TestDpushStrictWithoutChanges,
TestDpushStrictWithChanges): Copied from test_push with minimal
changes to have running tests with some even passing.

* bzrlib/foreign.py:
(cmd_dpush): Add the --strict option.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    bzrdir,
26
26
    foreign,
27
27
    tests,
28
 
    )
29
 
from bzrlib.tests.test_foreign import (
30
 
    DummyForeignVcsDirFormat,
31
 
    InterToDummyVcsBranch,
 
28
    workingtree,
32
29
    )
33
30
from bzrlib.tests import (
34
31
    blackbox,
35
32
    test_foreign,
36
33
    )
37
34
 
 
35
 
 
36
def load_tests(standard_tests, module, loader):
 
37
    """Multiply tests for the dpush command."""
 
38
    result = loader.suiteClass()
 
39
 
 
40
    # one for each king of change
 
41
    changes_tests, remaining_tests = tests.split_suite_by_condition(
 
42
        standard_tests, tests.condition_isinstance((
 
43
                TestDpushStrictWithChanges,
 
44
                )))
 
45
    changes_scenarios = [
 
46
        ('uncommitted',
 
47
         dict(_changes_type= '_uncommitted_changes')),
 
48
        ('pending-merges',
 
49
         dict(_changes_type= '_pending_merges')),
 
50
        ('out-of-sync-trees',
 
51
         dict(_changes_type= '_out_of_sync_trees')),
 
52
        ]
 
53
    tests.multiply_tests(changes_tests, changes_scenarios, result)
 
54
    # No parametrization for the remaining tests
 
55
    result.addTests(remaining_tests)
 
56
 
 
57
    return result
 
58
 
 
59
 
38
60
class TestDpush(blackbox.ExternalBase):
39
61
 
40
62
    def setUp(self):
124
146
        output, error = self.run_bzr("dpush -d dc d", retcode=3)
125
147
        self.assertEquals(output, "")
126
148
        self.assertContainsRe(error, "have diverged")
 
149
 
 
150
 
 
151
class TestDpushStrictMixin(object):
 
152
 
 
153
    # FIXME: setUp and unregister_format needs to be dedupe from TestDpush
 
154
    # (made into functions in test_foreign ?).
 
155
    def setUp(self):
 
156
        bzrdir.BzrDirFormat.register_control_format(
 
157
            test_foreign.DummyForeignVcsDirFormat)
 
158
        branch.InterBranch.register_optimiser(
 
159
            test_foreign.InterToDummyVcsBranch)
 
160
        self.addCleanup(self.unregister_format)
 
161
 
 
162
    def unregister_format(self):
 
163
        try:
 
164
            bzrdir.BzrDirFormat.unregister_control_format(
 
165
                test_foreign.DummyForeignVcsDirFormat)
 
166
        except ValueError:
 
167
            pass
 
168
        branch.InterBranch.unregister_optimiser(
 
169
            test_foreign.InterToDummyVcsBranch)
 
170
 
 
171
    def make_local_branch_and_tree(self):
 
172
        self.tree = self.make_branch_and_tree('local')
 
173
        self.build_tree_contents([('local/file', 'initial')])
 
174
        self.tree.add('file')
 
175
        self.tree.commit('adding file', rev_id='added')
 
176
        self.build_tree_contents([('local/file', 'modified')])
 
177
        self.tree.commit('modify file', rev_id='modified')
 
178
 
 
179
    def make_foreign_branch(self, relpath='to'):
 
180
        # Create an empty branch where we will be able to push
 
181
        self.foreign = self.make_branch(
 
182
            relpath, format=test_foreign.DummyForeignVcsDirFormat())
 
183
 
 
184
    def set_config_dpush_strict(self, value):
 
185
        # set config var (any of bazaar.conf, locations.conf, branch.conf
 
186
        # should do)
 
187
        conf = self.tree.branch.get_config()
 
188
        conf.set_user_option('dpush_strict', value)
 
189
 
 
190
    _default_command = ['dpush', '../to']
 
191
    _default_wd = 'local'
 
192
    _default_errors = ['Working tree ".*/local/" has uncommitted '
 
193
                       'changes \(See bzr status\)\.',]
 
194
    _default_dpushed_revid = 'modified'
 
195
 
 
196
    def assertDpushFails(self, args):
 
197
        self.run_bzr_error(self._default_errors, self._default_command + args,
 
198
                           working_dir=self._default_wd, retcode=3)
 
199
 
 
200
    def assertDpushSucceeds(self, args, pushed_revid=None):
 
201
        self.run_bzr(self._default_command + args,
 
202
                     working_dir=self._default_wd)
 
203
        if pushed_revid is None:
 
204
            # dpush change the revids, so we need to get back to it
 
205
            branch_from = branch.Branch.open(self._default_wd)
 
206
            pushed_revid = branch_from.last_revision()
 
207
        branch_to = branch.Branch.open('to')
 
208
        repo_to = branch_to.repository
 
209
        self.assertTrue(repo_to.has_revision(pushed_revid))
 
210
        self.assertEqual(branch_to.last_revision(), pushed_revid)
 
211
 
 
212
 
 
213
 
 
214
class TestDpushStrictWithoutChanges(tests.TestCaseWithTransport,
 
215
                                    TestDpushStrictMixin):
 
216
 
 
217
    def setUp(self):
 
218
        super(TestDpushStrictWithoutChanges, self).setUp()
 
219
        TestDpushStrictMixin.setUp(self)
 
220
        self.make_local_branch_and_tree()
 
221
        self.make_foreign_branch()
 
222
 
 
223
    def test_dpush_default(self):
 
224
        self.assertDpushSucceeds([])
 
225
 
 
226
    def test_dpush_strict(self):
 
227
        self.assertDpushSucceeds(['--strict'])
 
228
 
 
229
    def test_dpush_no_strict(self):
 
230
        self.assertDpushSucceeds(['--no-strict'])
 
231
 
 
232
    def test_dpush_config_var_strict(self):
 
233
        self.set_config_dpush_strict('true')
 
234
        self.assertDpushSucceeds([])
 
235
 
 
236
    def test_dpush_config_var_no_strict(self):
 
237
        self.set_config_dpush_strict('false')
 
238
        self.assertDpushSucceeds([])
 
239
 
 
240
 
 
241
class TestDpushStrictWithChanges(tests.TestCaseWithTransport,
 
242
                                 TestDpushStrictMixin):
 
243
 
 
244
    _changes_type = None # Set by load_tests
 
245
 
 
246
    def setUp(self):
 
247
        super(TestDpushStrictWithChanges, self).setUp()
 
248
        TestDpushStrictMixin.setUp(self)
 
249
        # Apply the changes defined in load_tests: one of _uncommitted_changes,
 
250
        # _pending_merges or _out_of_sync_trees
 
251
        getattr(self, self._changes_type)()
 
252
        self.make_foreign_branch()
 
253
 
 
254
    def _uncommitted_changes(self):
 
255
        self.make_local_branch_and_tree()
 
256
        # Make a change without committing it
 
257
        self.build_tree_contents([('local/file', 'in progress')])
 
258
 
 
259
    def _pending_merges(self):
 
260
        self.make_local_branch_and_tree()
 
261
        # Create 'other' branch containing a new file
 
262
        other_bzrdir = self.tree.bzrdir.sprout('other')
 
263
        other_tree = other_bzrdir.open_workingtree()
 
264
        self.build_tree_contents([('other/other-file', 'other')])
 
265
        other_tree.add('other-file')
 
266
        other_tree.commit('other commit', rev_id='other')
 
267
        # Merge and revert, leaving a pending merge
 
268
        self.tree.merge_from_branch(other_tree.branch)
 
269
        self.tree.revert(filenames=['other-file'], backups=False)
 
270
 
 
271
    def _out_of_sync_trees(self):
 
272
        self.make_local_branch_and_tree()
 
273
        self.run_bzr(['checkout', '--lightweight', 'local', 'checkout'])
 
274
        # Make a change and commit it
 
275
        self.build_tree_contents([('local/file', 'modified in local')])
 
276
        self.tree.commit('modify file', rev_id='modified-in-local')
 
277
        # Exercise commands from the checkout directory
 
278
        self._default_wd = 'checkout'
 
279
        self._default_errors = ["Working tree is out of date, please run"
 
280
                                " 'bzr update'\.",]
 
281
        self._default_dpushed_revid = 'modified-in-local'
 
282
 
 
283
    def test_dpush_default(self):
 
284
        self.assertDpushFails([])
 
285
 
 
286
    def test_dpush_no_strict(self):
 
287
        self.assertDpushSucceeds(['--no-strict'])
 
288
 
 
289
    def test_dpush_strict_with_changes(self):
 
290
        self.assertDpushFails(['--strict'])
 
291
 
 
292
    def test_dpush_respect_config_var_strict(self):
 
293
        self.set_config_dpush_strict('true')
 
294
        self.assertDpushFails([])
 
295
 
 
296
    def test_dpush_bogus_config_var_ignored(self):
 
297
        self.set_config_dpush_strict("I don't want you to be strict")
 
298
        self.assertDpushFails([])
 
299
 
 
300
    def test_dpush_no_strict_command_line_override_config(self):
 
301
        self.set_config_dpush_strict('yES')
 
302
        self.assertDpushFails([])
 
303
        self.assertDpushSucceeds(['--no-strict'])
 
304
 
 
305
    def test_dpush_strict_command_line_override_config(self):
 
306
        self.set_config_dpush_strict('oFF')
 
307
        self.assertDpushFails(['--strict'])
 
308
        self.assertDpushSucceeds([])