|
1 | 1 | from enum import StrEnum |
| 2 | +import os |
| 3 | +from pathlib import Path |
2 | 4 |
|
3 | 5 | import click |
4 | 6 | from click.testing import CliRunner |
5 | 7 | import pytest |
6 | 8 |
|
7 | | -from ..base import EnumChoice |
| 9 | +from ..base import EnumChoice, LinkAwarePath |
8 | 10 |
|
9 | 11 |
|
10 | 12 | class _Existing(StrEnum): |
@@ -69,3 +71,40 @@ def cmd(existing): |
69 | 71 | r = CliRunner().invoke(cmd, []) |
70 | 72 | assert r.exit_code == 0, r.output |
71 | 73 | assert captured["existing"] is _Existing.SKIP |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.ai_generated |
| 77 | +def test_path_lexists_rejects_resolve_path(): |
| 78 | + with pytest.raises(ValueError, match="resolve_path"): |
| 79 | + LinkAwarePath(lexists=True, resolve_path=True) |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.ai_generated |
| 83 | +@pytest.mark.parametrize( |
| 84 | + "kwargs,broken_ok", |
| 85 | + [ |
| 86 | + ({"lexists": True}, True), |
| 87 | + ({"lexists": True, "exists": True}, False), |
| 88 | + ({"exists": True}, False), |
| 89 | + ], |
| 90 | +) |
| 91 | +def test_path_lexists(tmp_path: Path, kwargs: dict, broken_ok: bool) -> None: |
| 92 | + @click.command() |
| 93 | + @click.argument("path", type=LinkAwarePath(allow_dash=True, **kwargs)) |
| 94 | + def cmd(path): |
| 95 | + click.echo(f"got:{path}") |
| 96 | + |
| 97 | + (tmp_path / "file.txt").write_text("content") |
| 98 | + broken = tmp_path / "broken" |
| 99 | + try: |
| 100 | + os.symlink(tmp_path / "missing", broken) |
| 101 | + except OSError: |
| 102 | + pytest.skip("symlinks are not supported here") |
| 103 | + for ok in [str(tmp_path / "file.txt"), "-"] + ([str(broken)] if broken_ok else []): |
| 104 | + r = CliRunner().invoke(cmd, [ok]) |
| 105 | + assert r.exit_code == 0, r.output |
| 106 | + assert r.output == f"got:{ok}\n" |
| 107 | + for bad in [str(tmp_path / "missing")] + ([] if broken_ok else [str(broken)]): |
| 108 | + r = CliRunner().invoke(cmd, [bad]) |
| 109 | + assert r.exit_code == 2 |
| 110 | + assert f"Path {bad!r} does not exist." in r.output |
0 commit comments