Commit 6efd3999 authored by Jason Madden's avatar Jason Madden

Extra tests for _config. Fix a long-standing bug where package paths in import...

Extra tests for _config. Fix a long-standing bug where package paths in import names did not clean up after themselves.
parent f498cffb
...@@ -247,7 +247,7 @@ class ImportableSetting(object): ...@@ -247,7 +247,7 @@ class ImportableSetting(object):
raise ImportError('Cannot import %r from %r' % (item, module)) raise ImportError('Cannot import %r from %r' % (item, module))
return x return x
finally: finally:
if '/' in path: if package_path:
try: try:
sys.path.remove(package_path) sys.path.remove(package_path)
except ValueError: except ValueError:
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
import os import os
import unittest import unittest
import sys
from gevent import _config from gevent import _config
...@@ -77,5 +78,62 @@ class TestResolver(unittest.TestCase): ...@@ -77,5 +78,62 @@ class TestResolver(unittest.TestCase):
conf.resolver = 'block' conf.resolver = 'block'
self.assertEqual(conf.resolver, Resolver) self.assertEqual(conf.resolver, Resolver)
class TestFunctions(unittest.TestCase):
def test_validate_bool(self):
self.assertTrue(_config.validate_bool('on'))
self.assertTrue(_config.validate_bool('1'))
self.assertFalse(_config.validate_bool('off'))
self.assertFalse(_config.validate_bool('0'))
self.assertFalse(_config.validate_bool(''))
with self.assertRaises(ValueError):
_config.validate_bool(' hmm ')
def test_validate_invalid(self):
with self.assertRaises(ValueError):
_config.validate_invalid(self)
class TestConfig(unittest.TestCase):
def test__dir__(self):
self.assertEqual(sorted(_config.config.settings),
sorted(dir(_config.config)))
def test__getattr__invalid(self):
with self.assertRaises(AttributeError):
getattr(_config.config, 'no_such_setting')
def test_set_invalid(self):
with self.assertRaises(AttributeError):
_config.config.set('no such setting', True)
class TestImportableSetting(unittest.TestCase):
assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegex',
unittest.TestCase.assertRaisesRegexp)
def test_empty_list(self):
i = _config.ImportableSetting()
with self.assertRaisesRegex(ImportError,
"Cannot import from empty list"):
i._import([])
def test_path(self):
import warnings
i = _config.ImportableSetting()
path = list(sys.path)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
with self.assertRaisesRegex(ImportError,
"Cannot import 'no_such_module'"):
i._import('foo/bar/gevent.no_such_module')
# We restored the path
self.assertEqual(path, sys.path)
self.assertEqual(len(w), 1)
self.assertEqual(w[0].category, DeprecationWarning)
self.assertIn('Absolute paths', str(w[0].message))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment