Skip to content

Commit

Permalink
Extra tests for _config. Fix a long-standing bug where package paths …
Browse files Browse the repository at this point in the history
…in import names did not clean up after themselves.
  • Loading branch information
jamadden committed Feb 13, 2018
1 parent f498cff commit 6efd399
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/gevent/_config.py
Expand Up @@ -247,7 +247,7 @@ def _import(self, path, _NONE=object):
raise ImportError('Cannot import %r from %r' % (item, module))
return x
finally:
if '/' in path:
if package_path:
try:
sys.path.remove(package_path)
except ValueError:
Expand Down
58 changes: 58 additions & 0 deletions src/greentest/test___config.py
Expand Up @@ -2,6 +2,7 @@

import os
import unittest
import sys

from gevent import _config

Expand Down Expand Up @@ -77,5 +78,62 @@ def test_set_through_config(self):
conf.resolver = 'block'
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__':
unittest.main()

0 comments on commit 6efd399

Please sign in to comment.