| 1 |
import io |
| 2 |
import sys |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from rich.console import Console |
| 7 |
from rich.file_proxy import FileProxy |
| 8 |
|
| 9 |
|
| 10 |
def test_empty_bytes(): |
| 11 |
console = Console() |
| 12 |
file_proxy = FileProxy(console, sys.stdout) |
| 13 |
# File should raise TypeError when writing bytes |
| 14 |
with pytest.raises(TypeError): |
| 15 |
file_proxy.write(b"") # type: ignore |
| 16 |
with pytest.raises(TypeError): |
| 17 |
file_proxy.write(b"foo") # type: ignore |
| 18 |
|
| 19 |
|
| 20 |
def test_flush(): |
| 21 |
file = io.StringIO() |
| 22 |
console = Console(file=file) |
| 23 |
file_proxy = FileProxy(console, file) |
| 24 |
file_proxy.write("foo") |
| 25 |
assert file.getvalue() == "" |
| 26 |
file_proxy.flush() |
| 27 |
assert file.getvalue() == "foo\n" |
| 28 |
|
| 29 |
|
| 30 |
def test_new_lines(): |
| 31 |
file = io.StringIO() |
| 32 |
console = Console(file=file) |
| 33 |
file_proxy = FileProxy(console, file) |
| 34 |
file_proxy.write("-\n-") |
| 35 |
assert file.getvalue() == "-\n" |
| 36 |
file_proxy.flush() |
| 37 |
assert file.getvalue() == "-\n-\n" |
| 38 |
|
| 39 |
|
| 40 |
def test_isatty(): |
| 41 |
"""Check isatty is proxied |
| 42 |
|
| 43 |
Regression test for https://github.com/Textualize/rich/issues/4041 |
| 44 |
|
| 45 |
""" |
| 46 |
|
| 47 |
class TTYFile: |
| 48 |
def isatty(self) -> bool: |
| 49 |
return True |
| 50 |
|
| 51 |
file = TTYFile() |
| 52 |
console = Console() |
| 53 |
file_proxy = FileProxy(console, file) |
| 54 |
assert file_proxy.isatty() |