Skip to content

Commit

Permalink
Fix handling of stderr in case command fails
Browse files Browse the repository at this point in the history
  • Loading branch information
uwefladrich committed Nov 28, 2023
1 parent 9f59616 commit ee19eae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
16 changes: 12 additions & 4 deletions src/scriptengine/tasks/base/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions tests/tasks/base/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()

Expand All @@ -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

0 comments on commit ee19eae

Please sign in to comment.