118
115
rel = osutils.relpath(self.test_dir, cache_dir)
119
116
self.log("moving %s in %s to %s", name, rel, magicless_name)
120
117
os.rename(os.path.join(cache_dir, name),
121
os.path.join(directory, magicless_name))
118
os.path.join(directory, magicless_name))
123
120
def _unregister_finder(self):
124
121
"""Removes any test copies of _PluginsAtFinder from sys.meta_path."""
140
137
def assertPluginModules(self, plugin_dict):
141
138
self.assertEqual(
142
139
dict((k[len(self.module_prefix):], sys.modules[k])
143
for k in sys.modules if k.startswith(self.module_prefix)),
140
for k in sys.modules if k.startswith(self.module_prefix)),
146
143
def assertPluginUnknown(self, name):
166
163
# set a place for the plugins to record their loading, and at the same
167
164
# time validate that the location the plugins should record to is
168
165
# valid and correct.
169
self.__class__.activeattributes [tempattribute] = []
166
self.__class__.activeattributes[tempattribute] = []
170
167
self.assertTrue(tempattribute in self.activeattributes)
171
168
# create two plugin directories
172
169
os.mkdir('first')
203
200
# set a place for the plugins to record their loading, and at the same
204
201
# time validate that the location the plugins should record to is
205
202
# valid and correct.
206
breezy.tests.test_plugins.TestLoadingPlugins.activeattributes \
203
breezy.tests.test_plugins.TestLoadingPlugins.activeattributes[tempattribute] = [
208
205
self.assertTrue(tempattribute in self.activeattributes)
209
206
# create two plugin directories
210
207
os.mkdir('first')
230
227
self.assertEqual(['first'], self.activeattributes[tempattribute])
231
228
exec("import %splugintwo" % self.module_prefix)
232
229
self.assertEqual(['first', 'second'],
233
self.activeattributes[tempattribute])
230
self.activeattributes[tempattribute])
235
232
del self.activeattributes[tempattribute]
244
241
# set a place for the plugin to record its loading, and at the same
245
242
# time validate that the location the plugin should record to is
246
243
# valid and correct.
247
breezy.tests.test_plugins.TestLoadingPlugins.activeattributes \
244
breezy.tests.test_plugins.TestLoadingPlugins.activeattributes[tempattribute] = [
249
246
self.assertTrue(tempattribute in self.activeattributes)
250
247
# create a directory for the plugin
251
248
os.mkdir('plugin_test')
259
256
outfile.write('\n')
262
self.load_with_paths(['plugin_test'+os.sep])
259
self.load_with_paths(['plugin_test' + os.sep])
263
260
self.assertEqual(['plugin'], self.activeattributes[tempattribute])
264
261
self.assertPluginKnown('ts_plugin')
298
295
name = 'wants100.py'
299
296
with open(name, 'w') as f:
300
297
f.write("import breezy\n"
301
"from breezy.errors import IncompatibleVersion\n"
302
"raise IncompatibleVersion(breezy, [(1, 0, 0)], (0, 0, 5))\n")
298
"from breezy.errors import IncompatibleVersion\n"
299
"raise IncompatibleVersion(breezy, [(1, 0, 0)], (0, 0, 5))\n")
303
300
log = self.load_and_capture(name)
304
301
self.assertNotContainsRe(log,
305
r"It supports breezy version")
302
r"It supports breezy version")
306
303
self.assertEqual({'wants100'}, viewkeys(self.plugin_warnings))
307
304
self.assertContainsRe(
308
305
self.plugin_warnings['wants100'][0],
314
311
open(name, 'w').close()
315
312
log = self.load_and_capture(name)
316
313
self.assertContainsRe(log,
317
r"Unable to load 'brz-bad plugin-name\.' in '\.' as a plugin "
318
"because the file path isn't a valid module name; try renaming "
319
"it to 'bad_plugin_name_'\\.")
314
r"Unable to load 'brz-bad plugin-name\.' in '\.' as a plugin "
315
"because the file path isn't a valid module name; try renaming "
316
"it to 'bad_plugin_name_'\\.")
322
319
class TestPlugins(BaseTestPlugins):
326
323
# check the plugin is not loaded already
327
324
self.assertPluginUnknown('plugin')
328
325
# write a plugin that _cannot_ fail to load.
329
with open('plugin.py', 'w') as f: f.write(source + '\n')
326
with open('plugin.py', 'w') as f:
327
f.write(source + '\n')
330
328
self.load_with_paths(['.'])
332
330
def test_plugin_loaded(self):
361
359
self.setup_plugin()
362
360
self.promote_cache(self.test_dir)
364
self.load_with_paths(['.']) # import plugin.pyc
362
self.load_with_paths(['.']) # import plugin.pyc
365
363
p = plugin.plugins()['plugin']
366
364
plugin_path = self.test_dir + '/plugin.py'
367
365
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
372
370
os.unlink(self.test_dir + '/plugin.py')
373
371
self.promote_cache(self.test_dir)
375
self.load_with_paths(['.']) # import plugin.pyc (or .pyo)
373
self.load_with_paths(['.']) # import plugin.pyc (or .pyo)
376
374
p = plugin.plugins()['plugin']
377
375
plugin_path = self.test_dir + '/plugin' + plugin.COMPILED_EXT
378
376
self.assertIsSameRealPath(plugin_path, osutils.normpath(p.path()))
556
554
mod = FakeModule(None, 'demo')
557
555
topic = plugin.ModuleHelpTopic(mod)
558
556
self.assertEqual("Plugin 'demo' has no docstring.\n",
559
topic.get_help_text())
557
topic.get_help_text())
561
559
def test_get_help_text_no_carriage_return(self):
562
560
"""ModuleHelpTopic.get_help_text adds a \n if needed."""
563
561
mod = FakeModule('one line of help', 'demo')
564
562
topic = plugin.ModuleHelpTopic(mod)
565
563
self.assertEqual("one line of help\n",
566
topic.get_help_text())
564
topic.get_help_text())
568
566
def test_get_help_text_carriage_return(self):
569
567
"""ModuleHelpTopic.get_help_text adds a \n if needed."""
570
568
mod = FakeModule('two lines of help\nand more\n', 'demo')
571
569
topic = plugin.ModuleHelpTopic(mod)
572
570
self.assertEqual("two lines of help\nand more\n",
573
topic.get_help_text())
571
topic.get_help_text())
575
573
def test_get_help_text_with_additional_see_also(self):
576
574
mod = FakeModule('two lines of help\nand more', 'demo')
577
575
topic = plugin.ModuleHelpTopic(mod)
578
self.assertEqual("two lines of help\nand more\n\n:See also: bar, foo\n",
579
topic.get_help_text(['foo', 'bar']))
577
"two lines of help\nand more\n\n:See also: bar, foo\n",
578
topic.get_help_text(['foo', 'bar']))
581
580
def test_get_help_topic(self):
582
581
"""The help topic for a plugin is its module name."""
602
601
os.environ['BRZ_PLUGIN_PATH'] = os.pathsep.join(setting_dirs)
603
602
actual = [(p if t == 'path' else t.upper())
604
for p, t in plugin._env_plugin_path()]
603
for p, t in plugin._env_plugin_path()]
605
604
self.assertEqual(expected_dirs, actual)
607
606
def test_default(self):
727
726
def test_mixed(self):
728
727
value = os.pathsep.join(['valid', 'in-valid'])
729
728
self.assertEqual(['valid'], self._get_names(value))
730
self.assertContainsRe(self.get_log(),
729
self.assertContainsRe(
731
731
r"Invalid name 'in-valid' in BRZ_DISABLE_PLUGINS=" + repr(value))
825
826
self.create_plugin_package('test_bar', dir='non-standard-dir/test_bar')
826
827
self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@non-standard-dir')
827
828
self.update_module_paths(['standard'])
828
import breezy.testingplugins.test_foo
829
import breezy.testingplugins.test_foo # noqa: F401
829
830
self.assertEqual(self.module_prefix + 'test_foo',
830
831
self.module.test_foo.__package__)
831
import breezy.testingplugins.test_foo.test_bar
832
import breezy.testingplugins.test_foo.test_bar # noqa: F401
832
833
self.assertIsSameRealPath('non-standard-dir/test_bar/__init__.py',
833
834
self.module.test_foo.test_bar.__file__)
840
841
self.create_plugin_package('test_bar', dir='another-dir/test_bar')
841
842
self.overrideEnv('BRZ_PLUGINS_AT', 'test_foo@another-dir')
842
843
self.update_module_paths(['standard'])
843
import breezy.testingplugins.test_foo
844
import breezy.testingplugins.test_foo # noqa: F401
844
845
self.assertEqual(self.module_prefix + 'test_foo',
845
846
self.module.test_foo.__package__)
846
847
self.assertIsSameRealPath('another-dir/test_bar/__init__.py',