2020-03-13 11:38:10 +00:00
|
|
|
# try to import all .py files from a given directory
|
|
|
|
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import importlib
|
|
|
|
import importlib.util
|
2021-06-20 09:45:52 +00:00
|
|
|
import pytest
|
2020-03-13 11:38:10 +00:00
|
|
|
|
|
|
|
def _module_name(path):
|
|
|
|
task = os.path.splitext(path)[0]
|
|
|
|
parts = task.split(os.path.sep)
|
|
|
|
package = parts[0]
|
|
|
|
name = ''.join('.' + c for c in parts[1:])
|
|
|
|
return package, name
|
|
|
|
|
|
|
|
def _import_file(path):
|
|
|
|
package, mod_name = _module_name(path)
|
|
|
|
line = f'Importing {package}{mod_name} from {path}'
|
|
|
|
print(f'{line:<80}', end='')
|
|
|
|
mod_spec = importlib.util.find_spec(mod_name, package)
|
2021-06-20 09:45:52 +00:00
|
|
|
mod = mod_spec.loader.load_module(f'{package}{mod_name}')
|
2020-03-13 11:38:10 +00:00
|
|
|
if mod is None:
|
|
|
|
result = 'FAIL'
|
|
|
|
else:
|
|
|
|
result = 'DONE'
|
|
|
|
print(f'{result:>6}')
|
|
|
|
mod_spec.loader.exec_module(mod)
|
2021-06-20 09:45:52 +00:00
|
|
|
return result
|
2020-03-13 11:38:10 +00:00
|
|
|
|
2021-06-20 09:45:52 +00:00
|
|
|
def get_paths():
|
|
|
|
for g in ['tasks/**/*.py']:
|
|
|
|
for p in glob.glob(g, recursive=True):
|
|
|
|
yield p
|
2020-03-13 11:38:10 +00:00
|
|
|
|
2021-06-20 09:45:52 +00:00
|
|
|
@pytest.mark.parametrize("path", list(sorted(get_paths())))
|
|
|
|
def test_import(path):
|
|
|
|
assert _import_file(path) == 'DONE'
|
2020-03-13 11:38:10 +00:00
|
|
|
|