diff --git a/src/debugpy/common/log.py b/src/debugpy/common/log.py index e7f5e3fb..7802f6a8 100644 --- a/src/debugpy/common/log.py +++ b/src/debugpy/common/log.py @@ -279,7 +279,7 @@ def prefixed(format_string, *args, **kwargs): _tls.prefix = old_prefix -def describe_environment(header): +def get_environment_description(header): import sysconfig import site # noqa @@ -342,9 +342,31 @@ def describe_environment(header): report_paths("os.__file__") report_paths("threading.__file__") report_paths("debugpy.__file__") + report("\n") - result = "".join(result).rstrip("\n") - info("{0}", result) + importlib_metadata = None + try: + import importlib_metadata + except ImportError: + try: + from importlib import metadata as importlib_metadata + except ImportError: + pass + if importlib_metadata is None: + report("Cannot enumerate installed packages - missing importlib_metadata.") + else: + report("Installed packages:\n") + try: + for pkg in importlib_metadata.distributions(): + report(" {0}=={1}\n", pkg.name, pkg.version) + except Exception: + swallow_exception("Error while enumerating installed packages.") + + return "".join(result).rstrip("\n") + + +def describe_environment(header): + info("{0}", get_environment_description(header)) stderr = LogFile( diff --git a/tests/pytest_fixtures.py b/tests/pytest_fixtures.py index ac8c7f5b..bbba2c2e 100644 --- a/tests/pytest_fixtures.py +++ b/tests/pytest_fixtures.py @@ -69,6 +69,7 @@ def test_wrapper(request, long_tmpdir): with log.to_file(prefix="tests"): timestamp.reset() log.info("{0} started.", request.node.nodeid) + log.describe_environment("Environment:") try: yield finally: diff --git a/tests/pytest_hooks.py b/tests/pytest_hooks.py index 21912ff2..f37eecb8 100644 --- a/tests/pytest_hooks.py +++ b/tests/pytest_hooks.py @@ -36,7 +36,7 @@ def pytest_configure(config): def pytest_report_header(config): - log.describe_environment(f"Test environment for tests-{os.getpid()}") + return log.get_environment_description(f"Test environment for tests-{os.getpid()}") @pytest.hookimpl(hookwrapper=True, tryfirst=True) diff --git a/tests/requirements.txt b/tests/requirements.txt index 201d4b2e..f6678ce6 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -7,6 +7,7 @@ pytest-timeout ## Used by test helpers: +importlib_metadata psutil ## Used in Python code that is run/debugged by the tests: