debugpy/tests/pytest_hooks.py
Rich Chiodo ae6812bdac
Pull in pydevd sys.monitoring support (#1680)
This is pulling in @fabioz's latest changes for sys.monitoring and then fixing up any issues found with the debugpy tests.

A lot of the changes were made by Fabio since the latest pull from pydevd, so I also created this PR to compare what changes I made to Fabio's baseline:
rchiodo/PyDev.Debugger#1

Meaning you really only need to look at that other PR to see what changes I made. The rest of the changes here are from Fabio or ruff doing reformating.

After this goes through, we should have sys.monitoring support in debugpy. We can decide later if we want to implement our own support as @int19h started.

Fixes #1496
2024-09-23 11:22:54 -07:00

63 lines
2.1 KiB
Python

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root
# for license information.
import os
import pytest
import pytest_timeout
import sys
from debugpy.common import log # pyright: ignore[reportAttributeAccessIssue]
import tests
from tests import logs
def pytest_addoption(parser):
parser.addoption(
"--debugpy-log-dir",
type=str,
help="Write debugpy and pydevd logs to the specified directory",
)
parser.addoption(
"--debugpy-log-passed",
action="store_true",
help="Keep debugpy and pydevd logs for tests that passed",
)
def pytest_configure(config):
if config.option.debugpy_log_dir:
log.log_dir = config.option.debugpy_log_dir
else:
bits = 64 if sys.maxsize > 2**32 else 32
ver = "{0}.{1}-{bits}".format(*sys.version_info, bits=bits)
log.log_dir = (tests.root / "_logs" / ver).strpath
log.info("debugpy and pydevd logs will be under {0}", log.log_dir)
def pytest_report_header(config):
return log.get_environment_description(f"Test environment for tests-{os.getpid()}")
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
# Adds attributes setup_report, call_report, and teardown_report to the item,
# referencing TestReport instances for the corresponding phases, after the scope
# finished running its tests. This can be used in function-level fixtures to
# detect test failures, e.g.:
#
# if request.node.call_report.failed: ...
outcome = yield
report = outcome.get_result()
setattr(item, report.when + "_report", report)
def pytest_make_parametrize_id(config, val):
return getattr(val, "pytest_id", None)
# If a test times out and pytest tries to print the stacks of where it was hanging,
# we want to print the pydevd log as well. This is not a normal pytest hook - we
# just detour pytest_timeout.dump_stacks directly.
_dump_stacks = pytest_timeout.dump_stacks
pytest_timeout.dump_stacks = lambda terminal: (_dump_stacks(terminal), logs.dump())