##// END OF EJS Templates
Test for #2727 (%run -m doesn't support relative imports)...
Takafumi Arakaki -
Show More
@@ -16,6 +16,9 b' from __future__ import absolute_import'
16 16 import os
17 17 import sys
18 18 import tempfile
19 import unittest
20 import textwrap
21 import random
19 22
20 23 import nose.tools as nt
21 24 from nose import SkipTest
@@ -23,6 +26,7 b' from nose import SkipTest'
23 26 from IPython.testing import decorators as dec
24 27 from IPython.testing import tools as tt
25 28 from IPython.utils import py3compat
29 from IPython.utils.tempdir import TemporaryDirectory
26 30
27 31 #-----------------------------------------------------------------------------
28 32 # Test functions begin
@@ -337,3 +341,53 b' tclass.py: deleting object: C-third'
337 341 self.mktmp(src)
338 342 _ip.magic('run -t -N 1 %s' % self.fname)
339 343 _ip.magic('run -t -N 10 %s' % self.fname)
344
345
346 class TestMagicRunWithPackage(unittest.TestCase):
347
348 def writefile(self, name, content):
349 path = os.path.join(self.tempdir.name, name)
350 d = os.path.dirname(path)
351 if not os.path.isdir(d):
352 os.makedirs(d)
353 with open(path, 'w') as f:
354 f.write(textwrap.dedent(content))
355
356 def setUp(self):
357 self.package = package = 'tmp{0}'.format(repr(random.random())[2:])
358 """Temporary valid python package name."""
359
360 self.value = int(random.random() * 10000)
361
362 self.tempdir = TemporaryDirectory()
363 self.__orig_cwd = os.getcwdu()
364 sys.path.insert(0, self.tempdir.name)
365
366 self.writefile(os.path.join(package, '__init__.py'), '')
367 self.writefile(os.path.join(package, 'foo.py'), """
368 x = {0!r}
369 """.format(self.value))
370 self.writefile(os.path.join(package, 'relative.py'), """
371 from .foo import x
372 """)
373 self.writefile(os.path.join(package, 'absolute.py'), """
374 from {0}.foo import x
375 """.format(package))
376
377 def tearDown(self):
378 os.chdir(self.__orig_cwd)
379 sys.path[:] = filter(lambda x: x != self.tempdir.name, sys.path)
380 self.tempdir.cleanup()
381
382 def check_run_submodule(self, submodule):
383 _ip.magic('run -m {0}.{1}'.format(self.package, submodule))
384 self.assertEqual(_ip.user_ns['x'], self.value,
385 'Variable `x` is not loaded from module `{0}`.'
386 .format(submodule))
387
388 def test_run_submodule_with_absolute_import(self):
389 self.check_run_submodule('absolute')
390
391 def test_run_submodule_with_relative_import(self):
392 """Run submodule that has a relative import statement (#2727)."""
393 self.check_run_submodule('relative')
General Comments 0
You need to be logged in to leave comments. Login now