##// END OF EJS Templates
[core][tests][run] Remove nose
Samuel Gaist -
Show More
@@ -26,8 +26,7 b' import textwrap'
26 import unittest
26 import unittest
27 from unittest.mock import patch
27 from unittest.mock import patch
28
28
29 import nose.tools as nt
29 import pytest
30 from nose import SkipTest
31
30
32 from IPython.testing import decorators as dec
31 from IPython.testing import decorators as dec
33 from IPython.testing import tools as tt
32 from IPython.testing import tools as tt
@@ -168,13 +167,13 b' class TestMagicRunPass(tt.TempFileMixin):'
168 def setUp(self):
167 def setUp(self):
169 content = "a = [1,2,3]\nb = 1"
168 content = "a = [1,2,3]\nb = 1"
170 self.mktmp(content)
169 self.mktmp(content)
171
170
172 def run_tmpfile(self):
171 def run_tmpfile(self):
173 _ip = get_ipython()
172 _ip = get_ipython()
174 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
173 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
175 # See below and ticket https://bugs.launchpad.net/bugs/366353
174 # See below and ticket https://bugs.launchpad.net/bugs/366353
176 _ip.magic('run %s' % self.fname)
175 _ip.magic('run %s' % self.fname)
177
176
178 def run_tmpfile_p(self):
177 def run_tmpfile_p(self):
179 _ip = get_ipython()
178 _ip = get_ipython()
180 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
179 # This fails on Windows if self.tmpfile.name has spaces or "~" in it.
@@ -188,7 +187,7 b' class TestMagicRunPass(tt.TempFileMixin):'
188 bid1 = id(_ip.user_ns['__builtins__'])
187 bid1 = id(_ip.user_ns['__builtins__'])
189 self.run_tmpfile()
188 self.run_tmpfile()
190 bid2 = id(_ip.user_ns['__builtins__'])
189 bid2 = id(_ip.user_ns['__builtins__'])
191 nt.assert_equal(bid1, bid2)
190 assert bid1 == bid2
192
191
193 def test_builtins_type(self):
192 def test_builtins_type(self):
194 """Check that the type of __builtins__ doesn't change with %run.
193 """Check that the type of __builtins__ doesn't change with %run.
@@ -199,9 +198,9 b' class TestMagicRunPass(tt.TempFileMixin):'
199 """
198 """
200 _ip = get_ipython()
199 _ip = get_ipython()
201 self.run_tmpfile()
200 self.run_tmpfile()
202 nt.assert_equal(type(_ip.user_ns['__builtins__']),type(sys))
201 assert type(_ip.user_ns["__builtins__"]) == type(sys)
203
202
204 def test_run_profile( self ):
203 def test_run_profile(self):
205 """Test that the option -p, which invokes the profiler, do not
204 """Test that the option -p, which invokes the profiler, do not
206 crash by invoking execfile"""
205 crash by invoking execfile"""
207 self.run_tmpfile_p()
206 self.run_tmpfile_p()
@@ -232,9 +231,9 b' class TestMagicRunSimple(tt.TempFileMixin):'
232 src = ("class foo: pass\n"
231 src = ("class foo: pass\n"
233 "def f(): return foo()")
232 "def f(): return foo()")
234 self.mktmp(src)
233 self.mktmp(src)
235 _ip.magic('run %s' % self.fname)
234 _ip.magic("run %s" % self.fname)
236 _ip.run_cell('t = isinstance(f(), foo)')
235 _ip.run_cell("t = isinstance(f(), foo)")
237 nt.assert_true(_ip.user_ns['t'])
236 assert _ip.user_ns["t"] is True
238
237
239 def test_obj_del(self):
238 def test_obj_del(self):
240 """Test that object's __del__ methods are called on exit."""
239 """Test that object's __del__ methods are called on exit."""
@@ -242,7 +241,7 b' class TestMagicRunSimple(tt.TempFileMixin):'
242 try:
241 try:
243 import win32api
242 import win32api
244 except ImportError as e:
243 except ImportError as e:
245 raise SkipTest("Test requires pywin32") from e
244 raise unittest.SkipTest("Test requires pywin32") from e
246 src = ("class A(object):\n"
245 src = ("class A(object):\n"
247 " def __del__(self):\n"
246 " def __del__(self):\n"
248 " print('object A deleted')\n"
247 " print('object A deleted')\n"
@@ -250,42 +249,41 b' class TestMagicRunSimple(tt.TempFileMixin):'
250 self.mktmp(src)
249 self.mktmp(src)
251 err = None
250 err = None
252 tt.ipexec_validate(self.fname, 'object A deleted', err)
251 tt.ipexec_validate(self.fname, 'object A deleted', err)
253
252
254 def test_aggressive_namespace_cleanup(self):
253 def test_aggressive_namespace_cleanup(self):
255 """Test that namespace cleanup is not too aggressive GH-238
254 """Test that namespace cleanup is not too aggressive GH-238
256
255
257 Returning from another run magic deletes the namespace"""
256 Returning from another run magic deletes the namespace"""
258 # see ticket https://github.com/ipython/ipython/issues/238
257 # see ticket https://github.com/ipython/ipython/issues/238
259
258
260 with tt.TempFileMixin() as empty:
259 with tt.TempFileMixin() as empty:
261 empty.mktmp('')
260 empty.mktmp("")
262 # On Windows, the filename will have \users in it, so we need to use the
261 # On Windows, the filename will have \users in it, so we need to use the
263 # repr so that the \u becomes \\u.
262 # repr so that the \u becomes \\u.
264 src = ("ip = get_ipython()\n"
263 src = (
265 "for i in range(5):\n"
264 "ip = get_ipython()\n"
266 " try:\n"
265 "for i in range(5):\n"
267 " ip.magic(%r)\n"
266 " try:\n"
268 " except NameError as e:\n"
267 " ip.magic(%r)\n"
269 " print(i)\n"
268 " except NameError as e:\n"
270 " break\n" % ('run ' + empty.fname))
269 " print(i)\n"
270 " break\n" % ("run " + empty.fname)
271 )
271 self.mktmp(src)
272 self.mktmp(src)
272 _ip.magic('run %s' % self.fname)
273 _ip.magic("run %s" % self.fname)
273 _ip.run_cell('ip == get_ipython()')
274 _ip.run_cell("ip == get_ipython()")
274 nt.assert_equal(_ip.user_ns['i'], 4)
275 assert _ip.user_ns["i"] == 4
275
276
276 def test_run_second(self):
277 def test_run_second(self):
277 """Test that running a second file doesn't clobber the first, gh-3547
278 """Test that running a second file doesn't clobber the first, gh-3547"""
278 """
279 self.mktmp("avar = 1\n" "def afunc():\n" " return avar\n")
279 self.mktmp("avar = 1\n"
280 "def afunc():\n"
281 " return avar\n")
282
280
283 with tt.TempFileMixin() as empty:
281 with tt.TempFileMixin() as empty:
284 empty.mktmp("")
282 empty.mktmp("")
285
283
286 _ip.magic('run %s' % self.fname)
284 _ip.magic("run %s" % self.fname)
287 _ip.magic('run %s' % empty.fname)
285 _ip.magic("run %s" % empty.fname)
288 nt.assert_equal(_ip.user_ns['afunc'](), 1)
286 assert _ip.user_ns["afunc"]() == 1
289
287
290 @dec.skip_win32
288 @dec.skip_win32
291 def test_tclass(self):
289 def test_tclass(self):
@@ -312,24 +310,24 b' tclass.py: deleting object: C-third'
312 self.mktmp(src)
310 self.mktmp(src)
313 _ip.run_cell("zz = 23")
311 _ip.run_cell("zz = 23")
314 try:
312 try:
315 _ip.magic('run -i %s' % self.fname)
313 _ip.magic("run -i %s" % self.fname)
316 nt.assert_equal(_ip.user_ns['yy'], 23)
314 assert _ip.user_ns["yy"] == 23
317 finally:
315 finally:
318 _ip.magic('reset -f')
316 _ip.magic('reset -f')
319
317
320 _ip.run_cell("zz = 23")
318 _ip.run_cell("zz = 23")
321 try:
319 try:
322 _ip.magic('run -i %s' % self.fname)
320 _ip.magic("run -i %s" % self.fname)
323 nt.assert_equal(_ip.user_ns['yy'], 23)
321 assert _ip.user_ns["yy"] == 23
324 finally:
322 finally:
325 _ip.magic('reset -f')
323 _ip.magic('reset -f')
326
324
327 def test_unicode(self):
325 def test_unicode(self):
328 """Check that files in odd encodings are accepted."""
326 """Check that files in odd encodings are accepted."""
329 mydir = os.path.dirname(__file__)
327 mydir = os.path.dirname(__file__)
330 na = os.path.join(mydir, 'nonascii.py')
328 na = os.path.join(mydir, 'nonascii.py')
331 _ip.magic('run "%s"' % na)
329 _ip.magic('run "%s"' % na)
332 nt.assert_equal(_ip.user_ns['u'], u'ΠŽΡ‚β„–Π€')
330 assert _ip.user_ns["u"] == "ΠŽΡ‚β„–Π€"
333
331
334 def test_run_py_file_attribute(self):
332 def test_run_py_file_attribute(self):
335 """Test handling of `__file__` attribute in `%run <file>.py`."""
333 """Test handling of `__file__` attribute in `%run <file>.py`."""
@@ -342,10 +340,10 b' tclass.py: deleting object: C-third'
342
340
343 # Check that __file__ was equal to the filename in the script's
341 # Check that __file__ was equal to the filename in the script's
344 # namespace.
342 # namespace.
345 nt.assert_equal(_ip.user_ns['t'], self.fname)
343 assert _ip.user_ns["t"] == self.fname
346
344
347 # Check that __file__ was not leaked back into user_ns.
345 # Check that __file__ was not leaked back into user_ns.
348 nt.assert_equal(file1, file2)
346 assert file1 == file2
349
347
350 def test_run_ipy_file_attribute(self):
348 def test_run_ipy_file_attribute(self):
351 """Test handling of `__file__` attribute in `%run <file.ipy>`."""
349 """Test handling of `__file__` attribute in `%run <file.ipy>`."""
@@ -358,10 +356,10 b' tclass.py: deleting object: C-third'
358
356
359 # Check that __file__ was equal to the filename in the script's
357 # Check that __file__ was equal to the filename in the script's
360 # namespace.
358 # namespace.
361 nt.assert_equal(_ip.user_ns['t'], self.fname)
359 assert _ip.user_ns["t"] == self.fname
362
360
363 # Check that __file__ was not leaked back into user_ns.
361 # Check that __file__ was not leaked back into user_ns.
364 nt.assert_equal(file1, file2)
362 assert file1 == file2
365
363
366 def test_run_formatting(self):
364 def test_run_formatting(self):
367 """ Test that %run -t -N<N> does not raise a TypeError for N > 1."""
365 """ Test that %run -t -N<N> does not raise a TypeError for N > 1."""
@@ -369,14 +367,14 b' tclass.py: deleting object: C-third'
369 self.mktmp(src)
367 self.mktmp(src)
370 _ip.magic('run -t -N 1 %s' % self.fname)
368 _ip.magic('run -t -N 1 %s' % self.fname)
371 _ip.magic('run -t -N 10 %s' % self.fname)
369 _ip.magic('run -t -N 10 %s' % self.fname)
372
370
373 def test_ignore_sys_exit(self):
371 def test_ignore_sys_exit(self):
374 """Test the -e option to ignore sys.exit()"""
372 """Test the -e option to ignore sys.exit()"""
375 src = "import sys; sys.exit(1)"
373 src = "import sys; sys.exit(1)"
376 self.mktmp(src)
374 self.mktmp(src)
377 with tt.AssertPrints('SystemExit'):
375 with tt.AssertPrints('SystemExit'):
378 _ip.magic('run %s' % self.fname)
376 _ip.magic('run %s' % self.fname)
379
377
380 with tt.AssertNotPrints('SystemExit'):
378 with tt.AssertNotPrints('SystemExit'):
381 _ip.magic('run -e %s' % self.fname)
379 _ip.magic('run -e %s' % self.fname)
382
380
@@ -391,19 +389,19 b' tclass.py: deleting object: C-third'
391 )
389 )
392 src = writes(nb, version=4)
390 src = writes(nb, version=4)
393 self.mktmp(src, ext='.ipynb')
391 self.mktmp(src, ext='.ipynb')
394
392
395 _ip.magic("run %s" % self.fname)
393 _ip.magic("run %s" % self.fname)
396
394
397 nt.assert_equal(_ip.user_ns['answer'], 42)
395 assert _ip.user_ns["answer"] == 42
398
396
399 def test_run_nb_error(self):
397 def test_run_nb_error(self):
400 """Test %run notebook.ipynb error"""
398 """Test %run notebook.ipynb error"""
401 from nbformat import v4, writes
399 from nbformat import v4, writes
402 # %run when a file name isn't provided
400 # %run when a file name isn't provided
403 nt.assert_raises(Exception, _ip.magic, "run")
401 pytest.raises(Exception, _ip.magic, "run")
404
402
405 # %run when a file doesn't exist
403 # %run when a file doesn't exist
406 nt.assert_raises(Exception, _ip.magic, "run foobar.ipynb")
404 pytest.raises(Exception, _ip.magic, "run foobar.ipynb")
407
405
408 # %run on a notebook with an error
406 # %run on a notebook with an error
409 nb = v4.new_notebook(
407 nb = v4.new_notebook(
@@ -413,15 +411,15 b' tclass.py: deleting object: C-third'
413 )
411 )
414 src = writes(nb, version=4)
412 src = writes(nb, version=4)
415 self.mktmp(src, ext='.ipynb')
413 self.mktmp(src, ext='.ipynb')
416 nt.assert_raises(Exception, _ip.magic, "run %s" % self.fname)
414 pytest.raises(Exception, _ip.magic, "run %s" % self.fname)
417
415
418 def test_file_options(self):
416 def test_file_options(self):
419 src = ('import sys\n'
417 src = ('import sys\n'
420 'a = " ".join(sys.argv[1:])\n')
418 'a = " ".join(sys.argv[1:])\n')
421 self.mktmp(src)
419 self.mktmp(src)
422 test_opts = '-x 3 --verbose'
420 test_opts = "-x 3 --verbose"
423 _ip.run_line_magic("run", '{0} {1}'.format(self.fname, test_opts))
421 _ip.run_line_magic("run", "{0} {1}".format(self.fname, test_opts))
424 nt.assert_equal(_ip.user_ns['a'], test_opts)
422 assert _ip.user_ns["a"] == test_opts
425
423
426
424
427 class TestMagicRunWithPackage(unittest.TestCase):
425 class TestMagicRunWithPackage(unittest.TestCase):
@@ -500,33 +498,34 b' class TestMagicRunWithPackage(unittest.TestCase):'
500 self.check_run_submodule('relative', '-d')
498 self.check_run_submodule('relative', '-d')
501
499
502 def test_module_options(self):
500 def test_module_options(self):
503 _ip.user_ns.pop('a', None)
501 _ip.user_ns.pop("a", None)
504 test_opts = '-x abc -m test'
502 test_opts = "-x abc -m test"
505 _ip.run_line_magic('run', '-m {0}.args {1}'.format(self.package, test_opts))
503 _ip.run_line_magic("run", "-m {0}.args {1}".format(self.package, test_opts))
506 nt.assert_equal(_ip.user_ns['a'], test_opts)
504 assert _ip.user_ns["a"] == test_opts
507
505
508 def test_module_options_with_separator(self):
506 def test_module_options_with_separator(self):
509 _ip.user_ns.pop('a', None)
507 _ip.user_ns.pop("a", None)
510 test_opts = '-x abc -m test'
508 test_opts = "-x abc -m test"
511 _ip.run_line_magic('run', '-m {0}.args -- {1}'.format(self.package, test_opts))
509 _ip.run_line_magic("run", "-m {0}.args -- {1}".format(self.package, test_opts))
512 nt.assert_equal(_ip.user_ns['a'], test_opts)
510 assert _ip.user_ns["a"] == test_opts
511
513
512
514 def test_run__name__():
513 def test_run__name__():
515 with TemporaryDirectory() as td:
514 with TemporaryDirectory() as td:
516 path = pjoin(td, 'foo.py')
515 path = pjoin(td, 'foo.py')
517 with open(path, 'w') as f:
516 with open(path, 'w') as f:
518 f.write("q = __name__")
517 f.write("q = __name__")
519
518
520 _ip.user_ns.pop('q', None)
519 _ip.user_ns.pop("q", None)
521 _ip.magic('run {}'.format(path))
520 _ip.magic("run {}".format(path))
522 nt.assert_equal(_ip.user_ns.pop('q'), '__main__')
521 assert _ip.user_ns.pop("q") == "__main__"
523
522
524 _ip.magic('run -n {}'.format(path))
523 _ip.magic("run -n {}".format(path))
525 nt.assert_equal(_ip.user_ns.pop('q'), 'foo')
524 assert _ip.user_ns.pop("q") == "foo"
526
525
527 try:
526 try:
528 _ip.magic('run -i -n {}'.format(path))
527 _ip.magic("run -i -n {}".format(path))
529 nt.assert_equal(_ip.user_ns.pop('q'), 'foo')
528 assert _ip.user_ns.pop("q") == "foo"
530 finally:
529 finally:
531 _ip.magic('reset -f')
530 _ip.magic('reset -f')
532
531
@@ -546,9 +545,9 b' def test_run_tb():'
546 with capture_output() as io:
545 with capture_output() as io:
547 _ip.magic('run {}'.format(path))
546 _ip.magic('run {}'.format(path))
548 out = io.stdout
547 out = io.stdout
549 nt.assert_not_in("execfile", out)
548 assert "execfile" not in out
550 nt.assert_in("RuntimeError", out)
549 assert "RuntimeError" in out
551 nt.assert_equal(out.count("---->"), 3)
550 assert out.count("---->") == 3
552 del ip.user_ns['bar']
551 del ip.user_ns['bar']
553 del ip.user_ns['foo']
552 del ip.user_ns['foo']
554
553
@@ -572,10 +571,10 b' def test_multiprocessing_run():'
572 _ip.run_line_magic('run', path)
571 _ip.run_line_magic('run', path)
573 _ip.run_cell("i_m_undefined")
572 _ip.run_cell("i_m_undefined")
574 out = io.stdout
573 out = io.stdout
575 nt.assert_in("hoy", out)
574 assert "hoy" in out
576 nt.assert_not_in("AttributeError", out)
575 assert "AttributeError" not in out
577 nt.assert_in("NameError", out)
576 assert "NameError" in out
578 nt.assert_equal(out.count("---->"), 1)
577 assert out.count("---->") == 1
579 except:
578 except:
580 raise
579 raise
581 finally:
580 finally:
@@ -595,7 +594,6 b' def test_script_tb():'
595 "foo()",
594 "foo()",
596 ]))
595 ]))
597 out, err = tt.ipexec(path)
596 out, err = tt.ipexec(path)
598 nt.assert_not_in("execfile", out)
597 assert "execfile" not in out
599 nt.assert_in("RuntimeError", out)
598 assert "RuntimeError" in out
600 nt.assert_equal(out.count("---->"), 3)
599 assert out.count("---->") == 3
601
General Comments 0
You need to be logged in to leave comments. Login now