Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Test for #2727 (%run -m doesn't support relative imports)

test_run_submodule_with_relative_import fails.
  • Loading branch information...
commit ced51b9deccafe0d3b1800d56f2fb8b1ad310b90 1 parent 3fb35dc
@tkf authored
Showing with 54 additions and 0 deletions.
  1. +54 −0 IPython/core/tests/test_run.py
View
54 IPython/core/tests/test_run.py
@@ -16,6 +16,9 @@
import os
import sys
import tempfile
+import unittest
+import textwrap
+import random
import nose.tools as nt
from nose import SkipTest
@@ -23,6 +26,7 @@
from IPython.testing import decorators as dec
from IPython.testing import tools as tt
from IPython.utils import py3compat
+from IPython.utils.tempdir import TemporaryDirectory
#-----------------------------------------------------------------------------
# Test functions begin
@@ -337,3 +341,53 @@ def test_run_formatting(self):
self.mktmp(src)
_ip.magic('run -t -N 1 %s' % self.fname)
_ip.magic('run -t -N 10 %s' % self.fname)
+
+
+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)
+ with open(path, 'w') as f:
+ f.write(textwrap.dedent(content))
+
+ def setUp(self):
+ self.package = package = 'tmp{0}'.format(repr(random.random())[2:])
+ """Temporary valid python package name."""
+
+ self.value = int(random.random() * 10000)
+
+ self.tempdir = TemporaryDirectory()
+ self.__orig_cwd = os.getcwdu()
+ sys.path.insert(0, self.tempdir.name)
+
+ self.writefile(os.path.join(package, '__init__.py'), '')
+ self.writefile(os.path.join(package, 'foo.py'), """
+ x = {0!r}
+ """.format(self.value))
+ self.writefile(os.path.join(package, 'relative.py'), """
+ from .foo import x
+ """)
+ self.writefile(os.path.join(package, 'absolute.py'), """
+ from {0}.foo import x
+ """.format(package))
+
+ def tearDown(self):
+ os.chdir(self.__orig_cwd)
+ sys.path[:] = filter(lambda x: x != self.tempdir.name, sys.path)
+ self.tempdir.cleanup()
+
+ def check_run_submodule(self, submodule):
+ _ip.magic('run -m {0}.{1}'.format(self.package, submodule))
+ 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')
Please sign in to comment.
Something went wrong with that request. Please try again.