Skip to content

Commit

Permalink
Merge pull request #185 from common-workflow-language/compare_prefer_…
Browse files Browse the repository at this point in the history
…path

favor "path" over "location" when testing
  • Loading branch information
mvdbeek authored Oct 31, 2023
2 parents 774a8c4 + 952d951 commit cc5ef54
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
28 changes: 16 additions & 12 deletions cwltest/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]}",
)


Expand Down
28 changes: 23 additions & 5 deletions tests/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit cc5ef54

Please sign in to comment.