~bzr-pqm/bzr/bzr.dev

750 by Martin Pool
- stubbed-out tests for python plugins
1
# Copyright (C) 2005 by Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
13
# You should have received a copy of the GNU General Public License
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
16
17
18
"""Tests for plugins"""
19
1185.16.83 by mbp at sourcefrog
- notes on testability of plugins
20
# XXX: There are no plugin tests at the moment because the plugin module
21
# affects the global state of the process.  See bzrlib/plugins.py for more
22
# comments.
23
24
import os
750 by Martin Pool
- stubbed-out tests for python plugins
25
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
26
import bzrlib.plugin
27
import bzrlib.plugins
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
28
from bzrlib.tests import TestCaseInTempDir
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
29
1143 by Martin Pool
- remove dead code and remove some small errors (pychecker)
30
class PluginTest(TestCaseInTempDir):
750 by Martin Pool
- stubbed-out tests for python plugins
31
    """Create an external plugin and test loading."""
1185.16.84 by mbp at sourcefrog
- fix indents
32
#    def test_plugin_loading(self):
33
#        orig_help = self.run_bzr_captured('bzr help commands')[0]
34
#        os.mkdir('plugin_test')
35
#        f = open(os.path.join('plugin_test', 'myplug.py'), 'wt')
36
#        f.write(PLUGIN_TEXT)
37
#        f.close()
38
#        newhelp = self.run_bzr_captured('bzr help commands')[0]
39
#        assert newhelp.startswith('You have been overridden\n')
40
#        # We added a line, but the rest should work
41
#        assert newhelp[25:] == help
42
#
43
#        assert backtick('bzr commit -m test') == "I'm sorry dave, you can't do that\n"
44
#
45
#        shutil.rmtree('plugin_test')
46
#
750 by Martin Pool
- stubbed-out tests for python plugins
47
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
48
#         os.environ['BZRPLUGINPATH'] = os.path.abspath('plugin_test')
49
#         help = backtick('bzr help commands')
50
#         assert help.find('myplug') != -1
51
#         assert help.find('Just a simple test plugin.') != -1
52
53
54
#         assert backtick('bzr myplug') == 'Hello from my plugin\n'
55
#         assert backtick('bzr mplg') == 'Hello from my plugin\n'
56
57
#         f = open(os.path.join('plugin_test', 'override.py'), 'wb')
58
#         f.write("""import bzrlib, bzrlib.commands
59
#     class cmd_commit(bzrlib.commands.cmd_commit):
60
#         '''Commit changes into a new revision.'''
61
#         def run(self, *args, **kwargs):
62
#             print "I'm sorry dave, you can't do that"
63
64
#     class cmd_help(bzrlib.commands.cmd_help):
65
#         '''Show help on a command or other topic.'''
66
#         def run(self, *args, **kwargs):
67
#             print "You have been overridden"
68
#             bzrlib.commands.cmd_help.run(self, *args, **kwargs)
69
70
#         """
1185.16.83 by mbp at sourcefrog
- notes on testability of plugins
71
1185.16.84 by mbp at sourcefrog
- fix indents
72
PLUGIN_TEXT = """\
73
import bzrlib.commands
74
class cmd_myplug(bzrlib.commands.Command):
75
    '''Just a simple test plugin.'''
76
    aliases = ['mplg']
77
    def run(self):
78
        print 'Hello from my plugin'
79
"""
1492 by Robert Collins
Support decoration of commands.
80
81
# TODO: Write a test for plugin decoration of commands.
1515 by Robert Collins
* Plugins with the same name in different directories in the bzr plugin
82
83
class TestOneNamedPluginOnly(TestCaseInTempDir):
84
85
    activeattributes = {}
86
87
    def test_plugins_with_the_same_name_are_not_loaded(self):
88
        # This test tests that having two plugins in different
89
        # directories does not result in both being loaded.
90
        # get a file name we can use which is also a valid attribute
91
        # for accessing in activeattributes. - we cannot give import parameters.
92
        tempattribute = "0"
93
        self.failIf(tempattribute in self.activeattributes)
94
        # set a place for the plugins to record their loading, and at the same
95
        # time validate that the location the plugins should record to is
96
        # valid and correct.
97
        bzrlib.tests.test_plugins.TestOneNamedPluginOnly.activeattributes \
98
            [tempattribute] = []
99
        self.failUnless(tempattribute in self.activeattributes)
100
        # create two plugin directories
101
        os.mkdir('first')
102
        os.mkdir('second')
103
        # write a plugin that will record when its loaded in the 
104
        # tempattribute list.
105
        template = ("from bzrlib.tests.test_plugins import TestOneNamedPluginOnly\n"
106
                    "TestOneNamedPluginOnly.activeattributes[%r].append('%s')\n")
107
        print >> file(os.path.join('first', 'plugin.py'), 'w'), template % (tempattribute, 'first')
108
        print >> file(os.path.join('second', 'plugin.py'), 'w'), template % (tempattribute, 'second')
109
        try:
110
            bzrlib.plugin.load_from_dirs(['first', 'second'])
111
            self.assertEqual(['first'], self.activeattributes[tempattribute])
112
        finally:
113
            # remove the plugin 'plugin'
114
            del self.activeattributes[tempattribute]
115
            if getattr(bzrlib.plugins, 'plugin', None):
116
                del bzrlib.plugins.plugin
117
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
118
119
120
class TestAllPlugins(TestCaseInTempDir):
121
122
    def test_plugin_appears_in_all_plugins(self):
123
        # This test tests a new plugin appears in bzrlib.plugin.all_plugins().
124
        # check the plugin is not loaded already
125
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
126
        # write a plugin that _cannot_ fail to load.
127
        print >> file('plugin.py', 'w'), ""
128
        try:
129
            bzrlib.plugin.load_from_dirs(['.'])
130
            self.failUnless('plugin' in bzrlib.plugin.all_plugins())
131
            self.failUnless(getattr(bzrlib.plugins, 'plugin', None))
132
            self.assertEqual(bzrlib.plugin.all_plugins()['plugin'],
133
                             bzrlib.plugins.plugin)
134
        finally:
135
            # remove the plugin 'plugin'
136
            if getattr(bzrlib.plugins, 'plugin', None):
137
                del bzrlib.plugins.plugin
138
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))