~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
1185.31.37 by John Arbash Meinel
Switched os.path.abspath and os.path.realpath to osutils.* (still passes on cygwin)
29
from bzrlib.osutils import pathjoin, abspath
1141 by Martin Pool
- rename FunctionalTest to TestCaseInTempDir
30
1143 by Martin Pool
- remove dead code and remove some small errors (pychecker)
31
class PluginTest(TestCaseInTempDir):
750 by Martin Pool
- stubbed-out tests for python plugins
32
    """Create an external plugin and test loading."""
1185.16.84 by mbp at sourcefrog
- fix indents
33
#    def test_plugin_loading(self):
34
#        orig_help = self.run_bzr_captured('bzr help commands')[0]
35
#        os.mkdir('plugin_test')
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
36
#        f = open(pathjoin('plugin_test', 'myplug.py'), 'wt')
1185.16.84 by mbp at sourcefrog
- fix indents
37
#        f.write(PLUGIN_TEXT)
38
#        f.close()
39
#        newhelp = self.run_bzr_captured('bzr help commands')[0]
40
#        assert newhelp.startswith('You have been overridden\n')
41
#        # We added a line, but the rest should work
42
#        assert newhelp[25:] == help
43
#
44
#        assert backtick('bzr commit -m test') == "I'm sorry dave, you can't do that\n"
45
#
46
#        shutil.rmtree('plugin_test')
47
#
750 by Martin Pool
- stubbed-out tests for python plugins
48
1185.31.37 by John Arbash Meinel
Switched os.path.abspath and os.path.realpath to osutils.* (still passes on cygwin)
49
#         os.environ['BZRPLUGINPATH'] = abspath('plugin_test')
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
50
#         help = backtick('bzr help commands')
51
#         assert help.find('myplug') != -1
52
#         assert help.find('Just a simple test plugin.') != -1
53
54
55
#         assert backtick('bzr myplug') == 'Hello from my plugin\n'
56
#         assert backtick('bzr mplg') == 'Hello from my plugin\n'
57
1185.31.32 by John Arbash Meinel
Updated the bzr sourcecode to use bzrlib.osutils.pathjoin rather than os.path.join to enforce internal use of / instead of \
58
#         f = open(pathjoin('plugin_test', 'override.py'), 'wb')
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
59
#         f.write("""import bzrlib, bzrlib.commands
60
#     class cmd_commit(bzrlib.commands.cmd_commit):
61
#         '''Commit changes into a new revision.'''
62
#         def run(self, *args, **kwargs):
63
#             print "I'm sorry dave, you can't do that"
64
65
#     class cmd_help(bzrlib.commands.cmd_help):
66
#         '''Show help on a command or other topic.'''
67
#         def run(self, *args, **kwargs):
68
#             print "You have been overridden"
69
#             bzrlib.commands.cmd_help.run(self, *args, **kwargs)
70
71
#         """
1185.16.83 by mbp at sourcefrog
- notes on testability of plugins
72
1185.16.84 by mbp at sourcefrog
- fix indents
73
PLUGIN_TEXT = """\
74
import bzrlib.commands
75
class cmd_myplug(bzrlib.commands.Command):
76
    '''Just a simple test plugin.'''
77
    aliases = ['mplg']
78
    def run(self):
79
        print 'Hello from my plugin'
80
"""
1492 by Robert Collins
Support decoration of commands.
81
82
# 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
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))
1516 by Robert Collins
* bzrlib.plugin.all_plugins has been changed from an attribute to a
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))