Files
dvc/tests/unit/ui/test_console.py
T
Saugat Pachhai b5f31daab0 ui: Use sys.stdout and sys.stderr dynamically (#5729)
Before, we used to set it during initialization. But this
made it hard to test using capsys as capsys changes the
reference to sys.stdout and sys.stderr with the mocked one
during the test. But since we are using dvc.ui globally and
setting it to a real sys.stdout and sys.stderr before capsys
comes into effect, testing was not possible with it.

Now, by using property hack, we will be using the values of
sys.stdout and sys.stderr during the time of write, so the mocking
would work and would be easier to test.
2021-03-30 23:44:46 +03:00

29 lines
740 B
Python

from pytest import CaptureFixture
from dvc.ui import Console
def test_write(capsys: CaptureFixture[str]):
"""Test that ui.write works."""
console = Console(enable=True)
message = "hello world"
console.write(message)
console.error_write(message)
captured = capsys.readouterr()
assert captured.out == f"{message}\n"
assert captured.err == f"{message}\n"
def test_capsys_works(capsys: CaptureFixture[str]):
"""Sanity check that capsys can capture outputs from a global ui."""
from dvc.ui import ui
message = "hello world"
ui.write(message)
ui.error_write(message)
captured = capsys.readouterr()
assert captured.out == f"{message}\n"
assert captured.err == f"{message}\n"