~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commands.py

  • Committer: Robert Collins
  • Date: 2005-08-23 06:52:09 UTC
  • mto: (974.1.50) (1185.1.10) (1092.3.1)
  • mto: This revision was merged to the branch mainline in revision 1139.
  • Revision ID: robertc@robertcollins.net-20050823065209-81cd5962c401751b
move io redirection into each test case from the global runner

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 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
 
from cStringIO import StringIO
18
 
import errno
19
 
import sys
20
 
 
21
 
from bzrlib import (
22
 
    commands,
23
 
    config,
24
 
    errors,
25
 
    tests,
26
 
    )
27
 
from bzrlib.commands import display_command
28
 
from bzrlib.tests import TestSkipped
29
 
 
30
 
 
31
 
class TestCommands(tests.TestCase):
32
 
 
33
 
    def test_display_command(self):
34
 
        """EPIPE message is selectively suppressed"""
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
 
 
47
 
    def test_unicode_command(self):
48
 
        # This error is thrown when we can't find the command in the
49
 
        # list of available commands
50
 
        self.assertRaises(errors.BzrCommandError,
51
 
                          commands.run_bzr, [u'cmd\xb5'])
52
 
 
53
 
    def test_unicode_option(self):
54
 
        # This error is actually thrown by optparse, when it
55
 
        # can't find the given option
56
 
        import optparse
57
 
        if optparse.__version__ == "1.5.3":
58
 
            raise TestSkipped("optparse 1.5.3 can't handle unicode options")
59
 
        self.assertRaises(errors.BzrCommandError,
60
 
                          commands.run_bzr, ['log', u'--option\xb5'])
61
 
 
62
 
 
63
 
class TestGetAlias(tests.TestCase):
64
 
 
65
 
    def _get_config(self, config_text):
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):
72
 
        my_config = self._get_config("[ALIASES]\n"
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):
78
 
        my_config = self._get_config("[ALIASES]\n"
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):
86
 
        my_config = self._get_config("[ALIASES]\n"
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):
94
 
        my_config = self._get_config("[ALIASES]\n"
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))
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())
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
 
 
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)