Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,32 @@ If you want to test with pre-built wheels instead, use the ``--wheel-path`` opti

See :doc:`wheel_sources` for more details on using pre-built wheels.


Overriding the Command
-----------------------

Each venv instance runs the command it was configured with. To run a
different command inside a matching venv without changing ``riotfile.py``,
pass ``--command`` to ``run``:

.. code-block:: bash

$ riot run --command "mypy --strict" test

The override replaces the venv's ``command`` entirely, but still benefits
from the venv's dependencies and environment setup, including
``--pass-env`` and any environment variables declared on the ``Venv``.
Instances with no ``command`` of their own are otherwise skipped, but will
still run when ``--command`` is given.

As with a venv's regular command, ``{cmdargs}`` in the override is replaced
with any extra arguments passed after ``--``:

.. code-block:: bash

$ riot run --command "pytest {cmdargs}" test -- -k test_foo

This is useful for running external tooling (e.g. a separate test runner)
inside a riot venv so that it inherits the venv's full environment without
having to reconstruct riot's activation logic itself.

11 changes: 11 additions & 0 deletions releasenotes/notes/run-command-override-c2305da0609b4ee8.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
features:
- |
Add ``riot run --command`` to override a venv's baked command. When
set, riot runs the given command inside the matched venv(s) instead of
the venv's configured ``command``, using the same environment
preparation, ``--pass-env`` and dependency-install semantics. ``{cmdargs}``
is substituted within the override, just like with a venv's regular
command. This allows external runners to execute inside a riot venv and
inherit its full environment without reconstructing the activation env
themselves.
8 changes: 8 additions & 0 deletions riot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ def generate(ctx, recreate_venvs, skip_base_install, pythons, pattern):
@PYTHON_VERSIONS_ARG
@click.option("--skip-missing", "skip_missing", is_flag=True, default=False)
@click.option("--exitfirst", "-x", "exit_first", is_flag=True, default=False)
@click.option(
"--command",
"command_override",
default=None,
help="Override the venv's command. When set, run this command in the venv instead of the venv's baked command. {cmdargs} is substituted within the override.",
)
@PATTERN_ARG
@VENV_PATTERN_ARG
@RECOMPILE_REQS_ARG
Expand All @@ -208,6 +214,7 @@ def run(
pythons,
skip_missing,
exit_first,
command_override,
pattern,
venv_pattern,
recompile_reqs,
Expand All @@ -223,6 +230,7 @@ def run(
pythons=pythons,
skip_missing=skip_missing,
exit_first=exit_first,
command_override=command_override,
recompile_reqs=recompile_reqs,
wheel_path=wheel_path,
)
Expand Down
7 changes: 5 additions & 2 deletions riot/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def run(
pythons: t.Optional[t.Set[Interpreter]] = None,
skip_missing: bool = False,
exit_first: bool = False,
command_override: t.Optional[str] = None,
recompile_reqs: bool = False,
wheel_path: t.Optional[str] = None,
) -> None:
Expand All @@ -91,7 +92,7 @@ def run(
)

for inst in self.venv.instances():
if inst.command is None:
if command_override is None and inst.command is None:
logger.debug("Skipping venv instance %s due to missing command", inst)
continue

Expand Down Expand Up @@ -173,7 +174,9 @@ def run(

try:
# Finally, run the test in the base venv.
command = inst.command
command = (
command_override if command_override is not None else inst.command
)
assert command is not None
if cmdargs is not None:
command = command.format(
Expand Down
146 changes: 146 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def assert_args(args):
"pythons",
"skip_missing",
"exit_first",
"command_override",
"recompile_reqs",
"wheel_path",
]
Expand Down Expand Up @@ -691,3 +692,148 @@ def test_env():
)
assert result.exit_code == 0
assert "1 passed with 0 warnings, 0 failed" in result.stdout


def test_run_command_override(cli: click.testing.CliRunner) -> None:
"""--command overrides the venv's baked command."""
with cli.isolated_filesystem():
with open("riotfile.py", "w") as f:
f.write("""
from riot import Venv

venv = Venv(
venvs=[
Venv(
pys=[3],
name="test",
command="echo baked",
),
],
)
""")

with mock.patch("subprocess.run") as subprocess_run:
subprocess_run.return_value.returncode = 0
result = cli.invoke(
riot.cli.main,
["run", "-s", "--command", "echo override", "test"],
catch_exceptions=False,
)
assert result.exit_code == 0, result.stdout

subprocess_run.assert_called()
cmd = subprocess_run.call_args_list[-1].args[0]
assert cmd.endswith("echo override"), cmd


def test_run_command_override_with_cmdargs(cli: click.testing.CliRunner) -> None:
"""--command with {cmdargs} substitution."""
with cli.isolated_filesystem():
with open("riotfile.py", "w") as f:
f.write("""
from riot import Venv

venv = Venv(
venvs=[
Venv(
pys=[3],
name="test",
command="echo baked",
),
],
)
""")

with mock.patch("subprocess.run") as subprocess_run:
subprocess_run.return_value.returncode = 0
result = cli.invoke(
riot.cli.main,
["run", "-s", "--command", "echo {cmdargs}", "test", "--", "hello"],
catch_exceptions=False,
)
assert result.exit_code == 0, result.stdout

subprocess_run.assert_called()
cmd = subprocess_run.call_args_list[-1].args[0]
assert cmd.endswith("echo 'hello'"), cmd


def test_run_command_override_long_arg(cli: click.testing.CliRunner) -> None:
"""--command with long arg name passes override through to Session.run."""
with mock.patch("riot.cli.Session.run") as run:
with with_riotfile(cli, "empty_riotfile.py"):
result = cli.invoke(
riot.cli.main,
["run", "--command", "echo override"],
)
assert result.exit_code == 0
run.assert_called_once()
kwargs = run.call_args.kwargs
assert kwargs["command_override"] == "echo override"


def test_run_command_override_without_baked_command(
cli: click.testing.CliRunner,
) -> None:
"""--command runs venv instances that have no baked command of their own."""
with cli.isolated_filesystem():
with open("riotfile.py", "w") as f:
f.write("""
from riot import Venv

venv = Venv(
venvs=[
Venv(
pys=[3],
name="test",
pkgs={},
),
],
)
""")

with mock.patch("subprocess.run") as subprocess_run:
subprocess_run.return_value.returncode = 0
result = cli.invoke(
riot.cli.main,
["run", "-s", "--command", "echo override", "test"],
catch_exceptions=False,
)
assert result.exit_code == 0, result.stdout

subprocess_run.assert_called()
cmd = subprocess_run.call_args_list[-1].args[0]
assert cmd.endswith("echo override"), cmd


def test_run_without_command_override_skips_missing_command(
cli: click.testing.CliRunner,
) -> None:
"""Without --command, venv instances with no baked command are still skipped."""
with cli.isolated_filesystem():
with open("riotfile.py", "w") as f:
f.write("""
from riot import Venv

venv = Venv(
venvs=[
Venv(
pys=[3],
name="test",
pkgs={},
),
],
)
""")

with mock.patch("subprocess.run") as subprocess_run, mock.patch(
"riot.session.run_cmd_venv"
) as run_cmd_venv:
subprocess_run.return_value.returncode = 0
result = cli.invoke(
riot.cli.main,
["run", "-s", "test"],
catch_exceptions=False,
)
assert result.exit_code == 0, result.stdout
run_cmd_venv.assert_not_called()
Loading