tests: make tests (nearly) work with python3

Tests work now with python 3 with the exception of the deletion of remote
testfolders which fails for some reasons.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2012-02-17 14:57:11 +01:00
parent d0723fb8a2
commit d1e6e1f09e
4 changed files with 22 additions and 15 deletions

View File

@ -22,7 +22,10 @@ import sys
import shutil
import subprocess
import tempfile
from ConfigParser import SafeConfigParser
try:
from configparser import SafeConfigParser
except ImportError: # python 2
from ConfigParser import SafeConfigParser
from . import default_conf
class OLITestLib():
@ -90,7 +93,7 @@ class OLITestLib():
config = cls.get_default_config()
localfolders = os.path.join(cls.testdir, 'mail')
config.set("Repository Maildir", "localfolders", localfolders)
with open(os.path.join(cls.testdir, 'offlineimap.conf'), "wa") as f:
with open(os.path.join(cls.testdir, 'offlineimap.conf'), "wt") as f:
config.write(f)
@classmethod
@ -105,7 +108,7 @@ class OLITestLib():
def run_OLI(cls):
"""Runs OfflineImap
:returns: (rescode, stdout)
:returns: (rescode, stdout (as unicode))
"""
try:
output = subprocess.check_output(
@ -113,8 +116,8 @@ class OLITestLib():
"-c%s" % os.path.join(cls.testdir, 'offlineimap.conf')],
shell=False)
except subprocess.CalledProcessError as e:
return (e.returncode, e.output)
return (0, output)
return (e.returncode, e.output.decode('utf-8'))
return (0, output.decode('utf-8'))
@classmethod
def delete_remote_testfolders(cls, reponame=None):
@ -128,7 +131,7 @@ class OLITestLib():
sections = [r for r in config.sections() \
if r.startswith('Repository')]
sections = filter(lambda s: \
config.get(s, 'Type', None).lower() == 'imap',
config.get(s, 'Type').lower() == 'imap',
sections)
for sec in sections:
# Connect to each IMAP repo and delete all folders
@ -144,21 +147,22 @@ class OLITestLib():
assert res_t == 'OK'
dirs = []
for d in data:
m = re.search(r''' # Find last quote
m = re.search(br''' # Find last quote
"((?: # Non-tripple quoted can contain...
[^"] | # a non-quote
\\" # a backslashded quote
)*)" # closing quote
[^"]*$ # followed by no more quotes
''', d, flags=re.VERBOSE)
folder = m.group(1)
folder = folder.replace(r'\"', '"') # remove quoting
folder = bytearray(m.group(1))
folder = folder.replace(br'\"', b'"') # remove quoting
dirs.append(folder)
# 2) filter out those not starting with INBOX.OLItest and del...
dirs = [d for d in dirs if d.startswith('INBOX.OLItest')]
dirs = [d for d in dirs if d.startswith(b'INBOX.OLItest')]
for folder in dirs:
res_t, data = imapobj.delete(folder)
assert res_t == 'OK'
res_t, data = imapobj.delete(str(folder))
assert res_t == 'OK', "Folder deletion of {} failed with error"\
":\n{} {}".format(folder.decode('utf-8'), res_t, data)
imapobj.logout()
@classmethod

View File

@ -31,4 +31,4 @@ banner = """%(__productname__)s %(__version__)s
import unittest
from unittest import TestLoader, TextTestRunner
from .globals import default_conf
from TestRunner import OLITestLib
from .TestRunner import OLITestLib

View File

@ -14,7 +14,10 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
from cStringIO import StringIO
try:
from cStringIO import StringIO
except ImportError: #python3
from io import StringIO
default_conf=StringIO("""[general]
#will be set automatically

View File

@ -65,7 +65,7 @@ class TestBasicFunctions(unittest.TestCase):
code, res = OLITestLib.run_OLI()
self.assertEqual(res, "")
boxes, mails = OLITestLib.count_maildir_mails('')
self.assertTrue((boxes, mails)==(0,0), msg="Expected 0 folders and 0"
self.assertTrue((boxes, mails)==(0,0), msg="Expected 0 folders and 0 "
"mails, but sync led to {} folders and {} mails".format(
boxes, mails))