diff --git a/src/tox_current_env/hooks4.py b/src/tox_current_env/hooks4.py index 40a263b..5628eff 100644 --- a/src/tox_current_env/hooks4.py +++ b/src/tox_current_env/hooks4.py @@ -15,6 +15,11 @@ from tox.plugin import impl from tox.tox_env.python.api import PythonInfo from tox.tox_env.python.runner import PythonRun +try: + import importlib.metadata as importlib_metadata +except ImportError: + import importlib_metadata + @impl def tox_register_tox_env(register): @@ -106,6 +111,15 @@ class Installer: def install(self, *args, **kwargs): return None + def installed(self): + """Return list of installed packages like `pip freeze`.""" + return [ + "{}=={}".format(d.metadata.get("name"), d.version) + for d in sorted( + importlib_metadata.distributions(), key=lambda d: d.metadata.get("name") + ) + ] + class CurrentEnvLocalSubProcessExecutor(Execute): def build_instance( diff --git a/tests/test_integration_tox4.py b/tests/test_integration_tox4.py index 90cf7df..34e8b0d 100644 --- a/tests/test_integration_tox4.py +++ b/tests/test_integration_tox4.py @@ -421,3 +421,12 @@ def test_pass_env(projdir, pass_env): assert result.returncode == 0 assert "\nassertme\n" in result.stdout assert "\nNone\n" not in result.stdout + + +def test_report_installed(projdir): + # tox4 only reports installed when a CI is detected + env = {"CI": "true"} + result = tox("-e", NATIVE_TOXENV, "--current-env", env=env, quiet=False) + assert result.returncode == 0 + assert "tox==" in result.stdout + assert "pytest==" in result.stdout