Debian Codegraph @

tests/terminals.py

raw
1 import fcntl
2 import termios
3
4 from unittest.mock import Mock, patch
5 from pytest import skip, mark
6
7 from invoke.terminals import pty_size, bytes_to_read, WINDOWS
8
9 # Skip on Windows CI, it may blow up on one of these tests
10 pytestmark = mark.skipif(
11 WINDOWS, reason="Low level terminal tests only work well on POSIX"
12 )
13
14
15 # NOTE: 'with character_buffered()' tests are in runners.py as it's a lot
16 # easier to test some aspects in a non-unit sense (e.g. a keyboard-interrupting
17 # Runner subclass). MEH.
18
19
20 class terminals:
21 class pty_size:
22 @patch("fcntl.ioctl", wraps=fcntl.ioctl)
23 def calls_fcntl_with_TIOCGWINSZ(self, ioctl):
24 # Test the default (Unix) implementation because that's all we
25 # can realistically do here.
26 pty_size()
27 assert ioctl.call_args_list[0][0][1] == termios.TIOCGWINSZ
28
29 @patch("sys.stdout")
30 @patch("fcntl.ioctl")
31 def defaults_to_80x24_when_stdout_not_a_tty(self, ioctl, stdout):
32 # Make sure stdout acts like a real stream (means failure is
33 # more obvious)
34 stdout.fileno.return_value = 1
35 # Ensure it fails the isatty() test too
36 stdout.isatty.return_value = False
37 # Test
38 assert pty_size() == (80, 24)
39
40 @patch("sys.stdout")
41 @patch("fcntl.ioctl")
42 def uses_default_when_stdout_lacks_fileno(self, ioctl, stdout):
43 # i.e. when accessing it throws AttributeError
44 stdout.fileno.side_effect = AttributeError
45 assert pty_size() == (80, 24)
46
47 @patch("sys.stdout")
48 @patch("fcntl.ioctl")
49 def uses_default_when_stdout_triggers_ioctl_error(self, ioctl, stdout):
50 ioctl.side_effect = TypeError
51 assert pty_size() == (80, 24)
52
53 class bytes_to_read_:
54 @patch("invoke.terminals.fcntl")
55 def returns_1_when_stream_lacks_fileno(self, fcntl):
56 # A fileno() that exists but returns a non-int is a quick way
57 # to fail util.has_fileno().
58 assert bytes_to_read(Mock(fileno=lambda: None)) == 1
59 assert not fcntl.ioctl.called
60
61 @patch("invoke.terminals.fcntl")
62 def returns_1_when_stream_has_fileno_but_is_not_a_tty(self, fcntl):
63 # It blows up otherwise anyways (struct.unpack gets mad because
64 # result isn't a string of the right length) but let's make
65 # ioctl die similarly to the real world case we're testing for
66 # here (#425)
67 fcntl.ioctl.side_effect = IOError(
68 "Operation not supported by device"
69 )
70 stream = Mock(isatty=lambda: False, fileno=lambda: 17) # arbitrary
71 assert bytes_to_read(stream) == 1
72 assert not fcntl.ioctl.called
73
74 def returns_FIONREAD_result_when_stream_is_a_tty(self):
75 skip()
76
77 def returns_1_on_windows(self):
78 skip()