Debian Codegraph @

tests/execute/local_subprocess/test_local_subprocess.py

raw
1 from __future__ import annotations
2
3 import contextlib
4 import errno
5 import json
6 import locale
7 import logging
8 import os
9 import re
10 import selectors
11 import shutil
12 import stat
13 import subprocess
14 import sys
15 from io import TextIOWrapper
16 from pathlib import Path
17 from typing import TYPE_CHECKING, Any, NoReturn
18 from unittest.mock import MagicMock, create_autospec
19
20 import psutil
21 import pytest
22 from colorama import Fore
23 from psutil import AccessDenied
24
25 from tox.execute.api import ExecuteOptions, Outcome
26 from tox.execute.local_sub_process import SIG_INTERRUPT, LocalSubProcessExecuteInstance, LocalSubProcessExecutor
27 from tox.execute.local_sub_process.read_via_thread_unix import ReadViaThreadUnix
28 from tox.execute.request import ExecuteRequest, StdinSource
29 from tox.execute.stream import SyncWrite
30 from tox.report import NamedBytesIO
31
32 if TYPE_CHECKING:
33 from pytest_mock import MockerFixture
34
35 from tox.pytest import CaptureFixture, LogCaptureFixture, MonkeyPatch
36
37
38 class FakeOutErr:
39 def __init__(self) -> None:
40 self.out_err = (
41 TextIOWrapper(NamedBytesIO("out"), encoding=locale.getpreferredencoding(False)),
42 TextIOWrapper(NamedBytesIO("err"), encoding=locale.getpreferredencoding(False)),
43 )
44
45 def read_out_err(self) -> tuple[str, str]:
46 out_got = self.out_err[0].buffer.getvalue().decode(self.out_err[0].encoding)
47 err_got = self.out_err[1].buffer.getvalue().decode(self.out_err[1].encoding)
48 return out_got, err_got
49
50
51 def _create_mock_env() -> MagicMock:
52 """Create a mock tox environment with no_capture=False to prevent console inheritance."""
53 mock_env = MagicMock()
54 mock_env.options.no_capture = False
55 return mock_env
56
57
58 @pytest.mark.parametrize("color", [True, False], ids=["color", "no_color"])
59 @pytest.mark.parametrize(("out", "err"), [("out", "err"), ("", "")], ids=["simple", "nothing"])
60 @pytest.mark.parametrize("show", [True, False], ids=["show", "no_show"])
61 @pytest.mark.parametrize(
62 "stderr_color",
63 ["RED", "YELLOW", "RESET"],
64 ids=["stderr_color_default", "stderr_color_yellow", "stderr_color_reset"],
65 )
66 def test_local_execute_basic_pass(
67 caplog: LogCaptureFixture,
68 os_env: dict[str, str],
69 out: str,
70 err: str,
71 show: bool,
72 color: bool,
73 stderr_color: str,
74 ) -> None:
75 caplog.set_level(logging.NOTSET)
76 executor = LocalSubProcessExecutor(colored=color)
77
78 tox_env = _create_mock_env()
79 tox_env.conf._conf.options.stderr_color = stderr_color # noqa: SLF001
80 code = f"import sys; print({out!r}, end=''); print({err!r}, end='', file=sys.stderr)"
81 request = ExecuteRequest(cmd=[sys.executable, "-c", code], cwd=Path(), env=os_env, stdin=StdinSource.OFF, run_id="")
82 out_err = FakeOutErr()
83
84 with executor.call(request, show=show, out_err=out_err.out_err, env=tox_env) as status:
85 while status.exit_code is None: # pragma: no branch
86 status.wait()
87 assert status.out == out.encode()
88 assert status.err == err.encode()
89 outcome = status.outcome
90 assert outcome is not None
91 assert bool(outcome) is True, outcome
92 assert outcome.exit_code == Outcome.OK
93 assert outcome.err == err
94 assert outcome.out == out
95 assert outcome.request == request
96
97 out_got, err_got = out_err.read_out_err()
98 if show:
99 assert out_got == out
100 expected = f"{getattr(Fore, stderr_color)}{err}{Fore.RESET}" if color and err else err
101 assert err_got == expected
102 else:
103 assert not out_got
104 assert not err_got
105 assert not caplog.records
106
107
108 def test_local_execute_basic_pass_show_on_standard_newline_flush(caplog: LogCaptureFixture) -> None:
109 caplog.set_level(logging.NOTSET)
110 executor = LocalSubProcessExecutor(colored=False)
111 request = ExecuteRequest(
112 cmd=[sys.executable, "-c", "import sys; print('out'); print('yay')"],
113 cwd=Path(),
114 env=os.environ.copy(),
115 stdin=StdinSource.OFF,
116 run_id="",
117 )
118 out_err = FakeOutErr()
119 with executor.call(request, show=True, out_err=out_err.out_err, env=_create_mock_env()) as status:
120 while status.exit_code is None: # pragma: no branch
121 status.wait()
122 outcome = status.outcome
123 assert outcome is not None
124 assert repr(outcome)
125 assert bool(outcome) is True, outcome
126 assert outcome.exit_code == Outcome.OK
127 assert not outcome.err
128 assert outcome.out == f"out{os.linesep}yay{os.linesep}"
129 out, err = out_err.read_out_err()
130 assert out == f"out{os.linesep}yay{os.linesep}"
131 assert not err
132 assert not caplog.records
133
134
135 def test_local_execute_write_a_lot(os_env: dict[str, str]) -> None:
136 count = 10_000
137 executor = LocalSubProcessExecutor(colored=False)
138 request = ExecuteRequest(
139 cmd=[
140 sys.executable,
141 "-c",
142 (
143 "import sys; import time; from datetime import datetime; import os;"
144 f"print('e' * {count}, file=sys.stderr);"
145 f"print('o' * {count}, file=sys.stdout);"
146 "time.sleep(0.5);"
147 f"print('a' * {count}, file=sys.stderr);"
148 f"print('b' * {count}, file=sys.stdout);"
149 ),
150 ],
151 cwd=Path(),
152 env=os_env,
153 stdin=StdinSource.OFF,
154 run_id="",
155 )
156 out_err = FakeOutErr()
157 with executor.call(request, show=False, out_err=out_err.out_err, env=_create_mock_env()) as status:
158 while status.exit_code is None: # pragma: no branch
159 status.wait()
160 outcome = status.outcome
161 assert outcome is not None
162 assert bool(outcome), outcome
163 expected_out = f"{'o' * count}{os.linesep}{'b' * count}{os.linesep}"
164 assert outcome.out == expected_out, expected_out[len(outcome.out) :]
165 expected_err = f"{'e' * count}{os.linesep}{'a' * count}{os.linesep}"
166 assert outcome.err == expected_err, expected_err[len(outcome.err) :]
167
168
169 @pytest.mark.skipif(sys.platform == "win32", reason="Unix terminal size test")
170 def test_local_execute_terminal_size(os_env: dict[str, str], monkeypatch: MonkeyPatch) -> None:
171 """Regression test for #2999 - check terminal size is set correctly in tox subprocess."""
172 import pty # noqa: PLC0415
173
174 terminal_size = os.terminal_size((84, 42))
175 main, child = pty.openpty() # Unix-only
176
177 # Use ReadViaThreadUnix to help with debugging the test itself.
178 pipe_out = ReadViaThreadUnix(main, sys.stdout.buffer.write, name="testout", drain=True)
179 with (
180 pipe_out,
181 monkeypatch.context() as monkey,
182 open( # noqa: PTH123
183 child, "w", encoding=locale.getpreferredencoding(False)
184 ) as stdout_mock,
185 ):
186 # Switch stdout with test pty
187 monkey.setattr(sys, "stdout", stdout_mock)
188 monkey.setenv("COLUMNS", "84")
189 monkey.setenv("LINES", "42")
190
191 executor = LocalSubProcessExecutor(colored=False)
192 request = ExecuteRequest(
193 cmd=[sys.executable, "-c", "import os; print(os.get_terminal_size())"],
194 cwd=Path(),
195 env=os_env,
196 stdin=StdinSource.OFF,
197 run_id="",
198 )
199 out_err = FakeOutErr()
200 with executor.call(request, show=False, out_err=out_err.out_err, env=_create_mock_env()) as status:
201 while status.exit_code is None: # pragma: no branch
202 status.wait()
203 outcome = status.outcome
204 assert outcome is not None
205 assert bool(outcome), outcome
206 expected_out = f"{terminal_size!r}\r\n"
207 assert outcome.out == expected_out, expected_out[len(outcome.out) :]
208 assert not outcome.err
209
210
211 def test_local_execute_basic_fail(capsys: CaptureFixture, caplog: LogCaptureFixture, monkeypatch: MonkeyPatch) -> None:
212 monkeypatch.chdir(Path(__file__).parents[3])
213 caplog.set_level(logging.NOTSET)
214 executor = LocalSubProcessExecutor(colored=False)
215 cwd = Path().absolute()
216 cmd = [
217 sys.executable,
218 "-c",
219 "import sys; print('out', end=''); print('err', file=sys.stderr, end=''); sys.exit(3)",
220 ]
221 request = ExecuteRequest(cmd=cmd, cwd=cwd, env=os.environ.copy(), stdin=StdinSource.OFF, run_id="")
222
223 # run test
224 out_err = FakeOutErr()
225 with executor.call(request, show=False, out_err=out_err.out_err, env=_create_mock_env()) as status:
226 while status.exit_code is None: # pragma: no branch
227 status.wait()
228 outcome = status.outcome
229 assert outcome is not None
230
231 assert repr(outcome)
232
233 # assert no output, no logs
234 out, err = out_err.read_out_err()
235 assert not out
236 assert not err
237 assert not caplog.records
238
239 # assert return object
240 assert bool(outcome) is False, outcome
241 assert outcome.exit_code == 3
242 assert outcome.err == "err"
243 assert outcome.out == "out"
244 assert outcome.request == request
245
246 # asset fail
247 with pytest.raises(SystemExit) as context:
248 outcome.assert_success()
249 # asset fail
250 assert context.value.code == 3
251
252 out, err = capsys.readouterr()
253 assert out == "out\n"
254 expected = f"{Fore.RED}err{Fore.RESET}\n"
255 assert err == expected
256
257 assert len(caplog.records) == 1
258 record = caplog.records[0]
259 assert record.levelno == logging.CRITICAL
260 assert record.msg == "exit %s (%.2f seconds) %s> %s%s"
261 assert record.args is not None
262 code, duration, cwd_, cmd_, metadata = record.args
263 assert code == 3
264 assert cwd_ == cwd
265 assert cmd_ == request.shell_cmd
266 assert isinstance(duration, float)
267 assert duration > 0
268 assert isinstance(metadata, str)
269 assert metadata.startswith(" pid=")
270
271
272 def test_command_does_not_exist(caplog: LogCaptureFixture, os_env: dict[str, str]) -> None:
273 caplog.set_level(logging.NOTSET)
274 executor = LocalSubProcessExecutor(colored=False)
275 request = ExecuteRequest(
276 cmd=["sys-must-be-missing"],
277 cwd=Path().absolute(),
278 env=os_env,
279 stdin=StdinSource.OFF,
280 run_id="",
281 )
282 out_err = FakeOutErr()
283 with executor.call(request, show=False, out_err=out_err.out_err, env=_create_mock_env()) as status:
284 while status.exit_code is None: # pragma: no branch
285 status.wait() # pragma: no cover
286 outcome = status.outcome
287 assert outcome is not None
288
289 assert bool(outcome) is False, outcome
290 assert outcome.exit_code != Outcome.OK
291 assert not outcome.out
292 assert not outcome.err
293 assert len(caplog.records) == 1
294 assert caplog.records[0].levelname == "ERROR"
295 assert re.match(
296 r".*(No such file or directory|The system cannot find the file specified).*", caplog.records[0].message
297 )
298
299
300 @pytest.mark.skipif(sys.platform == "win32", reason="You need a conhost shell for keyboard interrupt")
301 @pytest.mark.flaky(max_runs=3, min_passes=1)
302 def test_command_keyboard_interrupt(tmp_path: Path, monkeypatch: MonkeyPatch, capfd: CaptureFixture) -> None:
303 monkeypatch.chdir(tmp_path)
304 process_up_signal = tmp_path / "signal"
305 cmd = [sys.executable, str(Path(__file__).parent / "local_subprocess_sigint.py"), str(process_up_signal)]
306 process = subprocess.Popen(cmd)
307 while not process_up_signal.exists():
308 assert process.poll() is None
309 root = process.pid
310 try:
311 child = next(iter(psutil.Process(pid=root).children())).pid
312 except AccessDenied as exc: # pragma: no cover # on termux for example
313 pytest.skip(str(exc)) # pragma: no cover
314 raise # pragma: no cover
315
316 print(f"test running in {os.getpid()} and sending CTRL+C to {process.pid}", file=sys.stderr) # noqa: T201
317 process.send_signal(SIG_INTERRUPT)
318 try:
319 process.communicate(timeout=3)
320 except subprocess.TimeoutExpired: # pragma: no cover
321 process.kill()
322 raise
323
324 out, err = capfd.readouterr()
325 assert f"W requested interrupt of {child} from {root}, activate in 0.01" in err, err
326 assert f"W send signal SIGINT(2) to {child} from {root} with timeout 0.05" in err, err
327 assert f"W send signal SIGTERM(15) to {child} from {root} with timeout 0.07" in err, err
328 assert f"W send signal SIGKILL(9) to {child} from {root}" in err, err
329
330 outs = out.split("\n")
331
332 exit_code = int(outs[0])
333 assert exit_code == -9
334 assert float(outs[3]) > 0 # duration
335 assert "how about no signal 2" in outs[1], outs[1] # 2 - Interrupt
336 assert "how about no signal 15" in outs[1], outs[1] # 15 - Terminated
337
338
339 @pytest.mark.parametrize("tty_mode", ["on", "off"])
340 def test_local_subprocess_tty(monkeypatch: MonkeyPatch, mocker: MockerFixture, tty_mode: str) -> None:
341 monkeypatch.setenv("COLUMNS", "100")
342 monkeypatch.setenv("LINES", "100")
343 tty = tty_mode == "on"
344 mocker.patch("sys.stdout.isatty", return_value=tty)
345 mocker.patch("sys.stderr.isatty", return_value=tty)
346 try:
347 import termios # noqa: F401, PLC0415
348 except ImportError:
349 exp_tty = False # platforms without tty support at all
350 else:
351 # to avoid trying (and failing) to copy mode bits
352 exp_tty = tty
353 mocker.patch("termios.tcgetattr")
354 mocker.patch("termios.tcsetattr")
355
356 executor = LocalSubProcessExecutor(colored=False)
357 cmd: list[str] = [sys.executable, str(Path(__file__).parent / "tty_check.py")]
358 request = ExecuteRequest(cmd=cmd, stdin=StdinSource.API, cwd=Path.cwd(), env=dict(os.environ), run_id="")
359 out_err = FakeOutErr()
360 with executor.call(request, show=False, out_err=out_err.out_err, env=_create_mock_env()) as status:
361 while status.exit_code is None: # pragma: no branch
362 status.wait()
363 outcome = status.outcome
364 assert outcome is not None
365
366 assert outcome
367 info = json.loads(outcome.out)
368 assert info == {
369 "stdout": exp_tty,
370 "stderr": exp_tty,
371 "stdin": False,
372 "terminal": [100, 100],
373 }
374
375
376 @pytest.mark.parametrize("mode", ["stem", "full", "stem-pattern", "full-pattern", "all"])
377 def test_allow_list_external_ok(fake_exe_on_path: Path, mode: str) -> None:
378 exe = f"{fake_exe_on_path}{'.EXE' if sys.platform == 'win32' else ''}"
379 allow = exe if "full" in mode else fake_exe_on_path.stem
380 allow = f"{allow[:-2]}*" if "pattern" in mode else allow
381 allow = "*" if mode == "all" else allow
382
383 request = ExecuteRequest(
384 cmd=[fake_exe_on_path.stem],
385 cwd=Path.cwd(),
386 env={"PATH": os.environ["PATH"]},
387 stdin=StdinSource.OFF,
388 run_id="run-id",
389 allow=[allow],
390 )
391 inst = LocalSubProcessExecuteInstance(request, MagicMock(), out=SyncWrite("out", None), err=SyncWrite("err", None))
392
393 assert inst.cmd == [exe]
394
395
396 def test_shebang_limited_on(tmp_path: Path) -> None:
397 exe, script, instance = _create_shebang_test(tmp_path, env={"TOX_LIMITED_SHEBANG": "1"})
398 if sys.platform == "win32": # pragma: win32 cover
399 assert instance.cmd == [str(script), "--magic"]
400 else:
401 assert instance.cmd == [exe, "-s", str(script), "--magic"]
402
403
404 @pytest.mark.parametrize("env", [{}, {"TOX_LIMITED_SHEBANG": ""}])
405 def test_shebang_limited_off(tmp_path: Path, env: dict[str, str]) -> None:
406 _, script, instance = _create_shebang_test(tmp_path, env=env)
407 assert instance.cmd == [str(script), "--magic"]
408
409
410 def test_shebang_failed_to_parse(tmp_path: Path) -> None:
411 _, script, instance = _create_shebang_test(tmp_path, env={"TOX_LIMITED_SHEBANG": "yes"})
412 script.write_text("")
413 assert instance.cmd == [str(script), "--magic"]
414
415
416 def _create_shebang_test(tmp_path: Path, env: dict[str, str]) -> tuple[str, Path, LocalSubProcessExecuteInstance]:
417 exe = shutil.which("python3") or shutil.which("python")
418 assert exe is not None
419 script = tmp_path / f"s{'.EXE' if sys.platform == 'win32' else ''}"
420 script.write_text(f"#!{exe} -s")
421 script.chmod(script.stat().st_mode | stat.S_IEXEC) # mark it executable
422 env["PATH"] = str(script.parent)
423 request = create_autospec(ExecuteRequest, cmd=["s", "--magic"], env=env, allow=None)
424 writer = create_autospec(SyncWrite)
425 instance = LocalSubProcessExecuteInstance(request, create_autospec(ExecuteOptions), writer, writer)
426 return exe, script, instance
427
428
429 @pytest.mark.parametrize("key", ["COLUMNS", "ROWS"])
430 def test_local_execute_does_not_overwrite(key: str, mocker: MockerFixture) -> None:
431 mocker.patch("shutil.get_terminal_size", return_value=(101, 102))
432 env = dict(os.environ)
433 env[key] = key
434 executor = LocalSubProcessExecutor(colored=False)
435 cmd = [sys.executable, "-c", f"import os; print(os.environ['{key}'], end='')"]
436 request = ExecuteRequest(cmd=cmd, stdin=StdinSource.API, cwd=Path.cwd(), env=env, run_id="")
437 out_err = FakeOutErr()
438 with executor.call(request, show=False, out_err=out_err.out_err, env=_create_mock_env()) as status:
439 while status.exit_code is None: # pragma: no branch
440 status.wait()
441 outcome = status.outcome
442
443 assert outcome is not None
444 assert outcome.out == key
445
446
447 @pytest.mark.skipif(sys.platform == "win32", reason="Unix-specific tests")
448 def test_read_via_thread_eintr_during_select(mocker: MockerFixture) -> None:
449 """Test that EINTR during selector.select() is handled correctly."""
450 # Create a pipe
451 read_fd, write_fd = os.pipe()
452 try:
453 data_received = []
454 eintr_raised = [False]
455
456 def handler(data: bytes) -> int:
457 data_received.append(data)
458 return len(data)
459
460 # Mock selector.select to raise OSError with EINTR once
461 original_select = selectors.DefaultSelector.select
462
463 def mock_select(self: Any, timeout: float | None = None) -> list[tuple[selectors.SelectorKey, int]]:
464 if not eintr_raised[0]:
465 eintr_raised[0] = True
466 err = OSError("Interrupted system call")
467 err.errno = errno.EINTR
468 raise err
469 return original_select(self, timeout)
470
471 mocker.patch.object(selectors.DefaultSelector, "select", mock_select)
472
473 # Write some data
474 os.write(write_fd, b"test data")
475 os.close(write_fd)
476
477 # Read the data
478 reader = ReadViaThreadUnix(read_fd, handler, "test", drain=True)
479 with reader:
480 reader.thread.join(timeout=2)
481
482 assert eintr_raised[0], "EINTR should have been raised"
483 assert b"test data" in data_received, "Data should still be read after EINTR"
484 finally:
485 with contextlib.suppress(OSError):
486 os.close(read_fd)
487
488
489 @pytest.mark.skipif(sys.platform == "win32", reason="Unix-specific tests")
490 def test_read_via_thread_eintr_during_read(mocker: MockerFixture) -> None:
491 """Test that EINTR during os.read() is handled correctly."""
492 # Create a pipe
493 read_fd, write_fd = os.pipe()
494 try:
495 data_received = []
496
497 def handler(data: bytes) -> int:
498 data_received.append(data)
499 return len(data)
500
501 # Mock os.read to raise EINTR once, then work normally
502 original_read = os.read
503 call_count = [0]
504
505 def mock_read(fd: int, n: int) -> bytes:
506 call_count[0] += 1
507 if call_count[0] == 1:
508 err = OSError("Interrupted")
509 err.errno = errno.EINTR
510 raise err
511 return original_read(fd, n)
512
513 mocker.patch("os.read", mock_read)
514
515 # Write some data
516 os.write(write_fd, b"test data")
517 os.close(write_fd)
518
519 # Read the data
520 reader = ReadViaThreadUnix(read_fd, handler, "test", drain=True)
521 with reader:
522 reader.thread.join(timeout=1)
523
524 assert b"test data" in data_received
525 finally:
526 with contextlib.suppress(OSError):
527 os.close(read_fd)
528
529
530 @pytest.mark.skipif(sys.platform == "win32", reason="Unix-specific tests")
531 def test_read_via_thread_ebadf_during_read(mocker: MockerFixture) -> None:
532 """Test that EBADF during os.read() is handled correctly."""
533 # Create a pipe
534 read_fd, write_fd = os.pipe()
535 try:
536 data_received = []
537
538 def handler(data: bytes) -> int:
539 data_received.append(data)
540 return len(data)
541
542 # Mock os.read to raise EBADF
543 def mock_read(fd: int, n: int) -> NoReturn: # noqa: ARG001
544 err = OSError("Bad file descriptor")
545 err.errno = errno.EBADF
546 raise err
547
548 mocker.patch("os.read", mock_read)
549
550 # Close write end
551 os.close(write_fd)
552
553 # Try to read - should handle EBADF gracefully
554 reader = ReadViaThreadUnix(read_fd, handler, "test", drain=True)
555 with reader:
556 reader.thread.join(timeout=1)
557
558 # Should not crash, but won't receive data due to EBADF
559 finally:
560 with contextlib.suppress(OSError):
561 os.close(read_fd)
562
563
564 @pytest.mark.skipif(sys.platform == "win32", reason="Unix-specific tests")
565 def test_read_via_thread_drain_no_data() -> None:
566 """Test drain_stream when there's no data ready."""
567 # Create a pipe with no data
568 read_fd, write_fd = os.pipe()
569 try:
570 data_received = []
571
572 def handler(data: bytes) -> int:
573 data_received.append(data)
574 return len(data)
575
576 # Close write end immediately so no data will be ready
577 os.close(write_fd)
578
579 reader = ReadViaThreadUnix(read_fd, handler, "test", drain=True)
580 # Call drain directly to test the "if not ready: break" path
581 reader._drain_stream() # noqa: SLF001
582
583 # Should handle gracefully with no data
584 assert len(data_received) == 0
585 finally:
586 with contextlib.suppress(OSError):
587 os.close(read_fd)