import pathlib
import subprocess
import yaml
import shutil

from sophios.main import main
from sophios.cli import get_args


def test_generate_cwl_workflow() -> None:
    """
    Test that running sophios with --generate_cwl_workflow produces the correct cwl file
    """
    # remove output directory in case it exists to avoid false test pass
    out_path = pathlib.Path('autogenerated')

    if out_path.exists() and out_path.is_dir():
        shutil.rmtree(out_path)

    yaml_path = str(pathlib.Path(__file__).parent.parent.resolve() / "docs/tutorials/helloworld.wic")

    args = ["sophios", "--yaml", yaml_path, "--generate_cwl_workflow"]

    # run sophios with args
    subprocess.run(args)

    with open("autogenerated/helloworld.cwl", "r") as cwl_file:
        result_dict = yaml.safe_load(cwl_file)

    with open(
        str(pathlib.Path(__file__).parent.resolve() / "data/cwl/helloworld.cwl"), "r"
    ) as cwl_file:
        actual_dict = yaml.safe_load(cwl_file)

    assert result_dict == actual_dict
