|
1 | 1 | import os |
2 | 2 | import pickle |
| 3 | +import platform |
3 | 4 | import sys |
| 5 | +import uuid |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
@@ -149,3 +151,66 @@ def test_setitem_numpy(): |
149 | 151 | def test_empty_url(): |
150 | 152 | m = fsspec.get_mapper() |
151 | 153 | assert isinstance(m.fs, LocalFileSystem) |
| 154 | + |
| 155 | + |
| 156 | +def test_fsmap_access_with_root_prefix(tmp_path): |
| 157 | + # "/a" and "a" are the same for LocalFileSystem |
| 158 | + tmp_path.joinpath("a").write_bytes(b"data") |
| 159 | + m = fsspec.get_mapper(f"file://{tmp_path}") |
| 160 | + assert m["/a"] == m["a"] == b"data" |
| 161 | + |
| 162 | + # "/a" and "a" differ for MemoryFileSystem |
| 163 | + m = fsspec.get_mapper(f"memory://{uuid.uuid4()}") |
| 164 | + m["/a"] = b"data" |
| 165 | + |
| 166 | + assert m["/a"] == b"data" |
| 167 | + with pytest.raises(KeyError): |
| 168 | + _ = m["a"] |
| 169 | + |
| 170 | + |
| 171 | +@pytest.mark.parametrize( |
| 172 | + "key", |
| 173 | + [ |
| 174 | + pytest.param(b"k", id="bytes"), |
| 175 | + pytest.param(1234, id="int"), |
| 176 | + pytest.param((1,), id="tuple"), |
| 177 | + pytest.param([""], id="list"), |
| 178 | + ], |
| 179 | +) |
| 180 | +def test_fsmap_non_str_keys(key): |
| 181 | + m = fsspec.get_mapper() |
| 182 | + |
| 183 | + # Once the deprecation period passes |
| 184 | + # FSMap.__getitem__ should raise TypeError for non-str keys |
| 185 | + # with pytest.raises(TypeError): |
| 186 | + # _ = m[key] |
| 187 | + |
| 188 | + with pytest.warns(DeprecationWarning): |
| 189 | + with pytest.raises(KeyError): |
| 190 | + _ = m[key] |
| 191 | + |
| 192 | + |
| 193 | +def test_fsmap_error_on_protocol_keys(): |
| 194 | + root = uuid.uuid4() |
| 195 | + m = fsspec.get_mapper(f"memory://{root}", create=True) |
| 196 | + m["a"] = b"data" |
| 197 | + |
| 198 | + assert m["a"] == b"data" |
| 199 | + with pytest.raises(KeyError): |
| 200 | + _ = m[f"memory://{root}/a"] |
| 201 | + |
| 202 | + |
| 203 | +# on Windows opening a directory will raise PermissionError |
| 204 | +# see: https://bugs.python.org/issue43095 |
| 205 | +@pytest.mark.skipif( |
| 206 | + platform.system() == "Windows", reason="raises PermissionError on windows" |
| 207 | +) |
| 208 | +def test_fsmap_access_with_suffix(tmp_path): |
| 209 | + tmp_path.joinpath("b").mkdir() |
| 210 | + tmp_path.joinpath("b", "a").write_bytes(b"data") |
| 211 | + m = fsspec.get_mapper(f"file://{tmp_path}") |
| 212 | + |
| 213 | + with pytest.raises(KeyError): |
| 214 | + _ = m["b/"] |
| 215 | + |
| 216 | + assert m["b/a/"] == b"data" |
0 commit comments