Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ub.writeto(paths['subdir1_init'], ub.codeblock(
'''
simple_subattr1 = "hello world"
simple_subattr2 = "hello world"
_private_attr = "hello world"
'''))
ub.writeto(paths['subdir2_init'], ub.codeblock(
'''
__all__ = ['public_attr']
public_attr = "hello world"
private_attr = "hello world"
'''))
ub.writeto(paths['submod1'], ub.codeblock(
'''
import six
attr1 = True
attr2 = six.moves.zip
# ------------------------
if True:
good_attr_01 = None
if False:
bad_attr_false1 = None
if None:
bad_attr_none1 = None
'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"
_private_attr = "hello world"
'''))
ub.writeto(paths['subdir2_init'], ub.codeblock(
'''
__all__ = ['public_attr']
public_attr = "hello world"
private_attr = "hello world"
'''))
ub.writeto(paths['submod1'], ub.codeblock(
'''
import six
attr1 = True
attr2 = six.moves.zip
# ------------------------
"""
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"
_private_attr = "hello world"
'''))
ub.writeto(paths['subdir2_init'], ub.codeblock(
'''
__all__ = ['public_attr']
public_attr = "hello world"
private_attr = "hello world"
'''))
ub.writeto(paths['submod1'], ub.codeblock(
'''
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
content = ub.readfrom('base_diff.py') + '\n\n'
xdoc_version = content + ub.codeblock(
'''
if __name__ == '__main__':
import xdoctest
xdoctest.doctest_module(__file__)
''') + '\n'
doc_version = content + ub.codeblock(
'''
if __name__ == '__main__':
import doctest
doctest.testmod()
''') + '\n'
ub.writeto('_doc_version.py', doc_version)
ub.writeto('_xdoc_version.py', xdoc_version)
source = ub.readfrom(conf_path)
red = redbaron.RedBaron(source)
# Insert custom extensions
extra_extensions = [
'"sphinxcontrib.napoleon"'
]
ext_node = red.find('name', value='extensions').parent
ext_node.value.value.extend(extra_extensions)
# Overwrite theme to read-the-docs
theme_node = red.find('name', value='html_theme').parent
theme_node.value.value = '"sphinx_rtd_theme"'
ub.writeto(conf_path, red.dumps())
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
xdoc_version = content + ub.codeblock(
'''
if __name__ == '__main__':
import xdoctest
xdoctest.doctest_module(__file__)
''') + '\n'
doc_version = content + ub.codeblock(
'''
if __name__ == '__main__':
import doctest
doctest.testmod()
''') + '\n'
ub.writeto('_doc_version.py', doc_version)
ub.writeto('_xdoc_version.py', xdoc_version)