Merge pull request #33949 from toabctl/import-tasks-with-py3

qa: Add a tox env that can test importing files

Reviewed-by: Kefu Chai <kchai@redhat.com>
This commit is contained in:
Kefu Chai 2020-03-27 14:49:46 +08:00 committed by GitHub
commit a105dea117
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

43
qa/test_import.py Normal file
View File

@ -0,0 +1,43 @@
# try to import all .py files from a given directory
import argparse
import glob
import os
import importlib
import importlib.util
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)
mod = mod_spec.loader.load_module()
if mod is None:
result = 'FAIL'
else:
result = 'DONE'
print(f'{result:>6}')
mod_spec.loader.exec_module(mod)
def _parser():
parser = argparse.ArgumentParser(
description='Try to import a file',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('path', nargs='+', help='Glob to select files')
return parser
if __name__ == '__main__':
parser = _parser()
args = parser.parse_args()
for g in args.path:
for p in glob.glob(g, recursive=True):
_import_file(p)

View File

@ -18,3 +18,8 @@ commands=flake8 --select=F,E9 --exclude=venv,.tox
basepython = python3
deps = mypy==0.770
commands = mypy {posargs:.}
[testenv:import-tasks]
basepython = python3
deps = {env:TEUTHOLOGY_GIT:git+https://github.com/ceph/teuthology.git@master}#egg=teuthology[coverage,orchestra,test]
commands = python test_import.py {posargs:tasks/**/*.py}