Skip to content

Commit 8dd59f2

Browse files
authored
Merge pull request #4908 from boegel/post_run_shell_cmd
trigger `post_run_shell_cmd_hook` before raising error if shell command failed + pass down full `RunShellCmdResult` instance
2 parents 89d0f03 + 2b3dc2f commit 8dd59f2

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

easybuild/tools/run.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,17 @@ def to_cmd_str(cmd):
589589
work_dir=work_dir, out_file=cmd_out_fp, err_file=cmd_err_fp, cmd_sh=cmd_sh,
590590
thread_id=thread_id, task_id=task_id)
591591

592+
if with_hooks:
593+
run_hook_kwargs = {
594+
'exit_code': res.exit_code,
595+
'interactive': interactive,
596+
'output': res.output,
597+
'stderr': res.stderr,
598+
'work_dir': res.work_dir,
599+
'shell_cmd_result': res,
600+
}
601+
run_hook(RUN_SHELL_CMD, hooks, post_step_hook=True, args=[cmd], kwargs=run_hook_kwargs)
602+
592603
# always log command output
593604
cmd_name = cmd_str.split(' ')[0]
594605
if split_stderr:
@@ -618,16 +629,6 @@ def to_cmd_str(cmd):
618629
except OSError as err:
619630
raise EasyBuildError(f"Failed to return to {initial_work_dir} after executing command `{cmd_str}`: {err}")
620631

621-
if with_hooks:
622-
run_hook_kwargs = {
623-
'exit_code': res.exit_code,
624-
'interactive': interactive,
625-
'output': res.output,
626-
'stderr': res.stderr,
627-
'work_dir': res.work_dir,
628-
}
629-
run_hook(RUN_SHELL_CMD, hooks, post_step_hook=True, args=[cmd], kwargs=run_hook_kwargs)
630-
631632
if not hidden:
632633
time_since_start = time_str_since(start_time)
633634
trace_msg(f"command completed: exit {res.exit_code}, ran in {time_since_start}")

test/framework/run.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,7 @@ def pre_run_shell_cmd_hook(cmd, *args, **kwargs):
20682068
print("pre-run hook '%s' in %s" % (cmd, work_dir))
20692069
import sys
20702070
sys.stderr.write('pre-run hook done\\n')
2071-
if not cmd.startswith('echo'):
2071+
if cmd != 'false' and not cmd.startswith('echo'):
20722072
cmds = cmd.split(';')
20732073
return '; '.join(cmds[:-1] + ["echo " + cmds[-1].lstrip()])
20742074
@@ -2126,6 +2126,18 @@ def post_run_shell_cmd_hook(cmd, *args, **kwargs):
21262126
regex = re.compile('>> running shell command:\n\techo make', re.M)
21272127
self.assertTrue(regex.search(stdout), "Pattern '%s' found in: %s" % (regex.pattern, stdout))
21282128

2129+
with self.mocked_stdout_stderr():
2130+
# run_shell_cmd will raise RunShellCmdError which we don't care about here,
2131+
# we just want to verify that the post_run_shell_cmd_hook has run
2132+
try:
2133+
run_shell_cmd("false")
2134+
except RunShellCmdError:
2135+
pass
2136+
stdout = self.get_stdout()
2137+
2138+
expected_last_line = "\npost-run hook 'false' (exit code: 1, output: '')\n"
2139+
self.assertTrue(stdout.endswith(expected_last_line), f"Stdout should end with '{expected_last_line}': {stdout}")
2140+
21292141
def test_run_shell_cmd_delete_cwd(self):
21302142
"""
21312143
Test commands that destroy directories inside initial working directory

0 commit comments

Comments
 (0)