Merge lp:~bjornt/launchpad/compare-email-headers into lp:launchpad

Proposed by Björn Tillenius
Status: Merged
Approved by: Graham Binns
Approved revision: no longer in the source branch.
Merged at revision: 10879
Proposed branch: lp:~bjornt/launchpad/compare-email-headers
Merge into: lp:launchpad
Diff against target: 83 lines (+23/-7)
3 files modified
lib/lp/code/mail/tests/test_branchmergeproposal.py (+1/-1)
lib/lp/codehosting/scanner/tests/test_email.py (+9/-6)
lib/lp/testing/__init__.py (+13/-0)
To merge this branch: bzr merge lp:~bjornt/launchpad/compare-email-headers
Reviewer Review Type Date Requested Status
Graham Binns (community) code Approve
Review via email: mp+25520@code.launchpad.net

Description of the change

Add TestCase.assertEmailHeadersEqual() for comparing e-mail headers.

In case of long headers, the headers are unfolded before being compared.
Also change some tests to use the new method, so that they can cope with
factory.getUniqueString() being changed to return longer strings.

To post a comment you must log in.
Revision history for this message
Graham Binns (gmb) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/code/mail/tests/test_branchmergeproposal.py'
2--- lib/lp/code/mail/tests/test_branchmergeproposal.py 2010-04-06 23:47:36 +0000
3+++ lib/lp/code/mail/tests/test_branchmergeproposal.py 2010-05-18 14:28:36 +0000
4@@ -519,7 +519,7 @@
5 candidate = removeSecurityProxy(candidate)
6 expected_email = '%s <%s>' % (
7 candidate.displayname, candidate.preferredemail.email)
8- self.assertEqual(expected_email, mails[0]['To'])
9+ self.assertEmailHeadersEqual(expected_email, mails[0]['To'])
10
11
12 def test_suite():
13
14=== modified file 'lib/lp/codehosting/scanner/tests/test_email.py'
15--- lib/lp/codehosting/scanner/tests/test_email.py 2010-04-23 07:54:02 +0000
16+++ lib/lp/codehosting/scanner/tests/test_email.py 2010-05-18 14:28:36 +0000
17@@ -61,7 +61,7 @@
18 message = email.message_from_string(initial_email[2])
19 email_body = message.get_payload()
20 self.assertTextIn(expected, email_body)
21- self.assertEqual(
22+ self.assertEmailHeadersEqual(
23 '[Branch %s] 0 revisions' % self.db_branch.unique_name,
24 message['Subject'])
25
26@@ -76,7 +76,7 @@
27 message = email.message_from_string(initial_email[2])
28 email_body = message.get_payload()
29 self.assertTextIn(expected, email_body)
30- self.assertEqual(
31+ self.assertEmailHeadersEqual(
32 '[Branch %s] 1 revision' % self.db_branch.unique_name,
33 message['Subject'])
34
35@@ -94,7 +94,7 @@
36 message = email.message_from_string(uncommit_email[2])
37 email_body = message.get_payload()
38 self.assertTextIn(expected, email_body)
39- self.assertEqual(
40+ self.assertEmailHeadersEqual(
41 '[Branch %s] 1 revision removed' % self.db_branch.unique_name,
42 message['Subject'])
43
44@@ -122,10 +122,13 @@
45 subject = (
46 'Subject: [Branch %s] Test branch' % self.db_branch.unique_name)
47 self.assertTextIn(expected, uncommit_email_body)
48- recommit_email_body = recommit_email[2]
49+
50+ recommit_email_msg = email.message_from_string(recommit_email[2])
51+ recommit_email_body = recommit_email_msg.get_payload()[0].get_payload(
52+ decode=True)
53+ subject = '[Branch %s] Rev 1: second' % self.db_branch.unique_name
54+ self.assertEmailHeadersEqual(subject, recommit_email_msg['Subject'])
55 body_bits = [
56- 'Subject: [Branch %s] Rev 1: second'
57- % self.db_branch.unique_name,
58 'revno: 1',
59 'committer: %s' % author,
60 'branch nick: %s' % self.bzr_branch.nick,
61
62=== modified file 'lib/lp/testing/__init__.py'
63--- lib/lp/testing/__init__.py 2010-05-06 13:39:11 +0000
64+++ lib/lp/testing/__init__.py 2010-05-18 14:28:36 +0000
65@@ -413,6 +413,19 @@
66 os.chdir(tempdir)
67 self.addCleanup(os.chdir, cwd)
68
69+ def _unfoldEmailHeader(self, header):
70+ """Unfold a multiline e-mail header."""
71+ header = ''.join(header.splitlines())
72+ return header.replace('\t', ' ')
73+
74+ def assertEmailHeadersEqual(self, expected, observed):
75+ """Assert that two e-mail headers are equal.
76+
77+ The headers are unfolded before being compared.
78+ """
79+ return self.assertEqual(
80+ self._unfoldEmailHeader(expected),
81+ self._unfoldEmailHeader(observed))
82
83 class TestCaseWithFactory(TestCase):
84