diff --git a/cwlupgrader/main.py b/cwlupgrader/main.py index fc5ff0e..73dda8c 100755 --- a/cwlupgrader/main.py +++ b/cwlupgrader/main.py @@ -76,43 +76,78 @@ def run(args: argparse.Namespace) -> int: if args.v1_1_only: _logger.info("Skipping v1.1 document as requested: %s.", path) continue + + if args.v1_only: + target_version = "v1.0" + elif args.v1_1_only: + target_version = "v1.1" + else: + target_version = "latest" upgraded_document = upgrade_document( - document, args.v1_only, args.v1_1_only, args.dir, imports + document, + args.dir, + target_version=target_version, + imports=imports, ) write_cwl_document(upgraded_document, Path(path).name, args.dir) return 0 def upgrade_document( - document: Any, v1_only: bool, v1_1_only: bool, output_dir: str, imports: Set[str] + document: Any, + output_dir: str, + target_version: Optional[str] = "latest", + imports: Optional[Set[str]] = None, ) -> Any: + if imports is None: + imports = set() + supported_versions = ["v1.0", "v1.1", "v1.2", "latest"] + if target_version not in supported_versions: + _logger.error(f"Unsupported target cwlVersion: {target_version}") + return version = document["cwlVersion"] if version == "cwl:draft-3" or version == "draft-3": - if v1_only: + if target_version == "v1.0": main_updater = draft3_to_v1_0 inner_updater = _draft3_to_v1_0 - elif v1_1_only: + elif target_version == "v1.1": main_updater = draft3_to_v1_1 inner_updater = _draft3_to_v1_1 - else: + elif target_version == "v1.2": main_updater = draft3_to_v1_2 inner_updater = _draft3_to_v1_2 + elif target_version == "latest": + main_updater = draft3_to_v1_2 + inner_updater = _draft3_to_v1_2 + else: + pass # does not happen elif version == "v1.0": - if v1_only: + if target_version == "v1.0": _logger.info("Skipping v1.0 document as requested.") return - elif v1_1_only: + elif target_version == "v1.1": main_updater = v1_0_to_v1_1 inner_updater = _v1_0_to_v1_1 - else: + elif target_version == "v1.2": + main_updater = v1_0_to_v1_2 + inner_updater = _v1_0_to_v1_2 + elif target_version == "latest": main_updater = v1_0_to_v1_2 inner_updater = _v1_0_to_v1_2 + else: + pass # does not happen elif version == "v1.1": - if v1_1_only: + if target_version == "v1.1": _logger.info("Skipping v1.1 document as requested.") return - main_updater = v1_1_to_v1_2 - inner_updater = _v1_1_to_v1_2 + elif target_version == "v1.2": + main_updater = v1_1_to_v1_2 + inner_updater = _v1_1_to_v1_2 + elif target_version == "latest": + main_updater = v1_1_to_v1_2 + inner_updater = _v1_1_to_v1_2 + else: + pass # does not happen? How to do the case that base version is v1.0? else: _logger.error(f"Unsupported cwlVersion: {version}") process_imports(document, imports, inner_updater, output_dir) diff --git a/testdata/v1.0/listing_deep1.cwl b/testdata/v1.0/listing_deep1.cwl new file mode 100755 index 0000000..7877b7c --- /dev/null +++ b/testdata/v1.0/listing_deep1.cwl @@ -0,0 +1,13 @@ +#!/usr/bin/env cwl-runner +class: CommandLineTool +cwlVersion: v1.0 +requirements: + InlineJavascriptRequirement: {} +inputs: + d: Directory +outputs: + out: + type: boolean + outputBinding: + outputEval: '$(inputs.d.listing.length === 1 && inputs.d.listing[0].listing.length === 1)' +baseCommand: "true" diff --git a/testdata/v1.1/listing_deep1.cwl b/testdata/v1.1/listing_deep1.cwl new file mode 100755 index 0000000..09af932 --- /dev/null +++ b/testdata/v1.1/listing_deep1.cwl @@ -0,0 +1,15 @@ +#!/usr/bin/env cwl-runner +class: CommandLineTool +cwlVersion: v1.1 +requirements: + LoadListingRequirement: + loadListing: deep_listing + InlineJavascriptRequirement: {} +inputs: + d: Directory +outputs: + out: + type: boolean + outputBinding: + outputEval: '$(inputs.d.listing.length === 1 && inputs.d.listing[0].listing.length === 1)' +baseCommand: "true" diff --git a/testdata/v1.2/listing_deep1.cwl b/testdata/v1.2/listing_deep1.cwl new file mode 100755 index 0000000..7696466 --- /dev/null +++ b/testdata/v1.2/listing_deep1.cwl @@ -0,0 +1,15 @@ +#!/usr/bin/env cwl-runner +class: CommandLineTool +cwlVersion: v1.2 +requirements: + LoadListingRequirement: + loadListing: deep_listing + InlineJavascriptRequirement: {} +inputs: + d: Directory +outputs: + out: + type: boolean + outputBinding: + outputEval: '$(inputs.d.listing.length === 1 && inputs.d.listing[0].listing.length === 1)' +baseCommand: "true" diff --git a/tests/test_complete.py b/tests/test_complete.py index 19173d1..561f037 100644 --- a/tests/test_complete.py +++ b/tests/test_complete.py @@ -1,7 +1,7 @@ import filecmp from pathlib import Path -from cwlupgrader.main import main +from cwlupgrader.main import load_cwl_document, main, upgrade_document from .util import get_data @@ -15,3 +15,24 @@ def test_draft3_workflow(tmp_path: Path) -> None: shallow=False, ) assert result + + +def test_invalid_target(tmp_path: Path) -> None: + """Test for invalid target version""" + doc = load_cwl_document(get_data("testdata/v1.0/listing_deep1.cwl")) + result = upgrade_document(doc, str(tmp_path), "invalid-version") + assert result is None + + +def test_v1_0_to_v1_1(tmp_path: Path) -> None: + """Basic CWL v1.0 to CWL v1.1 test.""" + doc = load_cwl_document(get_data("testdata/v1.0/listing_deep1.cwl")) + upgraded = upgrade_document(doc, str(tmp_path), "v1.1") + assert doc == upgraded + + +def test_v1_1_to_v1_2(tmp_path: Path) -> None: + """Basic CWL v1.1 to CWL v1.2 test.""" + doc = load_cwl_document(get_data("testdata/v1.1/listing_deep1.cwl")) + upgraded = upgrade_document(doc, str(tmp_path), "v1.2") + assert doc == upgraded