Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_modname_to_modpath_namespace():
"""
Ignore:
import sys
sys.path.append('/home/joncrall/code/xdoctest/testing')
from test_static import *
temp = ub.TempDir()
temp.__enter__()
sys.path.append(temp.dpath)
temp.__exit__(None, None, None)
%timeit _syspath_modname_to_modpath('xdoctest.static_analysis')
%timeit _pkgutil_modname_to_modpath('xdoctest.static_analysis')
"""
with ub.TempDir() as temp:
dpath = temp.dpath
# Some "bad" non-module directories
tmpbad = ub.ensuredir((dpath, '_tmpbad'))
# Make a submodule of a bad directory, look good.
sub_bad = ub.ensuredir((tmpbad, 'sub_bad'))
ub.touch(join(tmpbad, '_inbad.py'))
subbad = ub.touch(join(sub_bad, '__init__.py')) # NOQA
b0 = ub.touch(join(sub_bad, 'b0.py')) # NOQA
with PythonPathContext(dpath):
assert _static_modname_to_modpath('_tmpbad') is None
# Tricky case, these modules look good outside of _tmpbad WOW, you
# can actually import this and it works, but pkgloader still
def test_modpath_to_modname():
"""
CommandLine:
pytest testing/test_static.py::test_modpath_to_modname -s
python testing/test_static.py test_modpath_to_modname
"""
with ub.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
mod2 = ub.touch(join(sub2, 'mod2.py'))
root_main = ub.touch(join(root, '__main__.py'))
def test_modname_to_modpath_package():
"""
CommandLine:
pytest testing/test_static.py::test_modname_to_modpath_package
Ignore:
import sys
sys.path.append('/home/joncrall/code/xdoctest/testing')
from test_static import *
temp = ub.TempDir()
temp.__enter__()
sys.path.append(temp.dpath)
temp.__exit__(None, None, None)
"""
with ub.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
mod2 = ub.touch(join(sub2, 'mod2.py'))
root_main = ub.touch(join(root, '__main__.py'))
pytest testing/test_static.py::test_package_submodules -s
xdoctest -m ~/code/ubelt/tests/test_import.py test_package_submodules
pass
Ignore:
import sys
sys.path.append('/home/joncrall/code/xdoctest/testing')
from test_static import *
temp = ub.TempDir()
temp.__enter__()
sys.path.append(temp.dpath)
temp.__exit__(None, None, None)
"""
from xdoctest import static_analysis as static
with ub.TempDir() as temp:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot927'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
root_init = ub.touch(join(root, '__init__.py'))
sub1_init = ub.touch(join(sub1, '__init__.py'))
sub2_init = ub.touch(join(sub2, '__init__.py'))
mod0 = ub.touch(join(root, 'mod0.py'))
mod1 = ub.touch(join(sub1, 'mod1.py'))
mod2 = ub.touch(join(sub2, 'mod2.py'))
root_main = ub.touch(join(root, '__main__.py'))
def test_tempdir():
temp = ub.TempDir()
assert temp.dpath is None
temp.ensure()
assert exists(temp.dpath)
# Double ensure for coverage
temp.ensure()
assert exists(temp.dpath)
dpath = temp.dpath
temp.cleanup()
assert not exists(dpath)
assert temp.dpath is None
def test_modname_to_modpath_single():
with ub.TempDir() as temp:
dpath = temp.dpath
# Single module
single = ub.touch(join(dpath, '_tmpsingle.py'))
single_main = ub.touch(join(dpath, '__main__.py'))
with PythonPathContext(dpath):
assert single == _static_modname_to_modpath('_tmpsingle')
assert single == _static_modname_to_modpath('_tmpsingle', hide_init=True, hide_main=False)
assert single == _static_modname_to_modpath('_tmpsingle', hide_init=False, hide_main=False)
assert single == _static_modname_to_modpath('_tmpsingle', hide_init=False, hide_main=True)
# Weird module named main not in a package
assert _static_modname_to_modpath('__main__') == single_main
assert _static_modname_to_modpath('__main__', hide_init=True, hide_main=False) == single_main
assert _static_modname_to_modpath('__main__', hide_init=False, hide_main=False) == single_main
def test_import_modpath_basic():
assert 'testmod' not in sys.modules
with ub.TempDir() as temp:
modpath = join(temp.dpath, 'testmod.py')
text = ub.codeblock(
'''
a = 'value'
''')
ub.writeto(modpath, text)
assert temp.dpath not in sys.path
module = ub.import_module_from_path(modpath)
assert temp.dpath not in sys.path, 'pythonpath should remain clean'
assert module.a == 'value'
assert module.__file__ == modpath
assert module.__name__ == 'testmod'
assert 'testmod' in sys.modules
def test_import_modpath_package():
assert '_tmproot373.sub1.sub2.testmod' not in sys.modules
temp = ub.TempDir().start()
# with ub.TempDir() as temp:
if True:
dpath = temp.dpath
# Create a dummy package heirachy
root = ub.ensuredir((dpath, '_tmproot373'))
sub1 = ub.ensuredir((root, 'sub1'))
sub2 = ub.ensuredir((sub1, 'sub2'))
ub.touch(join(root, '__init__.py'))
ub.touch(join(sub1, '__init__.py'))
ub.touch(join(sub2, '__init__.py'))
modpath = join(sub2, 'testmod.py')
text = ub.codeblock(
'''