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_dynamic_init():
"""
python ~/code/mkinit/tests/test_with_dummy.py test_dynamic_init
"""
import mkinit
cache_dpath = ub.ensure_app_cache_dir('mkinit/tests')
paths = make_dummy_package(cache_dpath, 'dynamic_dummy_mod1')
module = ub.import_module_from_path(paths['root'])
text = mkinit.dynamic_mkinit.dynamic_init(module.__name__)
print(text)
for i in range(1, 15):
want = 'good_attr_{:02d}'.format(i)
assert want in text, 'missing {}'.format(want)
def test_static_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)
modpath = paths['root']
text = mkinit.static_init(modpath)
check_dummy_root_init(text)
def test_static_find_locals():
"""
python ~/code/mkinit/tests/test_with_dummy.py test_static_find_locals
"""
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']
imports = list(mkinit.static_mkinit._find_local_submodules(modpath))
print('imports = {!r}'.format(imports))
def test_hash_file():
fpath = join(ub.ensure_app_cache_dir('ubelt'), 'tmp.txt')
ub.writeto(fpath, 'foobar')
hashid1_a = ub.hash_file(fpath, hasher='sha512', hashlen=8, stride=1, blocksize=1)
hashid2_a = ub.hash_file(fpath, hasher='sha512', hashlen=8, stride=2, blocksize=1)
hashid1_b = ub.hash_file(fpath, hasher='sha512', hashlen=8, stride=1, blocksize=10)
hashid2_b = ub.hash_file(fpath, hasher='sha512', hashlen=8, stride=2, blocksize=10)
assert hashid1_a == hashid1_b
assert hashid2_a != hashid2_b, 'blocksize matters when stride is > 1'
assert hashid1_a != hashid2_a
def test_ensuredir_recreate():
base = ub.ensure_app_cache_dir('ubelt/tests')
folder = join(base, 'foo')
member = join(folder, 'bar')
ub.ensuredir(folder, recreate=True)
ub.ensuredir(member)
assert exists(member)
ub.ensuredir(folder, recreate=True)
assert not exists(member)
def test_pathlib():
try:
import pathlib
base = pathlib.Path(ub.ensure_app_cache_dir('ubelt'))
dpath = base.joinpath('test_pathlib_mkdir')
# ensuredir
ub.delete(dpath)
assert not dpath.exists()
got = ub.ensuredir(dpath)
assert got.exists()
# shrinkuser
assert ub.shrinkuser(base) == '~/.cache/ubelt'
assert ub.augpath(base, prefix='foo') == '/home/joncrall/.cache/fooubelt'
ub.expandpath(base)
except Exception:
>>> ub.download(url, hasher='sha512', hash_prefix='c98a46cb31205cf')
>>> with pytest.raises(RuntimeError):
>>> ub.download(url, hasher='sha512', hash_prefix='BAD_HASH')
"""
from ubelt import ProgIter as Progress
from ubelt import ensure_app_cache_dir
import shutil
import tempfile
import hashlib
if six.PY2: # nocover
from urllib2 import urlopen # NOQA
else:
from urllib.request import urlopen # NOQA
if fpath is None:
dpath = ensure_app_cache_dir('ubelt')
fname = basename(url)
fpath = join(dpath, fname)
_dst_is_io_object = hasattr(fpath, 'write')
if verbose:
if _dst_is_io_object:
print('Downloading url=%r to IO object' % (url,))
else:
print('Downloading url=%r to fpath=%r' % (url, fpath))
urldata = urlopen(url)
meta = urldata.info()
try:
if hasattr(meta, 'getheaders'): # nocover
file_size = int(meta.getheaders("Content-Length")[0])