|
| 1 | +import sys |
| 2 | + |
| 3 | +from commitizen import wrap_stdio, wrap_stdio_linux, wrap_stdio_unix, wrap_stdio_windows |
| 4 | + |
| 5 | + |
| 6 | +def test_warp_stdio_exists(): |
| 7 | + assert hasattr(wrap_stdio_windows, "sys") |
| 8 | + assert hasattr(wrap_stdio_linux, "sys") |
| 9 | + assert hasattr(wrap_stdio_unix, "sys") |
| 10 | + |
| 11 | + |
| 12 | +if sys.platform == "win32": # pragma: no cover |
| 13 | + pass |
| 14 | +elif sys.platform == "linux": |
| 15 | + from commitizen.wrap_stdio_linux import WrapStdioLinux |
| 16 | + |
| 17 | + def test_wrap_stdio_linux(mocker): |
| 18 | + |
| 19 | + tmp_stdin = sys.stdin |
| 20 | + tmp_stdout = sys.stdout |
| 21 | + tmp_stderr = sys.stderr |
| 22 | + |
| 23 | + mocker.patch("os.open") |
| 24 | + readerwriter_mock = mocker.mock_open(read_data="data") |
| 25 | + mocker.patch("builtins.open", readerwriter_mock, create=True) |
| 26 | + |
| 27 | + mocker.patch.object(sys.stdin, "fileno", return_value=0) |
| 28 | + mocker.patch.object(sys.stdout, "fileno", return_value=1) |
| 29 | + mocker.patch.object(sys.stdout, "fileno", return_value=2) |
| 30 | + |
| 31 | + wrap_stdio.wrap_stdio() |
| 32 | + |
| 33 | + assert sys.stdin != tmp_stdin |
| 34 | + assert isinstance(sys.stdin, WrapStdioLinux) |
| 35 | + assert sys.stdin.encoding == "UTF-8" |
| 36 | + assert sys.stdin.read() == "data" |
| 37 | + |
| 38 | + assert sys.stdout != tmp_stdout |
| 39 | + assert isinstance(sys.stdout, WrapStdioLinux) |
| 40 | + sys.stdout.write("stdout") |
| 41 | + readerwriter_mock().write.assert_called_with("stdout") |
| 42 | + |
| 43 | + assert sys.stderr != tmp_stderr |
| 44 | + assert isinstance(sys.stderr, WrapStdioLinux) |
| 45 | + sys.stdout.write("stderr") |
| 46 | + readerwriter_mock().write.assert_called_with("stderr") |
| 47 | + |
| 48 | + wrap_stdio.unwrap_stdio() |
| 49 | + |
| 50 | + assert sys.stdin == tmp_stdin |
| 51 | + assert sys.stdout == tmp_stdout |
| 52 | + assert sys.stderr == tmp_stderr |
| 53 | + |
| 54 | + |
| 55 | +else: |
| 56 | + pass |
0 commit comments