From 9a28b27bd4ef0bbbc81f256402a2eaf6e46cfe3b Mon Sep 17 00:00:00 2001 From: Peter Bull Date: Sat, 7 Oct 2023 21:55:08 -0700 Subject: [PATCH] Test walk method --- tests/test_cloudpath_file_io.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/tests/test_cloudpath_file_io.py b/tests/test_cloudpath_file_io.py index 8b9a7051..e88c37ce 100644 --- a/tests/test_cloudpath_file_io.py +++ b/tests/test_cloudpath_file_io.py @@ -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] @@ -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 @@ -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())