Files
lix/tests/functional2/testlib/fixtures/test_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

92 lines
2.5 KiB
Python

import logging
import stat
from collections.abc import Callable
from subprocess import CalledProcessError
import pytest
from _pytest.logging import LogCaptureFixture
from functional2.testlib.fixtures.command import Command
from functional2.testlib.fixtures.env import ManagedEnv
from functional2.testlib.fixtures.file_helper import File
def test_command_valid_runs(command: Callable[[list[str]], Command]):
cmd = command(["echo", "water"])
cmd.run().ok()
def test_command_captures_stdout(command: Callable[[list[str]], Command]):
cmd = command(["echo", "fire"])
res = cmd.run().ok()
assert res.stdout_s == "fire\n"
def test_command_plain_strips(command: Callable[[list[str]], Command]):
cmd = command(["echo", " earth "])
res = cmd.run().ok()
assert res.stdout_plain == "earth"
def test_command_stdin_passed_correctly(command: Callable[[list[str]], Command]):
inp = b"air"
cmd = command(["cat", "/dev/stdin"]).with_stdin(inp)
res = cmd.run().ok()
assert res.stdout == inp
def test_command_expect_failure(env: ManagedEnv):
env.path.add_program("grep")
Command(_env=env, argv=["grep", "xxx"], stdin=b"").run().expect(1)
@pytest.mark.parametrize(
"files",
[
{
"script.sh": File(
"#!/bin/sh\necho forb\nexit 1", mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
)
}
],
indirect=True,
)
@pytest.mark.usefixtures("files")
def test_command_ok_fails_on_bad_exit_code(
command: Callable[[list[str]], Command], caplog: LogCaptureFixture
):
cmd = command(["./script.sh"])
with pytest.raises(CalledProcessError), caplog.at_level(logging.ERROR):
cmd.run().ok()
msgs = caplog.messages
assert len(msgs) == 2
out_msg, err_msg = msgs
assert out_msg == "stdout: forb\n"
assert err_msg == "stderr: "
@pytest.mark.parametrize(
"files",
[
{
"script.sh": File(
"#!/bin/sh\necho drgn fops\nexit 2", mode=stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
)
}
],
indirect=True,
)
@pytest.mark.usefixtures("files")
def test_command_exec_fails_on_other_bad_exit_code(
command: Callable[[list[str]], Command], caplog: LogCaptureFixture
):
cmd = command(["./script.sh"])
with pytest.raises(CalledProcessError), caplog.at_level(logging.ERROR):
cmd.run().expect(1)
msgs = caplog.messages
assert len(msgs) == 2
out_msg, err_msg = msgs
assert out_msg == "stdout: drgn fops\n"
assert err_msg == "stderr: "