Skip to content

Commit

Permalink
Test walk method
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbull committed Oct 8, 2023
1 parent bf79c41 commit 9a28b27
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions tests/test_cloudpath_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ def _make_glob_directory(root):
rmtree(local_root)


def _assert_glob_results_match(cloud_results, local_results, cloud_root, local_root):
def _lstrip_path_root(path, root):
rel_path = str(path)[len(str(root)) :]
return rel_path.rstrip("/") # agnostic to trailing slash
def _lstrip_path_root(path, root):
rel_path = str(path)[len(str(root)) :]
return rel_path.rstrip("/") # agnostic to trailing slash


def _assert_glob_results_match(cloud_results, local_results, cloud_root, local_root):
local_results_no_root = [_lstrip_path_root(c.as_posix(), local_root) for c in local_results]
cloud_results_no_root = [_lstrip_path_root(c, cloud_root) for c in cloud_results]

Expand All @@ -119,6 +120,19 @@ def _lstrip_path_root(path, root):
assert set(local_results_no_root) == set(cloud_results_no_root)


def _assert_walk_results_match(cloud_results, local_results, cloud_root, local_root):
for (cloud_item, cloud_dirs, cloud_files), (local_item, local_dirs, local_files) in zip(
cloud_results, local_results
):
# check items returned by walk by stripping the root and comparing strings
assert _lstrip_path_root(cloud_item, cloud_root) == _lstrip_path_root(
local_item.as_posix(), local_root
)

assert cloud_dirs == local_dirs
assert local_files == cloud_files


def test_iterdir(glob_test_dirs):
cloud_root, local_root = glob_test_dirs

Expand All @@ -138,6 +152,14 @@ def test_iterdir(glob_test_dirs):
)


def test_walk(glob_test_dirs):
cloud_root, local_root = glob_test_dirs
_assert_walk_results_match(cloud_root.walk(), local_root.walk(), cloud_root, local_root)
_assert_walk_results_match(
cloud_root.walk(top_down=False), local_root.walk(top_down=False), cloud_root, local_root
)


def test_list_buckets(rig):
# test we can list buckets
buckets = list(rig.path_class(f"{rig.path_class.cloud_prefix}").iterdir())
Expand Down

0 comments on commit 9a28b27

Please sign in to comment.