~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2011-10-14 13:56:45 UTC
  • mfrom: (6215 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6216.
  • Revision ID: jelmer@samba.org-20111014135645-phc3q3y21k2ks0s2
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
22
22
from bzrlib import (
23
23
    branch,
24
24
    bzrdir,
 
25
    controldir,
25
26
    errors,
26
27
    osutils,
27
28
    tests,
31
32
    workingtree
32
33
    )
33
34
from bzrlib.repofmt import knitrepo
34
 
from bzrlib.tests import http_server
 
35
from bzrlib.tests import (
 
36
    blackbox,
 
37
    http_server,
 
38
    scenarios,
 
39
    script,
 
40
    test_foreign,
 
41
    test_server,
 
42
    )
35
43
from bzrlib.transport import memory
36
44
 
37
45
 
38
 
def load_tests(standard_tests, module, loader):
39
 
    """Multiply tests for the push command."""
40
 
    result = loader.suiteClass()
41
 
 
42
 
    # one for each king of change
43
 
    changes_tests, remaining_tests = tests.split_suite_by_condition(
44
 
        standard_tests, tests.condition_isinstance((
45
 
                TestPushStrictWithChanges,
46
 
                )))
47
 
    changes_scenarios = [
48
 
        ('uncommitted',
49
 
         dict(_changes_type= '_uncommitted_changes')),
50
 
        ('pending-merges',
51
 
         dict(_changes_type= '_pending_merges')),
52
 
        ('out-of-sync-trees',
53
 
         dict(_changes_type= '_out_of_sync_trees')),
54
 
        ]
55
 
    tests.multiply_tests(changes_tests, changes_scenarios, result)
56
 
    # No parametrization for the remaining tests
57
 
    result.addTests(remaining_tests)
58
 
 
59
 
    return result
 
46
load_tests = scenarios.load_tests_apply_scenarios
60
47
 
61
48
 
62
49
class TestPush(tests.TestCaseWithTransport):
119
106
        transport.delete('branch_b/c')
120
107
        out, err = self.run_bzr('push', working_dir='branch_a')
121
108
        path = branch_a.get_push_location()
122
 
        self.assertEquals(out,
123
 
                          'Using saved push location: %s\n'
124
 
                          % urlutils.local_path_from_url(path))
125
109
        self.assertEqual(err,
 
110
                         'Using saved push location: %s\n'
126
111
                         'All changes applied successfully.\n'
127
 
                         'Pushed up to revision 2.\n')
 
112
                         'Pushed up to revision 2.\n'
 
113
                         % urlutils.local_path_from_url(path))
128
114
        self.assertEqual(path,
129
115
                         branch_b.bzrdir.root_transport.base)
130
116
        # test explicit --remember
141
127
        b2 = branch.Branch.open('pushed-location')
142
128
        self.assertEndsWith(b2.base, 'pushed-location/')
143
129
 
 
130
    def test_push_no_tree(self):
 
131
        # bzr push --no-tree of a branch with working trees
 
132
        b = self.make_branch_and_tree('push-from')
 
133
        self.build_tree(['push-from/file'])
 
134
        b.add('file')
 
135
        b.commit('commit 1')
 
136
        out, err = self.run_bzr('push --no-tree -d push-from push-to')
 
137
        self.assertEqual('', out)
 
138
        self.assertEqual('Created new branch.\n', err)
 
139
        self.assertPathDoesNotExist('push-to/file')
 
140
 
144
141
    def test_push_new_branch_revision_count(self):
145
142
        # bzr push of a branch with revisions to a new location
146
143
        # should print the number of revisions equal to the length of the
153
150
        self.assertEqual('', out)
154
151
        self.assertEqual('Created new branch.\n', err)
155
152
 
 
153
    def test_push_quiet(self):
 
154
        # test that using -q makes output quiet
 
155
        t = self.make_branch_and_tree('tree')
 
156
        self.build_tree(['tree/file'])
 
157
        t.add('file')
 
158
        t.commit('commit 1')
 
159
        self.run_bzr('push -d tree pushed-to')
 
160
        path = t.branch.get_push_location()
 
161
        out, err = self.run_bzr('push', working_dir="tree")
 
162
        self.assertEqual('Using saved push location: %s\n'
 
163
                         'No new revisions or tags to push.\n' %
 
164
                         urlutils.local_path_from_url(path), err)
 
165
        out, err = self.run_bzr('push -q', working_dir="tree")
 
166
        self.assertEqual('', out)
 
167
        self.assertEqual('', err)
 
168
 
156
169
    def test_push_only_pushes_history(self):
157
170
        # Knit branches should only push the history for the current revision.
158
171
        format = bzrdir.BzrDirMetaFormat1()
162
175
 
163
176
        def make_shared_tree(path):
164
177
            shared_repo.bzrdir.root_transport.mkdir(path)
165
 
            shared_repo.bzrdir.create_branch_convenience('repo/' + path)
 
178
            controldir.ControlDir.create_branch_convenience('repo/' + path)
166
179
            return workingtree.WorkingTree.open('repo/' + path)
167
180
        tree_a = make_shared_tree('a')
168
181
        self.build_tree(['repo/a/file'])
203
216
        t.commit(allow_pointless=True,
204
217
                message='first commit')
205
218
        self.run_bzr('push -d from to-one')
206
 
        self.failUnlessExists('to-one')
 
219
        self.assertPathExists('to-one')
207
220
        self.run_bzr('push -d %s %s'
208
221
            % tuple(map(urlutils.local_path_to_url, ['from', 'to-two'])))
209
 
        self.failUnlessExists('to-two')
 
222
        self.assertPathExists('to-two')
 
223
 
 
224
    def test_push_repository_no_branch_doesnt_fetch_all_revs(self):
 
225
        # See https://bugs.launchpad.net/bzr/+bug/465517
 
226
        target_repo = self.make_repository('target')
 
227
        source = self.make_branch_builder('source')
 
228
        source.start_series()
 
229
        source.build_snapshot('A', None, [
 
230
            ('add', ('', 'root-id', 'directory', None))])
 
231
        source.build_snapshot('B', ['A'], [])
 
232
        source.build_snapshot('C', ['A'], [])
 
233
        source.finish_series()
 
234
        self.run_bzr('push target -d source')
 
235
        self.addCleanup(target_repo.lock_read().unlock)
 
236
        # We should have pushed 'C', but not 'B', since it isn't in the
 
237
        # ancestry
 
238
        self.assertEqual([('A',), ('C',)], sorted(target_repo.revisions.keys()))
210
239
 
211
240
    def test_push_smart_non_stacked_streaming_acceptance(self):
212
241
        self.setup_smart_server_with_call_log()
235
264
        # being too low. If rpc_count increases, more network roundtrips have
236
265
        # become necessary for this use case. Please do not adjust this number
237
266
        # upwards without agreement from bzr's network support maintainers.
238
 
        self.assertLength(14, self.hpss_calls)
 
267
        self.assertLength(13, self.hpss_calls)
239
268
        remote = branch.Branch.open('public')
240
269
        self.assertEndsWith(remote.get_stacked_on_url(), '/parent')
241
270
 
253
282
        # upwards without agreement from bzr's network support maintainers.
254
283
        self.assertLength(11, self.hpss_calls)
255
284
 
 
285
    def test_push_smart_incremental_acceptance(self):
 
286
        self.setup_smart_server_with_call_log()
 
287
        t = self.make_branch_and_tree('from')
 
288
        rev_id1 = t.commit(allow_pointless=True, message='first commit')
 
289
        rev_id2 = t.commit(allow_pointless=True, message='second commit')
 
290
        self.run_bzr(
 
291
            ['push', self.get_url('to-one'), '-r1'], working_dir='from')
 
292
        self.reset_smart_call_log()
 
293
        self.run_bzr(['push', self.get_url('to-one')], working_dir='from')
 
294
        # This figure represent the amount of work to perform this use case. It
 
295
        # is entirely ok to reduce this number if a test fails due to rpc_count
 
296
        # being too low. If rpc_count increases, more network roundtrips have
 
297
        # become necessary for this use case. Please do not adjust this number
 
298
        # upwards without agreement from bzr's network support maintainers.
 
299
        self.assertLength(11, self.hpss_calls)
 
300
 
256
301
    def test_push_smart_with_default_stacking_url_path_segment(self):
257
302
        # If the default stacked-on location is a path element then branches
258
303
        # we push there over the smart server are stacked and their
296
341
                     working_dir='tree')
297
342
        new_tree = workingtree.WorkingTree.open('new/tree')
298
343
        self.assertEqual(tree.last_revision(), new_tree.last_revision())
299
 
        self.failUnlessExists('new/tree/a')
 
344
        self.assertPathExists('new/tree/a')
300
345
 
301
346
    def test_push_use_existing(self):
302
347
        """'bzr push --use-existing-dir' can push into an existing dir.
317
362
        new_tree = workingtree.WorkingTree.open('target')
318
363
        self.assertEqual(tree.last_revision(), new_tree.last_revision())
319
364
        # The push should have created target/a
320
 
        self.failUnlessExists('target/a')
 
365
        self.assertPathExists('target/a')
 
366
 
 
367
    def test_push_use_existing_into_empty_bzrdir(self):
 
368
        """'bzr push --use-existing-dir' into a dir with an empty .bzr dir
 
369
        fails.
 
370
        """
 
371
        tree = self.create_simple_tree()
 
372
        self.build_tree(['target/', 'target/.bzr/'])
 
373
        self.run_bzr_error(
 
374
            ['Target directory ../target already contains a .bzr directory, '
 
375
             'but it is not valid.'],
 
376
            'push ../target --use-existing-dir', working_dir='tree')
321
377
 
322
378
    def test_push_onto_repo(self):
323
379
        """We should be able to 'bzr push' into an existing bzrdir."""
553
609
 
554
610
class RedirectingMemoryServer(memory.MemoryServer):
555
611
 
556
 
    def setUp(self):
 
612
    def start_server(self):
557
613
        self._dirs = {'/': None}
558
614
        self._files = {}
559
615
        self._locks = {}
567
623
        result._locks = self._locks
568
624
        return result
569
625
 
570
 
    def tearDown(self):
 
626
    def stop_server(self):
571
627
        transport.unregister_transport(self._scheme, self._memory_factory)
572
628
 
573
629
 
576
632
    def setUp(self):
577
633
        tests.TestCaseWithTransport.setUp(self)
578
634
        self.memory_server = RedirectingMemoryServer()
579
 
        self.memory_server.setUp()
580
 
        self.addCleanup(self.memory_server.tearDown)
581
 
 
 
635
        self.start_server(self.memory_server)
582
636
        # Make the branch and tree that we'll be pushing.
583
637
        t = self.make_branch_and_tree('tree')
584
638
        self.build_tree(['tree/file'])
624
678
    def set_config_push_strict(self, value):
625
679
        # set config var (any of bazaar.conf, locations.conf, branch.conf
626
680
        # should do)
627
 
        conf = self.tree.branch.get_config()
628
 
        conf.set_user_option('push_strict', value)
 
681
        conf = self.tree.branch.get_config_stack()
 
682
        conf.set('push_strict', value)
629
683
 
630
684
    _default_command = ['push', '../to']
631
685
    _default_wd = 'local'
632
686
    _default_errors = ['Working tree ".*/local/" has uncommitted '
633
687
                       'changes \(See bzr status\)\.',]
634
 
    _default_pushed_revid = 'modified'
 
688
    _default_additional_error = 'Use --no-strict to force the push.\n'
 
689
    _default_additional_warning = 'Uncommitted changes will not be pushed.'
 
690
 
635
691
 
636
692
    def assertPushFails(self, args):
637
 
        self.run_bzr_error(self._default_errors, self._default_command + args,
638
 
                           working_dir=self._default_wd, retcode=3)
 
693
        out, err = self.run_bzr_error(self._default_errors,
 
694
                                      self._default_command + args,
 
695
                                      working_dir=self._default_wd, retcode=3)
 
696
        self.assertContainsRe(err, self._default_additional_error)
639
697
 
640
 
    def assertPushSucceeds(self, args, pushed_revid=None):
641
 
        self.run_bzr(self._default_command + args,
642
 
                     working_dir=self._default_wd)
643
 
        if pushed_revid is None:
644
 
            pushed_revid = self._default_pushed_revid
645
 
        tree_to = workingtree.WorkingTree.open('to')
646
 
        repo_to = tree_to.branch.repository
647
 
        self.assertTrue(repo_to.has_revision(pushed_revid))
648
 
        self.assertEqual(tree_to.branch.last_revision_info()[1], pushed_revid)
 
698
    def assertPushSucceeds(self, args, with_warning=False, revid_to_push=None):
 
699
        if with_warning:
 
700
            error_regexes = self._default_errors
 
701
        else:
 
702
            error_regexes = []
 
703
        out, err = self.run_bzr(self._default_command + args,
 
704
                                working_dir=self._default_wd,
 
705
                                error_regexes=error_regexes)
 
706
        if with_warning:
 
707
            self.assertContainsRe(err, self._default_additional_warning)
 
708
        else:
 
709
            self.assertNotContainsRe(err, self._default_additional_warning)
 
710
        branch_from = branch.Branch.open(self._default_wd)
 
711
        if revid_to_push is None:
 
712
            revid_to_push = branch_from.last_revision()
 
713
        branch_to = branch.Branch.open('to')
 
714
        repo_to = branch_to.repository
 
715
        self.assertTrue(repo_to.has_revision(revid_to_push))
 
716
        self.assertEqual(revid_to_push, branch_to.last_revision())
649
717
 
650
718
 
651
719
 
674
742
        self.assertPushSucceeds([])
675
743
 
676
744
 
 
745
strict_push_change_scenarios = [
 
746
    ('uncommitted',
 
747
        dict(_changes_type= '_uncommitted_changes')),
 
748
    ('pending-merges',
 
749
        dict(_changes_type= '_pending_merges')),
 
750
    ('out-of-sync-trees',
 
751
        dict(_changes_type= '_out_of_sync_trees')),
 
752
    ]
 
753
 
 
754
 
677
755
class TestPushStrictWithChanges(tests.TestCaseWithTransport,
678
756
                                TestPushStrictMixin):
679
757
 
 
758
    scenarios = strict_push_change_scenarios 
680
759
    _changes_type = None # Set by load_tests
681
760
 
682
761
    def setUp(self):
683
762
        super(TestPushStrictWithChanges, self).setUp()
 
763
        # Apply the changes defined in load_tests: one of _uncommitted_changes,
 
764
        # _pending_merges or _out_of_sync_trees
684
765
        getattr(self, self._changes_type)()
685
766
 
686
767
    def _uncommitted_changes(self):
710
791
        self._default_wd = 'checkout'
711
792
        self._default_errors = ["Working tree is out of date, please run"
712
793
                                " 'bzr update'\.",]
713
 
        self._default_pushed_revid = 'modified-in-local'
714
794
 
715
795
    def test_push_default(self):
716
 
        self.assertPushFails([])
 
796
        self.assertPushSucceeds([], with_warning=True)
717
797
 
718
798
    def test_push_with_revision(self):
719
 
        self.assertPushSucceeds(['-r', 'revid:added'], pushed_revid='added')
 
799
        self.assertPushSucceeds(['-r', 'revid:added'], revid_to_push='added')
720
800
 
721
801
    def test_push_no_strict(self):
722
802
        self.assertPushSucceeds(['--no-strict'])
730
810
 
731
811
    def test_push_bogus_config_var_ignored(self):
732
812
        self.set_config_push_strict("I don't want you to be strict")
733
 
        self.assertPushFails([])
 
813
        self.assertPushSucceeds([], with_warning=True)
734
814
 
735
815
    def test_push_no_strict_command_line_override_config(self):
736
816
        self.set_config_push_strict('yES')
741
821
        self.set_config_push_strict('oFF')
742
822
        self.assertPushFails(['--strict'])
743
823
        self.assertPushSucceeds([])
 
824
 
 
825
 
 
826
class TestPushForeign(tests.TestCaseWithTransport):
 
827
 
 
828
    def setUp(self):
 
829
        super(TestPushForeign, self).setUp()
 
830
        test_foreign.register_dummy_foreign_for_test(self)
 
831
 
 
832
    def make_dummy_builder(self, relpath):
 
833
        builder = self.make_branch_builder(
 
834
            relpath, format=test_foreign.DummyForeignVcsDirFormat())
 
835
        builder.build_snapshot('revid', None,
 
836
            [('add', ('', 'TREE_ROOT', 'directory', None)),
 
837
             ('add', ('foo', 'fooid', 'file', 'bar'))])
 
838
        return builder
 
839
 
 
840
    def test_no_roundtripping(self):
 
841
        target_branch = self.make_dummy_builder('dp').get_branch()
 
842
        source_tree = self.make_branch_and_tree("dc")
 
843
        output, error = self.run_bzr("push -d dc dp", retcode=3)
 
844
        self.assertEquals("", output)
 
845
        self.assertEquals(error, "bzr: ERROR: It is not possible to losslessly"
 
846
            " push to dummy. You may want to use dpush instead.\n")
 
847
 
 
848
 
 
849
class TestPushOutput(script.TestCaseWithTransportAndScript):
 
850
 
 
851
    def test_push_log_format(self):
 
852
        self.run_script("""
 
853
            $ bzr init trunk
 
854
            Created a standalone tree (format: 2a)
 
855
            $ cd trunk
 
856
            $ echo foo > file
 
857
            $ bzr add
 
858
            adding file
 
859
            $ bzr commit -m 'we need some foo'
 
860
            2>Committing to:...trunk/
 
861
            2>added file
 
862
            2>Committed revision 1.
 
863
            $ bzr init ../feature
 
864
            Created a standalone tree (format: 2a)
 
865
            $ bzr push -v ../feature -Olog_format=line
 
866
            Added Revisions:
 
867
            1: jrandom@example.com ...we need some foo
 
868
            2>All changes applied successfully.
 
869
            2>Pushed up to revision 1.
 
870
            """)