|
| 1 | +import warnings |
| 2 | + |
| 3 | +import fsspec |
1 | 4 | import pytest |
2 | 5 |
|
3 | 6 | from upath import UPath |
|
7 | 10 | from ..utils import OverrideMeta |
8 | 11 | from ..utils import extends_base |
9 | 12 | from ..utils import overrides_base |
| 13 | +from ..utils import posixify |
10 | 14 | from ..utils import skip_on_windows |
11 | 15 |
|
12 | 16 |
|
@@ -61,3 +65,56 @@ def test_broken_mkdir(self): |
61 | 65 |
|
62 | 66 | (path / "file").write_text("foo") |
63 | 67 | assert path.exists() |
| 68 | + |
| 69 | + |
| 70 | +@skip_on_windows |
| 71 | +@pytest.mark.xfail(reason="adlfs returns isdir false") |
| 72 | +def test_copy__object_key_collides_with_dir_prefix(azurite_credentials, tmp_path): |
| 73 | + account_name, connection_string = azurite_credentials |
| 74 | + storage_options = { |
| 75 | + "account_name": account_name, |
| 76 | + "connection_string": connection_string, |
| 77 | + } |
| 78 | + |
| 79 | + az = fsspec.filesystem("az", **storage_options, use_listings_cache=False) |
| 80 | + container = "copy-into-collision-container" |
| 81 | + az.mkdir(container) |
| 82 | + # store more objects with same prefix |
| 83 | + az.pipe_file(f"{container}/src/common_prefix/file1.txt", b"1") |
| 84 | + az.pipe_file(f"{container}/src/common_prefix/file2.txt", b"2") |
| 85 | + # object under common prefix as key |
| 86 | + az.pipe_file(f"{container}/src/common_prefix", b"hello world") |
| 87 | + az.invalidate_cache() |
| 88 | + |
| 89 | + # make sure the sources have a collision |
| 90 | + assert az.isfile(f"{container}/src/common_prefix") |
| 91 | + assert az.isdir(f"{container}/src/common_prefix") |
| 92 | + assert az.isfile(f"{container}/src/common_prefix/file1.txt") |
| 93 | + assert az.isfile(f"{container}/src/common_prefix/file2.txt") |
| 94 | + # prepare source and destination |
| 95 | + src = UPath(f"az://{container}/src", **storage_options) |
| 96 | + dst = UPath(tmp_path) |
| 97 | + |
| 98 | + def on_collision_rename_file(src, dst): |
| 99 | + warnings.warn( |
| 100 | + f"{src!s} collides with prefix. Renaming target file object to {dst!s}", |
| 101 | + UserWarning, |
| 102 | + stacklevel=3, |
| 103 | + ) |
| 104 | + return ( |
| 105 | + dst.with_suffix(dst.suffix + ".COLLISION"), |
| 106 | + dst, |
| 107 | + ) |
| 108 | + |
| 109 | + # perform copy |
| 110 | + src.copy_into(dst, on_name_collision=on_collision_rename_file) |
| 111 | + |
| 112 | + # check results |
| 113 | + dst_files = sorted(posixify(x.relative_to(tmp_path)) for x in dst.glob("**/*")) |
| 114 | + assert dst_files == [ |
| 115 | + "src", |
| 116 | + "src/common_prefix", |
| 117 | + "src/common_prefix.COLLISION", |
| 118 | + "src/common_prefix/file1.txt", |
| 119 | + "src/common_prefix/file2.txt", |
| 120 | + ] |
0 commit comments