~jameinel/bzr/fix-push2

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_trace.py

  • Committer: Aaron Bentley
  • Date: 2006-11-17 05:53:17 UTC
  • mto: This revision was merged to the branch mainline in revision 2147.
  • Revision ID: aaron.bentley@utoronto.ca-20061117055317-1wlrph7uug33p0tv
Revert buggy apport changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
from bzrlib import (
27
27
    errors,
28
 
    plugin,
29
 
    trace,
30
28
    )
31
 
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
 
29
from bzrlib.tests import TestCaseInTempDir, TestCase
32
30
from bzrlib.trace import mutter, report_exception
33
31
 
34
32
 
35
33
def _format_exception():
36
34
    """Format an exception as it would normally be displayed to the user"""
37
35
    buf = StringIO()
38
 
    report = report_exception(sys.exc_info(), buf)
39
 
    return buf.getvalue(), report
 
36
    report_exception(sys.exc_info(), buf)
 
37
    return buf.getvalue()
40
38
 
41
39
 
42
40
class TestTrace(TestCase):
43
41
 
44
 
    def test_format_sys_exception_no_apport(self):
 
42
    def test_format_sys_exception(self):
45
43
        try:
46
44
            raise NotImplementedError, "time travel"
47
45
        except NotImplementedError:
48
46
            pass
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)
 
47
        err = _format_exception()
56
48
        self.assertEqualDiff(err.splitlines()[0],
57
49
                'bzr: ERROR: exceptions.NotImplementedError: time travel')
58
50
        self.assertContainsRe(err,
59
51
                r'File.*test_trace.py')
60
52
 
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
 
 
106
53
    def test_format_interrupt_exception(self):
107
54
        try:
108
55
            raise KeyboardInterrupt()
109
56
        except KeyboardInterrupt:
110
57
            # XXX: Some risk that a *real* keyboard interrupt won't be seen
111
 
            # We can probably detect that by checking for the specific line
112
 
            # that we raise from in the test being in the backtrace.
113
58
            pass
114
 
        msg, report = _format_exception()
 
59
        msg = _format_exception()
115
60
        self.assertTrue(len(msg) > 0)
116
61
        self.assertEqualDiff(msg, 'bzr: interrupted\n')
117
62
 
120
65
            file('nosuchfile22222')
121
66
        except (OSError, IOError):
122
67
            pass
123
 
        msg, report = _format_exception()
 
68
        msg = _format_exception()
124
69
        self.assertContainsRe(msg, r'^bzr: ERROR: \[Errno .*\] No such file.*nosuchfile')
125
70
 
126
71
    def test_format_unicode_error(self):
128
73
            raise errors.BzrCommandError(u'argument foo\xb5 does not exist')
129
74
        except errors.BzrCommandError:
130
75
            pass
131
 
        msg, report = _format_exception()
 
76
        msg = _format_exception()
132
77
 
133
78
    def test_format_exception(self):
134
79
        """Short formatting of bzr exceptions"""
136
81
            raise errors.NotBranchError('wibble')
137
82
        except errors.NotBranchError:
138
83
            pass
139
 
        msg, report = _format_exception()
 
84
        msg = _format_exception()
140
85
        self.assertTrue(len(msg) > 0)
141
86
        self.assertEqualDiff(msg, 'bzr: ERROR: Not a branch: wibble\n')
142
87
 
163
108
        try:
164
109
            raise IOError(errno.EPIPE, 'broken pipe foofofo')
165
110
        except IOError, e:
166
 
            msg, report = _format_exception()
 
111
            msg = _format_exception()
167
112
            self.assertEquals(msg, "bzr: broken pipe\n")
168
113
        else:
169
114
            self.fail("expected error not raised")