Skip to content

Commit 379ab50

Browse files
authored
Merge pull request #30 from thegushi/fix/test-suite-python3
tests: fix Python 2 -> 3 portability issues in test suite
2 parents 5306ba3 + 30f913f commit 379ab50

5 files changed

Lines changed: 21 additions & 25 deletions

File tree

tests/TestBase.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,15 @@
3232

3333

3434
class TestBase(unittest.TestCase):
35-
if hasattr(difflib, 'ndiff'):
36-
# Python 2.2 and beyond
37-
def ndiffAssertEqual(self, first, second):
38-
"""Like failUnlessEqual except use ndiff for readable output."""
39-
if first != second:
40-
sfirst = str(first)
41-
ssecond = str(second)
42-
diff = difflib.ndiff(sfirst.splitlines(), ssecond.splitlines())
43-
fp = StringIO()
44-
print(NL, NL.join(diff), file=fp)
45-
raise (self.failureException, fp.getvalue())
46-
else:
47-
# Python 2.1
48-
ndiffAssertEqual = unittest.TestCase.assertEqual
35+
def ndiffAssertEqual(self, first, second):
36+
"""Like failUnlessEqual except use ndiff for readable output."""
37+
if first != second:
38+
sfirst = str(first)
39+
ssecond = str(second)
40+
diff = difflib.ndiff(sfirst.splitlines(), ssecond.splitlines())
41+
fp = StringIO()
42+
print(NL, NL.join(diff), file=fp)
43+
raise self.failureException(fp.getvalue())
4944

5045
def setUp(self):
5146
mlist = MailList.MailList()

tests/test_bounces.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import os
2222
import unittest
2323
import email
24+
25+
_TESTDIR = os.path.dirname(os.path.abspath(__file__))
2426
try:
2527
from Mailman import __init__
2628
except ImportError:
@@ -192,9 +194,9 @@ def test_bounce(self):
192194
for modname, file, addrs in self.DATA:
193195
module = 'Mailman.Bouncers.' + modname
194196
__import__(module)
195-
fp = open(os.path.join('tests', 'bounces', file))
197+
fp = open(os.path.join(_TESTDIR, 'bounces', file), 'rb')
196198
try:
197-
msg = email.message_from_file(fp)
199+
msg = email.message_from_binary_file(fp)
198200
finally:
199201
fp.close()
200202
foundaddrs = sys.modules[module].process(msg)
@@ -212,9 +214,9 @@ def test_bounce(self):
212214
def test_SMTP32_failure(self):
213215
from Mailman.Bouncers import SMTP32
214216
# This file has no X-Mailer: header
215-
fp = open(os.path.join('tests', 'bounces', 'postfix_01.txt'))
217+
fp = open(os.path.join(_TESTDIR, 'bounces', 'postfix_01.txt'), 'rb')
216218
try:
217-
msg = email.message_from_file(fp)
219+
msg = email.message_from_binary_file(fp)
218220
finally:
219221
fp.close()
220222
self.failIf(msg['x-mailer'] is not None)

tests/test_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767

6868
def password(plaintext):
69-
return sha_new(plaintext).hexdigest()
69+
return sha_new(plaintext.encode()).hexdigest()
7070

7171

7272

tests/test_safedict.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ def test_allmsg_no_key(self):
8484
def test_copy(self):
8585
sd = SafeDict.MsgSafeDict(self._msg, {'foo': 'bar'})
8686
copy = sd.copy()
87-
items = copy.items()
88-
items.sort()
87+
items = sorted(copy.items())
8988
self.assertEqual(items, [
9089
('allmsg_cc', 'aperson@dom.ain, bperson@dom.ain'),
9190
('foo', 'bar'),

tests/test_security_mgr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545

4646
def password(plaintext):
47-
return sha_new(plaintext).hexdigest()
47+
return sha_new(plaintext.encode()).hexdigest()
4848

4949

5050

@@ -137,7 +137,7 @@ def test_list_admin_unauth(self):
137137
def test_list_admin_upgrade(self):
138138
eq = self.assertEqual
139139
mlist = self._mlist
140-
mlist.password = md5_new('ssSSss').digest()
140+
mlist.password = md5_new(b'ssSSss').digest()
141141
eq(mlist.Authenticate(
142142
[mm_cfg.AuthListAdmin], 'ssSSss'), mm_cfg.AuthListAdmin)
143143
# Password should be upgraded to PBKDF2 on successful authentication
@@ -155,10 +155,10 @@ def test_list_admin_upgrade(self):
155155
def test_list_admin_oldstyle_unauth(self):
156156
eq = self.assertEqual
157157
mlist = self._mlist
158-
mlist.password = md5_new('ssSSss').digest()
158+
mlist.password = md5_new(b'ssSSss').digest()
159159
eq(mlist.Authenticate(
160160
[mm_cfg.AuthListAdmin], 'xxxxxx'), mm_cfg.UnAuthorized)
161-
eq(mlist.password, md5_new('ssSSss').digest())
161+
eq(mlist.password, md5_new(b'ssSSss').digest())
162162
# Test crypt upgrades if crypt is supported
163163
if crypt:
164164
mlist.password = crypted = crypt.crypt('rrRRrr', 'zc')

0 commit comments

Comments
 (0)