Files
lix/tests/functional2/testlib/fixtures/pytest_command.py
T
Commentator2.0andRutile 7869385b98 tests/functional2: Make Command Environment actually declarative
So far, the environment used by `command` was completely leaky and the
one used by `nix` was very leaky despite it trying to be a "hermetic"
environment.
This commit moves the hermaticity to `command` and changes its
implementation to be not leak anything.

To achieve this, the following changes were also nessecary:
- the `files` and `snapshot` fixture now use the folder `test-home`
within the tmp_path directory by default, as the `HOME` environment
variable is set to there. (extraction not possible due to dependencies
of command etc also using this directory)

Fixes: #847, #848

Change-Id: I55f86ee0e1615e73fcf442ee2f28f3b89893bbb4
2025-08-15 06:56:07 +00:00

32 lines
1.3 KiB
Python

import pytest
from _pytest.fixtures import FixtureRequest
from functional2.testlib.fixtures.command import Command
from functional2.testlib.fixtures.env import ManagedEnv
@pytest.fixture(name="pytest_command")
def _pytest_command(env: ManagedEnv, request: FixtureRequest, do_snapshot_update: bool) -> Command:
"""
returns a preconfigured pytest command.
The following things must be passed into the parametrization:
A list of additional flags to pass to the command
Optionally a boolean flag if snapshot updates should be propagated to the inside command
i.e. if the --accept-tests flag is set, if it should also be set for the pytest command returned by this fixture
True by default
Parametrization examples: `[]`, `["-k", "lang"]`, `([], False)`, `(["--accept-tests"], False)`
"""
params = request.param
if len(params) == 2 and isinstance(params[-1], bool):
flags, propagate_update = params
else:
flags = params
propagate_update = True
env.path.add_program("pytest")
cwd = env.get_env("HOME") / "functional2"
cmd = Command(argv=["pytest", "--basetemp", "../pytest_files", *flags], _env=env, cwd=cwd)
if propagate_update and do_snapshot_update:
flags.append("--accept-tests")
return cmd