File: pytest_hooks.py

package info (click to toggle)
debugpy 1.8.12%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,424 kB
  • sloc: python: 14,451; sh: 184; makefile: 33
file content (63 lines) | stat: -rw-r--r-- 2,134 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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())