~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-12-20 16:16:34 UTC
  • mfrom: (3123.5.18 hardlinks)
  • Revision ID: pqm@pqm.ubuntu.com-20071220161634-2kcjb650o21ydko4
Accelerate build_tree using similar workingtrees (abentley)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
import bzrlib.plugins
32
32
import bzrlib.commands
33
33
import bzrlib.help
34
 
from bzrlib.symbol_versioning import one_three
35
 
from bzrlib.tests import (
36
 
    TestCase,
37
 
    TestCaseInTempDir,
38
 
    TestUtil,
39
 
    )
 
34
from bzrlib.symbol_versioning import zero_ninetyone
 
35
from bzrlib.tests import TestCase, TestCaseInTempDir
40
36
from bzrlib.osutils import pathjoin, abspath, normpath
41
37
 
42
38
 
195
191
 
196
192
    def test_plugin_with_bad_name_does_not_load(self):
197
193
        # Create badly-named plugin
198
 
        file('bzr-bad plugin-name..py', 'w').close()
 
194
        file('bad plugin-name..py', 'w').close()
199
195
 
200
196
        # Capture output
201
197
        stream = StringIO()
211
207
        log.removeHandler(handler)
212
208
 
213
209
        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_'\.")
 
210
            r"Unable to load 'bad plugin-name\.' in '\.' as a plugin because"
 
211
            " file path isn't a valid module name; try renaming it to"
 
212
            " 'bad_plugin_name_'\.")
217
213
 
218
214
        stream.close()
219
215
 
220
216
 
 
217
class TestAllPlugins(TestCaseInTempDir):
 
218
 
 
219
    def test_plugin_appears_in_all_plugins(self):
 
220
        # This test tests a new plugin appears in bzrlib.plugin.all_plugins().
 
221
        # check the plugin is not loaded already
 
222
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
223
        # write a plugin that _cannot_ fail to load.
 
224
        file('plugin.py', 'w').write("\n")
 
225
        try:
 
226
            bzrlib.plugin.load_from_path(['.'])
 
227
            all_plugins = self.applyDeprecated(zero_ninetyone,
 
228
                bzrlib.plugin.all_plugins)
 
229
            self.failUnless('plugin' in all_plugins)
 
230
            self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
 
231
            self.assertEqual(all_plugins['plugin'], bzrlib.plugins.plugin)
 
232
        finally:
 
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
 
238
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
239
 
 
240
 
221
241
class TestPlugins(TestCaseInTempDir):
222
242
 
223
243
    def setup_plugin(self, source=""):
253
273
        plugin_path = self.test_dir + '/plugin.py'
254
274
        self.assertIsSameRealPath(plugin_path, normpath(plugin.path()))
255
275
 
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
276
    def test_no_test_suite_gives_None_for_test_suite(self):
279
277
        self.setup_plugin()
280
278
        plugin = bzrlib.plugin.plugins()['plugin']
286
284
        plugin = bzrlib.plugin.plugins()['plugin']
287
285
        self.assertEqual('foo', plugin.test_suite())
288
286
 
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
287
    def test_no_version_info(self):
305
288
        self.setup_plugin()
306
289
        plugin = bzrlib.plugin.plugins()['plugin']
403
386
        try:
404
387
            # this is normally done by load_plugins -> set_plugins_path
405
388
            bzrlib.plugins.__path__ = [zip_name]
406
 
            self.applyDeprecated(one_three,
407
 
                bzrlib.plugin.load_from_zip, zip_name)
 
389
            bzrlib.plugin.load_from_zip(zip_name)
408
390
            self.assertTrue(plugin_name in dir(bzrlib.plugins),
409
391
                            'Plugin is not loaded')
410
392
        finally:
437
419
 
438
420
    def test_set_plugins_path_with_trailing_slashes(self):
439
421
        """set_plugins_path should set the module __path__ based on
440
 
        BZR_PLUGIN_PATH after removing all trailing slashes."""
 
422
        BZR_PLUGIN_PATH."""
441
423
        old_path = bzrlib.plugins.__path__
442
424
        old_env = os.environ.get('BZR_PLUGIN_PATH')
443
425
        try:
445
427
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
446
428
                "second/\\/\\/"
447
429
            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)])
 
430
            expected_path = ['first', 'second',
 
431
                os.path.dirname(bzrlib.plugins.__file__)]
 
432
            self.assertEqual(expected_path, bzrlib.plugins.__path__)
453
433
        finally:
454
434
            bzrlib.plugins.__path__ = old_path
455
 
            if old_env is not None:
 
435
            if old_env != None:
456
436
                os.environ['BZR_PLUGIN_PATH'] = old_env
457
437
            else:
458
438
                del os.environ['BZR_PLUGIN_PATH']
459
439
 
460
 
 
461
440
class TestHelpIndex(tests.TestCase):
462
441
    """Tests for the PluginsHelpIndex class."""
463
442