| 1 |
import threading |
| 2 |
from io import StringIO |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from briefcase.integrations import subprocess |
| 7 |
from briefcase.integrations.subprocess import PopenOutputStreamer |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture |
| 11 |
def streamer(dummy_console, streaming_process): |
| 12 |
return PopenOutputStreamer( |
| 13 |
label="test", |
| 14 |
popen_process=streaming_process, |
| 15 |
console=dummy_console, |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
def test_output(streamer, capsys): |
| 20 |
"""Process output is printed.""" |
| 21 |
streamer.start() |
| 22 |
streamer.join(timeout=5) |
| 23 |
|
| 24 |
# fmt: off |
| 25 |
assert capsys.readouterr().out == ( |
| 26 |
"output line 1\n" |
| 27 |
"\n" |
| 28 |
"output line 3\n" |
| 29 |
) |
| 30 |
# fmt: on |
| 31 |
|
| 32 |
|
| 33 |
def test_stdout_closes_unexpectedly(streamer, monkeypatch, capsys): |
| 34 |
"""Streamer exits from ValueError because stdout was closed.""" |
| 35 |
|
| 36 |
def monkeypatch_ensure_str(value): |
| 37 |
"""Close stdout when ensure_str() runs on output from readline().""" |
| 38 |
streamer.popen_process.stdout.close() |
| 39 |
return value |
| 40 |
|
| 41 |
monkeypatch.setattr(subprocess, "ensure_str", monkeypatch_ensure_str) |
| 42 |
streamer.popen_process.stdout = StringIO("output line 1\noutput line 2") |
| 43 |
|
| 44 |
streamer.start() |
| 45 |
streamer.join(timeout=5) |
| 46 |
|
| 47 |
assert capsys.readouterr().out == ( |
| 48 |
"output line 1\n" |
| 49 |
"WARNING: stdout was unexpectedly closed while streaming output\n" |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
def test_readline_raises_exception(streamer, monkeypatch, capsys): |
| 54 |
"""Streamer aborts if readline() raises ValueError for reasons other than stdout |
| 55 |
closing.""" |
| 56 |
|
| 57 |
def monkeypatch_ensure_str(value): |
| 58 |
"""Simulate readline() raising an ValueError-derived exception.""" |
| 59 |
raise UnicodeError("readline() exception") |
| 60 |
|
| 61 |
monkeypatch.setattr(subprocess, "ensure_str", monkeypatch_ensure_str) |
| 62 |
|
| 63 |
streamer.start() |
| 64 |
streamer.join(timeout=5) |
| 65 |
|
| 66 |
assert capsys.readouterr().out == ( |
| 67 |
"Error while streaming output: UnicodeError: readline() exception\n" |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
def test_request_stop(streamer, capsys): |
| 72 |
"""Requesting the streamer to stop sets the stop flag.""" |
| 73 |
streamer.start() |
| 74 |
streamer.join(timeout=5) |
| 75 |
|
| 76 |
# fmt: off |
| 77 |
assert capsys.readouterr().out == ( |
| 78 |
"output line 1\n" |
| 79 |
"\n" |
| 80 |
"output line 3\n" |
| 81 |
) |
| 82 |
# fmt: on |
| 83 |
|
| 84 |
assert not streamer.stop_flag.is_set() |
| 85 |
|
| 86 |
streamer.request_stop() |
| 87 |
|
| 88 |
assert streamer.stop_flag.is_set() |
| 89 |
|
| 90 |
|
| 91 |
def test_request_stop_set_immediately(streamer, capsys): |
| 92 |
"""Nothing is printed if stop flag is immediately set.""" |
| 93 |
streamer.request_stop() |
| 94 |
|
| 95 |
streamer.start() |
| 96 |
streamer.join(timeout=5) |
| 97 |
|
| 98 |
assert capsys.readouterr().out == "" |
| 99 |
|
| 100 |
|
| 101 |
def test_request_stop_set_during_output(streamer, monkeypatch, capsys): |
| 102 |
"""Streamer prints nothing more after stop flag is set.""" |
| 103 |
|
| 104 |
def filter_func(value): |
| 105 |
"""Simulate stop flag set while output is being read.""" |
| 106 |
streamer.request_stop() |
| 107 |
yield value |
| 108 |
|
| 109 |
streamer.filter_func = filter_func |
| 110 |
|
| 111 |
streamer.start() |
| 112 |
streamer.join(timeout=5) |
| 113 |
|
| 114 |
assert capsys.readouterr().out == "output line 1\n" |
| 115 |
|
| 116 |
|
| 117 |
def test_captured_output(streamer, capsys): |
| 118 |
"""Captured output is available after streaming.""" |
| 119 |
streamer.capture_output = True |
| 120 |
|
| 121 |
streamer.start() |
| 122 |
streamer.join(timeout=5) |
| 123 |
|
| 124 |
assert capsys.readouterr().out == "" |
| 125 |
|
| 126 |
# fmt: off |
| 127 |
assert streamer.captured_output == ( |
| 128 |
"output line 1\n" |
| 129 |
"\n" |
| 130 |
"output line 3\n" |
| 131 |
) |
| 132 |
# fmt: on |
| 133 |
|
| 134 |
|
| 135 |
def test_captured_output_interim(streamer, monkeypatch, capsys): |
| 136 |
"""Captured output is available during streaming.""" |
| 137 |
streamer.capture_output = True |
| 138 |
|
| 139 |
streamer_is_waiting = threading.Event() |
| 140 |
continue_streaming = threading.Event() |
| 141 |
|
| 142 |
def mock_readline(): |
| 143 |
"""Wait on asserts while returning output lines.""" |
| 144 |
yield "output line 1\n" |
| 145 |
yield "output line 2\n" |
| 146 |
streamer_is_waiting.set() |
| 147 |
continue_streaming.wait(timeout=5) |
| 148 |
yield "output line 3\n" |
| 149 |
yield "output line 4\n" |
| 150 |
yield "" |
| 151 |
|
| 152 |
streamer.popen_process.stdout.readline.side_effect = mock_readline() |
| 153 |
|
| 154 |
streamer.start() |
| 155 |
streamer_is_waiting.wait(timeout=5) |
| 156 |
|
| 157 |
assert streamer.captured_output == "output line 1\noutput line 2\n" |
| 158 |
|
| 159 |
continue_streaming.set() |
| 160 |
streamer.join(timeout=5) |
| 161 |
|
| 162 |
assert streamer.captured_output == "output line 3\noutput line 4\n" |
| 163 |
|
| 164 |
|
| 165 |
def test_filter_func(streamer, capsys): |
| 166 |
"""A filter can be added to modify an output stream.""" |
| 167 |
|
| 168 |
# Define a filter function that converts "output" into "filtered" |
| 169 |
def filter_func(line): |
| 170 |
yield line.replace("output", "filtered") |
| 171 |
|
| 172 |
streamer.filter_func = filter_func |
| 173 |
|
| 174 |
streamer.start() |
| 175 |
streamer.join(timeout=5) |
| 176 |
|
| 177 |
# fmt: off |
| 178 |
# Output has been transformed |
| 179 |
assert capsys.readouterr().out == ( |
| 180 |
"filtered line 1\n" |
| 181 |
"\n" |
| 182 |
"filtered line 3\n" |
| 183 |
) |
| 184 |
# fmt: on |
| 185 |
|
| 186 |
|
| 187 |
def test_filter_func_reject(streamer, capsys): |
| 188 |
"""A filter that rejects lines can be added to modify an output stream.""" |
| 189 |
|
| 190 |
# Define a filter function that ignores blank lines |
| 191 |
def filter_func(line): |
| 192 |
if len(line) == 0: |
| 193 |
return |
| 194 |
yield line |
| 195 |
|
| 196 |
streamer.filter_func = filter_func |
| 197 |
|
| 198 |
streamer.start() |
| 199 |
streamer.join(timeout=5) |
| 200 |
|
| 201 |
# fmt: off |
| 202 |
# Output has been transformed |
| 203 |
assert capsys.readouterr().out == ( |
| 204 |
"output line 1\n" |
| 205 |
"output line 3\n" |
| 206 |
) |
| 207 |
# fmt: on |
| 208 |
|
| 209 |
|
| 210 |
def test_filter_func_line_ends(streamer, sleep_zero, capsys): |
| 211 |
"""Filter functions are not provided the newline.""" |
| 212 |
|
| 213 |
# Define a filter function that redacts lines that end with 1 |
| 214 |
# The newline is *not* included. |
| 215 |
def filter_func(line): |
| 216 |
if line.endswith("line 1"): |
| 217 |
yield line.replace("line 1", "**REDACTED**") |
| 218 |
else: |
| 219 |
yield line |
| 220 |
|
| 221 |
streamer.filter_func = filter_func |
| 222 |
|
| 223 |
streamer.start() |
| 224 |
streamer.join(timeout=5) |
| 225 |
|
| 226 |
# fmt: off |
| 227 |
# Output has been transformed; newline exists in output |
| 228 |
assert capsys.readouterr().out == ( |
| 229 |
"output **REDACTED**\n" |
| 230 |
"\n" |
| 231 |
"output line 3\n" |
| 232 |
) |
| 233 |
# fmt: on |
| 234 |
|
| 235 |
|
| 236 |
def test_filter_func_line_multiple_output(streamer, capsys): |
| 237 |
"""Filter functions can generate multiple lines from a single input.""" |
| 238 |
|
| 239 |
# Define a filter function that adds an extra line of content when the |
| 240 |
# lines that end with 1 |
| 241 |
def filter_func(line): |
| 242 |
yield line |
| 243 |
if line.endswith("line 1"): |
| 244 |
yield "Extra content!" |
| 245 |
|
| 246 |
streamer.filter_func = filter_func |
| 247 |
|
| 248 |
streamer.start() |
| 249 |
streamer.join(timeout=5) |
| 250 |
|
| 251 |
# fmt: off |
| 252 |
# Output has been transformed; newline exists in output |
| 253 |
assert capsys.readouterr().out == ( |
| 254 |
"output line 1\n" |
| 255 |
"Extra content!\n" |
| 256 |
"\n" |
| 257 |
"output line 3\n" |
| 258 |
) |
| 259 |
# fmt: on |
| 260 |
|
| 261 |
|
| 262 |
def test_filter_func_stop_iteration(streamer, capsys): |
| 263 |
"""A filter can indicate that logging should stop.""" |
| 264 |
|
| 265 |
# Define a filter function that converts "output" into "filtered", |
| 266 |
# and terminates streaming when a blank line is seen. |
| 267 |
def filter_func(line): |
| 268 |
if line == "": |
| 269 |
raise subprocess.StopStreaming() |
| 270 |
yield line.replace("output", "filtered") |
| 271 |
|
| 272 |
streamer.filter_func = filter_func |
| 273 |
|
| 274 |
streamer.start() |
| 275 |
streamer.join(timeout=5) |
| 276 |
|
| 277 |
# fmt: off |
| 278 |
# Output has been transformed, but is truncated when the empty line was received. |
| 279 |
assert capsys.readouterr().out == ( |
| 280 |
"filtered line 1\n" |
| 281 |
) |
| 282 |
# fmt: on |
| 283 |
|
| 284 |
|
| 285 |
def test_filter_func_output_and_stop_iteration(streamer, capsys): |
| 286 |
"""A filter can indicate that logging should stop, and also output content.""" |
| 287 |
|
| 288 |
# Define a filter function that converts "output" into "filtered", |
| 289 |
# and terminates streaming when a blank line is seen; but outputs |
| 290 |
# one more line before terminating. |
| 291 |
def filter_func(line): |
| 292 |
if line == "": |
| 293 |
yield "This should be the last line" |
| 294 |
raise subprocess.StopStreaming() |
| 295 |
yield line.replace("output", "filtered") |
| 296 |
|
| 297 |
streamer.filter_func = filter_func |
| 298 |
|
| 299 |
streamer.start() |
| 300 |
streamer.join(timeout=5) |
| 301 |
|
| 302 |
# fmt: off |
| 303 |
# Output has been transformed, but is truncated when the empty line was received. |
| 304 |
assert capsys.readouterr().out == ( |
| 305 |
"filtered line 1\n" |
| 306 |
"This should be the last line\n" |
| 307 |
) |
| 308 |
# fmt: on |
| 309 |
|
| 310 |
|
| 311 |
def test_filter_func_line_unexpected_error(streamer, capsys): |
| 312 |
"""If a filter function fails, the error is caught and logged.""" |
| 313 |
|
| 314 |
# Define a filter function that raises a RunTimeError |
| 315 |
# The newline is *not* included. |
| 316 |
def filter_func(line): |
| 317 |
if not line: |
| 318 |
raise RuntimeError("Like something totally went wrong") |
| 319 |
yield line |
| 320 |
|
| 321 |
streamer.filter_func = filter_func |
| 322 |
|
| 323 |
streamer.start() |
| 324 |
streamer.join(timeout=5) |
| 325 |
|
| 326 |
# fmt: off |
| 327 |
# Exception |
| 328 |
assert capsys.readouterr().out == ( |
| 329 |
"output line 1\n" |
| 330 |
"Error while streaming output: RuntimeError: Like something totally went wrong\n" |
| 331 |
) |
| 332 |
# fmt: on |