Skip to content

Commit

Permalink
CommandRunner.run_sync: Add new parameters to log stdout/stderr to lo…
Browse files Browse the repository at this point in the history
…gger
  • Loading branch information
szilard-nemeth committed Jul 24, 2024
1 parent ccb8792 commit 64afadb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
17 changes: 13 additions & 4 deletions pythoncommons/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@


class CommandOutput:
def __init__(self, single_result):
def __init__(self, logger, single_result, log_stdout_to_logger, log_stderr_to_logger):
self._logger = logger
self._log_stdout = log_stdout_to_logger
self._log_stderr = log_stderr_to_logger
self._stdout = []
self._stderr = []
self._single_result = single_result

def append_stdout(self, line):
self._stdout.append(line)
if self._log_stdout:
self._logger.info(line)

def append_stderr(self, line):
self._stderr.append(line)
if self._log_stderr:
self._logger.info(line)

def get_stdout(self):
return " ".join(self._stdout)
Expand Down Expand Up @@ -64,7 +71,9 @@ def run_sync(
_tee: bool = False,
_err_to_out: bool = False,
add_stdout_callback: bool = False,
add_stderr_callback: bool = False
add_stderr_callback: bool = False,
log_stdout_to_logger: bool = False,
log_stderr_to_logger: bool = False
):
if add_stdout_callback and _out:
raise ValueError("Invalid input parameters! Cannot specify '_out' and 'add_stdout_callback' at the same time!")
Expand All @@ -85,8 +94,6 @@ def _prepare_kwargs(_err, _out, _tee):
kwargs["_err_to_out"] = True
return kwargs

cmd_output = CommandOutput(single_result)

def stdout_callback(line: str):
"""
Documentation for kwargs '_out':
Expand All @@ -98,6 +105,8 @@ def stdout_callback(line: str):
"""
cmd_output.append_stdout(line)

cmd_output = CommandOutput(self.LOG, single_result, log_stdout_to_logger, log_stderr_to_logger)

def stderr_callback(line: str):
cmd_output.append_stderr(line)

Expand Down
28 changes: 27 additions & 1 deletion pythoncommons/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,28 @@ def test_command_runner_run_sync_stdout_callback_and_stderr_callback(self):
self._assert_empty_stdout(stdout, cmd)
self.assertEqual(len(stderr.getvalue()), 0)

def test_command_runner_run_sync_stdout_callback_and_stderr_callback_log_both(self):
with (tempfile.TemporaryDirectory() as tmpdirname,
redirect_stdout(io.StringIO()) as stdout,
redirect_stderr(io.StringIO()) as stderr):
test_dir = self.get_test_dir(tmpdirname)
CMD_LOG = SimpleLoggingSetup.create_command_logger(__name__)
cmd_runner = CommandRunner(CMD_LOG)

cmd = f". {TEST_SCRIPTS_DIR}/download.sh; download-random-jars {test_dir}"
exit_code, cmd_stdout, cmd_stderr = cmd_runner.run_sync(cmd,
add_stdout_callback=True,
add_stderr_callback=True,
_out=None, _err=None,
log_stdout_to_logger=True,
log_stderr_to_logger=True)
self.assertEqual(exit_code, 0)
self.assertTrue(len(cmd_stdout) > 50)
self.assertTrue(len(cmd_stderr) > 50)
# stdout holds all logs, stderr is empty as logger logs all records to console (stdout)
self.assertTrue(len(stdout.getvalue().splitlines()) > 1)
self.assertEqual(len(stderr.getvalue()), 0)

def test_command_runner_run_sync_stdout_callback_stderr_specified(self):
with (tempfile.TemporaryDirectory() as tmpdirname,
redirect_stdout(io.StringIO()) as stdout,
Expand Down Expand Up @@ -257,7 +279,9 @@ def test_command_runner_run_sync_stdout_stderr_both_unspecified(self):
self.assertEqual(len(stderr.getvalue()), 0)

def test_command_runner_run_sync_stdout_stderr_both_unspecified_without_capturing(self):
with (tempfile.TemporaryDirectory() as tmpdirname):
with (tempfile.TemporaryDirectory() as tmpdirname,
redirect_stdout(io.StringIO()) as stdout,
redirect_stderr(io.StringIO()) as stderr):
test_dir = self.get_test_dir(tmpdirname)
CMD_LOG = SimpleLoggingSetup.create_command_logger(__name__)
cmd_runner = CommandRunner(CMD_LOG)
Expand All @@ -267,6 +291,8 @@ def test_command_runner_run_sync_stdout_stderr_both_unspecified_without_capturin
self.assertEqual(exit_code, 0)
self.assertEqual(len(cmd_stdout), 0)
self.assertEqual(len(cmd_stderr), 0)
self._assert_empty_stdout(stdout, cmd)
self.assertEqual(len(stderr.getvalue()), 0)

def _assert_empty_stdout(self, stdout, cmd):
orig_lines = stdout.getvalue().splitlines()
Expand Down

0 comments on commit 64afadb

Please sign in to comment.