|
| 1 | +import warnings |
| 2 | + |
1 | 3 | import fsspec |
2 | 4 | import pytest |
3 | 5 |
|
|
8 | 10 | from ..utils import OverrideMeta |
9 | 11 | from ..utils import extends_base |
10 | 12 | from ..utils import overrides_base |
| 13 | +from ..utils import posixify |
11 | 14 | from ..utils import skip_on_windows |
12 | 15 |
|
13 | 16 |
|
@@ -49,3 +52,56 @@ def test_mkdir_in_empty_bucket(docker_gcs): |
49 | 52 | endpoint_url=docker_gcs, |
50 | 53 | token="anon", |
51 | 54 | ).parent.mkdir(parents=True, exist_ok=True) |
| 55 | + |
| 56 | + |
| 57 | +@skip_on_windows |
| 58 | +@pytest.mark.xfail(reason="gcsfs returns isdir false") |
| 59 | +def test_copy__object_key_collides_with_dir_prefix(docker_gcs, tmp_path): |
| 60 | + gcs = fsspec.filesystem( |
| 61 | + "gcs", |
| 62 | + endpoint_url=docker_gcs, |
| 63 | + token="anon", |
| 64 | + use_listings_cache=False, |
| 65 | + ) |
| 66 | + bucket = "copy_into_collision_bucket" |
| 67 | + gcs.mkdir(bucket) |
| 68 | + # gcs.mkdir(bucket + "/src" + "/common_prefix/") |
| 69 | + # object under common prefix as key |
| 70 | + gcs.pipe_file(f"{bucket}/src/common_prefix", b"hello world") |
| 71 | + # store more objects with same prefix |
| 72 | + gcs.pipe_file(f"{bucket}/src/common_prefix/file1.txt", b"1") |
| 73 | + gcs.pipe_file(f"{bucket}/src/common_prefix/file2.txt", b"2") |
| 74 | + gcs.invalidate_cache() |
| 75 | + |
| 76 | + # make sure the sources have a collision |
| 77 | + assert gcs.isfile(f"{bucket}/src/common_prefix") |
| 78 | + assert gcs.isdir(f"{bucket}/src/common_prefix") # BROKEN in gcsfs |
| 79 | + assert gcs.isfile(f"{bucket}/src/common_prefix/file1.txt") |
| 80 | + assert gcs.isfile(f"{bucket}/src/common_prefix/file2.txt") |
| 81 | + # prepare source and destination |
| 82 | + src = UPath(f"gs://{bucket}/src", endpoint_url=docker_gcs, token="anon") |
| 83 | + dst = UPath(tmp_path) |
| 84 | + |
| 85 | + def on_collision_rename_file(src, dst): |
| 86 | + warnings.warn( |
| 87 | + f"{src!s} collides with prefix. Renaming target file object to {dst!s}", |
| 88 | + UserWarning, |
| 89 | + stacklevel=3, |
| 90 | + ) |
| 91 | + return ( |
| 92 | + dst.with_suffix(dst.suffix + ".COLLISION"), |
| 93 | + dst, |
| 94 | + ) |
| 95 | + |
| 96 | + # perform copy |
| 97 | + src.copy_into(dst, on_name_collision=on_collision_rename_file) |
| 98 | + |
| 99 | + # check results |
| 100 | + dst_files = sorted(posixify(x.relative_to(tmp_path)) for x in dst.glob("**/*")) |
| 101 | + assert dst_files == [ |
| 102 | + "src", |
| 103 | + "src/common_prefix", |
| 104 | + "src/common_prefix.COLLISION", |
| 105 | + "src/common_prefix/file1.txt", |
| 106 | + "src/common_prefix/file2.txt", |
| 107 | + ] |
0 commit comments