Skip to content

Commit aef48e5

Browse files
committed
tests: add GCSPath test for copy_into (xfail)
1 parent 1f3a93f commit aef48e5

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

upath/tests/implementations/test_gcs.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import fsspec
24
import pytest
35

@@ -8,6 +10,7 @@
810
from ..utils import OverrideMeta
911
from ..utils import extends_base
1012
from ..utils import overrides_base
13+
from ..utils import posixify
1114
from ..utils import skip_on_windows
1215

1316

@@ -49,3 +52,56 @@ def test_mkdir_in_empty_bucket(docker_gcs):
4952
endpoint_url=docker_gcs,
5053
token="anon",
5154
).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

Comments
 (0)