/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
#   Authors: Robert Collins <robert.collins@canonical.com>
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
3
#            Martin Pool
1185.33.9 by Martin Pool
Add new selftest module.
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
# "weren't nothing promised to you.  do i look like i got a promise face?"
20
21
"""Tests for trace library"""
22
23
import os
24
import sys
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
25
from StringIO import StringIO
1185.33.9 by Martin Pool
Add new selftest module.
26
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
27
from bzrlib.tests import TestCaseInTempDir, TestCase
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
28
from bzrlib.trace import mutter, report_exception
1740.5.6 by Martin Pool
Clean up many exception classes.
29
from bzrlib.errors import NotBranchError
1185.33.9 by Martin Pool
Add new selftest module.
30
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
31
32
def _format_exception():
33
    """Format an exception as it would normally be displayed to the user"""
34
    buf = StringIO()
35
    report_exception(sys.exc_info(), buf)
36
    return buf.getvalue()
37
38
1185.33.9 by Martin Pool
Add new selftest module.
39
class TestTrace(TestCase):
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
40
1185.33.9 by Martin Pool
Add new selftest module.
41
    def test_format_sys_exception(self):
42
        try:
43
            raise NotImplementedError, "time travel"
44
        except NotImplementedError:
45
            pass
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
46
        err = _format_exception()
47
        self.assertEqualDiff(err.splitlines()[0],
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
48
                'bzr: ERROR: exceptions.NotImplementedError: time travel')
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
49
        self.assertContainsRe(err,
50
                r'File.*test_trace.py')
1185.33.9 by Martin Pool
Add new selftest module.
51
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
52
    def test_format_interrupt_exception(self):
53
        try:
54
            raise KeyboardInterrupt()
1740.5.5 by Martin Pool
Show short form for OSError and IOError too
55
        except KeyboardInterrupt:
1740.5.3 by Martin Pool
Cleanup more exception-formatting code
56
            # XXX: Some risk that a *real* keyboard interrupt won't be seen
57
            pass
58
        msg = _format_exception()
59
        self.assertTrue(len(msg) > 0)
60
        self.assertEqualDiff(msg, 'bzr: interrupted\n')
61
1740.5.5 by Martin Pool
Show short form for OSError and IOError too
62
    def test_format_os_error(self):
63
        try:
64
            file('nosuchfile22222')
65
        except (OSError, IOError):
66
            pass
67
        msg = _format_exception()
68
        self.assertContainsRe(msg, r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
69
70
1185.33.9 by Martin Pool
Add new selftest module.
71
    def test_format_exception(self):
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
72
        """Short formatting of bzr exceptions"""
1185.33.9 by Martin Pool
Add new selftest module.
73
        try:
74
            raise NotBranchError, 'wibble'
75
        except NotBranchError:
76
            pass
1740.5.2 by Martin Pool
Improved tests for display of exceptions.
77
        msg = _format_exception()
78
        self.assertTrue(len(msg) > 0)
79
        self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n')
1185.33.51 by Martin Pool
Fix trace of non-ascii messages, and add test.
80
81
    def test_trace_unicode(self):
82
        """Write Unicode to trace log"""
83
        self.log(u'the unicode character for benzene is \N{BENZENE RING}')
84
        self.assertContainsRe('the unicode character',
85
                self._get_log())