From 952d95197173cbb5f759036bb99a48bb09934797 Mon Sep 17 00:00:00 2001 From: "Michael R. Crusoe" Date: Tue, 31 Oct 2023 14:35:04 +0100 Subject: [PATCH] favor "path" over "location" when testing Co-authored-by: mvdbeek --- cwltest/compare.py | 28 ++++++++++++++++------------ tests/test_compare.py | 28 +++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/cwltest/compare.py b/cwltest/compare.py index f392239..18452ed 100644 --- a/cwltest/compare.py +++ b/cwltest/compare.py @@ -119,38 +119,42 @@ def _compare_location( expected: Dict[str, Any], actual: Dict[str, Any], skip_details: bool ) -> None: if "path" in expected: - comp = "path" + expected_comp = "path" if "path" not in actual: actual["path"] = actual["location"] elif "location" in expected: - comp = "location" + expected_comp = "location" else: return + if "path" in actual: + actual_comp = "path" + else: + actual_comp = "location" + path = urllib.parse.urlparse(actual[actual_comp]).path if actual.get("class") == "Directory": - actual[comp] = actual[comp].rstrip("/") + actual[actual_comp] = actual[actual_comp].rstrip("/") exist_fun: Callable[[str], bool] = os.path.isdir else: exist_fun = os.path.isfile - if "path" in actual: - path = urllib.parse.urlparse(actual["path"]).path - else: - path = urllib.parse.urlparse(actual["location"]).path if not exist_fun(path) and not skip_details: raise CompareFail.format( expected, actual, - f"{actual[comp]} does not exist", + f"{actual[actual_comp]} does not exist", ) - if expected[comp] != "Any" and ( + if expected[expected_comp] != "Any" and ( not ( - actual[comp].endswith("/" + expected[comp]) - or ("/" not in actual[comp] and expected[comp] == actual[comp]) + actual[actual_comp].endswith("/" + expected[expected_comp]) + or ( + "/" not in actual[actual_comp] + and expected[expected_comp] == actual[actual_comp] + ) ) ): raise CompareFail.format( expected, actual, - f"{actual[comp]} does not end with {expected[comp]}", + f"{actual[actual_comp]} does not end with {expected[expected_comp]}", ) diff --git a/tests/test_compare.py b/tests/test_compare.py index 6491f8d..b26c074 100644 --- a/tests/test_compare.py +++ b/tests/test_compare.py @@ -82,7 +82,7 @@ def test_compare_file_different_size(tmp_path: Path) -> None: actual = { "basename": "cores.txt", "class": "File", - "location": str(path), + "location": path.as_uri(), } with pytest.raises(CompareFail): _compare_file(expected, actual, False) @@ -102,7 +102,7 @@ def test_compare_file_different_checksum(tmp_path: Path) -> None: actual = { "basename": "cores.txt", "class": "File", - "location": str(path), + "location": path.as_uri(), } with pytest.raises(CompareFail): _compare_file(expected, actual, False) @@ -121,7 +121,7 @@ def test_compare_file_inconsistent_size(tmp_path: Path) -> None: actual = { "basename": "cores.txt", "class": "File", - "location": str(path), + "location": path.as_uri(), "size": 65535, } with pytest.raises(CompareFail): @@ -142,7 +142,7 @@ def test_compare_file_inconsistent_checksum(tmp_path: Path) -> None: "basename": "cores.txt", "checksum": "inconsistent-checksum", "class": "File", - "location": str(path), + "location": path.as_uri(), } with pytest.raises(CompareFail): _compare_file(expected, actual, False) @@ -160,7 +160,25 @@ def test_compare_directory(tmp_path: Path) -> None: actual = { "class": "Directory", - "location": str(path), + "location": path.as_uri(), + "listing": [], + } + _compare_directory(expected, actual, False) + + +def test_compare_directory_path(tmp_path: Path) -> None: + expected = { + "location": "dir", + "class": "Directory", + "listing": [], + } + + path = tmp_path / "dir" + os.makedirs(path) + + actual = { + "class": "Directory", + "path": str(path), "listing": [], } _compare_directory(expected, actual, False)