13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for plugins"""
71
70
# create two plugin directories
74
# write a plugin that will record when its loaded in the
73
# write a plugin that will record when its loaded in the
75
74
# tempattribute list.
76
75
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
77
76
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
119
118
# create two plugin directories
120
119
os.mkdir('first')
121
120
os.mkdir('second')
122
# write plugins that will record when they are loaded in the
121
# write plugins that will record when they are loaded in the
123
122
# tempattribute list.
124
123
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
125
124
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
171
170
self.failUnless(tempattribute in self.activeattributes)
172
171
# create a directory for the plugin
173
172
os.mkdir('plugin_test')
174
# write a plugin that will record when its loaded in the
173
# write a plugin that will record when its loaded in the
175
174
# tempattribute list.
176
175
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
177
176
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
196
195
def load_and_capture(self, name):
197
196
"""Load plugins from '.' capturing the output.
199
198
:param name: The name of the plugin.
200
199
:return: A string with the log from the plugin loading call.
258
257
file('plugin.py', 'w').write(source + '\n')
259
258
self.addCleanup(self.teardown_plugin)
260
259
bzrlib.plugin.load_from_path(['.'])
262
261
def teardown_plugin(self):
263
262
# remove the plugin 'plugin'
264
263
if 'bzrlib.plugins.plugin' in sys.modules:
455
454
delattr(bzrlib.plugins, 'myplug')
458
class TestPluginFromZip(TestCaseInTempDir):
460
def make_zipped_plugin(self, zip_name, filename):
461
z = zipfile.ZipFile(zip_name, 'w')
462
z.writestr(filename, PLUGIN_TEXT)
465
def check_plugin_load(self, zip_name, plugin_name):
466
self.assertFalse(plugin_name in dir(bzrlib.plugins),
467
'Plugin already loaded')
468
old_path = bzrlib.plugins.__path__
470
# this is normally done by load_plugins -> set_plugins_path
471
bzrlib.plugins.__path__ = [zip_name]
472
self.applyDeprecated(one_three,
473
bzrlib.plugin.load_from_zip, zip_name)
474
self.assertTrue(plugin_name in dir(bzrlib.plugins),
475
'Plugin is not loaded')
478
if getattr(bzrlib.plugins, plugin_name, None):
479
delattr(bzrlib.plugins, plugin_name)
480
del sys.modules['bzrlib.plugins.' + plugin_name]
481
bzrlib.plugins.__path__ = old_path
483
def test_load_module(self):
484
self.make_zipped_plugin('./test.zip', 'ziplug.py')
485
self.check_plugin_load('./test.zip', 'ziplug')
487
def test_load_package(self):
488
self.make_zipped_plugin('./test.zip', 'ziplug/__init__.py')
489
self.check_plugin_load('./test.zip', 'ziplug')
492
457
class TestSetPluginsPath(TestCase):
494
459
def test_set_plugins_path(self):
495
460
"""set_plugins_path should set the module __path__ correctly."""
496
461
old_path = bzrlib.plugins.__path__
668
633
path = plugin.get_standard_plugins_path()
669
634
self.assertEqual(plugin.get_default_plugin_path(), path[0])
670
635
for directory in path:
671
self.assertNotContainsRe(r'\\/$', directory)
636
self.assertNotContainsRe(directory, r'\\/$')
673
638
from distutils.sysconfig import get_python_lib
674
639
except ImportError: