diff --git a/cwlupgrader/main.py b/cwlupgrader/main.py index 9cdf456..a482f97 100755 --- a/cwlupgrader/main.py +++ b/cwlupgrader/main.py @@ -60,14 +60,14 @@ def parse_args(args: list[str]) -> argparse.Namespace: def main(args: Optional[list[str]] = None) -> int: - """Hook to set the args.""" + """Run with optional arguments override.""" if not args: args = sys.argv[1:] return run(parse_args(args)) def run(args: argparse.Namespace) -> int: - """Main function.""" + """Run the program using the provided arguments.""" imports: set[str] = set() if args.dir and not os.path.exists(args.dir): os.makedirs(args.dir) @@ -188,7 +188,7 @@ def load_cwl_document(path: str) -> Any: def write_cwl_document(document: Any, name: str, dirname: str) -> None: - """ + r""" Serialize the document using the Ruamel YAML round trip dumper. Will also prepend "#!/usr/bin/env cwl-runner\n" and @@ -265,7 +265,7 @@ def v1_1_to_v1_2(document: CommentedMap, outdir: str) -> CommentedMap: def draft3_to_v1_0(document: CommentedMap, outdir: str) -> CommentedMap: - """Transformation loop.""" + """Transform a draft3 document to a version 1.0 document.""" _draft3_to_v1_0(document, outdir) if isinstance(document, MutableMapping): for key, value in document.items(): @@ -281,12 +281,12 @@ def draft3_to_v1_0(document: CommentedMap, outdir: str) -> CommentedMap: def draft3_to_v1_1(document: CommentedMap, outdir: str) -> CommentedMap: - """transformation loop.""" + """Transform a draft3 document to a version 1.1 document.""" return v1_0_to_v1_1(draft3_to_v1_0(document, outdir), outdir) def draft3_to_v1_2(document: CommentedMap, outdir: str) -> CommentedMap: - """transformation loop.""" + """Transform a draft3 document to a version 1.2 document.""" return v1_1_to_v1_2(v1_0_to_v1_1(draft3_to_v1_0(document, outdir), outdir), outdir) @@ -556,7 +556,7 @@ def upgrade_v1_0_hints_and_reqs(document: dict[str, Any]) -> None: def has_hint_or_req(document: dict[str, Any], name: str) -> bool: - """Detects an existing named hint or requirement.""" + """Detect an existing named hint or requirement.""" for extra in ("requirements", "hints"): if extra in document: with SourceLine(document, extra, Exception): @@ -572,7 +572,7 @@ def has_hint_or_req(document: dict[str, Any], name: str) -> bool: def workflow_clean(document: dict[str, Any]) -> None: - """Transform draft-3 style Workflows to more idiomatic v1.0""" + """Transform draft-3 style Workflows to more idiomatic v1.0.""" input_output_clean(document) hints_and_requirements_clean(document) outputs = document["outputs"] @@ -760,7 +760,7 @@ def shorten_type(type_obj: Union[str, list[Any]]) -> Union[str, list[Any]]: def clean_secondary_files(document: dict[str, Any]) -> None: - """Cleanup for secondaryFiles""" + """Cleanup for secondaryFiles.""" if "secondaryFiles" in document: for i, sfile in enumerate(document["secondaryFiles"]): if "$(" in sfile or "${" in sfile: diff --git a/mypy-requirements.txt b/mypy-requirements.txt index 4acfc1c..13c4c06 100644 --- a/mypy-requirements.txt +++ b/mypy-requirements.txt @@ -1,2 +1 @@ -mypy==1.15.0 -types-setuptools +mypy==1.17.1 diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..49ddd07 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1 @@ +"""Tests for cwl-upgrader.""" diff --git a/tests/test_complete.py b/tests/test_complete.py index 8fc7c77..a37ccf8 100644 --- a/tests/test_complete.py +++ b/tests/test_complete.py @@ -3,14 +3,14 @@ from cwlupgrader.main import load_cwl_document, main, upgrade_document -from .util import get_data +from .util import get_data, get_path def test_draft3_workflow(tmp_path: Path) -> None: """Basic draft3 to CWL v1.1 test.""" main([f"--dir={tmp_path}", "--v1-only", get_data("testdata/draft-3/wf.cwl")]) result = filecmp.cmp( - get_data("testdata/v1.0/wf.cwl"), + get_path("testdata/v1.0/wf.cwl"), tmp_path / "wf.cwl", shallow=False, ) @@ -27,7 +27,7 @@ def test_draft3_tool_long_form_arrays(tmp_path: Path) -> None: ] ) result = filecmp.cmp( - get_data("testdata/v1.0/attributor-prok-cheetah.cwl"), + get_path("testdata/v1.0/attributor-prok-cheetah.cwl"), tmp_path / "attributor-prok-cheetah.cwl", shallow=False, ) @@ -95,7 +95,7 @@ def test_packed_graph(tmp_path: Path) -> None: [f"--dir={tmp_path}", "--v1.1-only", get_data("testdata/v1.0/conflict-wf.cwl")] ) assert filecmp.cmp( - get_data("testdata/v1.1/conflict-wf.cwl"), + get_path("testdata/v1.1/conflict-wf.cwl"), tmp_path / "conflict-wf.cwl", shallow=False, ) @@ -105,12 +105,12 @@ def test_multi_version_upgrade_external_steps(tmp_path: Path) -> None: """Test 1.0 to 1.2 upgrade of Workflow with external steps.""" main([f"--dir={tmp_path}", get_data("testdata/v1.0/1st-workflow.cwl")]) assert filecmp.cmp( - get_data("testdata/v1.2/arguments.cwl"), + get_path("testdata/v1.2/arguments.cwl"), tmp_path / "arguments.cwl", shallow=False, ) assert filecmp.cmp( - get_data("testdata/v1.2/tar-param.cwl"), + get_path("testdata/v1.2/tar-param.cwl"), tmp_path / "tar-param.cwl", shallow=False, ) diff --git a/tests/test_output_dir.py b/tests/test_output_dir.py index e3d8792..32d19c4 100644 --- a/tests/test_output_dir.py +++ b/tests/test_output_dir.py @@ -5,7 +5,7 @@ from cwlupgrader.main import main -from .util import get_data +from .util import get_data, get_path def test_draft3_workflow(tmp_path: Path) -> None: @@ -13,7 +13,7 @@ def test_draft3_workflow(tmp_path: Path) -> None: out_dir = tmp_path / "new" main([f"--dir={out_dir}", "--v1-only", get_data("testdata/draft-3/wf.cwl")]) result = filecmp.cmp( - get_data("testdata/v1.0/wf.cwl"), + get_path("testdata/v1.0/wf.cwl"), out_dir / "wf.cwl", shallow=False, ) diff --git a/tests/util.py b/tests/util.py index c8914ec..18c0877 100644 --- a/tests/util.py +++ b/tests/util.py @@ -1,20 +1,30 @@ -import os +"""Shared test utility functions.""" -from pkg_resources import Requirement, ResolutionError, resource_filename +import atexit +import os +import shutil +from contextlib import ExitStack +from importlib.resources import as_file, files +from pathlib import Path -def get_data(filename: str) -> str: +def get_path(filename: str) -> Path: + """Get the file Path for a given test file.""" + # normalizing path depending on OS or else it will cause problem when joining path filename = os.path.normpath(filename) - # normalizing path depending on OS or else it will cause problem when - # joining path filepath = None try: - filepath = resource_filename(Requirement.parse("cwlupgrader"), filename) - except ResolutionError: + file_manager = ExitStack() + atexit.register(file_manager.close) + traversable = files("cwlupgrader") / filename + filepath = file_manager.enter_context(as_file(traversable)) + except ModuleNotFoundError: pass - if not filepath or not os.path.isfile(filepath): - filepath = os.path.join(os.path.dirname(__file__), os.pardir, filename) - # warning, __file__ is all lowercase on Windows systems, this can - # sometimes conflict with docker toolkit. Workaround: pip install . - # and run the tests elsewhere via python -m pytest --pyarg cwltool - return filepath + if not filepath or not filepath.is_file(): + filepath = Path(os.path.dirname(__file__), os.pardir, filename) + return filepath.resolve() + + +def get_data(filename: str) -> str: + """Get the filename as string for a given test file.""" + return str(get_path(filename))