/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2004, 2005 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.12.59 by Aaron Bentley
Added command-quieting test
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.12.59 by Aaron Bentley
Added command-quieting test
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.12.59 by Aaron Bentley
Added command-quieting test
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
2155.2.3 by Marius Kruger
* commands.py
17
from cStringIO import StringIO
1948.1.1 by John Arbash Meinel
Raise a reasonable error when a command is non-ascii
18
import errno
3785.1.4 by Aaron Bentley
Enable lazy-loading of commands
19
import sys
1948.1.1 by John Arbash Meinel
Raise a reasonable error when a command is non-ascii
20
21
from bzrlib import (
22
    commands,
2155.2.1 by Marius Kruger
* Get command aliases to respect quoted arguments.
23
    config,
1948.1.1 by John Arbash Meinel
Raise a reasonable error when a command is non-ascii
24
    errors,
2425.2.1 by Robert Collins
Command objects can now declare related help topics by having _see_also
25
    tests,
1948.1.1 by John Arbash Meinel
Raise a reasonable error when a command is non-ascii
26
    )
27
from bzrlib.commands import display_command
2425.2.1 by Robert Collins
Command objects can now declare related help topics by having _see_also
28
from bzrlib.tests import TestSkipped
29
30
31
class TestCommands(tests.TestCase):
1948.1.1 by John Arbash Meinel
Raise a reasonable error when a command is non-ascii
32
1185.12.59 by Aaron Bentley
Added command-quieting test
33
    def test_display_command(self):
1185.33.18 by Martin Pool
[patch] handle bad IOError subclass raised by urlopen
34
        """EPIPE message is selectively suppressed"""
1185.12.59 by Aaron Bentley
Added command-quieting test
35
        def pipe_thrower():
36
            raise IOError(errno.EPIPE, "Bogus pipe error")
37
        self.assertRaises(IOError, pipe_thrower)
38
        @display_command
39
        def non_thrower():
40
            pipe_thrower()
41
        non_thrower()
42
        @display_command
43
        def other_thrower():
44
            raise IOError(errno.ESPIPE, "Bogus pipe error")
45
        self.assertRaises(IOError, other_thrower)
46
1948.1.1 by John Arbash Meinel
Raise a reasonable error when a command is non-ascii
47
    def test_unicode_command(self):
1948.1.8 by John Arbash Meinel
Don't raise UnicodeCommand on request, instead just let it fall out when we get to NoSuchCommand
48
        # This error is thrown when we can't find the command in the
49
        # list of available commands
50
        self.assertRaises(errors.BzrCommandError,
1948.1.1 by John Arbash Meinel
Raise a reasonable error when a command is non-ascii
51
                          commands.run_bzr, [u'cmd\xb5'])
1948.1.8 by John Arbash Meinel
Don't raise UnicodeCommand on request, instead just let it fall out when we get to NoSuchCommand
52
53
    def test_unicode_option(self):
54
        # This error is actually thrown by optparse, when it
55
        # can't find the given option
1913.2.4 by Martin Pool
python2.5 apparently has trouble with unicode options
56
        import optparse
57
        if optparse.__version__ == "1.5.3":
58
            raise TestSkipped("optparse 1.5.3 can't handle unicode options")
1948.1.8 by John Arbash Meinel
Don't raise UnicodeCommand on request, instead just let it fall out when we get to NoSuchCommand
59
        self.assertRaises(errors.BzrCommandError,
60
                          commands.run_bzr, ['log', u'--option\xb5'])
61
2155.2.3 by Marius Kruger
* commands.py
62
2425.2.1 by Robert Collins
Command objects can now declare related help topics by having _see_also
63
class TestGetAlias(tests.TestCase):
2155.2.3 by Marius Kruger
* commands.py
64
65
    def _get_config(self, config_text):
2155.2.1 by Marius Kruger
* Get command aliases to respect quoted arguments.
66
        my_config = config.GlobalConfig()
67
        config_file = StringIO(config_text.encode('utf-8'))
68
        my_config._parser = my_config._get_parser(file=config_file)
69
        return my_config
70
71
    def test_simple(self):
2155.2.3 by Marius Kruger
* commands.py
72
        my_config = self._get_config("[ALIASES]\n"
2155.2.1 by Marius Kruger
* Get command aliases to respect quoted arguments.
73
            "diff=diff -r -2..-1\n")
74
        self.assertEqual([u'diff', u'-r', u'-2..-1'],
75
            commands.get_alias("diff", config=my_config))
76
77
    def test_single_quotes(self):
2155.2.3 by Marius Kruger
* commands.py
78
        my_config = self._get_config("[ALIASES]\n"
2155.2.1 by Marius Kruger
* Get command aliases to respect quoted arguments.
79
            "diff=diff -r -2..-1 --diff-options "
80
            "'--strip-trailing-cr -wp'\n")
81
        self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
82
                          u'--strip-trailing-cr -wp'],
83
                          commands.get_alias("diff", config=my_config))
84
85
    def test_double_quotes(self):
2155.2.3 by Marius Kruger
* commands.py
86
        my_config = self._get_config("[ALIASES]\n"
2155.2.1 by Marius Kruger
* Get command aliases to respect quoted arguments.
87
            "diff=diff -r -2..-1 --diff-options "
88
            "\"--strip-trailing-cr -wp\"\n")
89
        self.assertEqual([u'diff', u'-r', u'-2..-1', u'--diff-options',
90
                          u'--strip-trailing-cr -wp'],
91
                          commands.get_alias("diff", config=my_config))
92
93
    def test_unicode(self):
2155.2.3 by Marius Kruger
* commands.py
94
        my_config = self._get_config("[ALIASES]\n"
2155.2.1 by Marius Kruger
* Get command aliases to respect quoted arguments.
95
            u"iam=whoami 'Erik B\u00e5gfors <erik@bagfors.nu>'\n")
96
        self.assertEqual([u'whoami', u'Erik B\u00e5gfors <erik@bagfors.nu>'],
97
                          commands.get_alias("iam", config=my_config))
2425.2.1 by Robert Collins
Command objects can now declare related help topics by having _see_also
98
99
100
class TestSeeAlso(tests.TestCase):
101
    """Tests for the see also functional of Command."""
102
103
    def test_default_subclass_no_see_also(self):
104
        class ACommand(commands.Command):
105
            """A sample command."""
106
        command = ACommand()
107
        self.assertEqual([], command.get_see_also())
108
109
    def test__see_also(self):
110
        """When _see_also is defined, it sets the result of get_see_also()."""
111
        class ACommand(commands.Command):
112
            _see_also = ['bar', 'foo']
113
        command = ACommand()
114
        self.assertEqual(['bar', 'foo'], command.get_see_also())
115
116
    def test_deduplication(self):
117
        """Duplicates in _see_also are stripped out."""
118
        class ACommand(commands.Command):
119
            _see_also = ['foo', 'foo']
120
        command = ACommand()
121
        self.assertEqual(['foo'], command.get_see_also())
122
123
    def test_sorted(self):
124
        """_see_also is sorted by get_see_also."""
125
        class ACommand(commands.Command):
126
            _see_also = ['foo', 'bar']
127
        command = ACommand()
128
        self.assertEqual(['bar', 'foo'], command.get_see_also())
2432.1.21 by Robert Collins
Teach Command.get_help_text to show additional help cross references when supplied.
129
130
    def test_additional_terms(self):
131
        """Additional terms can be supplied and are deduped and sorted."""
132
        class ACommand(commands.Command):
133
            _see_also = ['foo', 'bar']
134
        command = ACommand()
135
        self.assertEqual(['bar', 'foo', 'gam'],
136
            command.get_see_also(['gam', 'bar', 'gam']))
137
3785.1.4 by Aaron Bentley
Enable lazy-loading of commands
138
139
class TestRegisterLazy(tests.TestCase):
140
141
    def setUp(self):
142
        import bzrlib.tests.fake_command
143
        del sys.modules['bzrlib.tests.fake_command']
144
        global lazy_command_imported
145
        lazy_command_imported = False
146
147
    @staticmethod
148
    def remove_fake():
149
        commands.plugin_cmds.remove('fake')
150
151
    def assertIsFakeCommand(self, cmd_obj):
152
        from bzrlib.tests.fake_command import cmd_fake
153
        self.assertIsInstance(cmd_obj, cmd_fake)
154
155
    def test_register_lazy(self):
156
        """Ensure lazy registration works"""
157
        commands.plugin_cmds.register_lazy('cmd_fake', [],
158
                                           'bzrlib.tests.fake_command')
159
        self.addCleanup(self.remove_fake)
160
        self.assertFalse(lazy_command_imported)
161
        fake_instance = commands.get_cmd_object('fake')
162
        self.assertTrue(lazy_command_imported)
163
        self.assertIsFakeCommand(fake_instance)
164
165
    def test_get_unrelated_does_not_import(self):
166
        commands.plugin_cmds.register_lazy('cmd_fake', [],
167
                                           'bzrlib.tests.fake_command')
168
        self.addCleanup(self.remove_fake)
169
        commands.get_cmd_object('status')
170
        self.assertFalse(lazy_command_imported)
171
172
    def test_aliases(self):
173
        commands.plugin_cmds.register_lazy('cmd_fake', ['fake_alias'],
174
                                           'bzrlib.tests.fake_command')
175
        self.addCleanup(self.remove_fake)
176
        fake_instance = commands.get_cmd_object('fake_alias')
177
        self.assertIsFakeCommand(fake_instance)