1
# Copyright (C) 2009, 2010 Canonical Ltd
1
# Copyright (C) 2009, 2010, 2011, 2016 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from StringIO import StringIO
19
from io import StringIO
33
from bzrlib.tests import features
31
from . import features
36
34
class TestApportReporting(tests.TestCaseInTempDir):
40
38
def test_apport_report(self):
41
39
crash_dir = osutils.joinpath((self.test_base_dir, 'crash'))
42
40
os.mkdir(crash_dir)
43
os.environ['APPORT_CRASH_DIR'] = crash_dir
44
self.assertEquals(crash_dir, config.crash_dir())
41
self.overrideEnv('APPORT_CRASH_DIR', crash_dir)
42
self.assertEqual(crash_dir, bedding.crash_dir())
45
breezy.get_global_state(),
47
{'example': ['Failed to load plugin foo']})
46
49
stderr = StringIO()
49
52
raise AssertionError("my error")
50
except AssertionError, e:
53
crash_filename = crash.report_bug_to_apport(sys.exc_info(),
53
except AssertionError as e:
54
crash_filename = crash.report_bug_to_apport(sys.exc_info(), stderr)
56
56
# message explaining the crash
57
57
self.assertContainsRe(stderr.getvalue(),
58
" apport-bug %s" % crash_filename)
58
" apport-bug %s" % crash_filename)
60
crash_file = open(crash_filename)
60
with open(crash_filename) as crash_file:
62
61
report = crash_file.read()
66
63
self.assertContainsRe(report,
67
'(?m)^BzrVersion:') # should be in the traceback
64
'(?m)^BrzVersion:') # should be in the traceback
68
65
self.assertContainsRe(report, 'my error')
69
66
self.assertContainsRe(report, 'AssertionError')
70
67
# see https://bugs.launchpad.net/bzr/+bug/528114
72
69
self.assertContainsRe(report, 'test_apport_report')
73
70
# should also be in there
74
71
self.assertContainsRe(report, '(?m)^CommandLine:')
72
self.assertContainsRe(
74
'Failed to load plugin foo')
77
class TestNonApportReporting(tests.TestCase):
78
"""Reporting of crash-type bugs without apport.
80
This should work in all environments.
83
def setup_fake_plugins(self):
84
fake = plugin.PlugIn('fake_plugin', plugin)
85
fake.version_info = lambda: (1, 2, 3)
86
fake_plugins = {"fake_plugin": fake}
87
self.overrideAttr(breezy.get_global_state(), 'plugins', fake_plugins)
89
def test_report_bug_legacy(self):
90
self.setup_fake_plugins()
93
raise AssertionError("my error")
94
except AssertionError as e:
95
crash.report_bug_legacy(sys.exc_info(), err_file)
96
report = err_file.getvalue()
98
"brz: ERROR: AssertionError: my error",
99
r"Traceback \(most recent call last\):",
100
r"plugins: fake_plugin\[1\.2\.3\]",
102
self.assertContainsRe(report, needle)