~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: 2008-06-05 04:05:05 UTC
  • mfrom: (3473.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080605040505-i9kqxg2fps2qjdi0
Add the 'alias' command (Tim Penhey)

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 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
 
        """
 
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
 
202
200
        # Capture output
203
201
        stream = StringIO()
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,
 
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(),
246
214
            r"Unable to load 'bzr-bad plugin-name\.' in '\.' as a plugin "
247
215
            "because the file path isn't a valid module name; try renaming "
248
216
            "it to 'bad_plugin_name_'\.")
249
217
 
 
218
        stream.close()
 
219
 
250
220
 
251
221
class TestPlugins(TestCaseInTempDir):
252
222
 
412
382
            self.assertContainsRe(help, '\[myplug\]')
413
383
        finally:
414
384
            # unregister command
415
 
            if 'myplug' in bzrlib.commands.plugin_cmds:
416
 
                bzrlib.commands.plugin_cmds.remove('myplug')
 
385
            if bzrlib.commands.plugin_cmds.get('myplug', None):
 
386
                del bzrlib.commands.plugin_cmds['myplug']
417
387
            # remove the plugin 'myplug'
418
388
            if getattr(bzrlib.plugins, 'myplug', None):
419
389
                delattr(bzrlib.plugins, 'myplug')
467
437
 
468
438
    def test_set_plugins_path_with_trailing_slashes(self):
469
439
        """set_plugins_path should set the module __path__ based on
470
 
        BZR_PLUGIN_PATH after removing all trailing slashes."""
 
440
        BZR_PLUGIN_PATH."""
471
441
        old_path = bzrlib.plugins.__path__
472
442
        old_env = os.environ.get('BZR_PLUGIN_PATH')
473
443
        try:
475
445
            os.environ['BZR_PLUGIN_PATH'] = "first\\//\\" + os.pathsep + \
476
446
                "second/\\/\\/"
477
447
            bzrlib.plugin.set_plugins_path()
478
 
            # We expect our nominated paths to have all path-seps removed,
479
 
            # and this is testing only that.
480
 
            expected_path = ['first', 'second']
 
448
            expected_path = ['first', 'second',
 
449
                os.path.dirname(bzrlib.plugins.__file__)]
481
450
            self.assertEqual(expected_path,
482
451
                bzrlib.plugins.__path__[:len(expected_path)])
483
452
        finally: