##// END OF EJS Templates
Add failing test for issue gh-1456
Thomas Kluyver -
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 super(MyStringIO, self).write(s)
306 super(MyStringIO, self).write(s)
307
307
308 notprinted_msg = """Did not find {0!r} in printed output (on {1}):
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 class AssertPrints(object):
314 class AssertPrints(object):
312 """Context manager for testing that code prints certain text.
315 """Context manager for testing that code prints certain text.
@@ -337,7 +340,13 b' class AssertPrints(object):'
337 printed = self.buffer.getvalue()
340 printed = self.buffer.getvalue()
338 assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
341 assert self.s in printed, notprinted_msg.format(self.s, self.channel, printed)
339 return False
342 return False
340
343
344 printed_msg = """Found {0!r} in printed output (on {1}):
345 -------
346 {2!s}
347 -------
348 """
349
341 class AssertNotPrints(AssertPrints):
350 class AssertNotPrints(AssertPrints):
342 """Context manager for checking that certain output *isn't* produced.
351 """Context manager for checking that certain output *isn't* produced.
343
352
@@ -346,7 +355,7 b' class AssertNotPrints(AssertPrints):'
346 self.tee.flush()
355 self.tee.flush()
347 setattr(sys, self.channel, self.orig_stream)
356 setattr(sys, self.channel, self.orig_stream)
348 printed = self.buffer.getvalue()
357 printed = self.buffer.getvalue()
349 assert self.s not in printed, notprinted_msg.format(self.s, self.channel, printed)
358 assert self.s not in printed, printed_msg.format(self.s, self.channel, printed)
350 return False
359 return False
351
360
352 @contextmanager
361 @contextmanager
General Comments 0
You need to be logged in to leave comments. Login now