~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge_directive.py

  • Committer: Vincent Ladeuil
  • Date: 2008-09-11 19:36:38 UTC
  • mfrom: (3703 +trunk)
  • mto: (3705.1.1 trunk2)
  • mto: This revision was merged to the branch mainline in revision 3708.
  • Revision ID: v.ladeuil+lp@free.fr-20080911193638-wtjyc1kcmacc6t1f
merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import re
18
18
 
19
19
from bzrlib import (
20
20
    errors,
21
21
    gpg,
22
 
    mail_client,
23
22
    merge_directive,
24
23
    tests,
25
 
    trace,
26
24
    )
27
25
 
28
26
 
699
697
        self.assertEqual('booga', md.patch)
700
698
        self.assertEqual('diff', md.patch_type)
701
699
        self.assertEqual('Hi mom!', md.message)
702
 
 
703
 
 
704
 
class TestHook(object):
705
 
    """Hook callback for test purposes."""
706
 
 
707
 
    def __init__(self, result=None):
708
 
        self.calls = []
709
 
        self.result = result
710
 
 
711
 
    def __call__(self, params):
712
 
        self.calls.append(params)
713
 
        return self.result
714
 
 
715
 
 
716
 
class HookMailClient(mail_client.MailClient):
717
 
    """Mail client for testing hooks."""
718
 
 
719
 
    def __init__(self, config):
720
 
        self.body = None
721
 
        self.config = config
722
 
 
723
 
    def compose(self, prompt, to, subject, attachment, mime_subtype,
724
 
                extension, basename=None, body=None):
725
 
        self.body = body
726
 
 
727
 
 
728
 
class TestBodyHook(tests.TestCaseWithTransport):
729
 
 
730
 
    def compose_with_hooks(self, test_hooks, supports_body=True):
731
 
        client = HookMailClient({})
732
 
        client.supports_body = supports_body
733
 
        for test_hook in test_hooks:
734
 
            merge_directive.MergeDirective.hooks.install_named_hook(
735
 
                'merge_request_body', test_hook, 'test')
736
 
        tree = self.make_branch_and_tree('foo')
737
 
        tree.commit('foo')
738
 
        directive = merge_directive.MergeDirective2(
739
 
            tree.branch.last_revision(), 'sha', 0, 0, 'sha',
740
 
            source_branch=tree.branch.base,
741
 
            base_revision_id=tree.branch.last_revision(),
742
 
            message='This code rox')
743
 
        directive.compose_merge_request(client, 'jrandom@example.com',
744
 
            None, tree.branch)
745
 
        return client, directive
746
 
 
747
 
    def test_no_supports_body(self):
748
 
        test_hook = TestHook('foo')
749
 
        old_warn = trace.warning
750
 
        warnings = []
751
 
        def warn(*args):
752
 
            warnings.append(args)
753
 
        trace.warning = warn
754
 
        try:
755
 
            client, directive = self.compose_with_hooks([test_hook],
756
 
                supports_body=False)
757
 
        finally:
758
 
            trace.warning = old_warn
759
 
        self.assertEqual(0, len(test_hook.calls))
760
 
        self.assertEqual(('Cannot run merge_request_body hooks because mail'
761
 
                          ' client %s does not support message bodies.',
762
 
                          'HookMailClient'), warnings[0])
763
 
 
764
 
    def test_body_hook(self):
765
 
        test_hook = TestHook('foo')
766
 
        client, directive = self.compose_with_hooks([test_hook])
767
 
        self.assertEqual(1, len(test_hook.calls))
768
 
        self.assertEqual('foo', client.body)
769
 
        params = test_hook.calls[0]
770
 
        self.assertIsInstance(params,
771
 
                              merge_directive.MergeRequestBodyParams)
772
 
        self.assertIs(None, params.body)
773
 
        self.assertIs(None, params.orig_body)
774
 
        self.assertEqual('jrandom@example.com', params.to)
775
 
        self.assertEqual('[MERGE] This code rox', params.subject)
776
 
        self.assertEqual(directive, params.directive)
777
 
        self.assertEqual('foo-1', params.basename)
778
 
 
779
 
    def test_body_hook_chaining(self):
780
 
        test_hook1 = TestHook('foo')
781
 
        test_hook2 = TestHook('bar')
782
 
        client = self.compose_with_hooks([test_hook1, test_hook2])[0]
783
 
        self.assertEqual(None, test_hook1.calls[0].body)
784
 
        self.assertEqual(None, test_hook1.calls[0].orig_body)
785
 
        self.assertEqual('foo', test_hook2.calls[0].body)
786
 
        self.assertEqual(None, test_hook2.calls[0].orig_body)
787
 
        self.assertEqual('bar', client.body)