~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/tests/test_stacktrace.py

Merge bzr-code-imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# the tests to pass.
17
17
MY_LINE_NUMBER = 17
18
18
 
 
19
MY_FILE_NAME = __file__[:__file__.rindex('.py')] + '.py'
 
20
 
19
21
 
20
22
class Supplement:
21
23
    def __init__(self, kwargs):
35
37
    return sys._getframe()
36
38
 
37
39
 
 
40
class BadString:
 
41
 
 
42
    def __str__(self):
 
43
        raise ValueError()
 
44
 
 
45
 
38
46
class TestStacktrace(TestCase):
39
47
 
40
48
    layer = BaseLayer
71
79
    def test_get_frame_data_standard(self):
72
80
        filename, lineno, name, line, modname, supplement, info = (
73
81
            stacktrace._get_frame_data(get_frame(), MY_LINE_NUMBER))
74
 
        self.assertEqual(__file__, filename)
 
82
        self.assertEqual(MY_FILE_NAME, filename)
75
83
        self.assertEqual(MY_LINE_NUMBER, lineno)
76
84
        self.assertEqual('get_frame', name)
77
85
        self.assertStartsWith(line, 'MY_LINE_NUMBER = ')
91
99
                get_frame(supplement={}), MY_LINE_NUMBER))
92
100
        self.assertEqual(
93
101
            dict(source_url=None, line=None, column=None, expression=None,
94
 
                 warnings=(), extra=None),
 
102
                 warnings=[], extra=None),
95
103
            supplement)
96
104
 
97
105
    def test_get_frame_data_supplement_and_info(self):
101
109
                MY_LINE_NUMBER))
102
110
        self.assertEqual(
103
111
            dict(source_url=None, line=None, column=None, expression=None,
104
 
                 warnings=(), extra=None),
 
112
                 warnings=[], extra=None),
105
113
            supplement)
106
114
        self.assertEqual('foo bar', info)
107
115
 
119
127
                    )),
120
128
                MY_LINE_NUMBER))
121
129
        self.assertEqual(
122
 
            dict(source_url='/foo/bar.pt', line=42, column=84,
 
130
            dict(source_url='/foo/bar.pt', line='42', column='84',
123
131
                 expression='tal:define="foo view/foo"',
124
 
                 warnings=('watch out', 'pass auf'),
 
132
                 warnings=['watch out', 'pass auf'],
125
133
                 extra='read all about it'),
126
134
            supplement)
127
135
 
134
142
                MY_LINE_NUMBER))
135
143
        self.assertEqual(
136
144
            dict(source_url=None, line=None, column=None, expression=None,
137
 
                 warnings=(), extra=None),
 
145
                 warnings=[], extra=None),
138
146
            supplement)
139
147
 
140
148
    def test_get_frame_data_supplement_bad_getInfo_with_traceback(self):
153
161
            stacktrace.DEBUG_EXCEPTION_FORMATTER = False
154
162
        self.assertEqual(
155
163
            dict(source_url=None, line=None, column=None, expression=None,
156
 
                 warnings=(), extra=None),
 
164
                 warnings=[], extra=None),
157
165
            supplement)
158
166
        self.assertIn('boo_hiss', stderr.getvalue())
159
167
 
 
168
    def test_get_frame_data_broken_str(self):
 
169
        bad = BadString()
 
170
        filename, lineno, name, line, modname, supplement, info = (
 
171
            stacktrace._get_frame_data(
 
172
                get_frame(
 
173
                    supplement=dict(
 
174
                        source_url=bad,
 
175
                        line=bad,
 
176
                        column=bad,
 
177
                        expression=bad,
 
178
                        warnings=('watch out', bad),
 
179
                        getInfo=lambda: bad
 
180
                    ),
 
181
                    info=bad),
 
182
                MY_LINE_NUMBER))
 
183
        self.assertEqual(
 
184
            dict(source_url=None, line=None, column=None,
 
185
                 expression=None, warnings=['watch out'], extra=None),
 
186
            supplement)
 
187
        self.assertIs(None, info)
 
188
 
 
189
    def test_get_frame_data_broken_warnings(self):
 
190
        filename, lineno, name, line, modname, supplement, info = (
 
191
            stacktrace._get_frame_data(
 
192
                get_frame(
 
193
                    supplement=dict(
 
194
                        warnings=object()
 
195
                    )),
 
196
                MY_LINE_NUMBER))
 
197
        self.assertEqual([], supplement['warnings'])
 
198
 
160
199
    def test_extract_stack(self):
161
200
        extracted = stacktrace.extract_stack(get_frame())
162
201
        self.assertTrue(len(extracted) > 1)
163
202
        filename, lineno, name, line, modname, supplement, info = (
164
203
            extracted[-1])
165
 
        self.assertEqual(__file__, filename)
 
204
        self.assertEqual(MY_FILE_NAME, filename)
166
205
        self.assertIsInstance(lineno, int)
167
206
        self.assertEqual('get_frame', name)
168
207
        self.assertEqual('return sys._getframe()', line)
179
218
        self.assertEqual(1, len(extracted))
180
219
        filename, lineno, name, line, modname, supplement, info = (
181
220
            extracted[0])
182
 
        self.assertEqual(__file__, filename)
 
221
        self.assertEqual(MY_FILE_NAME, filename)
183
222
        self.assertIsInstance(lineno, int)
184
223
        self.assertEqual('test_extract_tb', name)
185
224
        self.assertEqual('raise ValueError()', line)
195
234
            self.assertEndsWith(line, '\n')
196
235
        line = formatted[-1].split('\n')
197
236
        self.assertStartsWith(
198
 
            line[0], '  File "' + __file__ + '", line ')
 
237
            line[0], '  File "' + MY_FILE_NAME + '", line ')
199
238
        self.assertEndsWith(line[0], ', in get_frame')
200
239
        self.assertEqual('    return sys._getframe()', line[1])
201
240
 
218
257
            self.assertEndsWith(line, '\n')
219
258
        line = formatted[-1].split('\n')
220
259
        self.assertStartsWith(
221
 
            line[0], '  File "' + __file__ + '", line ')
 
260
            line[0], '  File "' + MY_FILE_NAME + '", line ')
222
261
        self.assertEndsWith(line[0], ', in get_frame')
223
262
        self.assertEqual('    return sys._getframe()', line[1])
224
263
        self.assertEqual('   - /foo/bar.pt', line[2])
234
273
        self.assertEqual('   - I am the Walrus', line[8])
235
274
 
236
275
    def test_format_list_extra_errors(self):
237
 
        extracted = stacktrace.extract_stack(
238
 
            get_frame(
239
 
                supplement=dict(
240
 
                    # This will cause an error because it is not iterable.
241
 
                    warnings=object()
242
 
                ))
243
 
            )
 
276
        extracted = stacktrace.extract_stack(get_frame(supplement=dict()))
 
277
        extracted[-1][-2]['warnings'] = object()  # This should never happen.
244
278
        stderr = sys.stderr = StringIO.StringIO()
245
279
        self.assertFalse(stacktrace.DEBUG_EXCEPTION_FORMATTER)
246
280
        stacktrace.DEBUG_EXCEPTION_FORMATTER = True