~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

Merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
import os
25
25
 
26
 
from bzrlib.selftest import TestCaseInTempDir
 
26
import bzrlib.plugin
 
27
import bzrlib.plugins
 
28
from bzrlib.tests import TestCaseInTempDir
 
29
from bzrlib.osutils import pathjoin, abspath
27
30
 
28
31
class PluginTest(TestCaseInTempDir):
29
32
    """Create an external plugin and test loading."""
30
33
#    def test_plugin_loading(self):
31
34
#        orig_help = self.run_bzr_captured('bzr help commands')[0]
32
35
#        os.mkdir('plugin_test')
33
 
#        f = open(os.path.join('plugin_test', 'myplug.py'), 'wt')
 
36
#        f = open(pathjoin('plugin_test', 'myplug.py'), 'wt')
34
37
#        f.write(PLUGIN_TEXT)
35
38
#        f.close()
36
39
#        newhelp = self.run_bzr_captured('bzr help commands')[0]
43
46
#        shutil.rmtree('plugin_test')
44
47
#
45
48
 
46
 
#         os.environ['BZRPLUGINPATH'] = os.path.abspath('plugin_test')
 
49
#         os.environ['BZRPLUGINPATH'] = abspath('plugin_test')
47
50
#         help = backtick('bzr help commands')
48
51
#         assert help.find('myplug') != -1
49
52
#         assert help.find('Just a simple test plugin.') != -1
52
55
#         assert backtick('bzr myplug') == 'Hello from my plugin\n'
53
56
#         assert backtick('bzr mplg') == 'Hello from my plugin\n'
54
57
 
55
 
#         f = open(os.path.join('plugin_test', 'override.py'), 'wb')
 
58
#         f = open(pathjoin('plugin_test', 'override.py'), 'wb')
56
59
#         f.write("""import bzrlib, bzrlib.commands
57
60
#     class cmd_commit(bzrlib.commands.cmd_commit):
58
61
#         '''Commit changes into a new revision.'''
77
80
"""
78
81
 
79
82
# TODO: Write a test for plugin decoration of commands.
 
83
 
 
84
class TestOneNamedPluginOnly(TestCaseInTempDir):
 
85
 
 
86
    activeattributes = {}
 
87
 
 
88
    def test_plugins_with_the_same_name_are_not_loaded(self):
 
89
        # This test tests that having two plugins in different
 
90
        # directories does not result in both being loaded.
 
91
        # get a file name we can use which is also a valid attribute
 
92
        # for accessing in activeattributes. - we cannot give import parameters.
 
93
        tempattribute = "0"
 
94
        self.failIf(tempattribute in self.activeattributes)
 
95
        # set a place for the plugins to record their loading, and at the same
 
96
        # time validate that the location the plugins should record to is
 
97
        # valid and correct.
 
98
        bzrlib.tests.test_plugins.TestOneNamedPluginOnly.activeattributes \
 
99
            [tempattribute] = []
 
100
        self.failUnless(tempattribute in self.activeattributes)
 
101
        # create two plugin directories
 
102
        os.mkdir('first')
 
103
        os.mkdir('second')
 
104
        # write a plugin that will record when its loaded in the 
 
105
        # tempattribute list.
 
106
        template = ("from bzrlib.tests.test_plugins import TestOneNamedPluginOnly\n"
 
107
                    "TestOneNamedPluginOnly.activeattributes[%r].append('%s')\n")
 
108
        print >> file(os.path.join('first', 'plugin.py'), 'w'), template % (tempattribute, 'first')
 
109
        print >> file(os.path.join('second', 'plugin.py'), 'w'), template % (tempattribute, 'second')
 
110
        try:
 
111
            bzrlib.plugin.load_from_dirs(['first', 'second'])
 
112
            self.assertEqual(['first'], self.activeattributes[tempattribute])
 
113
        finally:
 
114
            # remove the plugin 'plugin'
 
115
            del self.activeattributes[tempattribute]
 
116
            if getattr(bzrlib.plugins, 'plugin', None):
 
117
                del bzrlib.plugins.plugin
 
118
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
119
 
 
120
 
 
121
class TestAllPlugins(TestCaseInTempDir):
 
122
 
 
123
    def test_plugin_appears_in_all_plugins(self):
 
124
        # This test tests a new plugin appears in bzrlib.plugin.all_plugins().
 
125
        # check the plugin is not loaded already
 
126
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
127
        # write a plugin that _cannot_ fail to load.
 
128
        print >> file('plugin.py', 'w'), ""
 
129
        try:
 
130
            bzrlib.plugin.load_from_dirs(['.'])
 
131
            self.failUnless('plugin' in bzrlib.plugin.all_plugins())
 
132
            self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
 
133
            self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
 
134
                             bzrlib.plugins.plugin)
 
135
        finally:
 
136
            # remove the plugin 'plugin'
 
137
            if getattr(bzrlib.plugins, 'plugin', None):
 
138
                del bzrlib.plugins.plugin
 
139
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))