Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jdddog committed Feb 21, 2024
1 parent c873d02 commit 8341b1e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 94 deletions.
36 changes: 0 additions & 36 deletions observatory_platform/proc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

# Author: James Diprose

import logging
from subprocess import Popen
from typing import Tuple

Expand All @@ -29,38 +28,3 @@ def wait_for_process(proc: Popen) -> Tuple[str, str]:
output = output.decode("utf-8")
error = error.decode("utf-8")
return output, error


import select


def stream_process(proc: Popen) -> bool:
"""Print output while a process is running, returning the std output and std error streams as strings.
:param proc: the process object.
:return: whether the command was successful or not.
"""

while True:
if proc.poll() is not None:
break

# Check which streams have data available
readable, _, _ = select.select([proc.stdout, proc.stderr], [], [], 0.1)

for stream in readable:
if stream is proc.stdout:
output = proc.stdout.readline()
if output:
logging.debug(output)
elif stream is proc.stderr:
error = proc.stderr.readline()
if error:
logging.error(error)

# Check if the process had a non-zero exit code (indicating an error)
if proc.returncode != 0:
logging.error(f"Command failed with return code {proc.returncode}")
return False

return True
59 changes: 1 addition & 58 deletions observatory_platform/tests/test_proc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
# Author: Aniek Roelofs

import unittest
import subprocess
from unittest.mock import MagicMock
from unittest.mock import patch

from observatory_platform.proc_utils import stream_process, wait_for_process
from observatory_platform.proc_utils import wait_for_process


class TestProcUtils(unittest.TestCase):
Expand All @@ -31,58 +29,3 @@ def test_wait_for_process(self, mock_popen):
out, err = wait_for_process(proc)
self.assertEqual("out", out)
self.assertEqual("err", err)

@patch("observatory_platform.utils.proc_utils.logging")
def test_stream_process_success(self, mock_logging):
command = """
for i in {1..10}; do
echo "Hello World"
sleep 1
done
"""
proc = subprocess.Popen(
["bash", "-c", command],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
result = stream_process(proc)

mock_logging.debug.assert_called_with()


a = 1

# a = 1
#
# proc = MagicMock()
# proc.poll.side_effect = [None, None, 0] # Simulate process running and then ending
# proc.returncode = 0 # Simulate successful exit code
# proc.stdout.readline.return_value = "output line\n".encode() # Simulate stdout output
# proc.stderr.readline.return_value = "".encode() # Simulate empty stderr
#
# result = stream_process(proc)
#
# self.assertTrue(result)
# proc.stdout.readline.assert_called()
# proc.stderr.readline.assert_called()
# mock_logging.debug.assert_called_with("output line\n".encode())
# mock_logging.error.assert_not_called()

@patch("observatory_platform.utils.proc_utils.logging")
def test_stream_process_failure(self, mock_logging):
proc = MagicMock()
proc.poll.side_effect = [None, None, 0] # Simulate process running and then ending
proc.returncode = 1 # Simulate failure exit code
proc.stdout.readline.return_value = "output line\n".encode() # Simulate stdout output
proc.stderr.readline.return_value = "error line\n".encode() # Simulate stderr output

result = stream_process(proc)

self.assertFalse(result)
proc.stdout.readline.assert_called()
proc.stderr.readline.assert_called()
mock_logging.debug.assert_called_with("output line\n".encode())
mock_logging.error.assert_called_with("error line\n".encode())
mock_logging.error.assert_called_with("Command failed with return code 1")

0 comments on commit 8341b1e

Please sign in to comment.