diff --git a/src/scriptengine/tasks/base/command.py b/src/scriptengine/tasks/base/command.py index a6f770f..c725e2f 100644 --- a/src/scriptengine/tasks/base/command.py +++ b/src/scriptengine/tasks/base/command.py @@ -124,12 +124,20 @@ def log_pipe(mode): except subprocess.CalledProcessError as e: if ignore_error: self.log_warning(f"Command returned error code {e.returncode}") + stdout = e.stdout + stderr = e.stderr else: self.log_error(f"Command returned error code {e.returncode}") raise ScriptEngineTaskRunError else: - if isinstance(stdout_mode, str): - context_update[stdout_mode] = cmd_proc.stdout.split("\n") - if isinstance(stderr_mode, str): - context_update[stderr_mode] = cmd_proc.stderr.split("\n") + stdout = cmd_proc.stdout + stderr = cmd_proc.stderr + + if isinstance(stdout_mode, str): + self.log_debug(f"Store stdout in context under {stdout_mode}") + context_update[stdout_mode] = stdout.split("\n") + if isinstance(stderr_mode, str): + self.log_debug(f"Store stderr in context under {stderr_mode}") + context_update[stderr_mode] = stderr.split("\n") + return context_update or None diff --git a/tests/tasks/base/test_command.py b/tests/tasks/base/test_command.py index c5e9cda..f9e66ce 100644 --- a/tests/tasks/base/test_command.py +++ b/tests/tasks/base/test_command.py @@ -11,7 +11,7 @@ def from_yaml(string): return parse(yaml.load(string, Loader=yaml.SafeLoader)) -def test_command_ls(tmp_path, caplog): +def test_command_ls(tmp_path): os.chdir(tmp_path) f = tmp_path / "foo" f.touch() @@ -22,12 +22,12 @@ def test_command_ls(tmp_path, caplog): base.command: name: ls args: [ {f.name} ] + stdout: ls_stdout """ ) - with caplog.at_level(logging.INFO, logger="se.task"): - t.run({}) - assert "foo" in [rec.message for rec in caplog.records] + c = t.run({}) + assert "foo" in c["ls_stdout"] f.unlink() @@ -41,11 +41,13 @@ def test_command_ls_not_exists(tmp_path, caplog): name: ls args: [ foo ] ignore_error: true + stderr: ls_stderr """ ) with caplog.at_level(logging.WARN, logger="se.task"): - t.run({}) + c = t.run({}) assert "Command returned error code 2" in [ rec.message for rec in caplog.records ] + assert len(c["ls_stderr"]) > 1