~bzr-pqm/bzr/bzr.dev

1740.5.2 by Martin Pool
Improved tests for display of exceptions.
1
# Copyright (C) 2005, 2006 by 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
1740.5.7 by Martin Pool
Add test for formatting of EPIPE
21
import errno
1185.33.9 by Martin Pool
Add new selftest module.
22
import os
23
import sys
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
24
from StringIO import StringIO
1185.33.9 by Martin Pool
Add new selftest module.
25
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
26
from bzrlib.tests import TestCaseInTempDir, TestCase
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
27
from bzrlib.trace import mutter, report_exception
1740.5.6 by Martin Pool
Clean up many exception classes.
28
from bzrlib.errors import NotBranchError
1185.33.9 by Martin Pool
Add new selftest module.
29
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
30
31
def _format_exception():
32
    """Format an exception as it would normally be displayed to the user"""
33
    buf = StringIO()
34
    report_exception(sys.exc_info(), buf)
35
    return buf.getvalue()
36
37
1185.33.9 by Martin Pool
Add new selftest module.
38
class TestTrace(TestCase):
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
39
1185.33.9 by Martin Pool
Add new selftest module.
40
    def test_format_sys_exception(self):
41
        try:
42
            raise NotImplementedError, "time travel"
43
        except NotImplementedError:
44
            pass
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
45
        err = _format_exception()
46
        self.assertEqualDiff(err.splitlines()[0],
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
47
                'bzr: ERROR: exceptions.NotImplementedError: time travel')
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
48
        self.assertContainsRe(err,
49
                r'File.*test_trace.py')
1185.33.9 by Martin Pool
Add new selftest module.
50
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
51
    def test_format_interrupt_exception(self):
52
        try:
53
            raise KeyboardInterrupt()
1740.5.5 by Martin Pool
Show short form for OSError and IOError too
54
        except KeyboardInterrupt:
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
55
            # XXX: Some risk that a *real* keyboard interrupt won't be seen
56
            pass
57
        msg = _format_exception()
58
        self.assertTrue(len(msg) > 0)
59
        self.assertEqualDiff(msg, 'bzr: interrupted\n')
60
1740.5.5 by Martin Pool
Show short form for OSError and IOError too
61
    def test_format_os_error(self):
62
        try:
63
            file('nosuchfile22222')
64
        except (OSError, IOError):
65
            pass
66
        msg = _format_exception()
67
        self.assertContainsRe(msg, r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
68
69
1185.33.9 by Martin Pool
Add new selftest module.
70
    def test_format_exception(self):
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
71
        """Short formatting of bzr exceptions"""
1185.33.9 by Martin Pool
Add new selftest module.
72
        try:
73
            raise NotBranchError, 'wibble'
74
        except NotBranchError:
75
            pass
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
76
        msg = _format_exception()
77
        self.assertTrue(len(msg) > 0)
78
        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.
79
1185.33.51 by Martin Pool
Fix trace of non-ascii messages, and add test.
80
    def test_trace_unicode(self):
81
        """Write Unicode to trace log"""
82
        self.log(u'the unicode character for benzene is \N{BENZENE RING}')
83
        self.assertContainsRe('the unicode character',
84
                self._get_log())
1185.85.5 by John Arbash Meinel
mutter() should not fail because of unicode errors
85
1740.5.7 by Martin Pool
Add test for formatting of EPIPE
86
    def test_report_broken_pipe(self):
87
        try:
88
            raise IOError(errno.EPIPE, 'broken pipe foofofo')
89
        except IOError, e:
90
            msg = _format_exception()
91
            self.assertEquals(msg, "bzr: broken pipe\n")
92
        else:
93
            self.fail("expected error not raised")
1740.5.9 by Martin Pool
[merge] bzr.dev
94
1185.85.5 by John Arbash Meinel
mutter() should not fail because of unicode errors
95
    def test_mutter_never_fails(self):
96
        # Even if the decode/encode stage fails, mutter should not
97
        # raise an exception
98
        mutter(u'Writing a greek mu (\xb5) works in a unicode string')
99
        mutter('But fails in an ascii string \xb5')
100
        # TODO: jam 20051227 mutter() doesn't flush the log file, and
101
        #       self._get_log() opens the file directly and reads it.
102
        #       So we need to manually flush the log file
103
        import bzrlib.trace
104
        bzrlib.trace._trace_file.flush()
105
        log = self._get_log()
106
        self.assertContainsRe(log, 'Writing a greek mu')
107
        self.assertContainsRe(log, 'UnicodeError')
108
        self.assertContainsRe(log, "'But fails in an ascii string")