Skip to content

Commit eb53d8d

Browse files
committed
Add command_input parameter to run_command helper function
1 parent 7bcc5f4 commit eb53d8d

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

tests/helpers.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44
from functools import wraps
55
from pathlib import Path
6-
from typing import Callable
6+
from typing import Callable, Optional
77

88
from tests.types import P, RawFilesDict, RequirementsBase, RequirementsDict
99

@@ -27,13 +27,29 @@ def _command_setup(venv_cli_path: Path, activate: bool = False) -> list[str]:
2727
return [*bash, full_command]
2828

2929

30-
def run_command(commands: str | list[str], cwd: Path = Path.cwd(), activated: bool = False) -> None:
30+
def run_command(
31+
commands: str | list[str],
32+
cwd: Path = Path.cwd(),
33+
activated: bool = False,
34+
command_input: Optional[str] = None,
35+
) -> None:
36+
"""Run a command in a subprocess, optionally activating the virtual environment first
37+
38+
Args:
39+
commands: The command(s) to run.
40+
cwd: The directory to run the command in. Defaults to the current working directory.
41+
activated: Whether to activate the virtual environment before running the command.
42+
command_input: The input to pass to the command. Defaults to None.
43+
44+
Raises:
45+
subprocess.CalledProcessError: If the command returns a non-zero exit code.
46+
"""
3147
input_commands = [commands] if isinstance(commands, str) else commands
3248

3349
setup_commands = _command_setup(venv_cli_path=_venv_cli_path, activate=activated)
3450
all_commands = [*setup_commands[:-1], setup_commands[-1] + "; ".join(input_commands)]
3551

36-
result = subprocess.run(all_commands, cwd=cwd)
52+
result = subprocess.run(all_commands, cwd=cwd, input=command_input, text=True)
3753
result.check_returncode()
3854

3955

0 commit comments

Comments
 (0)