https://prefect.io logo
#prefect-contributors
Title
# prefect-contributors
z

Zach Schumacher

06/14/2021, 3:07 PM
hi all, I wrote a little func for testing flow imports. I found this helpful and had written something similar in our airflow app (RIP) for testing DAG imports. We do some dynamic code generation w/ jinja, so we use this just to make sure none of the files are malformed. Wanted to drop it here in case anyone else finds it useful, and also if anyone thinks this would be worth PRing into a testing module or something.
Copy code
def flow_imports_tester(root_path: Path, module_dot_path: str):  # pragma: no cover
    """
    Helper for testing all flows in a project root_path at module_dot_path are importable
    Example:
        >>> flow_imports_tester(Path("~/my_project").expanduser(), "my_project.my_directory.flows")
    """
    split_dot_path = module_dot_path.split(".")
    package_directory = Path(root_path, *split_dot_path)

    flows = list(package_directory.iterdir())
    if not flows:
        raise RuntimeError("No flows found, did you specify the correct path?")

    for flow in flows:
        if "." in flow.name:
            filename, file_extension = flow.name.split(".")
        else:
            filename, file_extension = flow.name, ""

        if filename == "__init__" or file_extension != "py":
            continue

        import_module(".".join([module_dot_path, filename]))
marvin 3
2 Views