Skip to content

Commit 74d61ba

Browse files
authored
[launch_pytest] Modify how wait_for_output()/wait_for_stderr() work, add assert_*() alternatives (#553)
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent 56dbb37 commit 74d61ba

File tree

4 files changed

+95
-36
lines changed

4 files changed

+95
-36
lines changed

launch_pytest/launch_pytest/tools/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
# limitations under the License.
1414

1515
from . import process
16+
from .process import assert_output
17+
from .process import assert_output_sync
18+
from .process import assert_stderr
19+
from .process import assert_stderr_sync
1620
from .process import wait_for_exit
1721
from .process import wait_for_exit_sync
1822
from .process import wait_for_output
@@ -23,6 +27,10 @@
2327
from .process import wait_for_stderr_sync
2428

2529
__all__ = [
30+
'assert_output',
31+
'assert_output_sync',
32+
'assert_stderr',
33+
'assert_stderr_sync',
2634
'process',
2735
'wait_for_exit',
2836
'wait_for_exit_sync',

launch_pytest/launch_pytest/tools/process.py

Lines changed: 82 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ async def _wait_for_event_with_condition(
4747
):
4848
pyevent = asyncio.Event()
4949
event_handler = get_launch_event_handler(execute_process_action, pyevent)
50-
cond_value = False
51-
try:
52-
cond_value = condition()
53-
except AssertionError:
54-
pass
50+
cond_value = condition()
5551
with register_event_handler(launch_context, event_handler):
5652
start = time.time()
5753
now = start
@@ -61,15 +57,9 @@ async def _wait_for_event_with_condition(
6157
except asyncio.TimeoutError:
6258
break
6359
pyevent.clear()
64-
try:
65-
cond_value = condition()
66-
except AssertionError:
67-
pass
60+
cond_value = condition()
6861
now = time.time()
69-
# Call condition() again, if before it returned False.
70-
# If assertions were being used and the condition is still not satisfied it should raise here,
71-
# pytest renders assertion errors nicely.
72-
return condition() if not cond_value else cond_value
62+
return cond_value
7363

7464

7565
def _wait_for_event_sync(
@@ -86,26 +76,16 @@ def _wait_for_event_with_condition_sync(
8676
):
8777
pyevent = threading.Event()
8878
event_handler = get_launch_event_handler(execute_process_action, pyevent)
89-
cond_value = False
90-
try:
91-
cond_value = condition()
92-
except AssertionError:
93-
pass # Allow asserts in the condition closures
79+
cond_value = condition()
9480
with register_event_handler(launch_context, event_handler):
9581
start = time.time()
9682
now = start
9783
while not cond_value and (timeout is None or now < start + timeout):
9884
pyevent.wait(start - now + timeout)
9985
pyevent.clear()
100-
try:
101-
cond_value = condition()
102-
except AssertionError:
103-
pass
86+
cond_value = condition()
10487
now = time.time()
105-
# Call condition() again, if before it returned False.
106-
# If assertions were being used and the condition is still not satisfied it should raise here,
107-
# pytest renders assertion errors nicely.
108-
return condition() if not cond_value else cond_value
88+
return cond_value
10989

11090

11191
def _get_stdout_event_handler(action, pyevent):
@@ -124,6 +104,25 @@ async def wait_for_output(
124104
timeout)
125105

126106

107+
async def assert_output(
108+
launch_context, execute_process_action, validate_output, timeout=None
109+
):
110+
def condition():
111+
try:
112+
validate_output(execute_process_action.get_stdout())
113+
except AssertionError:
114+
return False
115+
return True
116+
cond_value = await _wait_for_event_with_condition(
117+
launch_context,
118+
execute_process_action,
119+
_get_stdout_event_handler,
120+
condition,
121+
timeout)
122+
if not cond_value:
123+
validate_output(execute_process_action.get_stdout())
124+
125+
127126
def wait_for_output_sync(
128127
launch_context, execute_process_action, validate_output, timeout=None
129128
):
@@ -135,6 +134,25 @@ def wait_for_output_sync(
135134
timeout)
136135

137136

137+
def assert_output_sync(
138+
launch_context, execute_process_action, validate_output, timeout=None
139+
):
140+
def condition():
141+
try:
142+
validate_output(execute_process_action.get_stdout())
143+
except AssertionError:
144+
return False
145+
return True
146+
cond_value = _wait_for_event_with_condition_sync(
147+
launch_context,
148+
execute_process_action,
149+
_get_stdout_event_handler,
150+
condition,
151+
timeout)
152+
if not cond_value:
153+
validate_output(execute_process_action.get_stdout())
154+
155+
138156
def _get_stderr_event_handler(action, pyevent):
139157
return event_handlers.OnProcessIO(
140158
target_action=action, on_stderr=lambda _1: pyevent.set())
@@ -151,6 +169,25 @@ async def wait_for_stderr(
151169
timeout)
152170

153171

172+
async def assert_stderr(
173+
launch_context, execute_process_action, validate_output, timeout=None
174+
):
175+
def condition():
176+
try:
177+
validate_output(execute_process_action.get_stderr())
178+
except AssertionError:
179+
return False
180+
return True
181+
cond_value = await _wait_for_event_with_condition(
182+
launch_context,
183+
execute_process_action,
184+
_get_stderr_event_handler,
185+
condition,
186+
timeout)
187+
if not cond_value:
188+
validate_output(execute_process_action.get_stderr())
189+
190+
154191
def wait_for_stderr_sync(
155192
launch_context, execute_process_action, validate_output, timeout=None
156193
):
@@ -162,6 +199,25 @@ def wait_for_stderr_sync(
162199
timeout)
163200

164201

202+
def assert_stderr_sync(
203+
launch_context, execute_process_action, validate_output, timeout=None
204+
):
205+
def condition():
206+
try:
207+
validate_output(execute_process_action.get_stderr())
208+
except AssertionError:
209+
return False
210+
return True
211+
cond_value = _wait_for_event_with_condition_sync(
212+
launch_context,
213+
execute_process_action,
214+
_get_stderr_event_handler,
215+
condition,
216+
timeout)
217+
if not cond_value:
218+
validate_output(execute_process_action.get_stderr())
219+
220+
165221
def _get_on_process_start_event_handler(execute_process_action, pyevent):
166222
return event_handlers.OnProcessStart(
167223
target_action=execute_process_action, on_start=lambda _1, _2: pyevent.set())

launch_pytest/test/launch_pytest/examples/pytest_hello_world.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ def validate_output(output):
5050
# this function can use assertions to validate the output or return a boolean.
5151
# pytest generates easier to understand failures when assertions are used.
5252
assert output.splitlines() == ['hello_world'], 'process never printed hello_world'
53-
return True
54-
assert process_tools.wait_for_output_sync(
53+
process_tools.assert_output_sync(
5554
launch_context, hello_world_proc, validate_output, timeout=5)
5655

5756
def validate_output(output):

launch_pytest/test/launch_pytest/tools/test_process.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,12 @@ async def test_async_process_tools(dut, launch_context):
5252

5353
def check_output(output):
5454
assert output.splitlines() == ['hello']
55-
return True
56-
assert await tools.wait_for_output(
55+
await tools.assert_output(
5756
launch_context, dut, check_output, timeout=10)
5857

5958
def check_stderr(err):
6059
assert err.splitlines() == ['world']
61-
return True
62-
assert await tools.wait_for_stderr(
60+
await tools.assert_stderr(
6361
launch_context, dut, check_stderr, timeout=10)
6462
assert await tools.wait_for_exit(launch_context, dut, timeout=10)
6563

@@ -70,13 +68,11 @@ def test_sync_process_tools(dut, launch_context):
7068

7169
def check_output(output):
7270
assert output.splitlines() == ['hello']
73-
return True
74-
assert tools.wait_for_output_sync(
71+
tools.assert_output_sync(
7572
launch_context, dut, check_output, timeout=10)
7673

7774
def check_stderr(err):
7875
assert err.splitlines() == ['world']
79-
return True
80-
assert tools.wait_for_stderr_sync(
76+
tools.assert_stderr_sync(
8177
launch_context, dut, check_stderr, timeout=10)
8278
assert tools.wait_for_exit_sync(launch_context, dut, timeout=10)

0 commit comments

Comments
 (0)