Skip to content

Commit bc0f823

Browse files
committed
tests: fix Python 2 -> 3 portability issues in test suite
- TestBase.py: fix `raise (exception, value)` tuple syntax -> `raise exception(value)`; drop dead Python 2.1 fallback branch for ndiffAssertEqual - test_handlers.py, test_security_mgr.py: encode plaintext before hashing — Python 3 hashlib requires bytes, not str - test_security_mgr.py: use b'...' literals for md5_new() calls - test_safedict.py: dict.items() returns a view in Python 3, use sorted() - test_bounces.py: fix relative path 'tests/bounces/' -> 'bounces/' since the test suite runs from within the tests/ directory
1 parent 51e0499 commit bc0f823

5 files changed

Lines changed: 17 additions & 23 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def test_bounce(self):
192192
for modname, file, addrs in self.DATA:
193193
module = 'Mailman.Bouncers.' + modname
194194
__import__(module)
195-
fp = open(os.path.join('tests', 'bounces', file))
195+
fp = open(os.path.join('bounces', file))
196196
try:
197197
msg = email.message_from_file(fp)
198198
finally:
@@ -212,7 +212,7 @@ def test_bounce(self):
212212
def test_SMTP32_failure(self):
213213
from Mailman.Bouncers import SMTP32
214214
# This file has no X-Mailer: header
215-
fp = open(os.path.join('tests', 'bounces', 'postfix_01.txt'))
215+
fp = open(os.path.join('bounces', 'postfix_01.txt'))
216216
try:
217217
msg = email.message_from_file(fp)
218218
finally:

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
eq(mlist.password, password('ssSSss'))
@@ -151,10 +151,10 @@ def test_list_admin_upgrade(self):
151151
def test_list_admin_oldstyle_unauth(self):
152152
eq = self.assertEqual
153153
mlist = self._mlist
154-
mlist.password = md5_new('ssSSss').digest()
154+
mlist.password = md5_new(b'ssSSss').digest()
155155
eq(mlist.Authenticate(
156156
[mm_cfg.AuthListAdmin], 'xxxxxx'), mm_cfg.UnAuthorized)
157-
eq(mlist.password, md5_new('ssSSss').digest())
157+
eq(mlist.password, md5_new(b'ssSSss').digest())
158158
# Test crypt upgrades if crypt is supported
159159
if crypt:
160160
mlist.password = crypted = crypt.crypt('rrRRrr', 'zc')

0 commit comments

Comments
 (0)