/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Ian Clatworthy
  • Date: 2008-12-15 06:18:29 UTC
  • mfrom: (3905 +trunk)
  • mto: (3586.1.23 views-ui)
  • mto: This revision was merged to the branch mainline in revision 4030.
  • Revision ID: ian.clatworthy@canonical.com-20081215061829-c8qwa93g71u9fsh5
merge bzr.dev 3905

Show diffs side-by-side

added added

removed removed

Lines of Context:
193
193
                del bzrlib.plugins.ts_plugin
194
194
        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
195
195
 
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()
199
 
 
 
196
    def load_and_capture(self, name):
 
197
        """Load plugins from '.' capturing the output.
 
198
        
 
199
        :param name: The name of the plugin.
 
200
        :return: A string with the log from the plugin loading call.
 
201
        """
200
202
        # Capture output
201
203
        stream = StringIO()
202
 
        handler = logging.StreamHandler(stream)
203
 
        log = logging.getLogger('bzr')
204
 
        log.addHandler(handler)
205
 
 
206
 
        bzrlib.plugin.load_from_dir('.')
207
 
 
208
 
        # Stop capturing output
209
 
        handler.flush()
210
 
        handler.close()
211
 
        log.removeHandler(handler)
212
 
 
213
 
        self.assertContainsRe(stream.getvalue(),
 
204
        try:
 
205
            handler = logging.StreamHandler(stream)
 
206
            log = logging.getLogger('bzr')
 
207
            log.addHandler(handler)
 
208
            try:
 
209
                try:
 
210
                    bzrlib.plugin.load_from_path(['.'])
 
211
                finally:
 
212
                    if 'bzrlib.plugins.%s' % name in sys.modules:
 
213
                        del sys.modules['bzrlib.plugins.%s' % name]
 
214
                    if getattr(bzrlib.plugins, name, None):
 
215
                        delattr(bzrlib.plugins, name)
 
216
            finally:
 
217
                # Stop capturing output
 
218
                handler.flush()
 
219
                handler.close()
 
220
                log.removeHandler(handler)
 
221
            return stream.getvalue()
 
222
        finally:
 
223
            stream.close()
 
224
    
 
225
    def test_plugin_with_bad_api_version_reports(self):
 
226
        # This plugin asks for bzrlib api version 1.0.0, which is not supported
 
227
        # anymore.
 
228
        name = 'wants100.py'
 
229
        f = file(name, 'w')
 
230
        try:
 
231
            f.write("import bzrlib.api\n"
 
232
                "bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
 
233
        finally:
 
234
            f.close()
 
235
 
 
236
        log = self.load_and_capture(name)
 
237
        self.assertContainsRe(log,
 
238
            r"It requested API version")
 
239
 
 
240
    def test_plugin_with_bad_name_does_not_load(self):
 
241
        # The file name here invalid for a python module.
 
242
        name = 'bzr-bad plugin-name..py'
 
243
        file(name, 'w').close()
 
244
        log = self.load_and_capture(name)
 
245
        self.assertContainsRe(log,
214
246
            r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
215
247
            "because the file path isn't a valid module name; try renaming "
216
248
            "it to 'bad_plugin_name_'\.")
217
249
 
218
 
        stream.close()
219
 
 
220
250
 
221
251
class TestPlugins(TestCaseInTempDir):
222
252
 
323
353
        plugin = bzrlib.plugin.plugins()['plugin']
324
354
        self.assertEqual("unknown", plugin.__version__)
325
355
 
326
 
    def test___version__with_version_info(self):
 
356
    def test_str__version__with_version_info(self):
 
357
        self.setup_plugin("version_info = '1.2.3'")
 
358
        plugin = bzrlib.plugin.plugins()['plugin']
 
359
        self.assertEqual("1.2.3", plugin.__version__)
 
360
 
 
361
    def test_noniterable__version__with_version_info(self):
 
362
        self.setup_plugin("version_info = (1)")
 
363
        plugin = bzrlib.plugin.plugins()['plugin']
 
364
        self.assertEqual("1", plugin.__version__)
 
365
 
 
366
    def test_1__version__with_version_info(self):
 
367
        self.setup_plugin("version_info = (1,)")
 
368
        plugin = bzrlib.plugin.plugins()['plugin']
 
369
        self.assertEqual("1", plugin.__version__)
 
370
 
 
371
    def test_1_2__version__with_version_info(self):
 
372
        self.setup_plugin("version_info = (1, 2)")
 
373
        plugin = bzrlib.plugin.plugins()['plugin']
 
374
        self.assertEqual("1.2", plugin.__version__)
 
375
 
 
376
    def test_1_2_3__version__with_version_info(self):
 
377
        self.setup_plugin("version_info = (1, 2, 3)")
 
378
        plugin = bzrlib.plugin.plugins()['plugin']
 
379
        self.assertEqual("1.2.3", plugin.__version__)
 
380
 
 
381
    def test_candidate__version__with_version_info(self):
 
382
        self.setup_plugin("version_info = (1, 2, 3, 'candidate', 1)")
 
383
        plugin = bzrlib.plugin.plugins()['plugin']
 
384
        self.assertEqual("1.2.3rc1", plugin.__version__)
 
385
 
 
386
    def test_dev__version__with_version_info(self):
 
387
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 0)")
 
388
        plugin = bzrlib.plugin.plugins()['plugin']
 
389
        self.assertEqual("1.2.3dev", plugin.__version__)
 
390
 
 
391
    def test_dev_fallback__version__with_version_info(self):
327
392
        self.setup_plugin("version_info = (1, 2, 3, 'dev', 4)")
328
393
        plugin = bzrlib.plugin.plugins()['plugin']
329
 
        self.assertEqual("1.2.3dev4", plugin.__version__)
 
394
        self.assertEqual("1.2.3.dev.4", plugin.__version__)
330
395
 
331
396
    def test_final__version__with_version_info(self):
332
 
        self.setup_plugin("version_info = (1, 2, 3, 'final', 4)")
 
397
        self.setup_plugin("version_info = (1, 2, 3, 'final', 0)")
333
398
        plugin = bzrlib.plugin.plugins()['plugin']
334
399
        self.assertEqual("1.2.3", plugin.__version__)
335
400
 
382
447
            self.assertContainsRe(help, '\[myplug\]')
383
448
        finally:
384
449
            # unregister command
385
 
            if bzrlib.commands.plugin_cmds.get('myplug', None):
386
 
                del bzrlib.commands.plugin_cmds['myplug']
 
450
            if 'myplug' in bzrlib.commands.plugin_cmds:
 
451
                bzrlib.commands.plugin_cmds.remove('myplug')
387
452
            # remove the plugin 'myplug'
388
453
            if getattr(bzrlib.plugins, 'myplug', None):
389
454
                delattr(bzrlib.plugins, 'myplug')
437
502
 
438
503
    def test_set_plugins_path_with_trailing_slashes(self):
439
504
        """set_plugins_path should set the module __path__ based on
440
 
        BZR_PLUGIN_PATH."""
 
505
        BZR_PLUGIN_PATH after removing all trailing slashes."""
441
506
        old_path = bzrlib.plugins.__path__
442
507
        old_env = os.environ.get('BZR_PLUGIN_PATH')
443
508
        try:
445
510
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
446
511
                "second/\\/\\/"
447
512
            bzrlib.plugin.set_plugins_path()
448
 
            expected_path = ['first', 'second',
449
 
                os.path.dirname(bzrlib.plugins.__file__)]
 
513
            # We expect our nominated paths to have all path-seps removed,
 
514
            # and this is testing only that.
 
515
            expected_path = ['first', 'second']
450
516
            self.assertEqual(expected_path,
451
517
                bzrlib.plugins.__path__[:len(expected_path)])
452
518
        finally:
563
629
        mod = FakeModule('two lines of help\nand more', 'bzrlib.plugins.foo_bar')
564
630
        topic = plugin.ModuleHelpTopic(mod)
565
631
        self.assertEqual('foo_bar', topic.get_help_topic())
 
632
 
 
633
 
 
634
def clear_plugins(test_case):
 
635
    old_plugins_path = bzrlib.plugins.__path__
 
636
    bzrlib.plugins.__path__ = []
 
637
    plugin._loaded = False
 
638
    def restore_plugins():
 
639
        bzrlib.plugins.__path__ = old_plugins_path
 
640
        plugin._loaded = False
 
641
    test_case.addCleanup(restore_plugins)
 
642
 
 
643
 
 
644
class TestPluginPaths(tests.TestCase):
 
645
 
 
646
    def test_set_plugins_path_with_args(self):
 
647
        clear_plugins(self)
 
648
        plugin.set_plugins_path(['a', 'b'])
 
649
        self.assertEqual(['a', 'b'], bzrlib.plugins.__path__)
 
650
 
 
651
    def test_set_plugins_path_defaults(self):
 
652
        clear_plugins(self)
 
653
        plugin.set_plugins_path()
 
654
        self.assertEqual(plugin.get_standard_plugins_path(),
 
655
                         bzrlib.plugins.__path__)
 
656
 
 
657
    def test_get_standard_plugins_path(self):
 
658
        path = plugin.get_standard_plugins_path()
 
659
        self.assertEqual(plugin.get_default_plugin_path(), path[0])
 
660
        for directory in path:
 
661
            self.assertNotContainsRe(r'\\/$', directory)
 
662
        try:
 
663
            from distutils.sysconfig import get_python_lib
 
664
        except ImportError:
 
665
            pass
 
666
        else:
 
667
            if sys.platform != 'win32':
 
668
                python_lib = get_python_lib()
 
669
                for directory in path:
 
670
                    if directory.startswith(python_lib):
 
671
                        break
 
672
                else:
 
673
                    self.fail('No path to global plugins')
 
674
 
 
675
    def test_get_standard_plugins_path_env(self):
 
676
        os.environ['BZR_PLUGIN_PATH'] = 'foo/'
 
677
        self.assertEqual('foo', plugin.get_standard_plugins_path()[0])
 
678
 
 
679
 
 
680
class TestLoadPlugins(tests.TestCaseInTempDir):
 
681
 
 
682
    def test_load_plugins(self):
 
683
        clear_plugins(self)
 
684
        plugin.load_plugins(['.'])
 
685
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
 
686
        # subsequent loads are no-ops
 
687
        plugin.load_plugins(['foo'])
 
688
        self.assertEqual(bzrlib.plugins.__path__, ['.'])
 
689
 
 
690
    def test_load_plugins_default(self):
 
691
        clear_plugins(self)
 
692
        plugin.load_plugins()
 
693
        path = plugin.get_standard_plugins_path()
 
694
        self.assertEqual(path, bzrlib.plugins.__path__)