~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Martin Pool
  • Date: 2009-07-27 06:28:35 UTC
  • mto: This revision was merged to the branch mainline in revision 4587.
  • Revision ID: mbp@sourcefrog.net-20090727062835-o66p8it658tq1sma
Add CountedLock.get_physical_lock_status

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for plugins"""
18
18
 
31
31
import bzrlib.plugins
32
32
import bzrlib.commands
33
33
import bzrlib.help
34
 
from bzrlib.symbol_versioning import one_three
35
34
from bzrlib.tests import (
36
35
    TestCase,
37
36
    TestCaseInTempDir,
71
70
        # create two plugin directories
72
71
        os.mkdir('first')
73
72
        os.mkdir('second')
74
 
        # write a plugin that will record when its loaded in the 
 
73
        # write a plugin that will record when its loaded in the
75
74
        # tempattribute list.
76
75
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
77
76
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
119
118
        # create two plugin directories
120
119
        os.mkdir('first')
121
120
        os.mkdir('second')
122
 
        # write plugins that will record when they are loaded in the 
 
121
        # write plugins that will record when they are loaded in the
123
122
        # tempattribute list.
124
123
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
125
124
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
171
170
        self.failUnless(tempattribute in self.activeattributes)
172
171
        # create a directory for the plugin
173
172
        os.mkdir('plugin_test')
174
 
        # write a plugin that will record when its loaded in the 
 
173
        # write a plugin that will record when its loaded in the
175
174
        # tempattribute list.
176
175
        template = ("from bzrlib.tests.test_plugins import TestLoadingPlugins\n"
177
176
                    "TestLoadingPlugins.activeattributes[%r].append('%s')\n")
195
194
 
196
195
    def load_and_capture(self, name):
197
196
        """Load plugins from '.' capturing the output.
198
 
        
 
197
 
199
198
        :param name: The name of the plugin.
200
199
        :return: A string with the log from the plugin loading call.
201
200
        """
221
220
            return stream.getvalue()
222
221
        finally:
223
222
            stream.close()
224
 
    
 
223
 
225
224
    def test_plugin_with_bad_api_version_reports(self):
226
225
        # This plugin asks for bzrlib api version 1.0.0, which is not supported
227
226
        # anymore.
258
257
        file('plugin.py', 'w').write(source + '\n')
259
258
        self.addCleanup(self.teardown_plugin)
260
259
        bzrlib.plugin.load_from_path(['.'])
261
 
    
 
260
 
262
261
    def teardown_plugin(self):
263
262
        # remove the plugin 'plugin'
264
263
        if 'bzrlib.plugins.plugin' in sys.modules:
455
454
                delattr(bzrlib.plugins, 'myplug')
456
455
 
457
456
 
458
 
class TestPluginFromZip(TestCaseInTempDir):
459
 
 
460
 
    def make_zipped_plugin(self, zip_name, filename):
461
 
        z = zipfile.ZipFile(zip_name, 'w')
462
 
        z.writestr(filename, PLUGIN_TEXT)
463
 
        z.close()
464
 
 
465
 
    def check_plugin_load(self, zip_name, plugin_name):
466
 
        self.assertFalse(plugin_name in dir(bzrlib.plugins),
467
 
                         'Plugin already loaded')
468
 
        old_path = bzrlib.plugins.__path__
469
 
        try:
470
 
            # this is normally done by load_plugins -> set_plugins_path
471
 
            bzrlib.plugins.__path__ = [zip_name]
472
 
            self.applyDeprecated(one_three,
473
 
                bzrlib.plugin.load_from_zip, zip_name)
474
 
            self.assertTrue(plugin_name in dir(bzrlib.plugins),
475
 
                            'Plugin is not loaded')
476
 
        finally:
477
 
            # unregister plugin
478
 
            if getattr(bzrlib.plugins, plugin_name, None):
479
 
                delattr(bzrlib.plugins, plugin_name)
480
 
                del sys.modules['bzrlib.plugins.' + plugin_name]
481
 
            bzrlib.plugins.__path__ = old_path
482
 
 
483
 
    def test_load_module(self):
484
 
        self.make_zipped_plugin('./test.zip', 'ziplug.py')
485
 
        self.check_plugin_load('./test.zip', 'ziplug')
486
 
 
487
 
    def test_load_package(self):
488
 
        self.make_zipped_plugin('./test.zip', 'ziplug/__init__.py')
489
 
        self.check_plugin_load('./test.zip', 'ziplug')
490
 
 
491
 
 
492
457
class TestSetPluginsPath(TestCase):
493
 
    
 
458
 
494
459
    def test_set_plugins_path(self):
495
460
        """set_plugins_path should set the module __path__ correctly."""
496
461
        old_path = bzrlib.plugins.__path__
668
633
        path = plugin.get_standard_plugins_path()
669
634
        self.assertEqual(plugin.get_default_plugin_path(), path[0])
670
635
        for directory in path:
671
 
            self.assertNotContainsRe(r'\\/$', directory)
 
636
            self.assertNotContainsRe(directory, r'\\/$')
672
637
        try:
673
638
            from distutils.sysconfig import get_python_lib
674
639
        except ImportError: