diff --git a/docs/changelog/3234.feature.rst b/docs/changelog/3234.feature.rst new file mode 100644 index 000000000..ea555d678 --- /dev/null +++ b/docs/changelog/3234.feature.rst @@ -0,0 +1,2 @@ +Allow plugins attaching additional information to ``--version`` via ``tox_append_version_info`` method in the plugin +module - by :user:`gaborbernat`. diff --git a/docs/plugins.rst b/docs/plugins.rst index 2a74dd3cc..6dae6febc 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -14,6 +14,15 @@ Extensions points .. automodule:: tox.plugin.spec :members: +A plugin can define its plugin module a: + +.. code-block:: python + + def tox_append_version_info() -> str: + return "magic" + +and this message will be appended to the output of the ``--version`` flag. + Adoption of a plugin under tox-dev Github organization ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/tox/session/cmd/version_flag.py b/src/tox/session/cmd/version_flag.py index e0e2f19d0..6b1a889fc 100644 --- a/src/tox/session/cmd/version_flag.py +++ b/src/tox/session/cmd/version_flag.py @@ -43,5 +43,7 @@ def get_version_info() -> str: out.append("registered plugins:") for module, egg_info in plugin_info: source = getattr(module, "__file__", repr(module)) - out.append(f" {egg_info.project_name}-{egg_info.version} at {source}") + info = module.tox_append_version_info() if hasattr(module, "tox_append_version_info") else "" + with_info = f" {info}" if info else "" + out.append(f" {egg_info.project_name}-{egg_info.version} at {source}{with_info}") return "\n".join(out) diff --git a/tests/test_version.py b/tests/test_version.py index 685a4528c..5bfdbb3cc 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -26,10 +26,13 @@ def test_version_without_plugin(tox_project: ToxProjectCreator) -> None: def test_version_with_plugin(tox_project: ToxProjectCreator, mocker: MockFixture) -> None: dist = [ ( - mocker.create_autospec("types.ModuleType", __file__=f"{i}-path"), - SimpleNamespace(project_name=i, version=v), - ) - for i, v in (("B", "1.0"), ("A", "2.0")) + mocker.create_autospec("types.ModuleType", __file__="B-path", tox_append_version_info=lambda: "magic"), + SimpleNamespace(project_name="B", version="1.0"), + ), + ( + mocker.create_autospec("types.ModuleType", __file__="A-path"), + SimpleNamespace(project_name="A", version="2.0"), + ), ] mocker.patch.object(MANAGER.manager, "list_plugin_distinfo", return_value=dist) @@ -42,6 +45,6 @@ def test_version_with_plugin(tox_project: ToxProjectCreator, mocker: MockFixture assert lines[1:] == [ "registered plugins:", - " B-1.0 at B-path", + " B-1.0 at B-path magic", " A-2.0 at A-path", ]