]> git.ozlabs.org Git - patchwork/blob - patchwork/tests/utils.py
trivial: Remove Python < 2.5 code
[patchwork] / patchwork / tests / utils.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 os
21 import codecs
22 from patchwork.models import Project, Person
23 from django.contrib.auth.models import User
24 from django.forms.fields import EmailField
25
26 from email import message_from_file
27 from email.mime.text import MIMEText
28 from email.mime.multipart import MIMEMultipart
29
30 # helper functions for tests
31 _test_mail_dir  = os.path.join(os.path.dirname(__file__), 'mail')
32 _test_patch_dir = os.path.join(os.path.dirname(__file__), 'patches')
33
34 class defaults(object):
35     project = Project(linkname = 'test-project', name = 'Test Project',
36                       listid = 'test.example.com')
37
38     patch_author = 'Patch Author <patch-author@example.com>'
39     patch_author_person = Person(name = 'Patch Author',
40         email = 'patch-author@example.com')
41
42     comment_author = 'Comment Author <comment-author@example.com>'
43
44     sender = 'Test Author <test-author@example.com>'
45
46     subject = 'Test Subject'
47
48     patch_name = 'Test Patch'
49
50     patch = """--- /dev/null    2011-01-01 00:00:00.000000000 +0800
51 +++ a   2011-01-01 00:00:00.000000000 +0800
52 @@ -0,0 +1 @@
53 +a
54 """
55
56 error_strings = {
57     'email': 'Enter a valid email address.',
58 }
59
60 _user_idx = 1
61 def create_user():
62     global _user_idx
63     userid = 'test%d' % _user_idx
64     email = '%s@example.com' % userid
65     _user_idx += 1
66
67     user = User.objects.create_user(userid, email, userid)
68     user.save()
69
70     person = Person(email = email, name = userid, user = user)
71     person.save()
72
73     return user
74
75 def create_maintainer(project):
76     user = create_user()
77     profile = user.profile
78     profile.maintainer_projects.add(project)
79     profile.save()
80     return user
81
82 def find_in_context(context, key):
83     if isinstance(context, list):
84         for c in context:
85             v = find_in_context(c, key)
86             if v is not None:
87                 return v
88     else:
89         if key in context:
90             return context[key]
91     return None
92
93 def read_patch(filename, encoding = None):
94     file_path = os.path.join(_test_patch_dir, filename)
95     if encoding is not None:
96         f = codecs.open(file_path, encoding = encoding)
97     else:
98         f = file(file_path)
99
100     return f.read()
101
102 def read_mail(filename, project = None):
103     file_path = os.path.join(_test_mail_dir, filename)
104     mail = message_from_file(open(file_path))
105     if project is not None:
106         mail['List-Id'] = project.listid
107     return mail
108
109 def create_email(content, subject = None, sender = None, multipart = False,
110         project = None, content_encoding = None):
111     if subject is None:
112         subject = defaults.subject
113     if sender is None:
114         sender = defaults.sender
115     if project is None:
116         project = defaults.project
117     if content_encoding is None:
118         content_encoding = 'us-ascii'
119
120     if multipart:
121         msg = MIMEMultipart()
122         body = MIMEText(content, _subtype = 'plain',
123                         _charset = content_encoding)
124         msg.attach(body)
125     else:
126         msg = MIMEText(content, _charset = content_encoding)
127
128     msg['Subject'] = subject
129     msg['From'] = sender
130     msg['List-Id'] = project.listid
131
132
133     return msg