| 1 |
"""Tests for StdioPath, a Path subclass that treats "-" as stdin/stdout.""" |
| 2 |
|
| 3 |
import sys |
| 4 |
from io import BytesIO, StringIO |
| 5 |
from pathlib import Path |
| 6 |
from typing import Annotated |
| 7 |
|
| 8 |
from cyclopts import Parameter |
| 9 |
from cyclopts.types import StdioPath |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
def test_stdio_path_dash_is_stdio(): |
| 16 |
p = StdioPath("-") |
| 17 |
assert p.is_stdio is True |
| 18 |
|
| 19 |
|
| 20 |
def test_stdio_path_regular_is_not_stdio(): |
| 21 |
p = StdioPath("/tmp/test.txt") |
| 22 |
assert p.is_stdio is False |
| 23 |
|
| 24 |
|
| 25 |
def test_stdio_path_isinstance_path(): |
| 26 |
p = StdioPath("-") |
| 27 |
assert isinstance(p, Path) |
| 28 |
|
| 29 |
|
| 30 |
def test_stdio_path_str_dash(): |
| 31 |
p = StdioPath("-") |
| 32 |
assert str(p) == "-" |
| 33 |
|
| 34 |
|
| 35 |
def test_stdio_path_str_regular(): |
| 36 |
path_str = "/tmp/test.txt" |
| 37 |
p = StdioPath(path_str) |
| 38 |
|
| 39 |
assert str(p) == str(Path(path_str)) |
| 40 |
|
| 41 |
|
| 42 |
def test_stdio_path_repr_dash(): |
| 43 |
p = StdioPath("-") |
| 44 |
assert repr(p) == "StdioPath('-')" |
| 45 |
|
| 46 |
|
| 47 |
def test_stdio_path_repr_regular(): |
| 48 |
path_str = "/tmp/test.txt" |
| 49 |
p = StdioPath(path_str) |
| 50 |
|
| 51 |
assert repr(p) == f"StdioPath({str(Path(path_str))!r})" |
| 52 |
|
| 53 |
|
| 54 |
def test_stdio_path_fspath_dash(): |
| 55 |
import os |
| 56 |
|
| 57 |
p = StdioPath("-") |
| 58 |
assert os.fspath(p) == "-" |
| 59 |
|
| 60 |
|
| 61 |
def test_stdio_path_fspath_regular(): |
| 62 |
import os |
| 63 |
|
| 64 |
path_str = "/tmp/test.txt" |
| 65 |
p = StdioPath(path_str) |
| 66 |
|
| 67 |
assert os.fspath(p) == os.fspath(Path(path_str)) |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
def test_stdio_path_exists_always_true_for_dash(): |
| 74 |
p = StdioPath("-") |
| 75 |
assert p.exists() is True |
| 76 |
|
| 77 |
|
| 78 |
def test_stdio_path_exists_false_for_nonexistent(tmp_path): |
| 79 |
p = StdioPath(tmp_path / "nonexistent.txt") |
| 80 |
assert p.exists() is False |
| 81 |
|
| 82 |
|
| 83 |
def test_stdio_path_exists_true_for_existing(tmp_path): |
| 84 |
f = tmp_path / "existing.txt" |
| 85 |
f.touch() |
| 86 |
p = StdioPath(f) |
| 87 |
assert p.exists() is True |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
def test_stdio_path_open_read_text_from_stdin(monkeypatch): |
| 94 |
p = StdioPath("-") |
| 95 |
fake_stdin = type("FakeStdin", (), {"buffer": BytesIO(b"hello world")})() |
| 96 |
monkeypatch.setattr(sys, "stdin", fake_stdin) |
| 97 |
with p.open("r") as f: |
| 98 |
assert f.read() == "hello world" |
| 99 |
|
| 100 |
|
| 101 |
def test_stdio_path_open_write_text_to_stdout(capfdbinary): |
| 102 |
p = StdioPath("-") |
| 103 |
with p.open("w") as f: |
| 104 |
f.write("hello world") |
| 105 |
assert capfdbinary.readouterr().out == b"hello world" |
| 106 |
|
| 107 |
|
| 108 |
def test_stdio_path_open_read_binary_from_stdin(monkeypatch): |
| 109 |
p = StdioPath("-") |
| 110 |
fake_stdin = type("FakeStdin", (), {"buffer": BytesIO(b"binary data")})() |
| 111 |
monkeypatch.setattr(sys, "stdin", fake_stdin) |
| 112 |
with p.open("rb") as f: |
| 113 |
assert f.read() == b"binary data" |
| 114 |
|
| 115 |
|
| 116 |
def test_stdio_path_open_write_binary_to_stdout(capfdbinary): |
| 117 |
p = StdioPath("-") |
| 118 |
with p.open("wb") as f: |
| 119 |
f.write(b"binary data") |
| 120 |
assert capfdbinary.readouterr().out == b"binary data" |
| 121 |
|
| 122 |
|
| 123 |
def test_stdio_path_open_regular_file_read(tmp_path): |
| 124 |
f = tmp_path / "test.txt" |
| 125 |
f.write_text("file content") |
| 126 |
p = StdioPath(f) |
| 127 |
with p.open("r") as fh: |
| 128 |
assert fh.read() == "file content" |
| 129 |
|
| 130 |
|
| 131 |
def test_stdio_path_open_regular_file_write(tmp_path): |
| 132 |
f = tmp_path / "test.txt" |
| 133 |
p = StdioPath(f) |
| 134 |
with p.open("w") as fh: |
| 135 |
fh.write("new content") |
| 136 |
assert f.read_text() == "new content" |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
def test_stdio_path_read_text_from_stdin(monkeypatch): |
| 143 |
p = StdioPath("-") |
| 144 |
fake_stdin = type("FakeStdin", (), {"buffer": BytesIO(b"hello world")})() |
| 145 |
monkeypatch.setattr(sys, "stdin", fake_stdin) |
| 146 |
assert p.read_text() == "hello world" |
| 147 |
|
| 148 |
|
| 149 |
def test_stdio_path_read_text_from_stdin_with_encoding(monkeypatch): |
| 150 |
p = StdioPath("-") |
| 151 |
fake_stdin = type("FakeStdin", (), {"buffer": BytesIO("café".encode("latin-1"))})() |
| 152 |
monkeypatch.setattr(sys, "stdin", fake_stdin) |
| 153 |
assert p.read_text(encoding="latin-1") == "café" |
| 154 |
|
| 155 |
|
| 156 |
def test_stdio_path_read_bytes_from_stdin(monkeypatch): |
| 157 |
p = StdioPath("-") |
| 158 |
fake_stdin = type("FakeStdin", (), {"buffer": BytesIO(b"binary data")})() |
| 159 |
monkeypatch.setattr(sys, "stdin", fake_stdin) |
| 160 |
assert p.read_bytes() == b"binary data" |
| 161 |
|
| 162 |
|
| 163 |
def test_stdio_path_write_text_to_stdout(capfdbinary): |
| 164 |
p = StdioPath("-") |
| 165 |
p.write_text("hello world") |
| 166 |
assert capfdbinary.readouterr().out == b"hello world" |
| 167 |
|
| 168 |
|
| 169 |
def test_stdio_path_write_text_to_stdout_with_encoding(capfdbinary): |
| 170 |
p = StdioPath("-") |
| 171 |
p.write_text("café", encoding="latin-1") |
| 172 |
assert capfdbinary.readouterr().out == "café".encode("latin-1") |
| 173 |
|
| 174 |
|
| 175 |
def test_stdio_path_write_bytes_to_stdout(capfdbinary): |
| 176 |
p = StdioPath("-") |
| 177 |
p.write_bytes(b"binary data") |
| 178 |
assert capfdbinary.readouterr().out == b"binary data" |
| 179 |
|
| 180 |
|
| 181 |
def test_stdio_path_read_text_regular_file(tmp_path): |
| 182 |
f = tmp_path / "test.txt" |
| 183 |
f.write_text("file content") |
| 184 |
p = StdioPath(f) |
| 185 |
assert p.read_text() == "file content" |
| 186 |
|
| 187 |
|
| 188 |
def test_stdio_path_read_bytes_regular_file(tmp_path): |
| 189 |
f = tmp_path / "test.bin" |
| 190 |
f.write_bytes(b"binary content") |
| 191 |
p = StdioPath(f) |
| 192 |
assert p.read_bytes() == b"binary content" |
| 193 |
|
| 194 |
|
| 195 |
def test_stdio_path_write_text_regular_file(tmp_path): |
| 196 |
f = tmp_path / "test.txt" |
| 197 |
p = StdioPath(f) |
| 198 |
p.write_text("new content") |
| 199 |
assert f.read_text() == "new content" |
| 200 |
|
| 201 |
|
| 202 |
def test_stdio_path_write_bytes_regular_file(tmp_path): |
| 203 |
f = tmp_path / "test.bin" |
| 204 |
p = StdioPath(f) |
| 205 |
p.write_bytes(b"binary content") |
| 206 |
assert f.read_bytes() == b"binary content" |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
def test_stdio_path_allow_leading_hyphen(app, assert_parse_args): |
| 213 |
"""StdioPath should accept '-' as a positional argument.""" |
| 214 |
|
| 215 |
@app.default |
| 216 |
def main(path: StdioPath): |
| 217 |
pass |
| 218 |
|
| 219 |
assert_parse_args(main, "-", StdioPath("-")) |
| 220 |
|
| 221 |
|
| 222 |
def test_stdio_path_regular_path(app, assert_parse_args, tmp_path): |
| 223 |
"""StdioPath should accept regular paths.""" |
| 224 |
f = tmp_path / "test.txt" |
| 225 |
|
| 226 |
@app.default |
| 227 |
def main(path: StdioPath): |
| 228 |
pass |
| 229 |
|
| 230 |
assert_parse_args(main, [str(f)], StdioPath(f)) |
| 231 |
|
| 232 |
|
| 233 |
def test_stdio_path_with_annotated(app, assert_parse_args): |
| 234 |
"""StdioPath should work when wrapped in Annotated.""" |
| 235 |
|
| 236 |
@app.default |
| 237 |
def main(path: Annotated[StdioPath, Parameter(help="Input path")]): |
| 238 |
pass |
| 239 |
|
| 240 |
assert_parse_args(main, "-", StdioPath("-")) |
| 241 |
|
| 242 |
|
| 243 |
def test_stdio_path_multiple_args(app, assert_parse_args, tmp_path): |
| 244 |
"""Multiple StdioPath arguments should work.""" |
| 245 |
f = tmp_path / "out.txt" |
| 246 |
|
| 247 |
@app.default |
| 248 |
def main( |
| 249 |
input_path: Annotated[StdioPath, Parameter(help="Input")], |
| 250 |
output_path: Annotated[StdioPath, Parameter(help="Output")], |
| 251 |
): |
| 252 |
pass |
| 253 |
|
| 254 |
assert_parse_args(main, ["-", str(f)], StdioPath("-"), StdioPath(f)) |
| 255 |
|
| 256 |
|
| 257 |
def test_stdio_path_keyword_args(app, assert_parse_args, tmp_path): |
| 258 |
"""StdioPath should accept '-' as a keyword argument.""" |
| 259 |
f = tmp_path / "out.txt" |
| 260 |
|
| 261 |
@app.default |
| 262 |
def main( |
| 263 |
input_path: Annotated[StdioPath, Parameter(help="Input")], |
| 264 |
output_path: Annotated[StdioPath, Parameter(help="Output")], |
| 265 |
): |
| 266 |
pass |
| 267 |
|
| 268 |
assert_parse_args(main, ["--input-path", "-", "--output-path", str(f)], StdioPath("-"), StdioPath(f)) |
| 269 |
assert_parse_args(main, ["--input-path=-", f"--output-path={f}"], StdioPath("-"), StdioPath(f)) |
| 270 |
|
| 271 |
|
| 272 |
def test_stdio_path_optional_without_annotated(app, assert_parse_args): |
| 273 |
"""StdioPath | None with default value should accept '-' as positional argument. |
| 274 |
|
| 275 |
Regression test for issue #740. |
| 276 |
""" |
| 277 |
|
| 278 |
@app.default |
| 279 |
def main( |
| 280 |
input_: StdioPath, |
| 281 |
output: StdioPath | None = None, |
| 282 |
): |
| 283 |
pass |
| 284 |
|
| 285 |
assert_parse_args(main, ["-", "-"], StdioPath("-"), StdioPath("-")) |
| 286 |
|
| 287 |
|
| 288 |
def test_stdio_path_annotated_without_default(app, assert_parse_args): |
| 289 |
"""Annotated StdioPath without default should accept '-' as positional argument. |
| 290 |
|
| 291 |
Regression test for issue #740. |
| 292 |
""" |
| 293 |
|
| 294 |
@app.default |
| 295 |
def main( |
| 296 |
input_: StdioPath, |
| 297 |
output: Annotated[StdioPath, Parameter(name=["--output", "-o"])], |
| 298 |
): |
| 299 |
pass |
| 300 |
|
| 301 |
assert_parse_args(main, ["-", "-"], StdioPath("-"), StdioPath("-")) |
| 302 |
assert_parse_args(main, ["-", "--output", "-"], StdioPath("-"), StdioPath("-")) |
| 303 |
assert_parse_args(main, ["-", "-o", "-"], StdioPath("-"), StdioPath("-")) |
| 304 |
|
| 305 |
|
| 306 |
def test_stdio_path_annotated_optional_with_default(app, assert_parse_args): |
| 307 |
"""Annotated StdioPath | None with default should accept '-' as positional argument. |
| 308 |
|
| 309 |
Regression test for issue #740. |
| 310 |
This is the specific combination that was broken. |
| 311 |
""" |
| 312 |
|
| 313 |
@app.default |
| 314 |
def main( |
| 315 |
input_: StdioPath, |
| 316 |
output: Annotated[StdioPath | None, Parameter(name=["--output", "-o"])] = None, |
| 317 |
): |
| 318 |
pass |
| 319 |
|
| 320 |
assert_parse_args(main, ["-", "-"], StdioPath("-"), StdioPath("-")) |
| 321 |
assert_parse_args(main, ["-", "--output", "-"], StdioPath("-"), StdioPath("-")) |
| 322 |
assert_parse_args(main, ["-", "-o", "-"], StdioPath("-"), StdioPath("-")) |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
def test_stdio_path_subclass_custom_string(): |
| 329 |
"""Subclasses can override STDIO_STRING for a different trigger.""" |
| 330 |
|
| 331 |
class StdinPath(StdioPath): |
| 332 |
STDIO_STRING = "STDIN" |
| 333 |
|
| 334 |
class StdoutPath(StdioPath): |
| 335 |
STDIO_STRING = "STDOUT" |
| 336 |
|
| 337 |
p_in = StdinPath("STDIN") |
| 338 |
assert p_in.is_stdio is True |
| 339 |
assert str(p_in) == "STDIN" |
| 340 |
assert repr(p_in) == "StdinPath('STDIN')" |
| 341 |
|
| 342 |
p_out = StdoutPath("STDOUT") |
| 343 |
assert p_out.is_stdio is True |
| 344 |
assert str(p_out) == "STDOUT" |
| 345 |
assert repr(p_out) == "StdoutPath('STDOUT')" |
| 346 |
|
| 347 |
|
| 348 |
assert StdinPath("-").is_stdio is False |
| 349 |
assert StdoutPath("-").is_stdio is False |
| 350 |
|
| 351 |
|
| 352 |
assert StdinPath("/tmp/test.txt").is_stdio is False |
| 353 |
assert StdoutPath("/tmp/test.txt").is_stdio is False |
| 354 |
|
| 355 |
|
| 356 |
def test_stdio_path_subclass_custom_matching(): |
| 357 |
"""Subclasses can override is_stdio for custom matching logic.""" |
| 358 |
|
| 359 |
class MultiStdioPath(StdioPath): |
| 360 |
@property |
| 361 |
def is_stdio(self) -> bool: |
| 362 |
return str(self) in ("-", "STDIN", "STDOUT") |
| 363 |
|
| 364 |
assert MultiStdioPath("-").is_stdio is True |
| 365 |
assert MultiStdioPath("STDIN").is_stdio is True |
| 366 |
assert MultiStdioPath("STDOUT").is_stdio is True |
| 367 |
assert MultiStdioPath("/tmp/test.txt").is_stdio is False |