plugin_tests.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. """Tests for cement.core.plugin."""
  2. import os
  3. import sys
  4. import shutil
  5. from tempfile import mkdtemp
  6. from cement.core import exc, backend, plugin, handler
  7. from cement.utils import test
  8. CONF = """
  9. [myplugin]
  10. enable_plugin = true
  11. foo = bar
  12. """
  13. CONF2 = """
  14. [myplugin]
  15. enable_plugin = false
  16. foo = bar
  17. """
  18. CONF3 = """
  19. [bogus_plugin]
  20. foo = bar
  21. """
  22. CONF4 = """
  23. [ext_json]
  24. enable_plugin = true
  25. foo = bar
  26. """
  27. CONF5 = ''
  28. PLUGIN = """
  29. from cement.core import handler, output
  30. class TestOutputHandler(output.CementOutputHandler):
  31. class Meta:
  32. interface = output.IOutput
  33. label = 'test_output_handler'
  34. def _setup(self, app_obj):
  35. self.app = app_obj
  36. def render(self, data_dict, template=None):
  37. pass
  38. def load():
  39. handler.register(TestOutputHandler)
  40. """
  41. class PluginTestCase(test.CementTestCase):
  42. def setUp(self):
  43. self.app = self.make_app()
  44. def test_load_plugins_from_files(self):
  45. tmpdir = mkdtemp()
  46. f = open(os.path.join(tmpdir, 'myplugin.conf'), 'w')
  47. f.write(CONF)
  48. f.close()
  49. f = open(os.path.join(tmpdir, 'myplugin.py'), 'w')
  50. f.write(PLUGIN)
  51. f.close()
  52. app = self.make_app('cement-testapp',
  53. config_files=[],
  54. plugin_config_dir=tmpdir,
  55. plugin_dir=tmpdir,
  56. plugin_bootstrap=None,
  57. )
  58. app.setup()
  59. try:
  60. han = handler.get('output', 'test_output_handler')()
  61. self.eq(han._meta.label, 'test_output_handler')
  62. finally:
  63. shutil.rmtree(tmpdir)
  64. def test_load_plugins_from_config(self):
  65. tmpdir = mkdtemp()
  66. f = open(os.path.join(tmpdir, 'myplugin.py'), 'w')
  67. f.write(PLUGIN)
  68. f.close()
  69. defaults = backend.defaults()
  70. defaults['myplugin'] = dict()
  71. defaults['myplugin']['enable_plugin'] = True
  72. defaults['myplugin2'] = dict()
  73. defaults['myplugin2']['enable_plugin'] = False
  74. app = self.make_app('cement-testapp', config_defaults=defaults,
  75. config_files=[],
  76. plugin_config_dir=tmpdir,
  77. plugin_dir=tmpdir,
  78. plugin_bootstrap=None,
  79. )
  80. app.setup()
  81. try:
  82. han = handler.get('output', 'test_output_handler')()
  83. self.eq(han._meta.label, 'test_output_handler')
  84. finally:
  85. shutil.rmtree(tmpdir)
  86. # some more checks
  87. res = 'myplugin' in app.plugin.get_enabled_plugins()
  88. self.ok(res)
  89. res = 'myplugin' in app.plugin.get_loaded_plugins()
  90. self.ok(res)
  91. res = 'myplugin2' in app.plugin.get_disabled_plugins()
  92. self.ok(res)
  93. res = 'myplugin2' not in app.plugin.get_enabled_plugins()
  94. self.ok(res)
  95. res = 'myplugin2' not in app.plugin.get_loaded_plugins()
  96. self.ok(res)
  97. def test_disabled_plugins_from_files(self):
  98. tmpdir = mkdtemp()
  99. f = open(os.path.join(tmpdir, 'myplugin.conf'), 'w')
  100. f.write(CONF2)
  101. f.close()
  102. f = open(os.path.join(tmpdir, 'myplugin.py'), 'w')
  103. f.write(PLUGIN)
  104. f.close()
  105. app = self.make_app('cement-testapp',
  106. config_files=[],
  107. plugin_config_dir=tmpdir,
  108. plugin_dir=tmpdir,
  109. plugin_bootstrap=None,
  110. )
  111. app.setup()
  112. shutil.rmtree(tmpdir)
  113. res = 'test_output_handler' not in backend.handlers['output']
  114. self.ok(res)
  115. res = 'myplugin2' not in app.plugin.get_enabled_plugins()
  116. self.ok(res)
  117. def test_bogus_plugin_from_files(self):
  118. tmpdir = mkdtemp()
  119. f = open(os.path.join(tmpdir, 'myplugin.conf'), 'w')
  120. f.write(CONF3)
  121. f.close()
  122. # do this for coverage... empty config file
  123. f = open(os.path.join(tmpdir, 'bogus.conf'), 'w')
  124. f.write(CONF5)
  125. f.close()
  126. app = self.make_app('cement-testapp',
  127. config_files=[],
  128. plugin_config_dir=tmpdir,
  129. plugin_dir=tmpdir,
  130. plugin_bootstrap=None,
  131. )
  132. app.setup()
  133. shutil.rmtree(tmpdir)
  134. res = 'bogus_plugin' not in app.plugin.get_enabled_plugins()
  135. self.ok(res)
  136. @test.raises(exc.FrameworkError)
  137. def test_bad_plugin_dir(self):
  138. tmpdir = mkdtemp()
  139. f = open(os.path.join(tmpdir, 'myplugin.conf'), 'w')
  140. f.write(CONF)
  141. f.close()
  142. app = self.make_app('cement-testapp',
  143. config_files=[],
  144. plugin_config_dir=tmpdir,
  145. plugin_dir='./some/bogus/path',
  146. plugin_bootstrap=None,
  147. )
  148. try:
  149. app.setup()
  150. except ImportError as e:
  151. raise
  152. except exc.FrameworkError as e:
  153. raise
  154. finally:
  155. shutil.rmtree(tmpdir)
  156. def test_load_plugin_from_module(self):
  157. # We mock this out by loading a cement ext, but it is essentially the
  158. # same type of code.
  159. tmpdir = mkdtemp()
  160. del sys.modules['cement.ext.ext_json']
  161. f = open(os.path.join(tmpdir, 'ext_json.conf'), 'w')
  162. f.write(CONF4)
  163. f.close()
  164. app = self.make_app('cement-testapp',
  165. config_files=[],
  166. plugin_config_dir=tmpdir,
  167. plugin_dir=tmpdir,
  168. plugin_bootstrap='cement.ext',
  169. )
  170. app.setup()
  171. res = 'ext_json' in app.plugin.get_enabled_plugins()
  172. self.ok(res)
  173. shutil.rmtree(tmpdir)