configobj_tests.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Tests for cement.ext.ext_configobj."""
  2. import os
  3. import sys
  4. from tempfile import mkstemp
  5. from cement.core import handler, backend, log
  6. from cement.utils import test
  7. from cement.utils.misc import rando
  8. if sys.version_info[0] < 3:
  9. import configobj
  10. else:
  11. raise test.SkipTest(
  12. 'ConfigObj does not support Python 3') # pragma: no cover
  13. APP = rando()[:12]
  14. CONFIG = """
  15. [my_section]
  16. my_param = my_value
  17. """
  18. class ConfigObjExtTestCase(test.CementTestCase):
  19. def setUp(self):
  20. _, self.tmppath = mkstemp()
  21. f = open(self.tmppath, 'w+')
  22. f.write(CONFIG)
  23. f.close()
  24. self.app = self.make_app(APP,
  25. extensions=['configobj'],
  26. config_handler='configobj',
  27. config_files=[self.tmppath],
  28. argv=[]
  29. )
  30. def tearDown(self):
  31. if os.path.exists(self.tmppath):
  32. os.remove(self.tmppath)
  33. def test_configobj(self):
  34. self.app.setup()
  35. def test_has_section(self):
  36. self.app.setup()
  37. self.ok(self.app.config.has_section('my_section'))
  38. def test_keys(self):
  39. self.app.setup()
  40. res = 'my_param' in self.app.config.keys('my_section')
  41. self.ok(res)
  42. def test_parse_file_bad_path(self):
  43. self.app._meta.config_files = ['./some_bogus_path']
  44. self.app.setup()
  45. def test_parse_file(self):
  46. self.app.setup()
  47. self.eq(self.app.config.get('my_section', 'my_param'), 'my_value')
  48. self.eq(self.app.config.get_section_dict('my_section'),
  49. {'my_param': 'my_value'})