~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-10-08 02:01:04 UTC
  • mfrom: (3766.3.4 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20081008020104-e68hyxx45qo19nzx
(robertc) Fix two api versioning related bugs - 279447 and 279451.
        (Robert Collins)

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