|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import sys |
| 4 | +from typing import TYPE_CHECKING |
4 | 5 | from unittest.mock import patch |
5 | 6 |
|
6 | 7 | import pytest |
|
9 | 10 | from zarr.core.buffer.core import default_buffer_prototype |
10 | 11 | from zarr.storage._utils import ParsedStoreUrl, _normalize_byte_range_index, parse_store_url |
11 | 12 |
|
| 13 | +if TYPE_CHECKING: |
| 14 | + from pathlib import Path |
| 15 | + |
12 | 16 |
|
13 | 17 | class TestParseStoreUrl: |
14 | 18 | """Tests for parse_store_url.""" |
@@ -39,18 +43,60 @@ def test_s3_url(self) -> None: |
39 | 43 | scheme="s3", name="bucket", path="key", raw="s3://bucket/key" |
40 | 44 | ) |
41 | 45 |
|
42 | | - def test_file_url(self) -> None: |
43 | | - result = parse_store_url("file:///tmp/test") |
44 | | - assert result.scheme == "file" |
| 46 | + @pytest.mark.parametrize( |
| 47 | + ("url", "expected_path"), |
| 48 | + [ |
| 49 | + ("file:///tmp/test", "/tmp/test"), |
| 50 | + ("file:///a/b", "/a/b"), |
| 51 | + ("file:///a/b/", "/a/b"), |
| 52 | + ("file:///", "/"), |
| 53 | + ], |
| 54 | + ) |
| 55 | + def test_absolute_file_url(self, url: str, expected_path: str) -> None: |
| 56 | + """file:///a/b (empty netloc) is an absolute path.""" |
| 57 | + result = parse_store_url(url) |
| 58 | + assert result == ParsedStoreUrl(scheme="file", name=None, path=expected_path, raw=url) |
| 59 | + |
| 60 | + @pytest.mark.parametrize( |
| 61 | + ("url", "relative_path"), |
| 62 | + [ |
| 63 | + ("file://a/b", "a/b"), |
| 64 | + ("file://a/b/", "a/b"), |
| 65 | + ("file://a", "a"), |
| 66 | + ("file://./a/b", "a/b"), |
| 67 | + ("file://", ""), |
| 68 | + ], |
| 69 | + ) |
| 70 | + def test_relative_file_url( |
| 71 | + self, url: str, relative_path: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 72 | + ) -> None: |
| 73 | + """file://a/b is a path relative to the cwd, not a host named "a".""" |
| 74 | + monkeypatch.chdir(tmp_path) |
| 75 | + result = parse_store_url(url) |
| 76 | + assert result == ParsedStoreUrl( |
| 77 | + scheme="file", name=None, path=str(tmp_path / relative_path), raw=url |
| 78 | + ) |
| 79 | + |
| 80 | + @pytest.mark.parametrize( |
| 81 | + "url", |
| 82 | + [ |
| 83 | + "file:///a/b?query=1", |
| 84 | + "file:///a/b#fragment", |
| 85 | + ], |
| 86 | + ) |
| 87 | + def test_file_url_rejects_params_query_fragment(self, url: str) -> None: |
| 88 | + with pytest.raises(ValueError, match="not supported in file:// URLs"): |
| 89 | + parse_store_url(url) |
45 | 90 |
|
46 | 91 | def test_local_absolute_path(self) -> None: |
47 | 92 | result = parse_store_url("/local/path") |
48 | 93 | assert result == ParsedStoreUrl(scheme="", name=None, path="/local/path", raw="/local/path") |
49 | 94 |
|
50 | | - def test_local_relative_path(self) -> None: |
| 95 | + def test_local_relative_path(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: |
| 96 | + monkeypatch.chdir(tmp_path) |
51 | 97 | result = parse_store_url("relative/path") |
52 | 98 | assert result == ParsedStoreUrl( |
53 | | - scheme="", name=None, path="relative/path", raw="relative/path" |
| 99 | + scheme="", name=None, path=str(tmp_path / "relative/path"), raw="relative/path" |
54 | 100 | ) |
55 | 101 |
|
56 | 102 | @pytest.mark.parametrize( |
|
0 commit comments