69
70
# tempattribute list.
70
71
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
71
72
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
72
print >> file(os.path.join('first', 'plugin.py'), 'w'), template % (tempattribute, 'first')
73
print >> file(os.path.join('second', 'plugin.py'), 'w'), template % (tempattribute, 'second')
74
outfile = open(os.path.join('first', 'plugin.py'), 'w')
76
print >> outfile, template % (tempattribute, 'first')
80
outfile = open(os.path.join('second', 'plugin.py'), 'w')
82
print >> outfile, template % (tempattribute, 'second')
75
87
bzrlib.plugin.load_from_path(['first', 'second'])
76
88
self.assertEqual(['first'], self.activeattributes[tempattribute])
78
90
# remove the plugin 'plugin'
79
91
del self.activeattributes[tempattribute]
92
if 'bzrlib.plugins.plugin' in sys.modules:
93
del sys.modules['bzrlib.plugins.plugin']
80
94
if getattr(bzrlib.plugins, 'plugin', None):
81
95
del bzrlib.plugins.plugin
82
96
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
102
116
# tempattribute list.
103
117
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
104
118
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
105
print >> file(os.path.join('first', 'pluginone.py'), 'w'), template % (tempattribute, 'first')
106
print >> file(os.path.join('second', 'plugintwo.py'), 'w'), template % (tempattribute, 'second')
120
outfile = open(os.path.join('first', 'pluginone.py'), 'w')
122
print >> outfile, template % (tempattribute, 'first')
126
outfile = open(os.path.join('second', 'plugintwo.py'), 'w')
128
print >> outfile, template % (tempattribute, 'second')
107
132
oldpath = bzrlib.plugins.__path__
109
134
bzrlib.plugins.__path__ = ['first', 'second']
116
141
# remove the plugin 'plugin'
117
142
del self.activeattributes[tempattribute]
118
if getattr(bzrlib.plugins, 'plugin', None):
119
del bzrlib.plugins.plugin
120
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
143
if getattr(bzrlib.plugins, 'pluginone', None):
144
del bzrlib.plugins.pluginone
145
if getattr(bzrlib.plugins, 'plugintwo', None):
146
del bzrlib.plugins.plugintwo
147
self.failIf(getattr(bzrlib.plugins, 'pluginone', None))
148
self.failIf(getattr(bzrlib.plugins, 'plugintwo', None))
150
def test_plugins_can_load_from_directory_with_trailing_slash(self):
151
# This test tests that a plugin can load from a directory when the
152
# directory in the path has a trailing slash.
153
# check the plugin is not loaded already
154
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
155
tempattribute = "trailing-slash"
156
self.failIf(tempattribute in self.activeattributes)
157
# set a place for the plugin to record its loading, and at the same
158
# time validate that the location the plugin should record to is
160
bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
162
self.failUnless(tempattribute in self.activeattributes)
163
# create a directory for the plugin
164
os.mkdir('plugin_test')
165
# write a plugin that will record when its loaded in the
166
# tempattribute list.
167
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
168
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
170
outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
172
print >> outfile, template % (tempattribute, 'plugin')
177
bzrlib.plugin.load_from_path(['plugin_test'+os.sep])
178
self.assertEqual(['plugin'], self.activeattributes[tempattribute])
180
# remove the plugin 'plugin'
181
del self.activeattributes[tempattribute]
182
if getattr(bzrlib.plugins, 'ts_plugin', None):
183
del bzrlib.plugins.ts_plugin
184
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
123
187
class TestAllPlugins(TestCaseInTempDir):
130
194
print >> file('plugin.py', 'w'), ""
132
196
bzrlib.plugin.load_from_path(['.'])
133
self.failUnless('plugin' in bzrlib.plugin.all_plugins())
197
all_plugins = self.applyDeprecated(zero_ninetyone,
198
bzrlib.plugin.all_plugins)
199
self.failUnless('plugin' in all_plugins)
134
200
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
135
self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
136
bzrlib.plugins.plugin)
201
self.assertEqual(all_plugins['plugin'], bzrlib.plugins.plugin)
138
203
# remove the plugin 'plugin'
139
204
if 'bzrlib.plugins.plugin' in sys.modules:
143
208
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
211
class TestPlugins(TestCaseInTempDir):
213
def setup_plugin(self, source=""):
214
# This test tests a new plugin appears in bzrlib.plugin.plugins().
215
# check the plugin is not loaded already
216
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
217
# write a plugin that _cannot_ fail to load.
218
print >> file('plugin.py', 'w'), source
219
self.addCleanup(self.teardown_plugin)
220
bzrlib.plugin.load_from_path(['.'])
222
def teardown_plugin(self):
223
# remove the plugin 'plugin'
224
if 'bzrlib.plugins.plugin' in sys.modules:
225
del sys.modules['bzrlib.plugins.plugin']
226
if getattr(bzrlib.plugins, 'plugin', None):
227
del bzrlib.plugins.plugin
228
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
230
def test_plugin_appears_in_plugins(self):
232
self.failUnless('plugin' in bzrlib.plugin.plugins())
233
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
234
plugins = bzrlib.plugin.plugins()
235
plugin = plugins['plugin']
236
self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
237
self.assertEqual(bzrlib.plugins.plugin, plugin.module)
239
def test_trivial_plugin_get_path(self):
241
plugins = bzrlib.plugin.plugins()
242
plugin = plugins['plugin']
243
plugin_path = self.test_dir + '/plugin.py'
244
self.assertEqual(plugin_path, plugin.path())
246
def test_no_test_suite_gives_None_for_test_suite(self):
248
plugin = bzrlib.plugin.plugins()['plugin']
249
self.assertEqual(None, plugin.test_suite())
251
def test_test_suite_gives_test_suite_result(self):
252
source = """def test_suite(): return 'foo'"""
253
self.setup_plugin(source)
254
plugin = bzrlib.plugin.plugins()['plugin']
255
self.assertEqual('foo', plugin.test_suite())
257
def test_no_version_info(self):
259
plugin = bzrlib.plugin.plugins()['plugin']
260
self.assertEqual(None, plugin.version_info())
262
def test_with_version_info(self):
263
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
264
plugin = bzrlib.plugin.plugins()['plugin']
265
self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
267
def test_short_version_info_gets_padded(self):
268
# the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
270
self.setup_plugin("version_info = (1, 2, 3)")
271
plugin = bzrlib.plugin.plugins()['plugin']
272
self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
274
def test_no_version_info___version__(self):
276
plugin = bzrlib.plugin.plugins()['plugin']
277
self.assertEqual("unknown", plugin.__version__)
279
def test___version__with_version_info(self):
280
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
281
plugin = bzrlib.plugin.plugins()['plugin']
282
self.assertEqual("1.2.3dev4", plugin.__version__)
284
def test_final__version__with_version_info(self):
285
self.setup_plugin("version_info = (1, 2, 3, 'final', 4)")
286
plugin = bzrlib.plugin.plugins()['plugin']
287
self.assertEqual("1.2.3", plugin.__version__)
146
290
class TestPluginHelp(TestCaseInTempDir):
148
292
def split_help_commands(self):
244
388
bzrlib.plugins.__path__ = old_path
390
def test_set_plugins_path_with_trailing_slashes(self):
391
"""set_plugins_path should set the module __path__ based on
393
old_path = bzrlib.plugins.__path__
394
old_env = os.environ.get('BZR_PLUGIN_PATH')
396
bzrlib.plugins.__path__ = []
397
os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
399
bzrlib.plugin.set_plugins_path()
400
expected_path = ['first', 'second',
401
os.path.dirname(bzrlib.plugins.__file__)]
402
self.assertEqual(expected_path, bzrlib.plugins.__path__)
404
bzrlib.plugins.__path__ = old_path
406
os.environ['BZR_PLUGIN_PATH'] = old_env
408
del os.environ['BZR_PLUGIN_PATH']
247
410
class TestHelpIndex(tests.TestCase):
248
411
"""Tests for the PluginsHelpIndex class."""