]> git.ozlabs.org Git - patchwork/blob - apps/patchwork/tests/patchparser.py
[parser] Don't remove --- update lines
[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.mime.text import MIMEText
23 from email.mime.multipart import MIMEMultipart
24 from patchwork.models import Project
25
26 test_mail_dir  = 'patchwork/tests/mail'
27 test_patch_dir = 'patchwork/tests/patches'
28
29 class PatchTest(unittest.TestCase):
30     default_sender = 'Test Author <test@exmaple.com>'
31     default_subject = 'Test Subject'
32     project = Project(linkname = 'test-project')
33
34     def create_email(self, content, subject = None, sender = None,
35             multipart = False):
36         if subject is None:
37             subject = self.default_subject
38         if sender is None:
39             sender = self.default_sender
40
41         if multipart:
42             msg = MIMEMultipart()
43             body = MIMEText(content, _subtype = 'plain')
44             msg.attach(body)
45         else:
46             msg = MIMEText(content)
47
48         msg['Subject'] = subject
49         msg['From'] = sender
50         msg['List-Id'] = self.project.linkname
51
52         return msg
53
54     def read_patch(self, filename):
55         return file(os.path.join(test_patch_dir, filename)).read()
56
57
58 from patchwork.bin.parsemail import find_content
59
60 class InlinePatchTest(PatchTest):
61     patch_filename = '0001-add-line.patch'
62     test_comment = 'Test for attached patch'
63
64     def setUp(self):
65         self.orig_patch = self.read_patch(self.patch_filename)
66         email = self.create_email(self.test_comment + '\n' + self.orig_patch)
67         (self.patch, self.comment) = find_content(self.project, email)
68
69     def testPatchPresence(self):
70         self.assertTrue(self.patch is not None)
71
72     def testPatchContent(self):
73         self.assertEquals(self.patch.content, self.orig_patch)
74
75     def testCommentPresence(self):
76         self.assertTrue(self.comment is not None)
77
78     def testCommentContent(self):
79         self.assertEquals(self.comment.content, self.test_comment)
80
81
82 class AttachmentPatchTest(InlinePatchTest):
83     patch_filename = '0001-add-line.patch'
84     test_comment = 'Test for attached patch'
85
86     def setUp(self):
87         self.orig_patch = self.read_patch(self.patch_filename)
88         email = self.create_email(self.test_comment, multipart = True)
89         attachment = MIMEText(self.orig_patch, _subtype = 'x-patch')
90         email.attach(attachment)
91         (self.patch, self.comment) = find_content(self.project, email)
92
93
94 class SignatureCommentTest(InlinePatchTest):
95     patch_filename = '0001-add-line.patch'
96     test_comment = 'Test comment\nmore comment'
97
98     def setUp(self):
99         self.orig_patch = self.read_patch(self.patch_filename)
100         email = self.create_email( \
101                 self.test_comment + '\n' + \
102                 '-- \nsig\n' + self.orig_patch)
103         (self.patch, self.comment) = find_content(self.project, email)
104
105
106 class ListFooterTest(InlinePatchTest):
107     patch_filename = '0001-add-line.patch'
108     test_comment = 'Test comment\nmore comment'
109
110     def setUp(self):
111         self.orig_patch = self.read_patch(self.patch_filename)
112         email = self.create_email( \
113                 self.test_comment + '\n' + \
114                 '_______________________________________________\n' + \
115                 'Linuxppc-dev mailing list\n' + \
116                 self.orig_patch)
117         (self.patch, self.comment) = find_content(self.project, email)
118
119
120 class UpdateCommentTest(InlinePatchTest):
121     """ Test for '---\nUpdate: v2' style comments to patches. """
122     patch_filename = '0001-add-line.patch'
123     test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'
124
125 class UpdateSigCommentTest(SignatureCommentTest):
126     """ Test for '---\nUpdate: v2' style comments to patches, with a sig """
127     patch_filename = '0001-add-line.patch'
128     test_comment = 'Test comment\nmore comment\n---\nUpdate: test update'