]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/patchparser.py
parser: allow words starting with "diff" at beginning of line
[patchwork] / apps / patchwork / tests / patchparser.py
1 # Patchwork - automated patch tracking system
2 # Copyright (C) 2008 Jeremy Kerr <jk@ozlabs.org>
3 #
4 # This file is part of the Patchwork package.
5 #
6 # Patchwork is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # Patchwork is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Patchwork; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20 import unittest
21 import os
22 from email import message_from_string
23 from patchwork.models import Project, Person, Patch, Comment
24 from patchwork.tests.utils import read_patch, read_mail, create_email, defaults
25
26 try:
27     from email.mime.text import MIMEText
28 except ImportError:
29     # Python 2.4 compatibility
30     from email.MIMEText import MIMEText
31
32 class PatchTest(unittest.TestCase):
33     default_sender = defaults.sender
34     default_subject = defaults.subject
35     project = defaults.project
36
37 from patchwork.bin.parsemail import find_content, find_author, find_project, \
38                                     parse_mail
39
40 class InlinePatchTest(PatchTest):
41     patch_filename = '0001-add-line.patch'
42     test_comment = 'Test for attached patch'
43
44     def setUp(self):
45         self.orig_patch = read_patch(self.patch_filename)
46         email = create_email(self.test_comment + '\n' + self.orig_patch)
47         (self.patch, self.comment) = find_content(self.project, email)
48
49     def testPatchPresence(self):
50         self.assertTrue(self.patch is not None)
51
52     def testPatchContent(self):
53         self.assertEquals(self.patch.content, self.orig_patch)
54
55     def testCommentPresence(self):
56         self.assertTrue(self.comment is not None)
57
58     def testCommentContent(self):
59         self.assertEquals(self.comment.content, self.test_comment)
60
61
62 class AttachmentPatchTest(InlinePatchTest):
63     patch_filename = '0001-add-line.patch'
64     test_comment = 'Test for attached patch'
65     content_subtype = 'x-patch'
66
67     def setUp(self):
68         self.orig_patch = read_patch(self.patch_filename)
69         email = create_email(self.test_comment, multipart = True)
70         attachment = MIMEText(self.orig_patch, _subtype = self.content_subtype)
71         email.attach(attachment)
72         (self.patch, self.comment) = find_content(self.project, email)
73
74 class AttachmentXDiffPatchTest(AttachmentPatchTest):
75     content_subtype = 'x-diff'
76
77 class UTF8InlinePatchTest(InlinePatchTest):
78     patch_filename = '0002-utf-8.patch'
79     patch_encoding = 'utf-8'
80
81     def setUp(self):
82         self.orig_patch = read_patch(self.patch_filename, self.patch_encoding)
83         email = create_email(self.test_comment + '\n' + self.orig_patch,
84                              content_encoding = self.patch_encoding)
85         (self.patch, self.comment) = find_content(self.project, email)
86
87 class NoCharsetInlinePatchTest(InlinePatchTest):
88     """ Test mails with no content-type or content-encoding header """
89     patch_filename = '0001-add-line.patch'
90
91     def setUp(self):
92         self.orig_patch = read_patch(self.patch_filename)
93         email = create_email(self.test_comment + '\n' + self.orig_patch)
94         del email['Content-Type']
95         del email['Content-Transfer-Encoding']
96         (self.patch, self.comment) = find_content(self.project, email)
97
98 class SignatureCommentTest(InlinePatchTest):
99     patch_filename = '0001-add-line.patch'
100     test_comment = 'Test comment\nmore comment'
101
102     def setUp(self):
103         self.orig_patch = read_patch(self.patch_filename)
104         email = create_email( \
105                 self.test_comment + '\n' + \
106                 '-- \nsig\n' + self.orig_patch)
107         (self.patch, self.comment) = find_content(self.project, email)
108
109
110 class ListFooterTest(InlinePatchTest):
111     patch_filename = '0001-add-line.patch'
112     test_comment = 'Test comment\nmore comment'
113
114     def setUp(self):
115         self.orig_patch = read_patch(self.patch_filename)
116         email = create_email( \
117                 self.test_comment + '\n' + \
118                 '_______________________________________________\n' + \
119                 'Linuxppc-dev mailing list\n' + \
120                 self.orig_patch)
121         (self.patch, self.comment) = find_content(self.project, email)
122
123
124 class DiffWordInCommentTest(InlinePatchTest):
125     test_comment = 'Lines can start with words beginning in "diff"\n' + \
126                    'difficult\nDifferent'
127
128
129 class UpdateCommentTest(InlinePatchTest):
130     """ Test for '---\nUpdate: v2' style comments to patches. """
131     patch_filename = '0001-add-line.patch'
132     test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
133
134 class UpdateSigCommentTest(SignatureCommentTest):
135     """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
136     patch_filename = '0001-add-line.patch'
137     test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
138
139 class SenderEncodingTest(unittest.TestCase):
140     sender_name = u'example user'
141     sender_email = 'user@example.com'
142     from_header = 'example user <user@example.com>'
143
144     def setUp(self):
145         mail = 'From: %s\n' % self.from_header + \
146                'Subject: test\n\n' + \
147                'test'
148         self.email = message_from_string(mail)
149         (self.person, new) = find_author(self.email)
150         self.person.save()
151
152     def tearDown(self):
153         self.person.delete()
154
155     def testName(self):
156         self.assertEquals(self.person.name, self.sender_name)
157
158     def testEmail(self):
159         self.assertEquals(self.person.email, self.sender_email)
160
161     def testDBQueryName(self):
162         db_person = Person.objects.get(name = self.sender_name)
163         self.assertEquals(self.person, db_person)
164
165     def testDBQueryEmail(self):
166         db_person = Person.objects.get(email = self.sender_email)
167         self.assertEquals(self.person, db_person)
168
169
170 class SenderUTF8QPEncodingTest(SenderEncodingTest):
171     sender_name = u'\xe9xample user'
172     from_header = '=?utf-8?q?=C3=A9xample=20user?= <user@example.com>'
173
174 class SenderUTF8QPSplitEncodingTest(SenderEncodingTest):
175     sender_name = u'\xe9xample user'
176     from_header = '=?utf-8?q?=C3=A9xample?= user <user@example.com>'
177
178 class SenderUTF8B64EncodingTest(SenderUTF8QPEncodingTest):
179     from_header = '=?utf-8?B?w6l4YW1wbGUgdXNlcg==?= <user@example.com>'
180
181 class SubjectEncodingTest(PatchTest):
182     sender = 'example user <user@example.com>'
183     subject = 'test subject'
184     subject_header = 'test subject'
185
186     def setUp(self):
187         mail = 'From: %s\n' % self.sender + \
188                'Subject: %s\n\n' % self.subject_header + \
189                'test\n\n' + defaults.patch
190         self.projects = defaults.project
191         self.email = message_from_string(mail)
192
193     def testSubjectEncoding(self):
194         (patch, comment) = find_content(self.project, self.email)
195         self.assertEquals(patch.name, self.subject)
196
197 class SubjectUTF8QPEncodingTest(SubjectEncodingTest):
198     subject = u'test s\xfcbject'
199     subject_header = '=?utf-8?q?test=20s=c3=bcbject?='
200
201 class SubjectUTF8QPMultipleEncodingTest(SubjectEncodingTest):
202     subject = u'test s\xfcbject'
203     subject_header = 'test =?utf-8?q?s=c3=bcbject?='
204
205 class SenderCorrelationTest(unittest.TestCase):
206     existing_sender = 'Existing Sender <existing@example.com>'
207     non_existing_sender = 'Non-existing Sender <nonexisting@example.com>'
208
209     def mail(self, sender):
210         return message_from_string('From: %s\nSubject: Test\n\ntest\n' % sender)
211
212     def setUp(self):
213         self.existing_sender_mail = self.mail(self.existing_sender)
214         self.non_existing_sender_mail = self.mail(self.non_existing_sender)
215         (self.person, new) = find_author(self.existing_sender_mail)
216         self.person.save()
217
218     def testExisingSender(self):
219         (person, new) = find_author(self.existing_sender_mail)
220         self.assertEqual(new, False)
221         self.assertEqual(person.id, self.person.id)
222
223     def testNonExisingSender(self):
224         (person, new) = find_author(self.non_existing_sender_mail)
225         self.assertEqual(new, True)
226         self.assertEqual(person.id, None)
227
228     def testExistingDifferentFormat(self):
229         mail = self.mail('existing@example.com')
230         (person, new) = find_author(mail)
231         self.assertEqual(new, False)
232         self.assertEqual(person.id, self.person.id)
233
234     def testExistingDifferentCase(self):
235         mail = self.mail(self.existing_sender.upper())
236         (person, new) = find_author(mail)
237         self.assertEqual(new, False)
238         self.assertEqual(person.id, self.person.id)
239
240     def tearDown(self):
241         self.person.delete()
242
243 class MultipleProjectPatchTest(unittest.TestCase):
244     """ Test that patches sent to multiple patchwork projects are
245         handled correctly """
246
247     test_comment = 'Test Comment'
248     patch_filename = '0001-add-line.patch'
249     msgid = '<1@example.com>'
250
251     def setUp(self):
252         self.p1 = Project(linkname = 'test-project-1', name = 'Project 1',
253                 listid = '1.example.com', listemail='1@example.com')
254         self.p2 = Project(linkname = 'test-project-2', name = 'Project 2',
255                 listid = '2.example.com', listemail='2@example.com')
256
257         self.p1.save()
258         self.p2.save()
259
260         patch = read_patch(self.patch_filename)
261         email = create_email(self.test_comment + '\n' + patch)
262         email['Message-Id'] = self.msgid
263
264         del email['List-ID']
265         email['List-ID'] = '<' + self.p1.listid + '>'
266         parse_mail(email)
267
268         del email['List-ID']
269         email['List-ID'] = '<' + self.p2.listid + '>'
270         parse_mail(email)
271
272     def testParsedProjects(self):
273         self.assertEquals(Patch.objects.filter(project = self.p1).count(), 1)
274         self.assertEquals(Patch.objects.filter(project = self.p2).count(), 1)
275
276     def tearDown(self):
277         self.p1.delete()
278         self.p2.delete()
279
280
281 class MultipleProjectPatchCommentTest(MultipleProjectPatchTest):
282     """ Test that followups to multiple-project patches end up on the
283         correct patch """
284
285     comment_msgid = '<2@example.com>'
286     comment_content = 'test comment'
287
288     def setUp(self):
289         super(MultipleProjectPatchCommentTest, self).setUp()
290
291         for project in [self.p1, self.p2]:
292             email = MIMEText(self.comment_content)
293             email['From'] = defaults.sender
294             email['Subject'] = defaults.subject
295             email['Message-Id'] = self.comment_msgid
296             email['List-ID'] = '<' + project.listid + '>'
297             email['In-Reply-To'] = self.msgid
298             parse_mail(email)
299
300     def testParsedComment(self):
301         for project in [self.p1, self.p2]:
302             patch = Patch.objects.filter(project = project)[0]
303             # we should see two comments now - the original mail with the patch,
304             # and the one we parsed in setUp()
305             self.assertEquals(Comment.objects.filter(patch = patch).count(), 2)
306
307 class ListIdHeaderTest(unittest.TestCase):
308     """ Test that we parse List-Id headers from mails correctly """
309     def setUp(self):
310         self.project = Project(linkname = 'test-project-1', name = 'Project 1',
311                 listid = '1.example.com', listemail='1@example.com')
312         self.project.save()
313
314     def testNoListId(self):
315         email = MIMEText('')
316         project = find_project(email)
317         self.assertEquals(project, None)
318
319     def testBlankListId(self):
320         email = MIMEText('')
321         email['List-Id'] = ''
322         project = find_project(email)
323         self.assertEquals(project, None)
324
325     def testWhitespaceListId(self):
326         email = MIMEText('')
327         email['List-Id'] = ' '
328         project = find_project(email)
329         self.assertEquals(project, None)
330
331     def testSubstringListId(self):
332         email = MIMEText('')
333         email['List-Id'] = 'example.com'
334         project = find_project(email)
335         self.assertEquals(project, None)
336
337     def testShortListId(self):
338         """ Some mailing lists have List-Id headers in short formats, where it
339             is only the list ID itself (without enclosing angle-brackets). """
340         email = MIMEText('')
341         email['List-Id'] = self.project.listid
342         project = find_project(email)
343         self.assertEquals(project, self.project)
344
345     def testLongListId(self):
346         email = MIMEText('')
347         email['List-Id'] = 'Test text <%s>' % self.project.listid
348         project = find_project(email)
349         self.assertEquals(project, self.project)
350
351     def tearDown(self):
352         self.project.delete()
353
354 class MBoxPatchTest(PatchTest):
355     def setUp(self):
356         self.mail = read_mail(self.mail_file, project = self.project)
357
358 class GitPullTest(MBoxPatchTest):
359     mail_file = '0001-git-pull-request.mbox'
360
361     def testGitPullRequest(self):
362         (patch, comment) = find_content(self.project, self.mail)
363         self.assertTrue(patch is not None)
364         self.assertTrue(patch.pull_url is not None)
365         self.assertTrue(patch.content is None)
366         self.assertTrue(comment is not None)
367
368 class GitPullWrappedTest(GitPullTest):
369     mail_file = '0002-git-pull-request-wrapped.mbox'
370
371 class GitPullWithDiffTest(MBoxPatchTest):
372     mail_file = '0003-git-pull-request-with-diff.mbox'
373
374     def testGitPullWithDiff(self):
375         (patch, comment) = find_content(self.project, self.mail)
376         self.assertTrue(patch is not None)
377         self.assertEqual('git://git.kernel.org/pub/scm/linux/kernel/git/tip/' +
378              'linux-2.6-tip.git x86-fixes-for-linus', patch.pull_url)
379         self.assertTrue(
380             patch.content.startswith('diff --git a/arch/x86/include/asm/smp.h'),
381             patch.content)
382         self.assertTrue(comment is not None)
383
384 class GitPullGitSSHUrlTest(GitPullTest):
385     mail_file = '0004-git-pull-request-git+ssh.mbox'
386
387 class GitPullSSHUrlTest(GitPullTest):
388     mail_file = '0005-git-pull-request-ssh.mbox'
389
390 class GitPullHTTPUrlTest(GitPullTest):
391     mail_file = '0006-git-pull-request-http.mbox'
392
393 class CVSFormatPatchTest(MBoxPatchTest):
394     mail_file = '0007-cvs-format-diff.mbox'
395
396     def testPatch(self):
397         (patch, comment) = find_content(self.project, self.mail)
398         self.assertTrue(patch is not None)
399         self.assertTrue(comment is not None)
400         self.assertTrue(patch.content.startswith('Index'))