Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions cwlupgrader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand All @@ -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)


Expand Down Expand Up @@ -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):
Expand All @@ -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"]
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions mypy-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
mypy==1.15.0
types-setuptools
mypy==1.17.1
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for cwl-upgrader."""
12 changes: 6 additions & 6 deletions tests/test_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
)
4 changes: 2 additions & 2 deletions tests/test_output_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

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:
"""Confirm that --dir works when the directory doesn't exist yet."""
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,
)
Expand Down
36 changes: 23 additions & 13 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -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))