~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Andrew Bennetts
  • Date: 2011-03-18 01:12:17 UTC
  • mto: (5609.24.3 2.3)
  • mto: This revision was merged to the branch mainline in revision 5732.
  • Revision ID: andrew.bennetts@canonical.com-20110318011217-h1206xizh4kj1ldw
Alternative fix: cache the result of get_master_branch for the lifetime of the branch lock.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
# TODO: Write a test for plugin decoration of commands.
40
40
 
41
 
class BaseTestPlugins(tests.TestCaseInTempDir):
 
41
class TestPluginMixin(object):
42
42
 
43
43
    def create_plugin(self, name, source=None, dir='.', file_name=None):
44
44
        if source is None:
91
91
                delattr(plugin, submodule_name)
92
92
 
93
93
    def assertPluginUnknown(self, name):
94
 
        self.assertFalse(getattr(bzrlib.plugins, name, None) is not None)
95
 
        self.assertFalse('bzrlib.plugins.%s' % name in sys.modules)
 
94
        self.failIf(getattr(bzrlib.plugins, name, None) is not None)
 
95
        self.failIf('bzrlib.plugins.%s' % name in sys.modules)
96
96
 
97
97
    def assertPluginKnown(self, name):
98
 
        self.assertTrue(getattr(bzrlib.plugins, name, None) is not None)
99
 
        self.assertTrue('bzrlib.plugins.%s' % name in sys.modules)
100
 
 
101
 
 
102
 
class TestLoadingPlugins(BaseTestPlugins):
 
98
        self.failUnless(getattr(bzrlib.plugins, name, None) is not None)
 
99
        self.failUnless('bzrlib.plugins.%s' % name in sys.modules)
 
100
 
 
101
 
 
102
class TestLoadingPlugins(tests.TestCaseInTempDir, TestPluginMixin):
103
103
 
104
104
    activeattributes = {}
105
105
 
109
109
        # file name we can use which is also a valid attribute for accessing in
110
110
        # activeattributes. - we cannot give import parameters.
111
111
        tempattribute = "0"
112
 
        self.assertFalse(tempattribute in self.activeattributes)
 
112
        self.failIf(tempattribute in self.activeattributes)
113
113
        # set a place for the plugins to record their loading, and at the same
114
114
        # time validate that the location the plugins should record to is
115
115
        # valid and correct.
116
116
        self.__class__.activeattributes [tempattribute] = []
117
 
        self.assertTrue(tempattribute in self.activeattributes)
 
117
        self.failUnless(tempattribute in self.activeattributes)
118
118
        # create two plugin directories
119
119
        os.mkdir('first')
120
120
        os.mkdir('second')
147
147
        self.assertPluginUnknown('plugin')
148
148
 
149
149
    def test_plugins_from_different_dirs_can_demand_load(self):
150
 
        self.assertFalse('bzrlib.plugins.pluginone' in sys.modules)
151
 
        self.assertFalse('bzrlib.plugins.plugintwo' in sys.modules)
 
150
        self.failIf('bzrlib.plugins.pluginone' in sys.modules)
 
151
        self.failIf('bzrlib.plugins.plugintwo' in sys.modules)
152
152
        # This test tests that having two plugins in different
153
153
        # directories with different names allows them both to be loaded, when
154
154
        # we do a direct import statement.
155
155
        # Determine a file name we can use which is also a valid attribute
156
156
        # for accessing in activeattributes. - we cannot give import parameters.
157
157
        tempattribute = "different-dirs"
158
 
        self.assertFalse(tempattribute in self.activeattributes)
 
158
        self.failIf(tempattribute in self.activeattributes)
159
159
        # set a place for the plugins to record their loading, and at the same
160
160
        # time validate that the location the plugins should record to is
161
161
        # valid and correct.
162
162
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
163
163
            [tempattribute] = []
164
 
        self.assertTrue(tempattribute in self.activeattributes)
 
164
        self.failUnless(tempattribute in self.activeattributes)
165
165
        # create two plugin directories
166
166
        os.mkdir('first')
167
167
        os.mkdir('second')
186
186
 
187
187
        oldpath = bzrlib.plugins.__path__
188
188
        try:
189
 
            self.assertFalse('bzrlib.plugins.pluginone' in sys.modules)
190
 
            self.assertFalse('bzrlib.plugins.plugintwo' in sys.modules)
 
189
            self.failIf('bzrlib.plugins.pluginone' in sys.modules)
 
190
            self.failIf('bzrlib.plugins.plugintwo' in sys.modules)
191
191
            bzrlib.plugins.__path__ = ['first', 'second']
192
192
            exec "import bzrlib.plugins.pluginone"
193
193
            self.assertEqual(['first'], self.activeattributes[tempattribute])
208
208
        # check the plugin is not loaded already
209
209
        self.assertPluginUnknown('ts_plugin')
210
210
        tempattribute = "trailing-slash"
211
 
        self.assertFalse(tempattribute in self.activeattributes)
 
211
        self.failIf(tempattribute in self.activeattributes)
212
212
        # set a place for the plugin to record its loading, and at the same
213
213
        # time validate that the location the plugin should record to is
214
214
        # valid and correct.
215
215
        bzrlib.tests.test_plugins.TestLoadingPlugins.activeattributes \
216
216
            [tempattribute] = []
217
 
        self.assertTrue(tempattribute in self.activeattributes)
 
217
        self.failUnless(tempattribute in self.activeattributes)
218
218
        # create a directory for the plugin
219
219
        os.mkdir('plugin_test')
220
220
        # write a plugin that will record when its loaded in the
267
267
            stream.close()
268
268
 
269
269
    def test_plugin_with_bad_api_version_reports(self):
270
 
        """Try loading a plugin that requests an unsupported api.
271
 
        
272
 
        Observe that it records the problem but doesn't complain on stderr.
273
 
 
274
 
        See https://bugs.launchpad.net/bzr/+bug/704195
275
 
        """
276
 
        self.overrideAttr(plugin, 'plugin_warnings', {})
 
270
        # This plugin asks for bzrlib api version 1.0.0, which is not supported
 
271
        # anymore.
277
272
        name = 'wants100.py'
278
273
        f = file(name, 'w')
279
274
        try:
281
276
                "bzrlib.api.require_any_api(bzrlib, [(1, 0, 0)])\n")
282
277
        finally:
283
278
            f.close()
 
279
 
284
280
        log = self.load_and_capture(name)
285
 
        self.assertNotContainsRe(log,
286
 
            r"It requested API version")
287
 
        self.assertEquals(
288
 
            ['wants100'],
289
 
            plugin.plugin_warnings.keys())
290
 
        self.assertContainsRe(
291
 
            plugin.plugin_warnings['wants100'][0],
 
281
        self.assertContainsRe(log,
292
282
            r"It requested API version")
293
283
 
294
284
    def test_plugin_with_bad_name_does_not_load(self):
302
292
            "it to 'bad_plugin_name_'\.")
303
293
 
304
294
 
305
 
class TestPlugins(BaseTestPlugins):
 
295
class TestPlugins(tests.TestCaseInTempDir, TestPluginMixin):
306
296
 
307
297
    def setup_plugin(self, source=""):
308
298
        # This test tests a new plugin appears in bzrlib.plugin.plugins().
778
768
                        ['+foo', '-bar'])
779
769
 
780
770
 
781
 
class TestDisablePlugin(BaseTestPlugins):
 
771
class TestDisablePlugin(tests.TestCaseInTempDir, TestPluginMixin):
782
772
 
783
773
    def setUp(self):
784
774
        super(TestDisablePlugin, self).setUp()
819
809
        self.assertLength(0, self.warnings)
820
810
 
821
811
 
822
 
 
823
812
class TestLoadPluginAtSyntax(tests.TestCase):
824
813
 
825
814
    def _get_paths(self, paths):
843
832
                          os.pathsep.join(['batman@cave', '', 'robin@mobile']))
844
833
 
845
834
 
846
 
class TestLoadPluginAt(BaseTestPlugins):
 
835
class TestLoadPluginAt(tests.TestCaseInTempDir, TestPluginMixin):
847
836
 
848
837
    def setUp(self):
849
838
        super(TestLoadPluginAt, self).setUp()
858
847
        self.create_plugin_package('test_foo', dir='standard/test_foo')
859
848
        # All the tests will load the 'test_foo' plugin from various locations
860
849
        self.addCleanup(self._unregister_plugin, 'test_foo')
861
 
        # Unfortunately there's global cached state for the specific
862
 
        # registered paths.
863
 
        self.addCleanup(plugin.PluginImporter.reset)
864
850
 
865
851
    def assertTestFooLoadedFrom(self, path):
866
852
        self.assertPluginKnown('test_foo')
959
945
        self.overrideEnv('BZR_PLUGINS_AT', 'test_foo@%s' % plugin_path)
960
946
        plugin.load_plugins(['standard'])
961
947
        self.assertTestFooLoadedFrom(plugin_path)
962
 
 
963
 
 
964
 
class TestDescribePlugins(BaseTestPlugins):
965
 
 
966
 
    def test_describe_plugins(self):
967
 
        class DummyModule(object):
968
 
            __doc__ = 'Hi there'
969
 
        class DummyPlugin(object):
970
 
            __version__ = '0.1.0'
971
 
            module = DummyModule()
972
 
        def dummy_plugins():
973
 
            return { 'good': DummyPlugin() }
974
 
        self.overrideAttr(plugin, 'plugin_warnings',
975
 
            {'bad': ['Failed to load (just testing)']})
976
 
        self.overrideAttr(plugin, 'plugins', dummy_plugins)
977
 
        self.assertEquals("""\
978
 
bad (failed to load)
979
 
  ** Failed to load (just testing)
980
 
 
981
 
good 0.1.0
982
 
  Hi there
983
 
 
984
 
""", ''.join(plugin.describe_plugins()))