From 969d359e9c2f3eb5ee4741208ef17639d5e1b180 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Sat, 20 Sep 2008 14:11:49 +1000 Subject: [PATCH] [test] Move helper functions into tests/utils.py So that we can use them for other tests Signed-off-by: Jeremy Kerr --- apps/patchwork/tests/patchparser.py | 52 +++++---------------- apps/patchwork/tests/utils.py | 71 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 apps/patchwork/tests/utils.py diff --git a/apps/patchwork/tests/patchparser.py b/apps/patchwork/tests/patchparser.py index 6d315f0..649da0a 100644 --- a/apps/patchwork/tests/patchparser.py +++ b/apps/patchwork/tests/patchparser.py @@ -21,46 +21,18 @@ import unittest import os from email import message_from_string from patchwork.models import Project, Person +from patchwork.tests.utils import read_patch, create_email, defaults try: from email.mime.text import MIMEText - from email.mime.multipart import MIMEMultipart except ImportError: # Python 2.4 compatibility from email.MIMEText import MIMEText - from email.MIMEMultipart import MIMEMultipart - -test_mail_dir = 'patchwork/tests/mail' -test_patch_dir = 'patchwork/tests/patches' class PatchTest(unittest.TestCase): - default_sender = 'Test Author ' - default_subject = 'Test Subject' - project = Project(linkname = 'test-project') - - def create_email(self, content, subject = None, sender = None, - multipart = False): - if subject is None: - subject = self.default_subject - if sender is None: - sender = self.default_sender - - if multipart: - msg = MIMEMultipart() - body = MIMEText(content, _subtype = 'plain') - msg.attach(body) - else: - msg = MIMEText(content) - - msg['Subject'] = subject - msg['From'] = sender - msg['List-Id'] = self.project.linkname - - return msg - - def read_patch(self, filename): - return file(os.path.join(test_patch_dir, filename)).read() - + default_sender = defaults.sender + default_subject = defaults.subject + project = defaults.project from patchwork.bin.parsemail import find_content, find_author @@ -69,8 +41,8 @@ class InlinePatchTest(PatchTest): test_comment = 'Test for attached patch' def setUp(self): - self.orig_patch = self.read_patch(self.patch_filename) - email = self.create_email(self.test_comment + '\n' + self.orig_patch) + self.orig_patch = read_patch(self.patch_filename) + email = create_email(self.test_comment + '\n' + self.orig_patch) (self.patch, self.comment) = find_content(self.project, email) def testPatchPresence(self): @@ -91,8 +63,8 @@ class AttachmentPatchTest(InlinePatchTest): test_comment = 'Test for attached patch' def setUp(self): - self.orig_patch = self.read_patch(self.patch_filename) - email = self.create_email(self.test_comment, multipart = True) + self.orig_patch = read_patch(self.patch_filename) + email = create_email(self.test_comment, multipart = True) attachment = MIMEText(self.orig_patch, _subtype = 'x-patch') email.attach(attachment) (self.patch, self.comment) = find_content(self.project, email) @@ -103,8 +75,8 @@ class SignatureCommentTest(InlinePatchTest): test_comment = 'Test comment\nmore comment' def setUp(self): - self.orig_patch = self.read_patch(self.patch_filename) - email = self.create_email( \ + self.orig_patch = read_patch(self.patch_filename) + email = create_email( \ self.test_comment + '\n' + \ '-- \nsig\n' + self.orig_patch) (self.patch, self.comment) = find_content(self.project, email) @@ -115,8 +87,8 @@ class ListFooterTest(InlinePatchTest): test_comment = 'Test comment\nmore comment' def setUp(self): - self.orig_patch = self.read_patch(self.patch_filename) - email = self.create_email( \ + self.orig_patch = read_patch(self.patch_filename) + email = create_email( \ self.test_comment + '\n' + \ '_______________________________________________\n' + \ 'Linuxppc-dev mailing list\n' + \ diff --git a/apps/patchwork/tests/utils.py b/apps/patchwork/tests/utils.py new file mode 100644 index 0000000..c7be6ab --- /dev/null +++ b/apps/patchwork/tests/utils.py @@ -0,0 +1,71 @@ +# Patchwork - automated patch tracking system +# Copyright (C) 2008 Jeremy Kerr +# +# This file is part of the Patchwork package. +# +# Patchwork is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# Patchwork is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Patchwork; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import os +from patchwork.models import Project +try: + from email.mime.text import MIMEText + from email.mime.multipart import MIMEMultipart +except ImportError: + # Python 2.4 compatibility + from email.MIMEText import MIMEText + from email.MIMEMultipart import MIMEMultipart + +# helper functions for tests +_test_mail_dir = 'patchwork/tests/mail' +_test_patch_dir = 'patchwork/tests/patches' + +class defaults(object): + project = Project(linkname = 'test-project', name = 'Test Project') + + patch_author = 'Patch Author ' + + comment_author = 'Comment Author ' + + sender = 'Test Author ' + + subject = 'Test Subject' + + patch_name = 'Test Patch' + + +def read_patch(filename): + return file(os.path.join(_test_patch_dir, filename)).read() + +def create_email(content, subject = None, sender = None, multipart = False, + project = None): + if subject is None: + subject = defaults.subject + if sender is None: + sender = defaults.sender + if project is None: + project = defaults.project + + if multipart: + msg = MIMEMultipart() + body = MIMEText(content, _subtype = 'plain') + msg.attach(body) + else: + msg = MIMEText(content) + + msg['Subject'] = subject + msg['From'] = sender + msg['List-Id'] = project.linkname + + return msg -- 2.39.2