Skip to content

Commit

Permalink
Add --print-dependency-groups-to
Browse files Browse the repository at this point in the history
  • Loading branch information
hroncok committed Oct 23, 2024
1 parent 0cf2785 commit cb15997
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 23 deletions.
27 changes: 25 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ The ``tox-current-env`` plugin adds these options:
This is useful for preparing the current environment for ``tox --current-env``.
Use ``-`` for ``FILE`` to print to standard output.

It is possible to use the two printing options together, as long as the ``FILE`` is different.
``tox --print-dependency-groups-to=FILE``
Instead of running any ``commands``, simply prints the names of the
`declared dependency_groups <https://tox.wiki/en/latest/config.html#dependency_groups>`_
in ``dependency_groups`` to the specified ``FILE``.
This is useful for preparing the current environment for ``tox --current-env``.
Use ``-`` for ``FILE`` to print to standard output.
This option only exists with tox 4 and requires at least tox 4.22.

It is possible to use the three printing options together, as long as the ``FILE`` is different.

Invoking ``tox`` without any of the above options should behave as regular ``tox`` invocation without this plugin.
Any deviation from this behavior is considered a bug.
Expand Down Expand Up @@ -93,7 +101,7 @@ Usage
-----

When the plugin is installed,
use ``tox`` with ``--current-env``, ``--print-deps-to`` or ``--print-extras-to``
use ``tox`` with ``--current-env``, ``--print-deps-to``, ``--print-extras-to`` or ``--print-dependency-groups-to``
and all the other options as usual.
Assuming your ``tox`` is installed on Python 3.7:

Expand Down Expand Up @@ -153,6 +161,20 @@ To get a list of names of extras, run:
py37: commands succeeded
congratulations :)
To get a list of names of dependency groups, run:

.. code-block:: console
$ tox -e py37 --print-dependency-groups-to -
py37 create: /home/pythonista/projects/holy-grail/tests/.tox/py37
py37 installed: ...you can see almost anything here...
py37 run-test-pre: PYTHONHASHSEED='3333333333'
group1
...
___________________________________ summary ____________________________________
py37: commands succeeded
congratulations :)
Caveats, warnings and limitations
---------------------------------
Expand All @@ -167,6 +189,7 @@ The plugin is available also for tox 4. Differences in behavior between tox 3 an
- The plugin does not check the requested Python version nor the environment name.
If you let it run for multiple environments they'll all use the same Python.
- Deprecated ``--print-deps-only`` option is no longer available.
- The ``--print-dependency-groups-to`` is only defined on tox 4.

Use an isolated environment
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
40 changes: 30 additions & 10 deletions src/tox_current_env/hooks4.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ def tox_add_option(parser):
help="Don't run tests, only print the names of the required extras to the given file "
+ "(use `-` for stdout)",
)
parser.add_argument(
"--print-dependency-groups-to",
"--print-dependency-groups-to-file",
action="store",
type=argparse.FileType("w"),
metavar="FILE",
default=False,
help="Don't run tests, only print the names of the required dependency-groups to the given file "
+ "(use `-` for stdout)",
)


@impl
def tox_add_core_config(core_conf, state):
opt = state.conf.options

if opt.current_env or opt.print_deps_to or opt.print_extras_to:
if opt.current_env or opt.print_deps_to or opt.print_extras_to or opt.print_dependency_groups_to:
# We do not want to install the main package.
# no_package is the same as skipsdist.
loader = MemoryLoader(no_package=True)
Expand All @@ -72,14 +82,14 @@ def tox_add_core_config(core_conf, state):
opt.default_runner = "current-env"
return

if getattr(opt.print_deps_to, "name", object()) == getattr(
opt.print_extras_to, "name", object()
):
exclusive = [getattr(getattr(opt, o), "name", object())
for o in ("print_deps_to", "print_extras_to", "print_dependency_groups_to")]
if len(exclusive) != len(set(exclusive)):
raise RuntimeError(
"The paths given to --print-deps-to and --print-extras-to cannot be identical."
"The paths given to --print-*-to options cannot be identical."
)

if opt.print_deps_to or opt.print_extras_to:
if opt.print_deps_to or opt.print_extras_to or opt.print_dependency_groups_to:
opt.default_runner = "print-env"
return

Expand All @@ -98,9 +108,8 @@ def tox_add_env_config(env_conf, state):
if opt.current_env:
allow_external_cmds = MemoryLoader(allowlist_externals=["*"], pass_env=["*"])
env_conf.loaders.insert(0, allow_external_cmds)
# For print-deps-to and print-extras-to, use empty
# list of commands so the tox does nothing.
if opt.print_deps_to or opt.print_extras_to:
# For print-*-to, use empty list of commands so that tox does nothing.
if opt.print_deps_to or opt.print_extras_to or opt.print_dependency_groups_to:
empty_commands = MemoryLoader(commands=[], commands_pre=[], commands_post=[])
env_conf.loaders.insert(0, empty_commands)

Expand Down Expand Up @@ -261,10 +270,21 @@ def prepend_env_var_path(self):
)
self.options.print_extras_to.flush()

if self.options.print_dependency_groups_to:
if "dependency_groups" not in self.conf:
raise RuntimeError(
"tox is too old to know about dependency_groups."
)
print(
*self.conf["dependency_groups"],
sep="\n",
file=self.options.print_dependency_groups_to,
)
self.options.print_dependency_groups_to.flush()

# https://github.com/fedora-python/tox-current-env/issues/75
return super().prepend_env_var_path()


@staticmethod
def id():
return "print-env"
15 changes: 14 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shutil

import pytest
from utils import FIXTURES_DIR, TOX4, modify_config, drop_unsupported_pythons
from utils import FIXTURES_DIR, TOX_VERSION, TOX4, modify_config, drop_unsupported_pythons


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -41,3 +41,16 @@ def print_deps_stdout_arg(request):
def print_extras_stdout_arg(request):
"""Argument for printing extras to stdout"""
return request.param


@pytest.fixture
def dependency_groups_support():
"""Support for dependency groups"""
if (TOX_VERSION.major, TOX_VERSION.minor) < (4, 22):
raise pytest.skip(reason="requires tox 4.22 or higher")


@pytest.fixture(params=("--print-dependency-groups-to-file=-", "--print-dependency-groups-to=-"))
def print_dependency_groups_stdout_arg(request, dependency_groups_support):
"""Argument for printing dependency groups to stdout"""
return request.param
3 changes: 3 additions & 0 deletions tests/fixtures/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[dependency-groups]
dg1 = ["build>=1"]
2 changes: 2 additions & 0 deletions tests/fixtures/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ deps =
extras =
dev
full
dependency_groups =
dg1
commands =
python -c 'import os, sys; print(os.path.realpath(sys.exec_prefix), "is the exec_prefix")'
# we explicitly clear this because the inner tox does not need to know
Expand Down
Loading

0 comments on commit cb15997

Please sign in to comment.