[generate-tests] black

This commit is contained in:
tobi-wan-kenobi 2020-07-20 13:58:46 +02:00
parent 548ccc5e94
commit 54a2fc3a41

View file

@ -5,6 +5,7 @@ import re
import sys import sys
import glob import glob
def is_psl(module): def is_psl(module):
lib_path = os.path.dirname(os.__file__) lib_path = os.path.dirname(os.__file__)
old_sys = sys.path old_sys = sys.path
@ -20,37 +21,45 @@ def is_psl(module):
def is_internal(module): def is_internal(module):
if module.startswith("core.") or module == "core": return True if module.startswith("core.") or module == "core":
if module.startswith("util.") or module == "util": return True return True
if module.startswith("."): return True if module.startswith("util.") or module == "util":
return True
if module.startswith("."):
return True
return is_psl(module) return is_psl(module)
def dependencies(filename): def dependencies(filename):
deps = [] deps = []
with open(filename) as f: with open(filename) as f:
for line in f: for line in f:
if "import" in line: if "import" in line:
match = re.match("\s*(from (\S+) )?import (\S+)", line) match = re.match("\s*(from (\S+) )?import (\S+)", line)
if not match: continue if not match:
continue
dep = match.group(2) or match.group(3) dep = match.group(2) or match.group(3)
if "util.popup" in dep or ("util" in line and "popup" in line): if "util.popup" in dep or ("util" in line and "popup" in line):
deps.append("tkinter") deps.append("tkinter")
if ".datetimetz" in line: if ".datetimetz" in line:
deps.extend(dependencies("bumblebee_status/modules/contrib/datetimetz.py")) deps.extend(
dependencies("bumblebee_status/modules/contrib/datetimetz.py")
)
elif not is_internal(dep): elif not is_internal(dep):
deps.append(dep) deps.append(dep)
return deps return deps
def write_test(testname, modname, deps): def write_test(testname, modname, deps):
fqmn = ".".join(["modules", testname.split(os.sep)[2], modname]) fqmn = ".".join(["modules", testname.split(os.sep)[2], modname])
if not os.path.exists(testname): if not os.path.exists(testname):
with open(testname, "w") as f: with open(testname, "w") as f:
f.writelines([ f.writelines(
"import pytest\n\n", ["import pytest\n\n",]
]) )
for dep in deps: for dep in deps:
f.write("pytest.importorskip(\"{}\")\n\n".format(dep)) f.write('pytest.importorskip("{}")\n\n'.format(dep))
with open(testname) as f: with open(testname) as f:
for line in f: for line in f:
@ -60,20 +69,23 @@ def write_test(testname, modname, deps):
print("writing base test for {}".format(modname)) print("writing base test for {}".format(modname))
with open(testname, "a+") as f: with open(testname, "a+") as f:
f.writelines([ f.writelines(
"def test_load_module():\n", ["def test_load_module():\n", ' __import__("{}")\n\n'.format(fqmn),]
" __import__(\"{}\")\n\n".format(fqmn), )
])
def main(): def main():
for f in glob.glob("bumblebee_status/modules/*/*.py"): for f in glob.glob("bumblebee_status/modules/*/*.py"):
if os.path.basename(f) == "__init__.py": continue if os.path.basename(f) == "__init__.py":
continue
modname = os.path.splitext(os.path.basename(f))[0] modname = os.path.splitext(os.path.basename(f))[0]
modpath = os.path.dirname(f) modpath = os.path.dirname(f)
deps = dependencies(f) deps = dependencies(f)
testname = os.path.join("tests", "modules", modpath.split(os.sep)[2], "test_{}.py".format(modname)) testname = os.path.join(
"tests", "modules", modpath.split(os.sep)[2], "test_{}.py".format(modname)
)
write_test(testname, modname, deps) write_test(testname, modname, deps)