~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_plugins.py

  • Committer: Michael Ellerman
  • Date: 2006-06-18 23:19:23 UTC
  • mto: (1711.2.63 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1792.
  • Revision ID: michael@ellerman.id.au-20060618231923-8a362933a83c8660
Show which plugin (if any) provides a command.

Changes 'bzr help <foo>' and 'bzr help commands', to show which plugin
provides a command. If the command is a builtin the output is unchanged.
Tests included. Based on an original patch by Robert Widhopf-Fenk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# comments.
23
23
 
24
24
import os
 
25
from StringIO import StringIO
25
26
 
26
27
import bzrlib.plugin
27
28
import bzrlib.plugins
 
29
import bzrlib.commands
 
30
import bzrlib.help
28
31
from bzrlib.tests import TestCaseInTempDir
29
32
from bzrlib.osutils import pathjoin, abspath
30
33
 
137
140
            if getattr(bzrlib.plugins, 'plugin', None):
138
141
                del bzrlib.plugins.plugin
139
142
        self.failIf(getattr(bzrlib.plugins, 'plugin', None))
 
143
 
 
144
 
 
145
class TestPluginHelp(TestCaseInTempDir):
 
146
 
 
147
    def split_help_commands(self):
 
148
        help = {}
 
149
        current = None
 
150
        for line in self.capture('help commands').splitlines():
 
151
            if line.startswith('bzr '):
 
152
                current = line.split()[1]
 
153
            help[current] = help.get(current, '') + line
 
154
 
 
155
        return help
 
156
 
 
157
    def test_plugin_help_builtins_unaffected(self):
 
158
        # Check we don't get false positives
 
159
        help_commands = self.split_help_commands()
 
160
        for cmd_name in bzrlib.commands.builtin_command_names():
 
161
            if cmd_name in bzrlib.commands.plugin_command_names():
 
162
                continue
 
163
            help = StringIO()
 
164
            try:
 
165
                bzrlib.help.help_on_command(cmd_name, help)
 
166
            except NotImplementedError:
 
167
                # some commands have no help
 
168
                pass
 
169
            else:
 
170
                help.seek(0)
 
171
                self.assertNotContainsRe(help.read(), 'From plugin "[^"]*"')
 
172
 
 
173
            if help in help_commands.keys():
 
174
                # some commands are hidden
 
175
                help = help_commands[cmd_name]
 
176
                self.assertNotContainsRe(help, 'From plugin "[^"]*"')
 
177
 
 
178
    def test_plugin_help_shows_plugin(self):
 
179
        # Create a test plugin
 
180
        os.mkdir('plugin_test')
 
181
        f = open(pathjoin('plugin_test', 'myplug.py'), 'w')
 
182
        f.write(PLUGIN_TEXT)
 
183
        f.close()
 
184
 
 
185
        try:
 
186
            # Check its help
 
187
            bzrlib.plugin.load_from_dirs(['plugin_test'])
 
188
            bzrlib.commands.register_command( bzrlib.plugins.myplug.cmd_myplug)
 
189
            help = self.capture('help myplug')
 
190
            self.assertContainsRe(help, 'From plugin "myplug"')
 
191
            help = self.split_help_commands()['myplug']
 
192
            self.assertContainsRe(help, 'From plugin "myplug"')
 
193
        finally:
 
194
            # remove the plugin 'plugin'
 
195
            if getattr(bzrlib.plugins, 'plugin', None):
 
196
                del bzrlib.plugins.plugin