Skip to content

Commit

Permalink
feat: add RIOT specific envs to every venv run (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettlangdon authored Mar 29, 2022
1 parent 33866ba commit 5763b2a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
features:
- |
Inject riot virtualenv instance data into each run via environment variables:
- ``RIOT``: always set to ``"1"``
- ``RIOT_PYTHON_HINT``: the Python version hint provided to riot, e.g. ``"3.6"``
- ``RIOT_PYTHON_VERSION``: the actual Python found/used by riot, e.g. ``"3.6.10"``
- ``RIOT_VENV_HASH``: the short hash for the running virtualenv instance, e.g. ``"d36fc71"``
- ``RIOT_VENV_IDENT``: the string identifier used to create the virtualenv, e.g. ``"pytest_pytest-cov_mock_typing-extensions"``
- Note: This is only set if the virtualenv instance has defined packages
- ``RIOT_VENV_NAME``: the name of the virtualenv instance running, e.g. ``"test"``, ``"mypy"``, etc
- ``RIOT_VENV_PKGS``: the string of packages + versions this virtualenv instance provided to the ``pip install`` command, e.g. ``"'pytest' 'pytest-cov' 'flask>=2.0.0'"``
- Note: this may be empty if only parent virtualenv packages are used
- ``RIOT_VENV_FULL_PKGS``: the full list of packages this virtualenv instance and all of it's parents provided to the ``pip install`` command, e.g. ``"'pytest' 'pytest-cov' 'flask>=2.0.0'"``
14 changes: 14 additions & 0 deletions riot/riot.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,20 @@ def run(
else:
env = dict(inst.env)

# Add riot specific environment variables
env.update(
{
"RIOT": "1",
"RIOT_PYTHON_HINT": str(inst.py),
"RIOT_PYTHON_VERSION": inst.py.version(),
"RIOT_VENV_HASH": inst.short_hash,
"RIOT_VENV_IDENT": inst.ident or "",
"RIOT_VENV_NAME": inst.name or "",
"RIOT_VENV_PKGS": inst.pkg_str,
"RIOT_VENV_FULL_PKGS": inst.full_pkg_str,
}
)

inst.prepare(env, recreate=recreate_venvs, skip_deps=skip_base_install)

pythonpath = inst.pythonpath
Expand Down
47 changes: 47 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,53 @@ def test_success():
assert re.search(r"✓ envtest: \[[0-9a-f]{7}\]", result.stdout)


def test_default_env(cli: click.testing.CliRunner) -> None:
with cli.isolated_filesystem():
with open("riotfile.py", "w") as f:
f.write(
"""
from riot import Venv, latest
venv = Venv(
pkgs={
"pytest": latest,
},
venvs=[
Venv(
pys=[3],
pkgs={"packaging": ">=21.3"},
name="envtest",
command="pytest",
),
],
)
"""
)

with open("test_success.py", "w") as f:
f.write(
"""
import os
def test_success():
assert os.environ["RIOT"] == "1"
assert os.environ["RIOT_PYTHON_HINT"] == "Interpreter(_hint='3')"
assert os.environ["RIOT_PYTHON_VERSION"].startswith("3.")
assert os.environ["RIOT_VENV_HASH"] == "f8691e0"
assert os.environ["RIOT_VENV_IDENT"] == "packaging213"
assert os.environ["RIOT_VENV_NAME"] == "envtest"
assert os.environ["RIOT_VENV_PKGS"] == "'packaging>=21.3'"
assert os.environ["RIOT_VENV_FULL_PKGS"] == "'pytest' 'packaging>=21.3'"
"""
)

result = cli.invoke(
riot.cli.main, ["run", "-s", "envtest"], catch_exceptions=False
)
assert result.exit_code == 0
assert "✓ envtest" in result.stdout


def test_pass_env_always(
cli: click.testing.CliRunner, monkeypatch: _pytest.monkeypatch.MonkeyPatch
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def test_venv_instance_pythonpath(tmp_path: pathlib.Path, tmp_run: _T_TmpRun) ->
""",
)
result = tmp_run("riot run -s test")
env = dict(_.split("=") for _ in result.stdout.splitlines() if "=" in _)
env = dict(_.split("=", maxsplit=1) for _ in result.stdout.splitlines() if "=" in _)
assert result.returncode == 0

version = "".join((str(_) for _ in sys.version_info[:3]))
Expand Down Expand Up @@ -689,7 +689,7 @@ def test_venv_instance_path(tmp_path: pathlib.Path, tmp_run: _T_TmpRun) -> None:
""",
)
result = tmp_run("riot run -s test")
env = dict(_.split("=") for _ in result.stdout.splitlines() if "=" in _)
env = dict(_.split("=", maxsplit=1) for _ in result.stdout.splitlines() if "=" in _)
assert result.returncode == 0, result.stderr

venv_name = "venv_py{}_pytest".format(
Expand Down

0 comments on commit 5763b2a

Please sign in to comment.