/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) 2005, 2006 Canonical Ltd
1185.33.9 by Martin Pool
Add new selftest module.
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
# "weren't nothing promised to you.  do i look like i got a promise face?"
18
19
"""Tests for trace library"""
20
1948.1.5 by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments
21
from cStringIO import StringIO
1740.5.7 by Martin Pool
Add test for formatting of EPIPE
22
import errno
1185.33.9 by Martin Pool
Add new selftest module.
23
import os
24
import sys
25
1948.1.5 by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments
26
from bzrlib import (
27
    errors,
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
28
    plugin,
29
    trace,
1948.1.5 by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments
30
    )
2123.1.2 by Robert Collins
Ensure TestSkipped is present for skipping when new test dependency on apport is missing.
31
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
32
from bzrlib.trace import mutter, report_exception
1185.33.9 by Martin Pool
Add new selftest module.
33
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
34
35
def _format_exception():
36
    """Format an exception as it would normally be displayed to the user"""
37
    buf = StringIO()
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
38
    report = report_exception(sys.exc_info(), buf)
39
    return buf.getvalue(), report
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
40
41
1185.33.9 by Martin Pool
Add new selftest module.
42
class TestTrace(TestCase):
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
43
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
44
    def test_format_sys_exception_no_apport(self):
1185.33.9 by Martin Pool
Add new selftest module.
45
        try:
46
            raise NotImplementedError, "time travel"
47
        except NotImplementedError:
48
            pass
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
49
        old_use_apport = trace._use_apport
50
        trace._use_apport = False
51
        try:
52
            err, report = _format_exception()
53
        finally:
54
            trace._use_apport = old_use_apport
55
        self.assertEqual(None, report)
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
56
        self.assertEqualDiff(err.splitlines()[0],
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
57
                'bzr: ERROR: exceptions.NotImplementedError: time travel')
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
58
        self.assertContainsRe(err,
59
                r'File.*test_trace.py')
1185.33.9 by Martin Pool
Add new selftest module.
60
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
61
    def test_format_sys_exception_apport(self):
62
        try:
63
            import problem_report
64
        except ImportError:
65
            raise TestSkipped('Apport not installed')
66
        try:
67
            raise NotImplementedError, "time travel"
68
        except NotImplementedError:
69
            pass
70
        old_argv = sys.argv
71
        sys.argv = ['foo', 'bar', 'quux']
72
        try:
73
            err, (report, report_filename) = _format_exception()
74
        finally:
75
            sys.argv = old_argv
76
        self.assertIsInstance(report, problem_report.ProblemReport)
77
        # the error formatting is checked by the blackbox ui command.
78
        # here we need to check that the file on disk - the problem report
79
        # will contain the right information.
80
        # the report needs:
81
        #  - the command line.
82
        #  - package data
83
        #  - plugins list
84
        #  - backtrace.
85
        # check the report logical data.
86
        self.assertEqual('foo bar quux', report['CommandLine'])
87
        known_plugins = ' '.join(plugin.all_plugins())
88
        self.assertEqual(known_plugins, report['BzrPlugins'])
89
        self.assertContainsRe(report['Traceback'], r'Traceback')
90
        # Stock apport facilities we just invoke, no need to test their
91
        # content
92
        self.assertNotEqual(None, report['Package'])
93
        self.assertNotEqual(None, report['Uname'])
94
        # check the file 'looks' like a good file, because we dont
95
        # want apport changes to break the user interface.
96
        report_file = file(report_filename, 'r')
97
        try:
98
            report_text = report_file.read()
99
        finally:
100
            report_file.close()
101
        # so we check this by looking across two fields and they should
102
        # be just \n separated.
103
        self.assertTrue('ProblemType: Crash\n'
104
            'BzrPlugins: ' in report_text)
105
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
106
    def test_format_interrupt_exception(self):
107
        try:
108
            raise KeyboardInterrupt()
1740.5.5 by Martin Pool
Show short form for OSError and IOError too
109
        except KeyboardInterrupt:
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
110
            # XXX: Some risk that a *real* keyboard interrupt won't be seen
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
111
            # We can probably detect that by checking for the specific line
112
            # that we raise from in the test being in the backtrace.
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
113
            pass
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
114
        msg, report = _format_exception()
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
115
        self.assertTrue(len(msg) > 0)
116
        self.assertEqualDiff(msg, 'bzr: interrupted\n')
117
1740.5.5 by Martin Pool
Show short form for OSError and IOError too
118
    def test_format_os_error(self):
119
        try:
120
            file('nosuchfile22222')
121
        except (OSError, IOError):
122
            pass
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
123
        msg, report = _format_exception()
1740.5.5 by Martin Pool
Show short form for OSError and IOError too
124
        self.assertContainsRe(msg, r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
125
1948.1.5 by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments
126
    def test_format_unicode_error(self):
127
        try:
128
            raise errors.BzrCommandError(u'argument foo\xb5 does not exist')
129
        except errors.BzrCommandError:
130
            pass
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
131
        msg, report = _format_exception()
1740.5.5 by Martin Pool
Show short form for OSError and IOError too
132
1185.33.9 by Martin Pool
Add new selftest module.
133
    def test_format_exception(self):
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
134
        """Short formatting of bzr exceptions"""
1185.33.9 by Martin Pool
Add new selftest module.
135
        try:
2067.3.1 by Martin Pool
Clean up BzrNewError, other exception classes and users.
136
            raise errors.NotBranchError('wibble')
1948.1.5 by John Arbash Meinel
Make sure BzrCommandError can handle unicode arguments
137
        except errors.NotBranchError:
1185.33.9 by Martin Pool
Add new selftest module.
138
            pass
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
139
        msg, report = _format_exception()
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
140
        self.assertTrue(len(msg) > 0)
141
        self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n')
1185.33.63 by Martin Pool
Better display of BzrError classes that are not BzrNewErrors.
142
1185.33.51 by Martin Pool
Fix trace of non-ascii messages, and add test.
143
    def test_trace_unicode(self):
144
        """Write Unicode to trace log"""
145
        self.log(u'the unicode character for benzene is \N{BENZENE RING}')
1927.3.1 by Carl Friedrich Bolz
Throw away on-disk logfile when possible.
146
        self.assertContainsRe(self._get_log(keep_log_file=True),
147
                              "the unicode character for benzene is")
1948.1.2 by John Arbash Meinel
Fix the test_trace functions to actually test that things are written to the log
148
    
149
    def test_trace_argument_unicode(self):
150
        """Write a Unicode argument to the trace log"""
151
        mutter(u'the unicode character for benzene is %s', u'\N{BENZENE RING}')
1927.3.4 by Carl Friedrich Bolz
Merge bzr.dev.
152
        self.assertContainsRe(self._get_log(keep_log_file=True),
153
                              'the unicode character')
1185.85.5 by John Arbash Meinel
mutter() should not fail because of unicode errors
154
1948.1.3 by John Arbash Meinel
Fix mutter() so even if args are invalid, it still works
155
    def test_trace_argument_utf8(self):
156
        """Write a Unicode argument to the trace log"""
157
        mutter(u'the unicode character for benzene is %s',
158
               u'\N{BENZENE RING}'.encode('utf-8'))
1927.3.4 by Carl Friedrich Bolz
Merge bzr.dev.
159
        self.assertContainsRe(self._get_log(keep_log_file=True),
160
                              'the unicode character')
1948.1.3 by John Arbash Meinel
Fix mutter() so even if args are invalid, it still works
161
1740.5.7 by Martin Pool
Add test for formatting of EPIPE
162
    def test_report_broken_pipe(self):
163
        try:
164
            raise IOError(errno.EPIPE, 'broken pipe foofofo')
165
        except IOError, e:
2123.1.1 by Robert Collins
(Robert Collins) Extend the problem reporting command line UI to use
166
            msg, report = _format_exception()
1740.5.7 by Martin Pool
Add test for formatting of EPIPE
167
            self.assertEquals(msg, "bzr: broken pipe\n")
168
        else:
169
            self.fail("expected error not raised")
1740.5.9 by Martin Pool
[merge] bzr.dev
170
1185.85.5 by John Arbash Meinel
mutter() should not fail because of unicode errors
171
    def test_mutter_never_fails(self):
172
        # Even if the decode/encode stage fails, mutter should not
173
        # raise an exception
174
        mutter(u'Writing a greek mu (\xb5) works in a unicode string')
175
        mutter('But fails in an ascii string \xb5')
1948.1.4 by John Arbash Meinel
Update test_never_fails, to cover one of the failure points
176
        mutter('and in an ascii argument: %s', '\xb5')
1927.3.1 by Carl Friedrich Bolz
Throw away on-disk logfile when possible.
177
        log = self._get_log(keep_log_file=True)
1185.85.5 by John Arbash Meinel
mutter() should not fail because of unicode errors
178
        self.assertContainsRe(log, 'Writing a greek mu')
1948.1.9 by John Arbash Meinel
Change mutter() so that it doesn't try so hard to write out perfect utf8, instead, rather than using a utf8 file, it changes unicode to utf8 manually
179
        self.assertContainsRe(log, "But fails in an ascii string")
180
        self.assertContainsRe(log, u"ascii argument: \xb5")