~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_merge_directive.py

  • Committer: Aaron Bentley
  • Date: 2009-05-11 18:35:20 UTC
  • mto: This revision was merged to the branch mainline in revision 4351.
  • Revision ID: aaron@aaronbentley.com-20090511183520-xtvepoauy7zjtb7x
Move hook to MergeDirective, implement MergeDirective.compose_merge_request.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
from bzrlib import (
20
20
    errors,
21
21
    gpg,
 
22
    mail_client,
22
23
    merge_directive,
23
24
    tests,
24
25
    )
697
698
        self.assertEqual('booga', md.patch)
698
699
        self.assertEqual('diff', md.patch_type)
699
700
        self.assertEqual('Hi mom!', md.message)
 
701
 
 
702
 
 
703
class TestHook(object):
 
704
    """Hook callback for test purposes."""
 
705
 
 
706
    def __init__(self, result=None):
 
707
        self.calls = []
 
708
        self.result = result
 
709
 
 
710
    def __call__(self, params):
 
711
        self.calls.append(params)
 
712
        return self.result
 
713
 
 
714
 
 
715
class HookMailClient(mail_client.MailClient):
 
716
    """Mail client for testing hooks."""
 
717
 
 
718
    def __init__(self, config):
 
719
        self.body = None
 
720
        self.config = config
 
721
 
 
722
    def compose(self, prompt, to, subject, attachment, mime_subtype,
 
723
                extension, basename=None, body=None):
 
724
        self.body = body
 
725
 
 
726
 
 
727
class TestBodyHook(tests.TestCaseWithTransport):
 
728
 
 
729
    def compose_with_hooks(self, test_hooks):
 
730
        client = HookMailClient({})
 
731
        for test_hook in test_hooks:
 
732
            merge_directive.MergeDirective.hooks.install_named_hook(
 
733
                'merge_request_body', test_hook, 'test')
 
734
        tree = self.make_branch_and_tree('foo')
 
735
        tree.commit('foo')
 
736
        directive = merge_directive.MergeDirective2(
 
737
            tree.branch.last_revision(), 'sha', 0, 0, 'sha',
 
738
            source_branch=tree.branch.base,
 
739
            base_revision_id=tree.branch.last_revision(),
 
740
            message='This code rox')
 
741
        directive.compose_merge_request(client, 'jrandom@example.com',
 
742
            None, tree.branch)
 
743
        return client, directive
 
744
 
 
745
    def test_body_hook(self):
 
746
        test_hook = TestHook('foo')
 
747
        client, directive = self.compose_with_hooks([test_hook])
 
748
        self.assertEqual(1, len(test_hook.calls))
 
749
        self.assertEqual('foo', client.body)
 
750
        params = test_hook.calls[0]
 
751
        self.assertIsInstance(params,
 
752
                              merge_directive.MergeRequestBodyParams)
 
753
        self.assertIs(None, params.body)
 
754
        self.assertIs(None, params.orig_body)
 
755
        self.assertEqual('jrandom@example.com', params.to)
 
756
        self.assertEqual('[MERGE] This code rox', params.subject)
 
757
        self.assertEqual(directive, params.directive)
 
758
        self.assertEqual('foo-1', params.basename)
 
759
 
 
760
    def test_body_hook_chaining(self):
 
761
        test_hook1 = TestHook('foo')
 
762
        test_hook2 = TestHook('bar')
 
763
        client = self.compose_with_hooks([test_hook1, test_hook2])[0]
 
764
        self.assertEqual(None, test_hook1.calls[0].body)
 
765
        self.assertEqual(None, test_hook1.calls[0].orig_body)
 
766
        self.assertEqual('foo', test_hook2.calls[0].body)
 
767
        self.assertEqual(None, test_hook2.calls[0].orig_body)
 
768
        self.assertEqual('bar', client.body)