~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-11-03 01:53:30 UTC
  • mfrom: (2955.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20071103015330-pt1tec7wyxwwcey8
Fix #158972 don't use timeout for HttpServer

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
# affects the global state of the process.  See bzrlib/plugins.py for more
21
21
# comments.
22
22
 
23
 
import logging
24
23
import os
25
24
from StringIO import StringIO
26
25
import sys
31
30
import bzrlib.plugins
32
31
import bzrlib.commands
33
32
import bzrlib.help
34
 
from bzrlib.symbol_versioning import one_three
35
 
from bzrlib.tests import (
36
 
    TestCase,
37
 
    TestCaseInTempDir,
38
 
    TestUtil,
39
 
    )
 
33
from bzrlib.symbol_versioning import zero_ninetyone
 
34
from bzrlib.tests import TestCase, TestCaseInTempDir
40
35
from bzrlib.osutils import pathjoin, abspath, normpath
41
36
 
42
37
 
193
188
                del bzrlib.plugins.ts_plugin
194
189
        self.failIf(getattr(bzrlib.plugins, 'ts_plugin', None))
195
190
 
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
 
 
200
 
        # Capture output
201
 
        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(),
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_'\.")
217
 
 
218
 
        stream.close()
 
191
 
 
192
class TestAllPlugins(TestCaseInTempDir):
 
193
 
 
194
    def test_plugin_appears_in_all_plugins(self):
 
195
        # This test tests a new plugin appears in bzrlib.plugin.all_plugins().
 
196
        # check the plugin is not loaded already
 
197
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
198
        # write a plugin that _cannot_ fail to load.
 
199
        file('plugin.py', 'w').write("\n")
 
200
        try:
 
201
            bzrlib.plugin.load_from_path(['.'])
 
202
            all_plugins = self.applyDeprecated(zero_ninetyone,
 
203
                bzrlib.plugin.all_plugins)
 
204
            self.failUnless('plugin' in all_plugins)
 
205
            self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
 
206
            self.assertEqual(all_plugins['plugin'], bzrlib.plugins.plugin)
 
207
        finally:
 
208
            # remove the plugin 'plugin'
 
209
            if 'bzrlib.plugins.plugin' in sys.modules:
 
210
                del sys.modules['bzrlib.plugins.plugin']
 
211
            if getattr(bzrlib.plugins, 'plugin', None):
 
212
                del bzrlib.plugins.plugin
 
213
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
219
214
 
220
215
 
221
216
class TestPlugins(TestCaseInTempDir):
253
248
        plugin_path = self.test_dir + '/plugin.py'
254
249
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
255
250
 
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()))
264
 
 
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']
272
 
        if __debug__:
273
 
            plugin_path = self.test_dir + '/plugin.pyc'
274
 
        else:
275
 
            plugin_path = self.test_dir + '/plugin.pyo'
276
 
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
277
 
 
278
251
    def test_no_test_suite_gives_None_for_test_suite(self):
279
252
        self.setup_plugin()
280
253
        plugin = bzrlib.plugin.plugins()['plugin']
286
259
        plugin = bzrlib.plugin.plugins()['plugin']
287
260
        self.assertEqual('foo', plugin.test_suite())
288
261
 
289
 
    def test_no_load_plugin_tests_gives_None_for_load_plugin_tests(self):
290
 
        self.setup_plugin()
291
 
        loader = TestUtil.TestLoader()
292
 
        plugin = bzrlib.plugin.plugins()['plugin']
293
 
        self.assertEqual(None, plugin.load_plugin_tests(loader))
294
 
 
295
 
    def test_load_plugin_tests_gives_load_plugin_tests_result(self):
296
 
        source = """
297
 
def load_tests(standard_tests, module, loader):
298
 
    return 'foo'"""
299
 
        self.setup_plugin(source)
300
 
        loader = TestUtil.TestLoader()
301
 
        plugin = bzrlib.plugin.plugins()['plugin']
302
 
        self.assertEqual('foo', plugin.load_plugin_tests(loader))
303
 
 
304
262
    def test_no_version_info(self):
305
263
        self.setup_plugin()
306
264
        plugin = bzrlib.plugin.plugins()['plugin']
403
361
        try:
404
362
            # this is normally done by load_plugins -> set_plugins_path
405
363
            bzrlib.plugins.__path__ = [zip_name]
406
 
            self.applyDeprecated(one_three,
407
 
                bzrlib.plugin.load_from_zip, zip_name)
 
364
            bzrlib.plugin.load_from_zip(zip_name)
408
365
            self.assertTrue(plugin_name in dir(bzrlib.plugins),
409
366
                            'Plugin is not loaded')
410
367
        finally:
437
394
 
438
395
    def test_set_plugins_path_with_trailing_slashes(self):
439
396
        """set_plugins_path should set the module __path__ based on
440
 
        BZR_PLUGIN_PATH after removing all trailing slashes."""
 
397
        BZR_PLUGIN_PATH."""
441
398
        old_path = bzrlib.plugins.__path__
442
399
        old_env = os.environ.get('BZR_PLUGIN_PATH')
443
400
        try:
445
402
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
446
403
                "second/\\/\\/"
447
404
            bzrlib.plugin.set_plugins_path()
448
 
            # We expect our nominated paths to have all path-seps removed,
449
 
            # and this is testing only that.
450
 
            expected_path = ['first', 'second']
451
 
            self.assertEqual(expected_path,
452
 
                bzrlib.plugins.__path__[:len(expected_path)])
 
405
            expected_path = ['first', 'second',
 
406
                os.path.dirname(bzrlib.plugins.__file__)]
 
407
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
453
408
        finally:
454
409
            bzrlib.plugins.__path__ = old_path
455
 
            if old_env is not None:
 
410
            if old_env != None:
456
411
                os.environ['BZR_PLUGIN_PATH'] = old_env
457
412
            else:
458
413
                del os.environ['BZR_PLUGIN_PATH']
459
414
 
460
 
 
461
415
class TestHelpIndex(tests.TestCase):
462
416
    """Tests for the PluginsHelpIndex class."""
463
417