Skip to content

Commit 0906b8e

Browse files
committed
enhance test_run_shell_cmd_with_hooks to check that hook is also triggered for failing shell commands
1 parent e6f2754 commit 0906b8e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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:
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)