Skip to content

Commit

Permalink
test(test_wrap_stdio): add wrap_stdio tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saygox committed Nov 19, 2021
1 parent a371fc7 commit a5860a8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
2 changes: 0 additions & 2 deletions commitizen/wrap_stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@

def wrap_stdio():
_wrap_stdio()
return None


def unwrap_stdio():
_unwrap_stdio()
return None
56 changes: 56 additions & 0 deletions tests/test_wrap_stdio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sys

from commitizen import wrap_stdio, wrap_stdio_linux, wrap_stdio_unix, wrap_stdio_windows


def test_warp_stdio_exists():
assert hasattr(wrap_stdio_windows, "sys")
assert hasattr(wrap_stdio_linux, "sys")
assert hasattr(wrap_stdio_unix, "sys")


if sys.platform == "win32": # pragma: no cover
pass
elif sys.platform == "linux":
from commitizen.wrap_stdio_linux import WrapStdioLinux

def test_wrap_stdio_linux(mocker):

tmp_stdin = sys.stdin
tmp_stdout = sys.stdout
tmp_stderr = sys.stderr

mocker.patch("os.open")
readerwriter_mock = mocker.mock_open(read_data="data")
mocker.patch("builtins.open", readerwriter_mock, create=True)

mocker.patch.object(sys.stdin, "fileno", return_value=0)
mocker.patch.object(sys.stdout, "fileno", return_value=1)
mocker.patch.object(sys.stdout, "fileno", return_value=2)

wrap_stdio.wrap_stdio()

assert sys.stdin != tmp_stdin
assert isinstance(sys.stdin, WrapStdioLinux)
assert sys.stdin.encoding == "UTF-8"
assert sys.stdin.read() == "data"

assert sys.stdout != tmp_stdout
assert isinstance(sys.stdout, WrapStdioLinux)
sys.stdout.write("stdout")
readerwriter_mock().write.assert_called_with("stdout")

assert sys.stderr != tmp_stderr
assert isinstance(sys.stderr, WrapStdioLinux)
sys.stdout.write("stderr")
readerwriter_mock().write.assert_called_with("stderr")

wrap_stdio.unwrap_stdio()

assert sys.stdin == tmp_stdin
assert sys.stdout == tmp_stdout
assert sys.stderr == tmp_stderr


else:
pass

0 comments on commit a5860a8

Please sign in to comment.