Skip to content

Commit

Permalink
fest: return subprocess command completed process object
Browse files Browse the repository at this point in the history
  • Loading branch information
narenaryan committed Dec 26, 2024
1 parent 6e1e432 commit 4959293
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/whispr/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from whispr.logging import logger


def execute_command(command: tuple, no_env: bool, secrets: dict):
def execute_command(command: tuple, no_env: bool, secrets: dict) -> subprocess.CompletedProcess[bytes]:
"""Executes a Unix/Windows command.
Arg: `no_env` decides whether secrets are passed vai environment or K:V pairs in command arguments.
"""
Expand All @@ -23,6 +23,7 @@ def execute_command(command: tuple, no_env: bool, secrets: dict):
os.environ.update(secrets)

sp = subprocess.run(usr_command, env=os.environ, shell=False, check=True)
return sp
except subprocess.CalledProcessError as e:
logger.error(
f"Encountered a problem while running command: '{command[0]}'. Aborting."
Expand Down
12 changes: 11 additions & 1 deletion tests/test_process_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,24 @@ def test_execute_command_with_env(
self, mock_env_update, mock_subprocess_run, mock_logger
):
"""Test execute_command with `no_env=False`, passing secrets via environment variables."""
execute_command(self.command, no_env=False, secrets=self.secrets)
completed_process = subprocess.CompletedProcess(
args=["echo", "Hello"], returncode=0, stdout=b"Hello\n", stderr=b""
)
mock_subprocess_run.return_value = completed_process

result = execute_command(self.command, no_env=False, secrets=self.secrets)

mock_env_update.assert_called_once_with(self.secrets)
expected_command = ["echo", "Hello"]
mock_subprocess_run.assert_called_once_with(
expected_command, env=os.environ, shell=False, check=True
)

self.assertIsInstance(result, subprocess.CompletedProcess)
self.assertEqual(
type(result.stdout), bytes
) # Additional sanity check on stdout type

@patch("whispr.utils.process.logger", new_callable=lambda: MagicMock())
@patch("subprocess.run", side_effect=subprocess.CalledProcessError(1, "test"))
def test_execute_command_called_process_error(
Expand Down

0 comments on commit 4959293

Please sign in to comment.