asanypath 2.4.0__tar.gz → 2.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. {asanypath-2.4.0 → asanypath-2.5.0}/PKG-INFO +31 -2
  2. {asanypath-2.4.0 → asanypath-2.5.0}/README.md +29 -0
  3. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/_version.py +2 -2
  4. asanypath-2.5.0/src/asanypath/click.py +108 -0
  5. asanypath-2.5.0/tests/test_click.py +227 -0
  6. {asanypath-2.4.0 → asanypath-2.5.0}/.copilot/skills/async-python-lib/SKILL.md +0 -0
  7. {asanypath-2.4.0 → asanypath-2.5.0}/.copilot/skills/gitguro/SKILL.md +0 -0
  8. {asanypath-2.4.0 → asanypath-2.5.0}/.github/CODEOWNERS +0 -0
  9. {asanypath-2.4.0 → asanypath-2.5.0}/.github/ISSUE_TEMPLATE/bug_report.md +0 -0
  10. {asanypath-2.4.0 → asanypath-2.5.0}/.github/ISSUE_TEMPLATE/feature_request.md +0 -0
  11. {asanypath-2.4.0 → asanypath-2.5.0}/.github/PULL_REQUEST_TEMPLATE.md +0 -0
  12. {asanypath-2.4.0 → asanypath-2.5.0}/.github/dependabot.yml +0 -0
  13. {asanypath-2.4.0 → asanypath-2.5.0}/.github/workflows/publish.yml +0 -0
  14. {asanypath-2.4.0 → asanypath-2.5.0}/.github/workflows/unittests.yml +0 -0
  15. {asanypath-2.4.0 → asanypath-2.5.0}/.gitignore +0 -0
  16. {asanypath-2.4.0 → asanypath-2.5.0}/.pre-commit-config.yaml +0 -0
  17. {asanypath-2.4.0 → asanypath-2.5.0}/CODE_OF_CONDUCT.md +0 -0
  18. {asanypath-2.4.0 → asanypath-2.5.0}/CONTRIBUTING.md +0 -0
  19. {asanypath-2.4.0 → asanypath-2.5.0}/LICENSE +0 -0
  20. {asanypath-2.4.0 → asanypath-2.5.0}/asanypath.png +0 -0
  21. {asanypath-2.4.0 → asanypath-2.5.0}/compose.yml +0 -0
  22. {asanypath-2.4.0 → asanypath-2.5.0}/hatch_build.py +0 -0
  23. {asanypath-2.4.0 → asanypath-2.5.0}/pyproject.toml +0 -0
  24. {asanypath-2.4.0 → asanypath-2.5.0}/scripts/bench.py +0 -0
  25. {asanypath-2.4.0 → asanypath-2.5.0}/scripts/bench_upload.py +0 -0
  26. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/__init__.py +0 -0
  27. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/__init__.pyi +0 -0
  28. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/_backend_opts.py +0 -0
  29. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/_credstore.py +0 -0
  30. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/_prompt.py +0 -0
  31. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/_resume.py +0 -0
  32. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/_transfer.py +0 -0
  33. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/artifactory.py +0 -0
  34. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/asanypath.py +0 -0
  35. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/asanypath.pyi +0 -0
  36. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/azure.py +0 -0
  37. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/batcher.py +0 -0
  38. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/cache.py +0 -0
  39. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/cli.py +0 -0
  40. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/cloud.py +0 -0
  41. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/common.py +0 -0
  42. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/exceptions.py +0 -0
  43. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/ftp.py +0 -0
  44. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/gcs.py +0 -0
  45. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/http.py +0 -0
  46. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/local.py +0 -0
  47. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/options.py +0 -0
  48. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/py.typed +0 -0
  49. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/s3.py +0 -0
  50. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/ssh.py +0 -0
  51. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/sync.py +0 -0
  52. {asanypath-2.4.0 → asanypath-2.5.0}/src/asanypath/unsupported.py +0 -0
  53. {asanypath-2.4.0 → asanypath-2.5.0}/tests/__init__.py +0 -0
  54. {asanypath-2.4.0 → asanypath-2.5.0}/tests/conftest.py +0 -0
  55. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_artifactory.py +0 -0
  56. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_asanypath.py +0 -0
  57. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_azure.py +0 -0
  58. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_backend_options.py +0 -0
  59. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_batcher.py +0 -0
  60. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_cache.py +0 -0
  61. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_cli.py +0 -0
  62. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_cloudpathmixin.py +0 -0
  63. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_commonpathmixin.py +0 -0
  64. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_credstore.py +0 -0
  65. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_endpoints.py +0 -0
  66. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_ftp.py +0 -0
  67. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_gcs.py +0 -0
  68. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_http.py +0 -0
  69. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_http_auth.py +0 -0
  70. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_isolation.py +0 -0
  71. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_local.py +0 -0
  72. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_options.py +0 -0
  73. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_py_typed.py +0 -0
  74. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_resume.py +0 -0
  75. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_s3.py +0 -0
  76. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_scandir.py +0 -0
  77. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_ssh.py +0 -0
  78. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_ssh_hostkeys.py +0 -0
  79. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_sync.py +0 -0
  80. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_syncpath.py +0 -0
  81. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_transport_hardening.py +0 -0
  82. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_unsupported_path.py +0 -0
  83. {asanypath-2.4.0 → asanypath-2.5.0}/tests/test_upload.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: asanypath
3
- Version: 2.4.0
3
+ Version: 2.5.0
4
4
  Summary: Async pathlib for local and cloud storage
5
5
  License-Expression: MIT
6
6
  License-File: LICENSE
@@ -19,7 +19,7 @@ Classifier: Programming Language :: Python :: 3.14
19
19
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
20
  Requires-Python: >=3.10
21
21
  Requires-Dist: anyio>=4
22
- Requires-Dist: asanypath-native~=2.4.0
22
+ Requires-Dist: asanypath-native~=2.5.0
23
23
  Requires-Dist: msgspec>=0.22.0
24
24
  Requires-Dist: yarl>=1.2
25
25
  Provides-Extra: all
@@ -599,6 +599,35 @@ asanypath rm s3://[TAB] # Lists buckets
599
599
 
600
600
  Completion respects environment variables for cloud credentials (e.g., `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` for S3). Bearer token auth (for custom endpoints) is not supported in completions; only env var-based auth is used.
601
601
 
602
+ ### Path options in your own click CLI
603
+
604
+ `click.Path` validates with `os.stat()`, so with `path_type=AsAnyPath` its
605
+ `exists`, `file_okay` and `dir_okay` checks are skipped for remote URIs
606
+ (`exists=False`) or always fail (`exists=True`). Use `ClickPath` instead
607
+ (requires the `cli` extra):
608
+
609
+ ```python
610
+ import click
611
+ from asanypath.click import ClickPath
612
+
613
+ @click.command()
614
+ @click.option("--model", type=ClickPath(exists=True, dir_okay=False))
615
+ @click.option("--out", type=ClickPath(file_okay=False))
616
+ def main(model, out):
617
+ ... # model and out are sync AsAnyPath objects
618
+ ```
619
+
620
+ `ClickPath` takes the same arguments as `click.Path`; `path_type` defaults to
621
+ `asanypath.sync.AsAnyPath`. Local paths (including `file://` URIs) behave exactly
622
+ like `click.Path`. Remote paths are checked with `exists()`, `is_dir()` and
623
+ `is_file()` while arguments are parsed:
624
+
625
+ - `file_okay=False` / `dir_okay=False` reject a path only when it is exclusively
626
+ the disallowed kind; an object-store key can be both a file and a directory.
627
+ - `readable`, `writable`, `executable` and `resolve_path` apply to local paths only.
628
+ - If the backend cannot be queried (credentials, network, unsupported protocol),
629
+ `exists=True` reports the error and `exists=False` accepts the path unchecked.
630
+
602
631
  ## Benchmarks
603
632
 
604
633
  Multi-backend benchmark suite in `scripts/bench.py` compares asanypath against
@@ -543,6 +543,35 @@ asanypath rm s3://[TAB] # Lists buckets
543
543
 
544
544
  Completion respects environment variables for cloud credentials (e.g., `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` for S3). Bearer token auth (for custom endpoints) is not supported in completions; only env var-based auth is used.
545
545
 
546
+ ### Path options in your own click CLI
547
+
548
+ `click.Path` validates with `os.stat()`, so with `path_type=AsAnyPath` its
549
+ `exists`, `file_okay` and `dir_okay` checks are skipped for remote URIs
550
+ (`exists=False`) or always fail (`exists=True`). Use `ClickPath` instead
551
+ (requires the `cli` extra):
552
+
553
+ ```python
554
+ import click
555
+ from asanypath.click import ClickPath
556
+
557
+ @click.command()
558
+ @click.option("--model", type=ClickPath(exists=True, dir_okay=False))
559
+ @click.option("--out", type=ClickPath(file_okay=False))
560
+ def main(model, out):
561
+ ... # model and out are sync AsAnyPath objects
562
+ ```
563
+
564
+ `ClickPath` takes the same arguments as `click.Path`; `path_type` defaults to
565
+ `asanypath.sync.AsAnyPath`. Local paths (including `file://` URIs) behave exactly
566
+ like `click.Path`. Remote paths are checked with `exists()`, `is_dir()` and
567
+ `is_file()` while arguments are parsed:
568
+
569
+ - `file_okay=False` / `dir_okay=False` reject a path only when it is exclusively
570
+ the disallowed kind; an object-store key can be both a file and a directory.
571
+ - `readable`, `writable`, `executable` and `resolve_path` apply to local paths only.
572
+ - If the backend cannot be queried (credentials, network, unsupported protocol),
573
+ `exists=True` reports the error and `exists=False` accepts the path unchecked.
574
+
546
575
  ## Benchmarks
547
576
 
548
577
  Multi-backend benchmark suite in `scripts/bench.py` compares asanypath against
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
18
18
  commit_id: str | None
19
19
  __commit_id__: str | None
20
20
 
21
- __version__ = version = '2.4.0'
22
- __version_tuple__ = version_tuple = (2, 4, 0)
21
+ __version__ = version = '2.5.0'
22
+ __version_tuple__ = version_tuple = (2, 5, 0)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -0,0 +1,108 @@
1
+ # Copyright (C) 2026 Axis Communications AB, Lund, Sweden
2
+ #
3
+ # Use of this source code is governed by an MIT-style
4
+ # license that can be found in the LICENSE file or at
5
+ # https://opensource.org/licenses/MIT.
6
+
7
+ """click parameter type for local and remote paths.
8
+
9
+ ``click.Path`` validates with ``os.stat()``, so for remote URIs such as
10
+ ``s3://bucket/key`` its ``exists``, ``file_okay`` and ``dir_okay`` checks are
11
+ skipped (``exists=False``) or always fail (``exists=True``), even with
12
+ ``path_type=AsAnyPath``. :class:`ClickPath` validates remote paths through
13
+ asanypath and leaves local paths to ``click.Path``.
14
+
15
+ Usage::
16
+
17
+ import click
18
+ from asanypath.click import ClickPath
19
+
20
+ @click.command()
21
+ @click.option("--src", type=ClickPath(exists=True, dir_okay=False))
22
+ @click.option("--out", type=ClickPath(file_okay=False))
23
+ def main(src, out):
24
+ ...
25
+
26
+ Requires click, e.g. through the ``cli`` extra: ``asanypath[cli]``.
27
+ """
28
+
29
+ from __future__ import annotations
30
+
31
+ import os
32
+ from typing import Any
33
+
34
+ try:
35
+ import click
36
+ except ImportError as exc: # pragma: no cover
37
+ raise ImportError(
38
+ "asanypath.click requires click; install asanypath with the 'cli' extra"
39
+ ) from exc
40
+
41
+ from asanypath.exceptions import AsAnyPathError
42
+ from asanypath.local import SyncPath
43
+ from asanypath.sync import AsAnyPath
44
+
45
+ __all__ = ["ClickPath"]
46
+
47
+
48
+ class ClickPath(click.Path):
49
+ """``click.Path`` that also validates remote AsAnyPath URIs.
50
+
51
+ Takes the same arguments as ``click.Path``. ``path_type`` defaults to the
52
+ synchronous :class:`asanypath.sync.AsAnyPath`.
53
+
54
+ Local paths, including ``file://`` URIs, behave exactly like ``click.Path``.
55
+ For remote paths:
56
+
57
+ * ``exists=True`` requires an object or a non-empty prefix at the path.
58
+ * ``file_okay=False`` rejects a path that is only a file and ``dir_okay=False``
59
+ a path that is only a directory. On object stores a key can be both.
60
+ * ``readable``, ``writable``, ``executable`` and ``resolve_path`` are not
61
+ checked.
62
+ * If the backend cannot be queried (missing credentials, network errors,
63
+ unsupported protocol), ``exists=True`` reports the error and ``exists=False``
64
+ accepts the path unchecked, as ``click.Path`` does for a local ``OSError``.
65
+
66
+ Remote checks send requests while arguments are parsed: ``exists()``, plus
67
+ ``is_dir()`` or ``is_file()`` when a kind is disallowed.
68
+ """
69
+
70
+ def __init__(self, **kwargs: Any) -> None:
71
+ kwargs.setdefault("path_type", AsAnyPath)
72
+ super().__init__(**kwargs)
73
+
74
+ def convert(self, value: Any, param: click.Parameter | None, ctx: click.Context | None) -> Any:
75
+ try:
76
+ path = AsAnyPath(value)
77
+ except AsAnyPathError as exc:
78
+ self.fail(str(exc), param, ctx)
79
+
80
+ if isinstance(path, SyncPath):
81
+ return super().convert(os.fspath(path), param, ctx)
82
+
83
+ label = f"{self.name.title()} {click.utils.format_filename(str(path))!r}"
84
+ try:
85
+ if not path.exists():
86
+ if self.exists:
87
+ self.fail(f"{label} does not exist.", param, ctx)
88
+ return self._coerce(path)
89
+ if not self.file_okay and not path.is_dir():
90
+ self.fail(f"{label} is a file.", param, ctx)
91
+ if not self.dir_okay and not path.is_file():
92
+ self.fail(f"{label} is a directory.", param, ctx)
93
+ except click.BadParameter:
94
+ raise
95
+ except Exception as exc: # backends raise different types for auth/network errors
96
+ if self.exists:
97
+ self.fail(f"{label} could not be checked: {exc}", param, ctx)
98
+ return self._coerce(path)
99
+
100
+ def _coerce(self, path: Any) -> Any:
101
+ """Return ``path`` as ``path_type``; remote paths have no ``os.fspath()``."""
102
+ if self.type is None or self.type is str:
103
+ return str(path)
104
+ if self.type is bytes:
105
+ return os.fsencode(str(path))
106
+ if isinstance(path, self.type):
107
+ return path
108
+ return self.type(str(path))
@@ -0,0 +1,227 @@
1
+ # Copyright (C) 2026 Axis Communications AB, Lund, Sweden
2
+ #
3
+ # Use of this source code is governed by an MIT-style
4
+ # license that can be found in the LICENSE file or at
5
+ # https://opensource.org/licenses/MIT.
6
+
7
+ import subprocess
8
+ import sys
9
+
10
+ import click
11
+ import pytest
12
+ from click.testing import CliRunner
13
+
14
+ from asanypath.click import ClickPath
15
+ from asanypath.local import SyncPath
16
+ from asanypath.s3 import S3Path
17
+ from asanypath.sync import AsAnyPath
18
+
19
+
20
+ def invoke(param_type, *args):
21
+ """Run a command taking any number of ``param_type`` arguments; return (result, values)."""
22
+ seen = {}
23
+
24
+ @click.command()
25
+ @click.argument("paths", nargs=-1, type=param_type)
26
+ def cmd(paths):
27
+ seen["paths"] = paths
28
+
29
+ result = CliRunner().invoke(cmd, list(args))
30
+ return result, seen.get("paths")
31
+
32
+
33
+ class FakeStore(dict):
34
+ """Maps URI -> (is_file, is_dir) and records the backend calls made."""
35
+
36
+ def __init__(self):
37
+ super().__init__()
38
+ self.calls = []
39
+
40
+
41
+ @pytest.fixture
42
+ def fake_s3(monkeypatch):
43
+ store = FakeStore()
44
+
45
+ def fake(method):
46
+ async def check(self):
47
+ store.calls.append((method, str(self)))
48
+ is_file, is_dir = store.get(str(self), (False, False))
49
+ return {"exists": is_file or is_dir, "is_file": is_file, "is_dir": is_dir}[method]
50
+
51
+ return check
52
+
53
+ for method in ("exists", "is_file", "is_dir"):
54
+ monkeypatch.setattr(S3Path, method, fake(method))
55
+ return store
56
+
57
+
58
+ # --- local paths: same behaviour as click.Path ---------------------------------------
59
+
60
+
61
+ def test_local_returns_syncpath(tmp_path):
62
+ (tmp_path / "f.txt").write_text("x")
63
+ result, paths = invoke(ClickPath(exists=True), str(tmp_path / "f.txt"))
64
+ assert result.exit_code == 0, result.output
65
+ assert isinstance(paths[0], SyncPath)
66
+ assert isinstance(paths[0], AsAnyPath)
67
+ assert paths[0] == SyncPath(tmp_path / "f.txt")
68
+
69
+
70
+ def test_local_missing_with_exists_fails(tmp_path):
71
+ result, _ = invoke(ClickPath(exists=True), str(tmp_path / "missing"))
72
+ assert result.exit_code == 2
73
+ assert "does not exist" in result.output
74
+
75
+
76
+ def test_local_missing_without_exists_is_accepted(tmp_path):
77
+ result, paths = invoke(ClickPath(), str(tmp_path / "missing"))
78
+ assert result.exit_code == 0, result.output
79
+ assert paths[0] == SyncPath(tmp_path / "missing")
80
+
81
+
82
+ def test_local_kind_checks(tmp_path):
83
+ (tmp_path / "f.txt").write_text("x")
84
+ result, _ = invoke(ClickPath(file_okay=False), str(tmp_path / "f.txt"))
85
+ assert result.exit_code == 2
86
+ assert "is a file" in result.output
87
+ result, _ = invoke(ClickPath(dir_okay=False), str(tmp_path))
88
+ assert result.exit_code == 2
89
+ assert "is a directory" in result.output
90
+
91
+
92
+ def test_local_file_uri(tmp_path):
93
+ (tmp_path / "f.txt").write_text("x")
94
+ result, paths = invoke(ClickPath(exists=True, dir_okay=False), (tmp_path / "f.txt").as_uri())
95
+ assert result.exit_code == 0, result.output
96
+ assert paths[0] == SyncPath(tmp_path / "f.txt")
97
+
98
+
99
+ def test_local_resolve_path(tmp_path, monkeypatch):
100
+ monkeypatch.chdir(tmp_path)
101
+ result, paths = invoke(ClickPath(resolve_path=True), "rel/x")
102
+ assert result.exit_code == 0, result.output
103
+ assert paths[0] == SyncPath(tmp_path / "rel" / "x")
104
+
105
+
106
+ def test_local_dash():
107
+ result, paths = invoke(ClickPath(allow_dash=True), "-")
108
+ assert result.exit_code == 0, result.output
109
+ assert str(paths[0]) == "-"
110
+
111
+
112
+ # --- remote paths ----------------------------------------------------------------------
113
+
114
+
115
+ def test_remote_returns_asanypath(fake_s3):
116
+ fake_s3["s3://bucket/key"] = (True, False)
117
+ result, paths = invoke(ClickPath(exists=True), "s3://bucket/key")
118
+ assert result.exit_code == 0, result.output
119
+ assert isinstance(paths[0], AsAnyPath)
120
+ assert str(paths[0]) == "s3://bucket/key"
121
+
122
+
123
+ def test_remote_missing_with_exists_fails(fake_s3):
124
+ result, _ = invoke(ClickPath(exists=True), "s3://bucket/missing")
125
+ assert result.exit_code == 2
126
+ assert "'s3://bucket/missing' does not exist" in result.output
127
+
128
+
129
+ def test_remote_missing_without_exists_is_accepted(fake_s3):
130
+ result, paths = invoke(ClickPath(file_okay=False), "s3://bucket/new/")
131
+ assert result.exit_code == 0, result.output
132
+ assert str(paths[0]) == "s3://bucket/new"
133
+
134
+
135
+ def test_remote_file_rejected_when_file_not_okay(fake_s3):
136
+ fake_s3["s3://bucket/key"] = (True, False)
137
+ result, _ = invoke(ClickPath(file_okay=False), "s3://bucket/key")
138
+ assert result.exit_code == 2
139
+ assert "Directory 's3://bucket/key' is a file" in result.output
140
+
141
+
142
+ def test_remote_dir_rejected_when_dir_not_okay(fake_s3):
143
+ fake_s3["s3://bucket/prefix"] = (False, True)
144
+ result, _ = invoke(ClickPath(dir_okay=False), "s3://bucket/prefix")
145
+ assert result.exit_code == 2
146
+ assert "File 's3://bucket/prefix' is a directory" in result.output
147
+
148
+
149
+ @pytest.mark.parametrize("kwargs", [{"file_okay": False}, {"dir_okay": False}])
150
+ def test_remote_key_that_is_file_and_dir_is_accepted(fake_s3, kwargs):
151
+ fake_s3["s3://bucket/both"] = (True, True)
152
+ result, _ = invoke(ClickPath(exists=True, **kwargs), "s3://bucket/both")
153
+ assert result.exit_code == 0, result.output
154
+
155
+
156
+ def test_remote_exists_only_costs_one_request(fake_s3):
157
+ fake_s3["s3://bucket/key"] = (True, False)
158
+ result, _ = invoke(ClickPath(exists=True), "s3://bucket/key")
159
+ assert result.exit_code == 0, result.output
160
+ assert fake_s3.calls == [("exists", "s3://bucket/key")]
161
+
162
+
163
+ def test_remote_backend_error(monkeypatch):
164
+ async def broken(self):
165
+ raise RuntimeError("no credentials")
166
+
167
+ monkeypatch.setattr(S3Path, "exists", broken)
168
+ result, paths = invoke(ClickPath(), "s3://bucket/key")
169
+ assert result.exit_code == 0, result.output
170
+ assert str(paths[0]) == "s3://bucket/key"
171
+
172
+ result, _ = invoke(ClickPath(exists=True), "s3://bucket/key")
173
+ assert result.exit_code == 2
174
+ assert "could not be checked: no credentials" in result.output
175
+
176
+
177
+ def test_unsupported_protocol():
178
+ result, paths = invoke(ClickPath(), "nope://host/x")
179
+ assert result.exit_code == 0, result.output
180
+ assert str(paths[0]) == "nope://host/x"
181
+
182
+ result, _ = invoke(ClickPath(exists=True), "nope://host/x")
183
+ assert result.exit_code == 2
184
+ assert "Unsupported protocol: nope" in result.output
185
+
186
+
187
+ @pytest.mark.parametrize(
188
+ "path_type, expected", [(str, "s3://bucket/key"), (bytes, b"s3://bucket/key")]
189
+ )
190
+ def test_remote_path_type(fake_s3, path_type, expected):
191
+ result, paths = invoke(ClickPath(path_type=path_type), "s3://bucket/key")
192
+ assert result.exit_code == 0, result.output
193
+ assert paths[0] == expected
194
+
195
+
196
+ def test_mixed_local_and_remote(fake_s3, tmp_path):
197
+ fake_s3["s3://bucket/key"] = (True, False)
198
+ result, paths = invoke(ClickPath(exists=True), str(tmp_path), "s3://bucket/key")
199
+ assert result.exit_code == 0, result.output
200
+ assert [str(p) for p in paths] == [str(tmp_path), "s3://bucket/key"]
201
+
202
+
203
+ def test_default_value_is_converted(fake_s3):
204
+ fake_s3["s3://bucket/default"] = (False, True)
205
+ seen = {}
206
+
207
+ @click.command()
208
+ @click.option(
209
+ "--out", type=ClickPath(exists=True, file_okay=False), default="s3://bucket/default"
210
+ )
211
+ def cmd(out):
212
+ seen["out"] = out
213
+
214
+ result = CliRunner().invoke(cmd, [])
215
+ assert result.exit_code == 0, result.output
216
+ assert isinstance(seen["out"], AsAnyPath)
217
+ assert str(seen["out"]) == "s3://bucket/default"
218
+
219
+
220
+ def test_import_asanypath_does_not_import_click():
221
+ out = subprocess.run(
222
+ [sys.executable, "-c", "import sys, asanypath; print('click' in sys.modules)"],
223
+ check=True,
224
+ capture_output=True,
225
+ text=True,
226
+ ).stdout.strip()
227
+ assert out == "False"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes