From: menno Date: Thu, 9 Apr 2009 12:16:39 +0000 (+0100) Subject: Fixed bug where unquoted mailbox names weren't being returned. X-Git-Tag: 0.7~97 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=9fff478f49e8cde555c990112a9e7871eacbb39d;p=imapclient.git Fixed bug where unquoted mailbox names weren't being returned. --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 554ad80..7e181c5 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -63,7 +63,7 @@ class IMAPClient(object): ReadOnlyError = imaplib.IMAP4.readonly re_sep = re.compile('^\(\("[^"]*" "([^"]+)"\)\)') - re_folder = re.compile('\([^)]*\) "[^"]+" "([^"]+)"') + re_folder = re.compile('\([^)]*\) "[^"]+" "?([^"]+)"?') re_status = re.compile(r'^\s*"(?P[^"]+)"\s+' r'\((?P.*)\)$') @@ -156,9 +156,9 @@ class IMAPClient(object): m = self.re_folder.match(line) if m: folders.append(m.group(1)) - return folders + def list_sub_folders(self, directory="", pattern="*"): '''Get a listing of subscribed folders on the server. @@ -179,9 +179,9 @@ class IMAPClient(object): m = self.re_folder.match(line) if m: folders.append(m.group(1)) - return folders + def select_folder(self, folder): '''Select the current folder on the server. Future calls to methods such as search and fetch will act on the selected folder. diff --git a/imapclient/test/mock.py b/imapclient/test/mock.py new file mode 100644 index 0000000..dd6d5f3 --- /dev/null +++ b/imapclient/test/mock.py @@ -0,0 +1,183 @@ +# mock.py +# Test tools for mocking and patching. +# Copyright (C) 2007-2008 Michael Foord +# E-mail: fuzzyman AT voidspace DOT org DOT uk + +# mock 0.4.0 +# http://www.voidspace.org.uk/python/mock.html + +# Released subject to the BSD License +# Please see http://www.voidspace.org.uk/python/license.shtml + +# Scripts maintained at http://www.voidspace.org.uk/python/index.shtml +# Comments, suggestions and bug reports welcome. + +__all__ = ( + 'Mock', + 'patch', + 'patch_object', + 'sentinel', + '__version__' +) + +__version__ = '0.4.0' + +DEFAULT = object() + + +class Mock(object): + def __init__(self, methods=None, spec=None, side_effect=None, + return_value=DEFAULT, name=None, parent=None): + self._parent = parent + self._name = name + if spec is not None and methods is None: + methods = [member for member in dir(spec) if not + (member.startswith('__') and member.endswith('__'))] + self._methods = methods + self._children = {} + self._return_value = return_value + self.side_effect = side_effect + + self.reset() + + + def reset(self): + self.called = False + self.call_args = None + self.call_count = 0 + self.call_args_list = [] + self.method_calls = [] + for child in self._children.itervalues(): + child.reset() + if isinstance(self._return_value, Mock): + self._return_value.reset() + + + def __get_return_value(self): + if self._return_value is DEFAULT: + self._return_value = Mock() + return self._return_value + + def __set_return_value(self, value): + self._return_value = value + + return_value = property(__get_return_value, __set_return_value) + + + def __call__(self, *args, **kwargs): + self.called = True + self.call_count += 1 + self.call_args = (args, kwargs) + self.call_args_list.append((args, kwargs)) + + parent = self._parent + name = self._name + while parent is not None: + parent.method_calls.append((name, args, kwargs)) + if parent._parent is None: + break + name = parent._name + '.' + name + parent = parent._parent + + if self.side_effect is not None: + self.side_effect() + + return self.return_value + + + def __getattr__(self, name): + if self._methods is not None and name not in self._methods: + raise AttributeError("object has no attribute '%s'" % name) + + if name not in self._children: + self._children[name] = Mock(parent=self, name=name) + + return self._children[name] + + + def assert_called_with(self, *args, **kwargs): + assert self.call_args == (args, kwargs), 'Expected: %s\nCalled with: %s' % ((args, kwargs), self.call_args) + + + +def _dot_lookup(thing, comp, import_path): + try: + return getattr(thing, comp) + except AttributeError: + __import__(import_path) + return getattr(thing, comp) + + +def _importer(target): + components = target.split('.') + import_path = components.pop(0) + thing = __import__(import_path) + + for comp in components: + import_path += ".%s" % comp + thing = _dot_lookup(thing, comp, import_path) + return thing + + +def _patch(target, attribute, new): + + def patcher(func): + original = getattr(target, attribute) + if hasattr(func, 'restore_list'): + func.restore_list.append((target, attribute, original)) + func.patch_list.append((target, attribute, new)) + return func + + func.restore_list = [(target, attribute, original)] + func.patch_list = [(target, attribute, new)] + + def patched(*args, **keywargs): + for target, attribute, new in func.patch_list: + if new is DEFAULT: + new = Mock() + args += (new,) + setattr(target, attribute, new) + try: + return func(*args, **keywargs) + finally: + for target, attribute, original in func.restore_list: + setattr(target, attribute, original) + + patched.__name__ = func.__name__ + patched.compat_co_firstlineno = getattr(func, "compat_co_firstlineno", + func.func_code.co_firstlineno) + return patched + + return patcher + + +def patch_object(target, attribute, new=DEFAULT): + return _patch(target, attribute, new) + + +def patch(target, new=DEFAULT): + try: + target, attribute = target.rsplit('.', 1) + except (TypeError, ValueError): + raise TypeError("Need a valid target to patch. You supplied: %s" % (target,)) + target = _importer(target) + return _patch(target, attribute, new) + + +class SentinelObject(object): + def __init__(self, name): + self.name = name + + def __repr__(self): + return '' % self.name + + +class Sentinel(object): + def __init__(self): + self._sentinels = {} + + def __getattr__(self, name): + return self._sentinels.setdefault(name, SentinelObject(name)) + + +sentinel = Sentinel() diff --git a/imapclient/test/test_IMAPClient.py b/imapclient/test/test_IMAPClient.py index efbe295..9f1df7b 100644 --- a/imapclient/test/test_IMAPClient.py +++ b/imapclient/test/test_IMAPClient.py @@ -1,10 +1,45 @@ import unittest from imapclient.test.testable_imapclient import TestableIMAPClient +from imapclient.test.mock import sentinel class IMAPClientTestBase(unittest.TestCase): + def setUp(self): + self.client = TestableIMAPClient() -class TestMailBoxListing(unittest.TestCase): - d +class TestListFolders(IMAPClientTestBase): + def test_simple(self): + self.client._imap.list.return_value = ('OK', ['(\\HasNoChildren) "/" "A"', + '(\\HasNoChildren) "/" "Foo Bar"', + ]) + + folders = self.client.list_folders(sentinel.dir, sentinel.pattern) + + self.assert_(self.client._imap.list.call_args == ((sentinel.dir, sentinel.pattern), {})) + self.assert_(folders == ['A', 'Foo Bar']) + + + def test_without_quotes(self): + self.client._imap.list.return_value = ('OK', ['(\\HasNoChildren) "/" A', + '(\\HasNoChildren) "/" B', + '(\\HasNoChildren) "/" C', + ]) + + folders = self.client.list_folders() + self.assert_(folders == ['A', 'B', 'C'], 'got %r' % folders) + + + def test_mixed(self): + self.client._imap.list.return_value = ('OK', ['(\\HasNoChildren) "/" Alpha', + '(\\HasNoChildren) "/" "Foo Bar"', + '(\\HasNoChildren) "/" C', + ]) + + folders = self.client.list_folders() + self.assert_(folders == ['Alpha', 'Foo Bar', 'C'], 'got %r' % folders) + + +if __name__ == '__main__': + unittest.main() diff --git a/imapclient/test/testable_imapclient.py b/imapclient/test/testable_imapclient.py index 80df59a..f29fb93 100644 --- a/imapclient/test/testable_imapclient.py +++ b/imapclient/test/testable_imapclient.py @@ -1,9 +1,9 @@ from imapclient.imapclient import IMAPClient +from imapclient.test.mock import Mock class TestableIMAPClient(IMAPClient): def __init__(self): - #XXX self._imap = Mock() diff --git a/livetest.py b/livetest.py index 402c01b..eaf29a3 100644 --- a/livetest.py +++ b/livetest.py @@ -138,7 +138,7 @@ def test_append(server): msginfo = resp.values()[0] # Time should match the time we specified - assert msginfo['INTERNALDATE'] == msg_time + assert msginfo['INTERNALDATE'] == msg_time, '%r != %r' % (msginfo['INTERNALDATE'], msg_time) # Flags should be the same assert 'abc' in msginfo['FLAGS']