##// END OF EJS Templates
Merge pull request #13750 from Carreau/sec-2...
Merge pull request #13750 from Carreau/sec-2 SEC: force workflows to be read-only.

File last commit:

r27548:6d40fd89 merge
r27791:4f6e132d merge
Show More
test_run.py
622 lines | 18.6 KiB | text/x-python | PythonLexer
Thomas Kluyver
Add encoding cookie to test_run.
r6303 # encoding: utf-8
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 """Tests for code execution (%run and related), which is particularly tricky.
Because of how %run manages namespaces, and the fact that we are trying here to
verify subtle object deletion and reference counting issues, the %run tests
will be kept in this separate file. This makes it easier to aggregate in one
place the tricks needed to handle it; most other magics are much easier to test
and we do so in a common test_magic file.
Craig Citro
Always reset after `%run -i` in tests....
r23967
Note that any test using `run -i` should make sure to do a `reset` afterwards,
as otherwise it may influence later tests.
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 """
MinRK
Don't use nbformat.current in core
r18604
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
Takafumi Arakaki
Sort imports in test_run.py
r10553 import functools
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 import os
Nikita Kniazev
Tweak tests for PyPy and add CI runner...
r27235 import platform
Takafumi Arakaki
Sort imports in test_run.py
r10553 import random
Matthias Bussonnier
Fix random test failure....
r23899 import string
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 import sys
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994 import textwrap
Takafumi Arakaki
Sort imports in test_run.py
r10553 import unittest
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509 from os.path import join as pjoin
Srinivas Reddy Thatiparthy
remove python2 specific code
r23064 from unittest.mock import patch
Min RK
remove testing.tools.monkeypatch...
r21116
Samuel Gaist
[core][tests][run] Remove nose
r26908 import pytest
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509 from tempfile import TemporaryDirectory
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509 from IPython.core import debugger
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 from IPython.testing import decorators as dec
from IPython.testing import tools as tt
MinRK
test traceback offset for %run and script
r14747 from IPython.utils.io import capture_output
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
def doctest_refbug():
"""Very nasty problem with references held by multiple runs of a script.
Thomas Kluyver
Replace links to launchpad bugs in comments/docstrings with equivalent github links.
r3917 See: https://github.com/ipython/ipython/issues/141
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
In [1]: _ip.clear_main_mod_cache()
# random
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 In [2]: %run refbug
In [3]: call_f()
lowercased: hello
In [4]: %run refbug
In [5]: call_f()
lowercased: hello
lowercased: hello
"""
def doctest_run_builtins():
r"""Check that %run doesn't damage __builtins__.
In [1]: import tempfile
In [2]: bid1 = id(__builtins__)
In [3]: fname = tempfile.mkstemp('.py')[1]
gousaiyang
Fix EncodingWarning on Python 3.10
r27494 In [3]: f = open(fname, 'w', encoding='utf-8')
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
Thomas Kluyver
Fix various tests in IPython.core for Python 3.
r4895 In [4]: dummy= f.write('pass\n')
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
In [5]: f.flush()
In [6]: t1 = type(__builtins__)
Fernando Perez
Do not call %run with quotes, since on Windows quotes aren't stripped....
r2484 In [7]: %run $fname
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
In [7]: f.close()
In [8]: bid2 = id(__builtins__)
In [9]: t2 = type(__builtins__)
In [10]: t1 == t2
Out[10]: True
In [10]: bid1 == bid2
Out[10]: True
In [12]: try:
....: os.unlink(fname)
....: except:
....: pass
Bernardo B. Marques
remove all trailling spaces
r4872 ....:
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 """
Fernando Perez
Complete fix for __del__ errors with .reset(), add unit test.
r3107
Takafumi Arakaki
Add tests for %run option parser
r8121
def doctest_run_option_parser():
r"""Test option parser in %run.
In [1]: %run print_argv.py
[]
In [2]: %run print_argv.py print*.py
['print_argv.py']
Takafumi Arakaki
Fix doctest_run_option_parser for Windows...
r8548 In [3]: %run -G print_argv.py print*.py
Takafumi Arakaki
Add tests for %run option parser
r8121 ['print*.py']
Takafumi Arakaki
Fix doctest_run_option_parser for Windows...
r8548 """
@dec.skip_win32
def doctest_run_option_parser_for_posix():
r"""Test option parser in %run (Linux/OSX specific).
You need double quote to escape glob in POSIX systems:
In [1]: %run print_argv.py print\\*.py
['print*.py']
You can't use quote to escape glob in POSIX systems:
In [2]: %run print_argv.py 'print*.py'
Takafumi Arakaki
Add tests for %run option parser
r8121 ['print_argv.py']
Takafumi Arakaki
Fix doctest_run_option_parser for Windows...
r8548 """
Nikita Kniazev
Remove iptest and other Nose-dependent code
r27042 doctest_run_option_parser_for_posix.__skip_doctest__ = sys.platform == "win32"
Takafumi Arakaki
Use skip_if_not_win32 instead of skip_linux+osx
r8636 @dec.skip_if_not_win32
Takafumi Arakaki
Fix doctest_run_option_parser for Windows...
r8548 def doctest_run_option_parser_for_windows():
r"""Test option parser in %run (Windows specific).
In Windows, you can't escape ``*` `by backslash:
In [1]: %run print_argv.py print\\*.py
Nikita Kniazev
Remove iptest and other Nose-dependent code
r27042 ['print\\\\*.py']
Takafumi Arakaki
Fix doctest_run_option_parser for Windows...
r8548
You can use quote to escape glob:
In [2]: %run print_argv.py 'print*.py'
Nikita Kniazev
Remove iptest and other Nose-dependent code
r27042 ["'print*.py'"]
Takafumi Arakaki
Add tests for %run option parser
r8121
"""
Nikita Kniazev
Remove iptest and other Nose-dependent code
r27042 doctest_run_option_parser_for_windows.__skip_doctest__ = sys.platform != "win32"
Fernando Perez
Complete fix for __del__ errors with .reset(), add unit test.
r3107 def doctest_reset_del():
"""Test that resetting doesn't cause errors in __del__ methods.
In [2]: class A(object):
...: def __del__(self):
Matthias Bussonnier
remove some py3compat usage
r24265 ...: print(str("Hi"))
Bernardo B. Marques
remove all trailling spaces
r4872 ...:
Fernando Perez
Complete fix for __del__ errors with .reset(), add unit test.
r3107
In [3]: a = A()
Nikita Kniazev
Tweak tests for PyPy and add CI runner...
r27235 In [4]: get_ipython().reset(); import gc; x = gc.collect(0)
Thomas Kluyver
Make test work (i.e. it now fails, as expected).
r3156 Hi
Fernando Perez
Complete fix for __del__ errors with .reset(), add unit test.
r3107
In [5]: 1+1
Out[5]: 2
"""
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
# For some tests, it will be handy to organize them in a class with a common
# setup that makes a temp file
Fernando Perez
Fix extensions test suite (small, but now it runs and passes!)
r2415 class TestMagicRunPass(tt.TempFileMixin):
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
Matthias Bussonnier
Fix Mixin Inheritence in Xunit unittest....
r25113 def setUp(self):
Ritesh Kadmawala
fix: Clear breakpoints before running any script with debugger
r23799 content = "a = [1,2,3]\nb = 1"
self.mktmp(content)
Samuel Gaist
[core][tests][run] Remove nose
r26908
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 def run_tmpfile(self):
_ip = get_ipython()
# This fails on Windows if self.tmpfile.name has spaces or "~" in it.
# See below and ticket https://bugs.launchpad.net/bugs/366353
Fernando Perez
Do not call %run with quotes, since on Windows quotes aren't stripped....
r2484 _ip.magic('run %s' % self.fname)
Samuel Gaist
[core][tests][run] Remove nose
r26908
Alcides
- Test case correctly fails....
r5238 def run_tmpfile_p(self):
_ip = get_ipython()
# This fails on Windows if self.tmpfile.name has spaces or "~" in it.
# See below and ticket https://bugs.launchpad.net/bugs/366353
_ip.magic('run -p %s' % self.fname)
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
def test_builtins_id(self):
"""Check that %run doesn't damage __builtins__ """
_ip = get_ipython()
# Test that the id of __builtins__ is not modified by %run
bid1 = id(_ip.user_ns['__builtins__'])
self.run_tmpfile()
bid2 = id(_ip.user_ns['__builtins__'])
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert bid1 == bid2
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
def test_builtins_type(self):
"""Check that the type of __builtins__ doesn't change with %run.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 However, the above could pass if __builtins__ was already modified to
be a dict (it should be a module) by a previous use of %run. So we
also check explicitly that it really is a module:
"""
_ip = get_ipython()
self.run_tmpfile()
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert type(_ip.user_ns["__builtins__"]) == type(sys)
def test_run_profile(self):
Alcides
- Test case correctly fails....
r5238 """Test that the option -p, which invokes the profiler, do not
crash by invoking execfile"""
self.run_tmpfile_p()
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
Thomas Kluyver
Add failing test for running %run -d twice
r22983 def test_run_debug_twice(self):
# https://github.com/ipython/ipython/issues/10028
_ip = get_ipython()
with tt.fake_input(['c']):
_ip.magic('run -d %s' % self.fname)
with tt.fake_input(['c']):
_ip.magic('run -d %s' % self.fname)
Ritesh Kadmawala
fix: Clear breakpoints before running any script with debugger
r23799 def test_run_debug_twice_with_breakpoint(self):
"""Make a valid python temp file."""
_ip = get_ipython()
with tt.fake_input(['b 2', 'c', 'c']):
_ip.magic('run -d %s' % self.fname)
with tt.fake_input(['c']):
with tt.AssertNotPrints('KeyError'):
_ip.magic('run -d %s' % self.fname)
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
Fernando Perez
Fix extensions test suite (small, but now it runs and passes!)
r2415 class TestMagicRunSimple(tt.TempFileMixin):
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
def test_simpledef(self):
"""Test that simple class definitions work."""
src = ("class foo: pass\n"
"def f(): return foo()")
self.mktmp(src)
Samuel Gaist
[core][tests][run] Remove nose
r26908 _ip.magic("run %s" % self.fname)
_ip.run_cell("t = isinstance(f(), foo)")
assert _ip.user_ns["t"] is True
Fernando Perez
Various fixes for IPython.core tests to pass under win32.
r2446
Nikita Kniazev
Tweak tests for PyPy and add CI runner...
r27235 @pytest.mark.xfail(
platform.python_implementation() == "PyPy",
reason="expecting __del__ call on exit is unreliable and doesn't happen on PyPy",
)
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 def test_obj_del(self):
"""Test that object's __del__ methods are called on exit."""
src = ("class A(object):\n"
" def __del__(self):\n"
Matthias Bussonnier
remove some py3compat usage
r24265 " print('object A deleted')\n"
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 "a = A()\n")
Matthias Bussonnier
remove some py3compat usage
r24265 self.mktmp(src)
Terry Davis
Assume sqlite3 always available in testing.
r25524 err = None
MinRK
Allow IPython to run without sqlite3...
r5147 tt.ipexec_validate(self.fname, 'object A deleted', err)
Samuel Gaist
[core][tests][run] Remove nose
r26908
Paul Ivanov
test for GH-238 %run's aggressive name cleaning
r3502 def test_aggressive_namespace_cleanup(self):
"""Test that namespace cleanup is not too aggressive GH-238
Paul Ivanov
added the skip_known decorator
r3503
Returning from another run magic deletes the namespace"""
Paul Ivanov
test for GH-238 %run's aggressive name cleaning
r3502 # see ticket https://github.com/ipython/ipython/issues/238
Samuel Gaist
[core][tests][run] Remove nose
r26908
Matthias Bussonnier
Make a few test non-optional....
r22379 with tt.TempFileMixin() as empty:
Samuel Gaist
[core][tests][run] Remove nose
r26908 empty.mktmp("")
Matthias Bussonnier
Make a few test non-optional....
r22379 # On Windows, the filename will have \users in it, so we need to use the
# repr so that the \u becomes \\u.
Samuel Gaist
[core][tests][run] Remove nose
r26908 src = (
"ip = get_ipython()\n"
"for i in range(5):\n"
" try:\n"
" ip.magic(%r)\n"
" except NameError as e:\n"
" print(i)\n"
" break\n" % ("run " + empty.fname)
)
Matthias Bussonnier
Make a few test non-optional....
r22379 self.mktmp(src)
Samuel Gaist
[core][tests][run] Remove nose
r26908 _ip.magic("run %s" % self.fname)
_ip.run_cell("ip == get_ipython()")
assert _ip.user_ns["i"] == 4
Thomas Kluyver
Add failing test for gh-3547
r11204 def test_run_second(self):
Samuel Gaist
[core][tests][run] Remove nose
r26908 """Test that running a second file doesn't clobber the first, gh-3547"""
self.mktmp("avar = 1\n" "def afunc():\n" " return avar\n")
Thomas Kluyver
Add failing test for gh-3547
r11204
Matthias Bussonnier
Make a few test non-optional....
r22379 with tt.TempFileMixin() as empty:
empty.mktmp("")
Samuel Gaist
[core][tests][run] Remove nose
r26908
_ip.magic("run %s" % self.fname)
_ip.magic("run %s" % empty.fname)
assert _ip.user_ns["afunc"]() == 1
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414
def test_tclass(self):
mydir = os.path.dirname(__file__)
Nikita Kniazev
Tweak tests for PyPy and add CI runner...
r27235 tc = os.path.join(mydir, "tclass")
src = f"""\
import gc
%run "{tc}" C-first
gc.collect(0)
%run "{tc}" C-second
gc.collect(0)
%run "{tc}" C-third
gc.collect(0)
%reset -f
"""
self.mktmp(src, ".ipy")
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 out = """\
MinRK
fix tests that depended on unicode sys.argv
r5126 ARGV 1-: ['C-first']
ARGV 1-: ['C-second']
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 tclass.py: deleting object: C-first
MinRK
fix tests that depended on unicode sys.argv
r5126 ARGV 1-: ['C-third']
Thomas Kluyver
Shell's reset method clears namespace from last %run command.
r3762 tclass.py: deleting object: C-second
Thomas Kluyver
Make test of %run more rigorous.
r3763 tclass.py: deleting object: C-third
Fernando Perez
Massive amount of work to improve the test suite, restores doctests....
r2414 """
Terry Davis
Assume sqlite3 always available in testing.
r25524 err = None
MinRK
Allow IPython to run without sqlite3...
r5147 tt.ipexec_validate(self.fname, out, err)
Thomas Kluyver
Add test for gh-693 (%run -i after %reset).
r5466
def test_run_i_after_reset(self):
"""Check that %run -i still works after %reset (gh-693)"""
src = "yy = zz\n"
self.mktmp(src)
_ip.run_cell("zz = 23")
Craig Citro
Always reset after `%run -i` in tests....
r23967 try:
Samuel Gaist
[core][tests][run] Remove nose
r26908 _ip.magic("run -i %s" % self.fname)
assert _ip.user_ns["yy"] == 23
Craig Citro
Always reset after `%run -i` in tests....
r23967 finally:
_ip.magic('reset -f')
Samuel Gaist
[core][tests][run] Remove nose
r26908
Thomas Kluyver
Add test for gh-693 (%run -i after %reset).
r5466 _ip.run_cell("zz = 23")
Craig Citro
Always reset after `%run -i` in tests....
r23967 try:
Samuel Gaist
[core][tests][run] Remove nose
r26908 _ip.magic("run -i %s" % self.fname)
assert _ip.user_ns["yy"] == 23
Craig Citro
Always reset after `%run -i` in tests....
r23967 finally:
_ip.magic('reset -f')
Samuel Gaist
[core][tests][run] Remove nose
r26908
Thomas Kluyver
Fix for %run on a Python file using non-default encoding.
r6300 def test_unicode(self):
"""Check that files in odd encodings are accepted."""
mydir = os.path.dirname(__file__)
na = os.path.join(mydir, 'nonascii.py')
Jonathan March
Fix Windows pathname issue in 'odd encoding' test....
r7674 _ip.magic('run "%s"' % na)
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert _ip.user_ns["u"] == "Ўт№Ф"
Bradley M. Froehle
Add test for `__file__` behavior in `%run`.
r8530
def test_run_py_file_attribute(self):
"""Test handling of `__file__` attribute in `%run <file>.py`."""
src = "t = __file__\n"
self.mktmp(src)
_missing = object()
file1 = _ip.user_ns.get('__file__', _missing)
_ip.magic('run %s' % self.fname)
file2 = _ip.user_ns.get('__file__', _missing)
# Check that __file__ was equal to the filename in the script's
# namespace.
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert _ip.user_ns["t"] == self.fname
Bradley M. Froehle
Add test for `__file__` behavior in `%run`.
r8530
# Check that __file__ was not leaked back into user_ns.
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert file1 == file2
Bradley M. Froehle
Add test for `__file__` behavior in `%run`.
r8530
def test_run_ipy_file_attribute(self):
"""Test handling of `__file__` attribute in `%run <file.ipy>`."""
src = "t = __file__\n"
self.mktmp(src, ext='.ipy')
_missing = object()
file1 = _ip.user_ns.get('__file__', _missing)
_ip.magic('run %s' % self.fname)
file2 = _ip.user_ns.get('__file__', _missing)
# Check that __file__ was equal to the filename in the script's
# namespace.
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert _ip.user_ns["t"] == self.fname
Bradley M. Froehle
Add test for `__file__` behavior in `%run`.
r8530
# Check that __file__ was not leaked back into user_ns.
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert file1 == file2
Robert Marchman
rlmv - Added test for issue #2784...
r9360
def test_run_formatting(self):
""" Test that %run -t -N<N> does not raise a TypeError for N > 1."""
src = "pass"
self.mktmp(src)
_ip.magic('run -t -N 1 %s' % self.fname)
_ip.magic('run -t -N 10 %s' % self.fname)
Samuel Gaist
[core][tests][run] Remove nose
r26908
Thomas Kluyver
Add a couple more tests for %run options
r12568 def test_ignore_sys_exit(self):
"""Test the -e option to ignore sys.exit()"""
src = "import sys; sys.exit(1)"
self.mktmp(src)
with tt.AssertPrints('SystemExit'):
_ip.magic('run %s' % self.fname)
Samuel Gaist
[core][tests][run] Remove nose
r26908
Thomas Kluyver
Add a couple more tests for %run options
r12568 with tt.AssertNotPrints('SystemExit'):
_ip.magic('run -e %s' % self.fname)
Thomas Kluyver
Skip some tests if nbformat not importable...
r16982
MinRK
test %run notebook.ipynb
r13646 def test_run_nb(self):
"""Test %run notebook.ipynb"""
Nikita Kniazev
Move optional dependencies to a separate set
r27104 pytest.importorskip("nbformat")
Min RK
jupyter_nbformat is now nbformat
r21345 from nbformat import v4, writes
MinRK
Don't use nbformat.current in core
r18604 nb = v4.new_notebook(
MinRK
update IPython.core to nbformat 4...
r18585 cells=[
MinRK
Don't use nbformat.current in core
r18604 v4.new_markdown_cell("The Ultimate Question of Everything"),
v4.new_code_cell("answer=42")
MinRK
test %run notebook.ipynb
r13646 ]
)
MinRK
Don't use nbformat.current in core
r18604 src = writes(nb, version=4)
MinRK
test %run notebook.ipynb
r13646 self.mktmp(src, ext='.ipynb')
Samuel Gaist
[core][tests][run] Remove nose
r26908
MinRK
test %run notebook.ipynb
r13646 _ip.magic("run %s" % self.fname)
Samuel Gaist
[core][tests][run] Remove nose
r26908
assert _ip.user_ns["answer"] == 42
Jarrod Janssen
Fixes #3664
r23621
palewire
Added tests for %run exceptions
r25723 def test_run_nb_error(self):
"""Test %run notebook.ipynb error"""
Nikita Kniazev
Move optional dependencies to a separate set
r27104 pytest.importorskip("nbformat")
palewire
Added tests for %run exceptions
r25723 from nbformat import v4, writes
Matthias Bussonnier
MAINT: cleanup imports of tempdir....
r27509
palewire
Added tests for %run exceptions
r25723 # %run when a file name isn't provided
Samuel Gaist
[core][tests][run] Remove nose
r26908 pytest.raises(Exception, _ip.magic, "run")
palewire
Added tests for %run exceptions
r25723
# %run when a file doesn't exist
Samuel Gaist
[core][tests][run] Remove nose
r26908 pytest.raises(Exception, _ip.magic, "run foobar.ipynb")
palewire
Added tests for %run exceptions
r25723
# %run on a notebook with an error
nb = v4.new_notebook(
cells=[
v4.new_code_cell("0/0")
]
)
src = writes(nb, version=4)
self.mktmp(src, ext='.ipynb')
Samuel Gaist
[core][tests][run] Remove nose
r26908 pytest.raises(Exception, _ip.magic, "run %s" % self.fname)
palewire
Added tests for %run exceptions
r25723
Jarrod Janssen
Fixes #3664
r23621 def test_file_options(self):
src = ('import sys\n'
'a = " ".join(sys.argv[1:])\n')
self.mktmp(src)
Samuel Gaist
[core][tests][run] Remove nose
r26908 test_opts = "-x 3 --verbose"
_ip.run_line_magic("run", "{0} {1}".format(self.fname, test_opts))
assert _ip.user_ns["a"] == test_opts
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994
class TestMagicRunWithPackage(unittest.TestCase):
def writefile(self, name, content):
path = os.path.join(self.tempdir.name, name)
d = os.path.dirname(path)
if not os.path.isdir(d):
os.makedirs(d)
gousaiyang
Format code
r27495 with open(path, "w", encoding="utf-8") as f:
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994 f.write(textwrap.dedent(content))
def setUp(self):
Matthias Bussonnier
Fix random test failure....
r23899 self.package = package = 'tmp{0}'.format(''.join([random.choice(string.ascii_letters) for i in range(10)]))
"""Temporary (probably) valid python package name."""
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994
self.value = int(random.random() * 10000)
self.tempdir = TemporaryDirectory()
Srinivas Reddy Thatiparthy
rename py3compat.getcwd() -> os.getcwd()
r23045 self.__orig_cwd = os.getcwd()
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994 sys.path.insert(0, self.tempdir.name)
self.writefile(os.path.join(package, '__init__.py'), '')
Takafumi Arakaki
Fix ChangedPyFileTest.test_changing_py_file failure
r10007 self.writefile(os.path.join(package, 'sub.py'), """
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994 x = {0!r}
""".format(self.value))
self.writefile(os.path.join(package, 'relative.py'), """
Takafumi Arakaki
Fix ChangedPyFileTest.test_changing_py_file failure
r10007 from .sub import x
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994 """)
self.writefile(os.path.join(package, 'absolute.py'), """
Takafumi Arakaki
Fix ChangedPyFileTest.test_changing_py_file failure
r10007 from {0}.sub import x
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994 """.format(package))
Jarrod Janssen
Fixes #3664
r23621 self.writefile(os.path.join(package, 'args.py'), """
import sys
a = " ".join(sys.argv[1:])
""".format(package))
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994
def tearDown(self):
os.chdir(self.__orig_cwd)
Takafumi Arakaki
Use list comprehension instead of filter+lambda
r10010 sys.path[:] = [p for p in sys.path if p != self.tempdir.name]
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994 self.tempdir.cleanup()
Takafumi Arakaki
Add a failing test for "%run -p -m ..."
r9998 def check_run_submodule(self, submodule, opts=''):
Thomas Kluyver
Make tests for '%run -m' more robust
r12567 _ip.user_ns.pop('x', None)
Takafumi Arakaki
Add a failing test for "%run -p -m ..."
r9998 _ip.magic('run {2} -m {0}.{1}'.format(self.package, submodule, opts))
Takafumi Arakaki
Test for #2727 (%run -m doesn't support relative imports)...
r9994 self.assertEqual(_ip.user_ns['x'], self.value,
'Variable `x` is not loaded from module `{0}`.'
.format(submodule))
def test_run_submodule_with_absolute_import(self):
self.check_run_submodule('absolute')
def test_run_submodule_with_relative_import(self):
"""Run submodule that has a relative import statement (#2727)."""
self.check_run_submodule('relative')
Takafumi Arakaki
Add a failing test for "%run -p -m ..."
r9998
def test_prun_submodule_with_absolute_import(self):
self.check_run_submodule('absolute', '-p')
def test_prun_submodule_with_relative_import(self):
self.check_run_submodule('relative', '-p')
Takafumi Arakaki
Add a failing test for "%run -d -m ..."
r10002
def with_fake_debugger(func):
@functools.wraps(func)
def wrapper(*args, **kwds):
Min RK
remove testing.tools.monkeypatch...
r21116 with patch.object(debugger.Pdb, 'run', staticmethod(eval)):
Takafumi Arakaki
Add a failing test for "%run -d -m ..."
r10002 return func(*args, **kwds)
return wrapper
@with_fake_debugger
def test_debug_run_submodule_with_absolute_import(self):
self.check_run_submodule('absolute', '-d')
@with_fake_debugger
def test_debug_run_submodule_with_relative_import(self):
self.check_run_submodule('relative', '-d')
Thomas Kluyver
Add a couple more tests for %run options
r12568
Jarrod Janssen
Fixes #3664
r23621 def test_module_options(self):
Samuel Gaist
[core][tests][run] Remove nose
r26908 _ip.user_ns.pop("a", None)
test_opts = "-x abc -m test"
_ip.run_line_magic("run", "-m {0}.args {1}".format(self.package, test_opts))
assert _ip.user_ns["a"] == test_opts
Jarrod Janssen
Fixes #3664
r23621
def test_module_options_with_separator(self):
Samuel Gaist
[core][tests][run] Remove nose
r26908 _ip.user_ns.pop("a", None)
test_opts = "-x abc -m test"
_ip.run_line_magic("run", "-m {0}.args -- {1}".format(self.package, test_opts))
assert _ip.user_ns["a"] == test_opts
Jarrod Janssen
Fixes #3664
r23621
Thomas Kluyver
Add a couple more tests for %run options
r12568 def test_run__name__():
with TemporaryDirectory() as td:
gousaiyang
Format code
r27495 path = pjoin(td, "foo.py")
with open(path, "w", encoding="utf-8") as f:
Thomas Kluyver
Add a couple more tests for %run options
r12568 f.write("q = __name__")
Samuel Gaist
[core][tests][run] Remove nose
r26908
_ip.user_ns.pop("q", None)
_ip.magic("run {}".format(path))
assert _ip.user_ns.pop("q") == "__main__"
_ip.magic("run -n {}".format(path))
assert _ip.user_ns.pop("q") == "foo"
MinRK
test traceback offset for %run and script
r14747
Craig Citro
Always reset after `%run -i` in tests....
r23967 try:
Samuel Gaist
[core][tests][run] Remove nose
r26908 _ip.magic("run -i -n {}".format(path))
assert _ip.user_ns.pop("q") == "foo"
Craig Citro
Always reset after `%run -i` in tests....
r23967 finally:
_ip.magic('reset -f')
Utkarsh Upadhyay
TST: Add tests for bug #10303.
r23379
MinRK
test traceback offset for %run and script
r14747 def test_run_tb():
"""Test traceback offset in %run"""
with TemporaryDirectory() as td:
gousaiyang
Format code
r27495 path = pjoin(td, "foo.py")
with open(path, "w", encoding="utf-8") as f:
f.write(
"\n".join(
[
"def foo():",
" return bar()",
"def bar():",
" raise RuntimeError('hello!')",
"foo()",
]
)
)
MinRK
test traceback offset for %run and script
r14747 with capture_output() as io:
_ip.magic('run {}'.format(path))
out = io.stdout
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert "execfile" not in out
assert "RuntimeError" in out
assert out.count("---->") == 3
Matthias Bussonnier
Fix state leakage and speedup in test-suite...
r25073 del ip.user_ns['bar']
del ip.user_ns['foo']
MinRK
test traceback offset for %run and script
r14747
Matthias Bussonnier
Add a failing test if we %run something with multiprocessing
r25259
def test_multiprocessing_run():
"""Set we can run mutiprocesgin without messing up up main namespace
Note that import `nose.tools as nt` mdify the value s
Dimitri Papadopoulos
Typos found by codespell
r26875 sys.module['__mp_main__'] so we need to temporarily set it to None to test
Matthias Bussonnier
Add a failing test if we %run something with multiprocessing
r25259 the issue.
"""
with TemporaryDirectory() as td:
mpm = sys.modules.get('__mp_main__')
sys.modules['__mp_main__'] = None
try:
gousaiyang
Format code
r27495 path = pjoin(td, "test.py")
with open(path, "w", encoding="utf-8") as f:
Matthias Bussonnier
Add a failing test if we %run something with multiprocessing
r25259 f.write("import multiprocessing\nprint('hoy')")
with capture_output() as io:
_ip.run_line_magic('run', path)
_ip.run_cell("i_m_undefined")
out = io.stdout
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert "hoy" in out
assert "AttributeError" not in out
assert "NameError" in out
assert out.count("---->") == 1
Matthias Bussonnier
Add a failing test if we %run something with multiprocessing
r25259 except:
raise
finally:
sys.modules['__mp_main__'] = mpm
Nikita Kniazev
Remove iptest and other Nose-dependent code
r27042
MinRK
test traceback offset for %run and script
r14747 def test_script_tb():
"""Test traceback offset in `ipython script.py`"""
with TemporaryDirectory() as td:
gousaiyang
Format code
r27495 path = pjoin(td, "foo.py")
with open(path, "w", encoding="utf-8") as f:
f.write(
"\n".join(
[
"def foo():",
" return bar()",
"def bar():",
" raise RuntimeError('hello!')",
"foo()",
]
)
)
MinRK
test traceback offset for %run and script
r14747 out, err = tt.ipexec(path)
Samuel Gaist
[core][tests][run] Remove nose
r26908 assert "execfile" not in out
assert "RuntimeError" in out
assert out.count("---->") == 3