18
18
"""Tests for plugins"""
20
# XXX: There are no plugin tests at the moment because the plugin module
21
# affects the global state of the process. See bzrlib/plugins.py for more
28
from bzrlib.tests import TestCaseInTempDir
22
# **************************************************
24
# **************************************************
33
from bzrlib.selftest import TestCaseInTempDir
30
36
class PluginTest(TestCaseInTempDir):
31
37
"""Create an external plugin and test loading."""
32
# def test_plugin_loading(self):
33
# orig_help = self.run_bzr_captured('bzr help commands')[0]
34
# os.mkdir('plugin_test')
35
# f = open(os.path.join('plugin_test', 'myplug.py'), 'wt')
36
# f.write(PLUGIN_TEXT)
38
# newhelp = self.run_bzr_captured('bzr help commands')[0]
39
# assert newhelp.startswith('You have been overridden\n')
40
# # We added a line, but the rest should work
41
# assert newhelp[25:] == help
43
# assert backtick('bzr commit -m test') == "I'm sorry dave, you can't do that\n"
45
# shutil.rmtree('plugin_test')
38
def test_plugin_loading(self):
41
orig_help = self.backtick('bzr help commands') # No plugins yet
42
os.mkdir('plugin_test')
43
f = open(os.path.join('plugin_test', 'myplug.py'), 'wt')
47
newhelp = backtick('bzr help commands')
48
assert newhelp.startswith('You have been overridden\n')
49
# We added a line, but the rest should work
50
assert newhelp[25:] == help
52
assert backtick('bzr commit -m test') == "I'm sorry dave, you can't do that\n"
54
shutil.rmtree('plugin_test')
60
# """import bzrlib, bzrlib.commands
61
# class cmd_myplug(bzrlib.commands.Command):
62
# '''Just a simple test plugin.'''
65
# print 'Hello from my plugin'
48
69
# os.environ['BZRPLUGINPATH'] = os.path.abspath('plugin_test')
49
70
# help = backtick('bzr help commands')
68
89
# bzrlib.commands.cmd_help.run(self, *args, **kwargs)
73
import bzrlib.commands
74
class cmd_myplug(bzrlib.commands.Command):
75
'''Just a simple test plugin.'''
78
print 'Hello from my plugin'
81
# TODO: Write a test for plugin decoration of commands.
83
class TestOneNamedPluginOnly(TestCaseInTempDir):
87
def test_plugins_with_the_same_name_are_not_loaded(self):
88
# This test tests that having two plugins in different
89
# directories does not result in both being loaded.
90
# get a file name we can use which is also a valid attribute
91
# for accessing in activeattributes. - we cannot give import parameters.
93
self.failIf(tempattribute in self.activeattributes)
94
# set a place for the plugins to record their loading, and at the same
95
# time validate that the location the plugins should record to is
97
bzrlib.tests.test_plugins.TestOneNamedPluginOnly.activeattributes \
99
self.failUnless(tempattribute in self.activeattributes)
100
# create two plugin directories
103
# write a plugin that will record when its loaded in the
104
# tempattribute list.
105
template = ("from bzrlib.tests.test_plugins import TestOneNamedPluginOnly\n"
106
"TestOneNamedPluginOnly.activeattributes[%r].append('%s')\n")
107
print >> file(os.path.join('first', 'plugin.py'), 'w'), template % (tempattribute, 'first')
108
print >> file(os.path.join('second', 'plugin.py'), 'w'), template % (tempattribute, 'second')
110
bzrlib.plugin.load_from_dirs(['first', 'second'])
111
self.assertEqual(['first'], self.activeattributes[tempattribute])
113
# remove the plugin 'plugin'
114
del self.activeattributes[tempattribute]
115
if getattr(bzrlib.plugins, 'plugin', None):
116
del bzrlib.plugins.plugin
117
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
120
class TestAllPlugins(TestCaseInTempDir):
122
def test_plugin_appears_in_all_plugins(self):
123
# This test tests a new plugin appears in bzrlib.plugin.all_plugins().
124
# check the plugin is not loaded already
125
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
126
# write a plugin that _cannot_ fail to load.
127
print >> file('plugin.py', 'w'), ""
129
bzrlib.plugin.load_from_dirs(['.'])
130
self.failUnless('plugin' in bzrlib.plugin.all_plugins())
131
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
132
self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
133
bzrlib.plugins.plugin)
135
# remove the plugin 'plugin'
136
if getattr(bzrlib.plugins, 'plugin', None):
137
del bzrlib.plugins.plugin
138
self.failIf(getattr(bzrlib.plugins, 'plugin', None))