116
150
# remove the plugin 'plugin'
117
151
del self.activeattributes[tempattribute]
118
if getattr(bzrlib.plugins, 'plugin', None):
119
del bzrlib.plugins.plugin
120
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
123
class TestAllPlugins(TestCaseInTempDir):
125
def test_plugin_appears_in_all_plugins(self):
126
# This test tests a new plugin appears in bzrlib.plugin.all_plugins().
152
if getattr(bzrlib.plugins, 'pluginone', None):
153
del bzrlib.plugins.pluginone
154
if getattr(bzrlib.plugins, 'plugintwo', None):
155
del bzrlib.plugins.plugintwo
156
self.failIf(getattr(bzrlib.plugins, 'pluginone', None))
157
self.failIf(getattr(bzrlib.plugins, 'plugintwo', None))
159
def test_plugins_can_load_from_directory_with_trailing_slash(self):
160
# This test tests that a plugin can load from a directory when the
161
# directory in the path has a trailing slash.
162
# check the plugin is not loaded already
163
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
164
tempattribute = "trailing-slash"
165
self.failIf(tempattribute in self.activeattributes)
166
# set a place for the plugin to record its loading, and at the same
167
# time validate that the location the plugin should record to is
169
bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
171
self.failUnless(tempattribute in self.activeattributes)
172
# create a directory for the plugin
173
os.mkdir('plugin_test')
174
# write a plugin that will record when its loaded in the
175
# tempattribute list.
176
template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
177
"TestLoadingPlugins.activeattributes[%r].append('%s')\n")
179
outfile = open(os.path.join('plugin_test', 'ts_plugin.py'), 'w')
181
outfile.write(template % (tempattribute, 'plugin'))
187
bzrlib.plugin.load_from_path(['plugin_test'+os.sep])
188
self.assertEqual(['plugin'], self.activeattributes[tempattribute])
190
# remove the plugin 'plugin'
191
del self.activeattributes[tempattribute]
192
if getattr(bzrlib.plugins, 'ts_plugin', None):
193
del bzrlib.plugins.ts_plugin
194
self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
196
def test_plugin_with_bad_name_does_not_load(self):
197
# Create badly-named plugin
198
file('bzr-bad plugin-name..py', 'w').close()
202
handler = logging.StreamHandler(stream)
203
log = logging.getLogger('bzr')
204
log.addHandler(handler)
206
bzrlib.plugin.load_from_dir('.')
208
# Stop capturing output
211
log.removeHandler(handler)
213
self.assertContainsRe(stream.getvalue(),
214
r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
215
"because the file path isn't a valid module name; try renaming "
216
"it to 'bad_plugin_name_'\.")
221
class TestPlugins(TestCaseInTempDir):
223
def setup_plugin(self, source=""):
224
# This test tests a new plugin appears in bzrlib.plugin.plugins().
127
225
# check the plugin is not loaded already
128
226
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
129
227
# write a plugin that _cannot_ fail to load.
130
print >> file('plugin.py', 'w'), ""
132
bzrlib.plugin.load_from_path(['.'])
133
self.failUnless('plugin' in bzrlib.plugin.all_plugins())
134
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
135
self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
136
bzrlib.plugins.plugin)
138
# remove the plugin 'plugin'
139
if 'bzrlib.plugins.plugin' in sys.modules:
140
del sys.modules['bzrlib.plugins.plugin']
141
if getattr(bzrlib.plugins, 'plugin', None):
142
del bzrlib.plugins.plugin
228
file('plugin.py', 'w').write(source + '\n')
229
self.addCleanup(self.teardown_plugin)
230
bzrlib.plugin.load_from_path(['.'])
232
def teardown_plugin(self):
233
# remove the plugin 'plugin'
234
if 'bzrlib.plugins.plugin' in sys.modules:
235
del sys.modules['bzrlib.plugins.plugin']
236
if getattr(bzrlib.plugins, 'plugin', None):
237
del bzrlib.plugins.plugin
143
238
self.failIf(getattr(bzrlib.plugins, 'plugin', None))
240
def test_plugin_appears_in_plugins(self):
242
self.failUnless('plugin' in bzrlib.plugin.plugins())
243
self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
244
plugins = bzrlib.plugin.plugins()
245
plugin = plugins['plugin']
246
self.assertIsInstance(plugin, bzrlib.plugin.PlugIn)
247
self.assertEqual(bzrlib.plugins.plugin, plugin.module)
249
def test_trivial_plugin_get_path(self):
251
plugins = bzrlib.plugin.plugins()
252
plugin = plugins['plugin']
253
plugin_path = self.test_dir + '/plugin.py'
254
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
256
def test_plugin_get_path_py_not_pyc(self):
257
self.setup_plugin() # after first import there will be plugin.pyc
258
self.teardown_plugin()
259
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
260
plugins = bzrlib.plugin.plugins()
261
plugin = plugins['plugin']
262
plugin_path = self.test_dir + '/plugin.py'
263
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
265
def test_plugin_get_path_pyc_only(self):
266
self.setup_plugin() # after first import there will be plugin.pyc
267
self.teardown_plugin()
268
os.unlink(self.test_dir + '/plugin.py')
269
bzrlib.plugin.load_from_path(['.']) # import plugin.pyc
270
plugins = bzrlib.plugin.plugins()
271
plugin = plugins['plugin']
273
plugin_path = self.test_dir + '/plugin.pyc'
275
plugin_path = self.test_dir + '/plugin.pyo'
276
self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
278
def test_no_test_suite_gives_None_for_test_suite(self):
280
plugin = bzrlib.plugin.plugins()['plugin']
281
self.assertEqual(None, plugin.test_suite())
283
def test_test_suite_gives_test_suite_result(self):
284
source = """def test_suite(): return 'foo'"""
285
self.setup_plugin(source)
286
plugin = bzrlib.plugin.plugins()['plugin']
287
self.assertEqual('foo', plugin.test_suite())
289
def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
291
loader = TestUtil.TestLoader()
292
plugin = bzrlib.plugin.plugins()['plugin']
293
self.assertEqual(None, plugin.load_plugin_tests(loader))
295
def test_load_plugin_tests_gives_load_plugin_tests_result(self):
297
def load_tests(standard_tests, module, loader):
299
self.setup_plugin(source)
300
loader = TestUtil.TestLoader()
301
plugin = bzrlib.plugin.plugins()['plugin']
302
self.assertEqual('foo', plugin.load_plugin_tests(loader))
304
def test_no_version_info(self):
306
plugin = bzrlib.plugin.plugins()['plugin']
307
self.assertEqual(None, plugin.version_info())
309
def test_with_version_info(self):
310
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
311
plugin = bzrlib.plugin.plugins()['plugin']
312
self.assertEqual((1, 2, 3, 'dev', 4), plugin.version_info())
314
def test_short_version_info_gets_padded(self):
315
# the gtk plugin has version_info = (1,2,3) rather than the 5-tuple.
317
self.setup_plugin("version_info = (1, 2, 3)")
318
plugin = bzrlib.plugin.plugins()['plugin']
319
self.assertEqual((1, 2, 3, 'final', 0), plugin.version_info())
321
def test_no_version_info___version__(self):
323
plugin = bzrlib.plugin.plugins()['plugin']
324
self.assertEqual("unknown", plugin.__version__)
326
def test___version__with_version_info(self):
327
self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
328
plugin = bzrlib.plugin.plugins()['plugin']
329
self.assertEqual("1.2.3dev4", plugin.__version__)
331
def test_final__version__with_version_info(self):
332
self.setup_plugin("version_info = (1, 2, 3, 'final', 4)")
333
plugin = bzrlib.plugin.plugins()['plugin']
334
self.assertEqual("1.2.3", plugin.__version__)
146
337
class TestPluginHelp(TestCaseInTempDir):