# Path: invoke/terminals.py
# def pty_size():
#     """
#     Determine current local pseudoterminal dimensions.
# 
#     :returns:
#         A ``(num_cols, num_rows)`` two-tuple describing PTY size. Defaults to
#         ``(80, 24)`` if unable to get a sensible result dynamically.
# 
#     .. versionadded:: 1.0
#     """
#     cols, rows = _pty_size() if not WINDOWS else _win_pty_size()
#     # TODO: make defaults configurable?
#     return ((cols or 80), (rows or 24))
# 
# def bytes_to_read(input_):
#     """
#     Query stream ``input_`` to see how many bytes may be readable.
# 
#     .. note::
#         If we are unable to tell (e.g. if ``input_`` isn't a true file
#         descriptor or isn't a valid TTY) we fall back to suggesting reading 1
#         byte only.
# 
#     :param input: Input stream object (file-like).
# 
#     :returns: `int` number of bytes to read.
# 
#     .. versionadded:: 1.0
#     """
#     # NOTE: we have to check both possibilities here; situations exist where
#     # it's not a tty but has a fileno, or vice versa; neither is typically
#     # going to work re: ioctl().
#     if not WINDOWS and isatty(input_) and has_fileno(input_):
#         fionread = fcntl.ioctl(input_, termios.FIONREAD, "  ")
#         return struct.unpack("h", fionread)[0]
#     return 1
# 
# WINDOWS = sys.platform == "win32"


import fcntl
import termios
from mock import Mock, patch
from pytest import skip, mark
from invoke.terminals import pty_size, bytes_to_read, WINDOWS



# Skip on Windows CI, it may blow up on one of these tests
pytestmark = mark.skipif(
    WINDOWS, reason="Low level terminal tests only work well on POSIX"
)


# NOTE: 'with character_buffered()' tests are in runners.py as it's a lot
# easier to test some aspects in a non-unit sense (e.g. a keyboard-interrupting
# Runner subclass). MEH.


class terminals:
    class pty_size:
        @patch("fcntl.ioctl", wraps=fcntl.ioctl)
        def calls_fcntl_with_TIOCGWINSZ(self, ioctl):
            # Test the default (Unix) implementation because that's all we
            # can realistically do here.

# Path: invoke/terminals.py
# def pty_size():
#     """
#     Determine current local pseudoterminal dimensions.
# 
#     :returns:
#         A ``(num_cols, num_rows)`` two-tuple describing PTY size. Defaults to
#         ``(80, 24)`` if unable to get a sensible result dynamically.
# 
#     .. versionadded:: 1.0
#     """
#     cols, rows = _pty_size() if not WINDOWS else _win_pty_size()
#     # TODO: make defaults configurable?
#     return ((cols or 80), (rows or 24))
# 
# def bytes_to_read(input_):
#     """
#     Query stream ``input_`` to see how many bytes may be readable.
# 
#     .. note::
#         If we are unable to tell (e.g. if ``input_`` isn't a true file
#         descriptor or isn't a valid TTY) we fall back to suggesting reading 1
#         byte only.
# 
#     :param input: Input stream object (file-like).
# 
#     :returns: `int` number of bytes to read.
# 
#     .. versionadded:: 1.0
#     """
#     # NOTE: we have to check both possibilities here; situations exist where
#     # it's not a tty but has a fileno, or vice versa; neither is typically
#     # going to work re: ioctl().
#     if not WINDOWS and isatty(input_) and has_fileno(input_):
#         fionread = fcntl.ioctl(input_, termios.FIONREAD, "  ")
#         return struct.unpack("h", fionread)[0]
#     return 1
# 
# WINDOWS = sys.platform == "win32"


# Path: tests/terminals.py
import fcntl
import termios
from mock import Mock, patch
from pytest import skip, mark
from invoke.terminals import pty_size, bytes_to_read, WINDOWS




# Skip on Windows CI, it may blow up on one of these tests
pytestmark = mark.skipif(
    WINDOWS, reason="Low level terminal tests only work well on POSIX"
)


# NOTE: 'with character_buffered()' tests are in runners.py as it's a lot
# easier to test some aspects in a non-unit sense (e.g. a keyboard-interrupting
# Runner subclass). MEH.


class terminals:
    class pty_size:
        @patch("fcntl.ioctl", wraps=fcntl.ioctl)
        def calls_fcntl_with_TIOCGWINSZ(self, ioctl):
            # Test the default (Unix) implementation because that's all we
            # can realistically do here.

            pty_size()