/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7015.1.3 by Martin
Tweak copyright headers
1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
2
# Copyright (C) 2017-2018 Breezy developers
7015.1.2 by Martin
Make last plugins tests pass again on Python 3
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18
"""Blackbox tests for plugins behaviour and commands."""
19
20
import os
21
22
from ... import (
23
    commands,
24
    )
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
25
from ...sixish import PY3
7015.1.2 by Martin
Make last plugins tests pass again on Python 3
26
from ..test_plugins import (
27
    BaseTestPlugins,
28
    )
29
30
31
class TestPluginHelp(BaseTestPlugins):
32
33
    def run_bzr_utf8_out(self, *args, **kwargs):
34
        out, _ = self.run_bzr(*args, **kwargs)
7027.4.1 by Jelmer Vernooij
Use StringIOWithEncoding on Python3.
35
        if PY3:
36
            return out
37
        else:
38
            return out.decode('utf-8')
7015.1.2 by Martin
Make last plugins tests pass again on Python 3
39
40
    def split_help_commands(self):
41
        help = {}
42
        current = None
43
        out = self.run_bzr_utf8_out('--no-plugins help commands')
44
        for line in out.splitlines():
45
            if not line.startswith(' '):
46
                current = line.split()[0]
47
            help[current] = help.get(current, '') + line
48
49
        return help
50
51
    def test_plugin_help_builtins_unaffected(self):
52
        # Check we don't get false positives
53
        help_commands = self.split_help_commands()
54
        for cmd_name in commands.builtin_command_names():
55
            if cmd_name in commands.plugin_command_names():
56
                continue
57
            try:
58
                help = commands.get_cmd_object(cmd_name).get_help_text()
59
            except NotImplementedError:
60
                # some commands have no help
61
                pass
62
            else:
63
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
64
65
            if cmd_name in help_commands:
66
                # some commands are hidden
67
                help = help_commands[cmd_name]
68
                self.assertNotContainsRe(help, 'plugin "[^"]*"')
69
70
    def test_plugin_help_shows_plugin(self):
71
        # Create a test plugin
72
        os.mkdir('plugin_test')
73
        source = (
74
            "from breezy import commands\n"
75
            "class cmd_myplug(commands.Command):\n"
76
            "    __doc__ = '''Just a simple test plugin.'''\n"
77
            "    aliases = ['mplg']\n"
78
            "    def run(self):\n"
79
            "        print ('Hello from my plugin')\n"
80
        )
81
        self.create_plugin('myplug', source, 'plugin_test')
82
83
        # Check its help
84
        self.load_with_paths(['plugin_test'])
85
        myplug = self.plugins['myplug'].module
86
        commands.register_command(myplug.cmd_myplug)
87
        self.addCleanup(commands.plugin_cmds.remove, 'myplug')
88
        help = self.run_bzr_utf8_out('help myplug')
89
        self.assertContainsRe(help, 'plugin "myplug"')
90
        help = self.split_help_commands()['myplug']
91
        self.assertContainsRe(help, '\\[myplug\\]')