Debian Codegraph @

integration/runners.py

raw
1 import os
2 import platform
3 import time
4 from unittest.mock import Mock
5
6 from _util import assert_cpu_usage
7 from pytest import raises, skip
8
9 from invoke import (
10 CommandTimedOut,
11 Context,
12 FailingResponder,
13 Failure,
14 Local,
15 Responder,
16 ThreadException,
17 WatcherError,
18 run,
19 )
20
21 PYPY = platform.python_implementation() == "PyPy"
22
23
24 class Runner_:
25 def setup(self) -> None:
26 os.chdir(os.path.join(os.path.dirname(__file__), "_support"))
27
28 class responding:
29 def base_case(self) -> None:
30 # Basic "doesn't explode" test: respond.py will exit nonzero unless
31 # this works, causing a Failure.
32 watcher = Responder(r"What's the password\?", "Rosebud\n")
33 # Gotta give -u or Python will line-buffer its stdout, so we'll
34 # never actually see the prompt.
35 run(
36 "python -u respond_base.py",
37 watchers=[watcher],
38 hide=True,
39 timeout=5,
40 )
41
42 def both_streams(self) -> None:
43 watchers = [
44 Responder("standard out", "with it\n"),
45 Responder("standard error", "between chair and keyboard\n"),
46 ]
47 run(
48 "python -u respond_both.py",
49 watchers=watchers,
50 hide=True,
51 timeout=5,
52 )
53
54 def watcher_errors_become_Failures(self) -> None:
55 watcher = FailingResponder(
56 pattern=r"What's the password\?",
57 response="Rosebud\n",
58 sentinel="You're not Citizen Kane!",
59 )
60 try:
61 run(
62 "python -u respond_fail.py",
63 watchers=[watcher],
64 hide=True,
65 timeout=5,
66 )
67 except Failure as e:
68 assert isinstance(e.reason, WatcherError)
69 assert e.result.exited is None
70 else:
71 assert False, "Did not raise Failure!"
72
73 class stdin_mirroring:
74 def piped_stdin_is_not_conflated_with_mocked_stdin(self) -> None:
75 # Re: GH issue #308
76 # Will die on broken-pipe OSError if bug is present.
77 run("echo 'lollerskates' | inv -c nested_or_piped foo", hide=True)
78
79 def nested_invoke_sessions_not_conflated_with_mocked_stdin(
80 self,
81 ) -> None:
82 # Also re: GH issue #308. This one will just hang forever. Woo!
83 run("inv -c nested_or_piped calls-foo", hide=True)
84
85 def isnt_cpu_heavy(self) -> None:
86 "stdin mirroring isn't CPU-heavy"
87 # CPU measurement under PyPy is...rather different. NBD.
88 if PYPY:
89 skip()
90 # Python 3.5 has been seen using up to ~6.0s CPU time under Travis
91 with assert_cpu_usage(lt=7.0):
92 run("python -u busywork.py 10", pty=True, hide=True)
93
94 def doesnt_break_when_stdin_exists_but_null(self) -> None:
95 # Re: #425 - IOError occurs when bug present
96 run("inv -c nested_or_piped foo < /dev/null", hide=True)
97
98 class IO_hangs:
99 "IO hangs"
100
101 def _hang_on_full_pipe(self, pty: bool) -> None:
102 class Whoops(Exception):
103 pass
104
105 runner = Local(Context())
106 # Force runner IO thread-body method to raise an exception to mimic
107 # real world encoding explosions/etc. When bug is present, this
108 # will make the test hang until forcibly terminated.
109 runner.handle_stdout = Mock(side_effect=Whoops, __name__="sigh")
110 # NOTE: both Darwin (10.10) and Linux (Travis' docker image) have
111 # this file. It's plenty large enough to fill most pipe buffers,
112 # which is the triggering behavior.
113 try:
114 runner.run("cat /usr/share/dict/words", pty=pty)
115 except ThreadException as e:
116 assert len(e.exceptions) == 1
117 assert e.exceptions[0].type is Whoops
118 else:
119 assert False, "Did not receive expected ThreadException!"
120
121 def pty_subproc_should_not_hang_if_IO_thread_has_an_exception(
122 self,
123 ) -> None:
124 self._hang_on_full_pipe(pty=True)
125
126 def nonpty_subproc_should_not_hang_if_IO_thread_has_an_exception(
127 self,
128 ) -> None:
129 self._hang_on_full_pipe(pty=False)
130
131 class timeouts:
132 def does_not_fire_when_command_quick(self) -> None:
133 assert run("sleep 1", timeout=5)
134
135 def triggers_exception_when_command_slow(self) -> None:
136 before = time.time()
137 with raises(CommandTimedOut) as info:
138 run("sleep 5", timeout=0.5)
139 after = time.time()
140 # Fudge real time check a bit, <=0.5 typically fails due to
141 # overhead etc. May need raising further to avoid races? Meh.
142 assert (after - before) <= 0.75
143 # Sanity checks of the exception obj
144 assert info.value.timeout == 0.5 # type:ignore[attr-defined]
145 cmd = info.value.result.command # type:ignore[attr-defined]
146 assert cmd == "sleep 5"