Skip to content

Commit

Permalink
Allow plugins to attach data to --version (#3234)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaborbernat committed Mar 5, 2024
1 parent 717e27f commit 5356d96
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
2 changes: 2 additions & 0 deletions 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`.
9 changes: 9 additions & 0 deletions docs/plugins.rst
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 3 additions & 1 deletion src/tox/session/cmd/version_flag.py
Expand Up @@ -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)
13 changes: 8 additions & 5 deletions tests/test_version.py
Expand Up @@ -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)

Expand All @@ -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",
]

0 comments on commit 5356d96

Please sign in to comment.