Show More
@@ -0,0 +1,51 b'' | |||
|
1 | """Tests for IPython.core.ultratb | |
|
2 | """ | |
|
3 | ||
|
4 | import os.path | |
|
5 | import unittest | |
|
6 | ||
|
7 | from IPython.testing import tools as tt | |
|
8 | from IPython.utils.syspathcontext import prepended_to_syspath | |
|
9 | from IPython.utils.tempdir import TemporaryDirectory | |
|
10 | ||
|
11 | ip = get_ipython() | |
|
12 | ||
|
13 | file_1 = """1 | |
|
14 | 2 | |
|
15 | 3 | |
|
16 | def f(): | |
|
17 | 1/0 | |
|
18 | """ | |
|
19 | ||
|
20 | file_2 = """def f(): | |
|
21 | 1/0 | |
|
22 | """ | |
|
23 | ||
|
24 | class ChangedPyFileTest(unittest.TestCase): | |
|
25 | def test_changing_py_file(self): | |
|
26 | """Traceback produced if the line where the error occurred is missing? | |
|
27 | ||
|
28 | https://github.com/ipython/ipython/issues/1456 | |
|
29 | """ | |
|
30 | with TemporaryDirectory() as td: | |
|
31 | fname = os.path.join(td, "foo.py") | |
|
32 | with open(fname, "w") as f: | |
|
33 | f.write(file_1) | |
|
34 | ||
|
35 | with prepended_to_syspath(td): | |
|
36 | ip.run_cell("import foo") | |
|
37 | ||
|
38 | with tt.AssertPrints("ZeroDivisionError"): | |
|
39 | ip.run_cell("foo.f()") | |
|
40 | ||
|
41 | # Make the file shorter, so the line of the error is missing. | |
|
42 | with open(fname, "w") as f: | |
|
43 | f.write(file_2) | |
|
44 | ||
|
45 | # For some reason, this was failing on the *second* call after | |
|
46 | # changing the file, so we call f() twice. | |
|
47 | with tt.AssertNotPrints("Internal Python error", channel='stderr'): | |
|
48 | with tt.AssertPrints("ZeroDivisionError"): | |
|
49 | ip.run_cell("foo.f()") | |
|
50 | with tt.AssertPrints("ZeroDivisionError"): | |
|
51 | ip.run_cell("foo.f()") |
@@ -306,7 +306,10 b' else:' | |||
|
306 | 306 | super(MyStringIO, self).write(s) |
|
307 | 307 | |
|
308 | 308 | notprinted_msg = """Did not find {0!r} in printed output (on {1}): |
|
309 | {2!r}""" | |
|
309 | ------- | |
|
310 | {2!s} | |
|
311 | ------- | |
|
312 | """ | |
|
310 | 313 | |
|
311 | 314 | class AssertPrints(object): |
|
312 | 315 | """Context manager for testing that code prints certain text. |
@@ -338,6 +341,12 b' class AssertPrints(object):' | |||
|
338 | 341 | assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed) |
|
339 | 342 | return False |
|
340 | 343 | |
|
344 | printed_msg = """Found {0!r} in printed output (on {1}): | |
|
345 | ------- | |
|
346 | {2!s} | |
|
347 | ------- | |
|
348 | """ | |
|
349 | ||
|
341 | 350 | class AssertNotPrints(AssertPrints): |
|
342 | 351 | """Context manager for checking that certain output *isn't* produced. |
|
343 | 352 | |
@@ -346,7 +355,7 b' class AssertNotPrints(AssertPrints):' | |||
|
346 | 355 | self.tee.flush() |
|
347 | 356 | setattr(sys, self.channel, self.orig_stream) |
|
348 | 357 | printed = self.buffer.getvalue() |
|
349 |
assert self.s not in printed, |
|
|
358 | assert self.s not in printed, printed_msg.format(self.s, self.channel, printed) | |
|
350 | 359 | return False |
|
351 | 360 | |
|
352 | 361 | @contextmanager |
General Comments 0
You need to be logged in to leave comments.
Login now