/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2009, 2010, 2011, 2016 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
import sys
21
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
22
import breezy
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
23
from .. import (
4634.128.18 by Martin Pool
Update apport crash tests
24
    config,
25
    crash,
5003.2.1 by Vincent Ladeuil
Avoid infinite recursion when probing for apport.
26
    osutils,
5616.7.2 by Martin Pool
Include plugin warnings in apport crash
27
    plugin,
4660.1.1 by Vincent Ladeuil
Let user decide what command is used to run the test suite.
28
    tests,
5003.2.1 by Vincent Ladeuil
Avoid infinite recursion when probing for apport.
29
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
30
from ..sixish import (
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
31
    StringIO,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
32
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
33
from . import features
5003.2.1 by Vincent Ladeuil
Avoid infinite recursion when probing for apport.
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)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
44
        self.assertEqual(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(
6759.4.2 by Jelmer Vernooij
Use get_global_state>
47
            breezy.get_global_state(),
5616.7.2 by Martin Pool
Include plugin warnings in apport crash
48
            'plugin_warnings',
49
            {'example': ['Failed to load plugin foo']})
50
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
51
        stderr = StringIO()
4634.128.18 by Martin Pool
Update apport crash tests
52
4584.3.21 by Martin Pool
Start adding tests for apport
53
        try:
54
            raise AssertionError("my error")
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
55
        except AssertionError as e:
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
56
            crash_filename = crash.report_bug_to_apport(sys.exc_info(), stderr)
4634.128.18 by Martin Pool
Update apport crash tests
57
58
        # message explaining the crash
59
        self.assertContainsRe(stderr.getvalue(),
60
            "    apport-bug %s" % crash_filename)
61
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
62
        with open(crash_filename) as crash_file:
4634.128.18 by Martin Pool
Update apport crash tests
63
            report = crash_file.read()
64
65
        self.assertContainsRe(report,
6681.2.1 by Jelmer Vernooij
Rename more uses of bzrdir to controldir.
66
            '(?m)^BrzVersion:') # should be in the traceback
4634.128.18 by Martin Pool
Update apport crash tests
67
        self.assertContainsRe(report, 'my error')
68
        self.assertContainsRe(report, 'AssertionError')
4797.33.9 by Martin Pool
Call apport add_proc_info to make sure necessary fields are present
69
        # see https://bugs.launchpad.net/bzr/+bug/528114
70
        self.assertContainsRe(report, 'ExecutablePath')
4634.128.18 by Martin Pool
Update apport crash tests
71
        self.assertContainsRe(report, 'test_apport_report')
4584.3.22 by Martin Pool
further tweaks to and tests of bzr apport reporting
72
        # should also be in there
4660.1.2 by Vincent Ladeuil
Review feedback, fix typo and more cleanups.
73
        self.assertContainsRe(report, '(?m)^CommandLine:')
5616.7.3 by Martin Pool
Put plugin warnings into both the apport and plain crash report
74
        self.assertContainsRe(
75
            report,
76
            'Failed to load plugin foo')
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
77
78
79
class TestNonApportReporting(tests.TestCase):
80
    """Reporting of crash-type bugs without apport.
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
81
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
82
    This should work in all environments.
83
    """
84
85
    def setup_fake_plugins(self):
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
86
        fake = plugin.PlugIn('fake_plugin', plugin)
87
        fake.version_info = lambda: (1, 2, 3)
88
        fake_plugins = {"fake_plugin": fake}
6759.4.2 by Jelmer Vernooij
Use get_global_state>
89
        self.overrideAttr(breezy.get_global_state(), 'plugins', fake_plugins)
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
90
91
    def test_report_bug_legacy(self):
92
        self.setup_fake_plugins()
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
93
        err_file = StringIO()
5609.23.6 by Martin Pool
Show concise list of plugins in non-apport crash; add test for this
94
        try:
95
            raise AssertionError("my error")
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
96
        except AssertionError as e:
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
97
            crash.report_bug_legacy(sys.exc_info(), err_file)
5784.2.1 by Martin Pool
Rewrite test_report_bug_legacy away from using doctest (see bug 764188)
98
        report = err_file.getvalue()
99
        for needle in [
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
100
            "brz: ERROR: AssertionError: my error",
5784.2.1 by Martin Pool
Rewrite test_report_bug_legacy away from using doctest (see bug 764188)
101
            r"Traceback \(most recent call last\):",
102
            r"plugins: fake_plugin\[1\.2\.3\]",
103
            ]:
6677.1.5 by Martin
Make crash debug and trace modules pass on Python 3
104
            self.assertContainsRe(report, needle)