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_static_import_without_init():
"""
python ~/code/mkinit/tests/test_with_dummy.py test_static_import_without_init
"""
import mkinit
cache_dpath = ub.ensure_app_cache_dir('mkinit/tests')
paths = make_dummy_package(cache_dpath)
ub.delete(paths['root_init'])
modpath = paths['root']
text = mkinit.static_init(modpath)
check_dummy_root_init(text)
def test_ensuredir_verbosity():
base = ub.ensure_app_cache_dir('ubelt/tests')
with ub.CaptureStdout() as cap:
ub.ensuredir(join(base, 'foo'), verbose=0)
assert cap.text == ''
# None defaults to verbose=0
with ub.CaptureStdout() as cap:
ub.ensuredir((base, 'foo'), verbose=None)
assert cap.text == ''
ub.delete(join(base, 'foo'))
with ub.CaptureStdout() as cap:
ub.ensuredir(join(base, 'foo'), verbose=1)
assert 'creating' in cap.text
with ub.CaptureStdout() as cap:
ub.ensuredir(join(base, 'foo'), verbose=1)
assert 'existing' in cap.text
def make_dummy_package(dpath, pkgname='mkinit_dummy_module'):
"""
Creates a dummy package structure with or without __init__ files
"""
root = ub.ensuredir(join(dpath, pkgname))
ub.delete(root)
ub.ensuredir(root)
paths = {
'root': root,
'submod1': ub.touch(join(root, 'submod1.py')),
'submod2': ub.touch(join(root, 'submod2.py')),
'subdir1': ub.ensuredir(join(root, 'subdir1')),
'subdir2': ub.ensuredir(join(root, 'subdir2')),
}
paths['subdir1_init'] = ub.touch(join(paths['subdir1'], '__init__.py'))
paths['subdir2_init'] = ub.touch(join(paths['subdir2'], '__init__.py'))
paths['root_init'] = ub.touch(join(paths['root'], '__init__.py'))
ub.writeto(paths['subdir1_init'], ub.codeblock(
'''
simple_subattr1 = "hello world"
simple_subattr2 = "hello world"
"""
What no errors happen when an external processes removes meta
"""
def func():
return 'expensive result'
cacher = ub.Cacher('name', 'params', verbose=10)
cacher.clear()
cacher.ensure(func)
data_fpath = cacher.get_fpath()
meta_fpath = data_fpath + '.meta'
assert exists(data_fpath)
assert exists(meta_fpath)
ub.delete(meta_fpath)
assert not exists(meta_fpath)
cacher.clear()
assert not exists(meta_fpath)
assert not exists(data_fpath)
>>> dpath = join(cache_dpath, 'ensuredir')
>>> if exists(dpath):
... os.rmdir(dpath)
>>> assert not exists(dpath)
>>> ub.ensuredir(dpath)
>>> assert exists(dpath)
>>> os.rmdir(dpath)
"""
if verbose is None:
verbose = 0
if isinstance(dpath, (list, tuple)):
dpath = join(*dpath)
if recreate:
import ubelt as ub
ub.delete(dpath, verbose=verbose)
if not exists(dpath):
if verbose:
print('Ensuring directory (creating {!r})'.format(dpath))
if sys.version_info.major == 2: # nocover
os.makedirs(normpath(dpath), mode=mode)
else:
os.makedirs(normpath(dpath), mode=mode, exist_ok=True)
else:
if verbose:
print('Ensuring directory (existing {!r})'.format(dpath))
return dpath