/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
4584.3.21 by Martin Pool
Start adding tests for apport
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
18
import doctest
19
import os
4584.3.21 by Martin Pool
Start adding tests for apport
20
from StringIO import StringIO
21
import sys
22
5609.23.9 by Martin Pool
Use testtools DocTestMatches rather than our own implementation
23
from testtools.matchers import DocTestMatches
4634.128.18 by Martin Pool
Update apport crash tests
24
25
from bzrlib import (
26
    config,
27
    crash,
5003.2.1 by Vincent Ladeuil
Avoid infinite recursion when probing for apport.
28
    osutils,
5616.7.2 by Martin Pool
Include plugin warnings in apport crash
29
    plugin,
4660.1.1 by Vincent Ladeuil
Let user decide what command is used to run the test suite.
30
    tests,
5003.2.1 by Vincent Ladeuil
Avoid infinite recursion when probing for apport.
31
    )
32
33
from bzrlib.tests import features
34
35
36
class TestApportReporting(tests.TestCaseInTempDir):
37
38
    _test_needs_features = [features.apport]
4584.3.21 by Martin Pool
Start adding tests for apport
39
4634.128.18 by Martin Pool
Update apport crash tests
40
    def test_apport_report(self):
41
        crash_dir = osutils.joinpath((self.test_base_dir, 'crash'))
42
        os.mkdir(crash_dir)
5570.3.9 by Vincent Ladeuil
More use cases for overrideEnv, _cleanEnvironment *may* contain too much variables now.
43
        self.overrideEnv('APPORT_CRASH_DIR', crash_dir)
4634.128.18 by Martin Pool
Update apport crash tests
44
        self.assertEquals(crash_dir, config.crash_dir())
5003.2.1 by Vincent Ladeuil
Avoid infinite recursion when probing for apport.
45
5616.7.2 by Martin Pool
Include plugin warnings in apport crash
46
        self.overrideAttr(
47
            plugin,
48
            'plugin_warnings',
49
            {'example': ['Failed to load plugin foo']})
50
4634.128.18 by Martin Pool
Update apport crash tests
51
        stderr = StringIO()
52
4584.3.21 by Martin Pool
Start adding tests for apport
53
        try:
54
            raise AssertionError("my error")
55
        except AssertionError, e:
56
            pass
4634.128.18 by Martin Pool
Update apport crash tests
57
58
        crash_filename = crash.report_bug_to_apport(sys.exc_info(),
59
            stderr)
60
61
        # message explaining the crash
62
        self.assertContainsRe(stderr.getvalue(),
63
            "    apport-bug %s" % crash_filename)
64
65
        crash_file = open(crash_filename)
66
        try:
67
            report = crash_file.read()
68
        finally:
69
            crash_file.close()
70
71
        self.assertContainsRe(report,
72
            '(?m)^BzrVersion:') # should be in the traceback
73
        self.assertContainsRe(report, 'my error')
74
        self.assertContainsRe(report, 'AssertionError')
4797.33.9 by Martin Pool
Call apport add_proc_info to make sure necessary fields are present
75
        # see https://bugs.launchpad.net/bzr/+bug/528114
76
        self.assertContainsRe(report, 'ExecutablePath')
4634.128.18 by Martin Pool
Update apport crash tests
77
        self.assertContainsRe(report, 'test_apport_report')
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
78
        # should also be in there
4660.1.2 by Vincent Ladeuil
Review feedback, fix typo and more cleanups.
79
        self.assertContainsRe(report, '(?m)^CommandLine:')
5616.7.3 by Martin Pool
Put plugin warnings into both the apport and plain crash report
80
        self.assertContainsRe(
81
            report,
82
            'Failed to load plugin foo')
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
83
84
85
class TestNonApportReporting(tests.TestCase):
86
    """Reporting of crash-type bugs without apport.
87
    
88
    This should work in all environments.
89
    """
90
91
    def setup_fake_plugins(self):
92
        def fake_plugins():
93
            fake = plugin.PlugIn('fake_plugin', plugin)
94
            fake.version_info = lambda: (1, 2, 3)
95
            return {"fake_plugin": fake}
96
        self.overrideAttr(plugin, 'plugins', fake_plugins)
97
98
    def test_report_bug_legacy(self):
99
        self.setup_fake_plugins()
100
        err_file = StringIO()
101
        try:
102
            raise AssertionError("my error")
103
        except AssertionError, e:
104
            pass
105
        crash.report_bug_legacy(sys.exc_info(), err_file)
5609.23.9 by Martin Pool
Use testtools DocTestMatches rather than our own implementation
106
        self.assertThat(
107
            err_file.getvalue(),
108
            DocTestMatches("""\
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
109
bzr: ERROR: exceptions.AssertionError: my error
5609.23.7 by Martin Pool
blank lines in doc test expected output can stand for themselves
110
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
111
Traceback (most recent call last):
112
  ...
113
AssertionError: my error
5609.23.7 by Martin Pool
blank lines in doc test expected output can stand for themselves
114
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
115
bzr ... on python ...
116
arguments: ...
117
plugins: fake_plugin[1.2.3]
118
encoding: ...
5609.23.7 by Martin Pool
blank lines in doc test expected output can stand for themselves
119
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
120
*** Bazaar has encountered an internal error.  This probably indicates a
121
    bug in Bazaar.  You can help us fix it by filing a bug report at
122
        https://bugs.launchpad.net/bzr/+filebug
123
    including this traceback and a description of the problem.
5609.23.9 by Martin Pool
Use testtools DocTestMatches rather than our own implementation
124
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))