]> git.ozlabs.org Git - patchwork/commitdiff
[tests] Add tests for utf-8 patches
authorJeremy Kerr <jk@ozlabs.org>
Mon, 29 Sep 2008 12:27:51 +0000 (22:27 +1000)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 29 Sep 2008 12:31:12 +0000 (22:31 +1000)
.. which expose a bug in the patch parser, so fix that.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
apps/patchwork/bin/parsemail.py
apps/patchwork/tests/patches/0002-utf-8.patch [new file with mode: 0644]
apps/patchwork/tests/patchparser.py
apps/patchwork/tests/utils.py

index e2beeae1b752d8ce03d64ee77d839caf294cd09d..2acceb5836a871835a42eff65f99360e078b6823 100755 (executable)
@@ -137,16 +137,7 @@ def find_content(project, mail):
         if part.get_content_maintype() != 'text':
             continue
 
-        #print "\t%s, %s" % \
-        #    (part.get_content_subtype(), part.get_content_charset())
-
-        charset = part.get_content_charset()
-        if not charset:
-            charset = mail.get_charset()
-        if not charset:
-            charset = 'utf-8'
-
-        payload = unicode(part.get_payload(decode=True), charset, "replace")
+        payload = part.get_payload(decode=True)
 
         if part.get_content_subtype() == 'x-patch':
             patchbuf = payload
diff --git a/apps/patchwork/tests/patches/0002-utf-8.patch b/apps/patchwork/tests/patches/0002-utf-8.patch
new file mode 100644 (file)
index 0000000..71a2f24
--- /dev/null
@@ -0,0 +1,7 @@
+diff --git a/meep.text b/meep.text
+index 3d75d48..a57f4dd 100644
+--- a/meep.text
++++ b/meep.text
+@@ -1,1 +1,2 @@
+ meep
++meëp
index 649da0a2f684f5ed3face1da5ffd968702afc76b..7b24bbcd62c43ce96ad7bbd6d75b46c8d6aabb6e 100644 (file)
@@ -69,6 +69,15 @@ class AttachmentPatchTest(InlinePatchTest):
         email.attach(attachment)
         (self.patch, self.comment) = find_content(self.project, email)
 
+class UTF8InlinePatchTest(InlinePatchTest):
+    patch_filename = '0002-utf-8.patch'
+    patch_encoding = 'utf-8'
+
+    def setUp(self):
+        self.orig_patch = read_patch(self.patch_filename, self.patch_encoding)
+        email = create_email(self.test_comment + '\n' + self.orig_patch,
+                             content_encoding = self.patch_encoding)
+        (self.patch, self.comment) = find_content(self.project, email)
 
 class SignatureCommentTest(InlinePatchTest):
     patch_filename = '0001-add-line.patch'
index c7be6ab40b5850b94582bf9376c0770d0cea4d04..a2bec15061a012c9ce8582ee93447da9b4af7fe4 100644 (file)
@@ -18,7 +18,8 @@
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 import os
-from patchwork.models import Project
+import codecs
+from patchwork.models import Project, Person
 try:
     from email.mime.text import MIMEText
     from email.mime.multipart import MIMEMultipart
@@ -35,6 +36,8 @@ class defaults(object):
     project = Project(linkname = 'test-project', name = 'Test Project')
 
     patch_author = 'Patch Author <patch-author@example.com>'
+    patch_author_person = Person(name = 'Patch Author',
+                                email = 'patch-author@example.com')
 
     comment_author = 'Comment Author <comment-author@example.com>'
 
@@ -45,11 +48,17 @@ class defaults(object):
     patch_name = 'Test Patch'
 
 
-def read_patch(filename):
-    return file(os.path.join(_test_patch_dir, filename)).read()
+def read_patch(filename, encoding = None):
+    file_path = os.path.join(_test_patch_dir, filename)
+    if encoding is not None:
+       f = codecs.open(file_path, encoding = encoding)
+    else:
+        f = file(file_path)
+
+    return f.read()
 
 def create_email(content, subject = None, sender = None, multipart = False,
-        project = None):
+        project = None, content_encoding = None):
     if subject is None:
         subject = defaults.subject
     if sender is None:
@@ -60,12 +69,17 @@ def create_email(content, subject = None, sender = None, multipart = False,
     if multipart:
         msg = MIMEMultipart()
         body = MIMEText(content, _subtype = 'plain')
+        if content_encoding is not None:
+            body.set_charset(content_encoding)
         msg.attach(body)
     else:
         msg = MIMEText(content)
+        if content_encoding is not None:
+            msg.set_charset(content_encoding)
 
     msg['Subject'] = subject
     msg['From'] = sender
     msg['List-Id'] = project.linkname
 
+
     return msg