Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output stdout from long running task #5

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,22 @@ impl Command {
indoc!(
r#"
output=$(mktemp)
export BASHTESTMD_LONG_RUNNING_OUTPUT=$output
{} &> $output &
background_process_pid=$!
echo "Waiting for process with PID: $background_process_pid"
echo "Waiting for process with PID: $background_process_pid to have a match in $output"
until grep -q -i {} $output
do
if ! ps $background_process_pid > /dev/null
then
echo "The background process died died" >&2
echo "The background process died, output:" >&2
cat $output
exit 1
fi
echo -n "."
sleep 5
done
echo ""
"#
),
self.cmd,
Expand All @@ -104,6 +107,8 @@ impl Command {
if ! [[ $output == *"$expected"* || $expected == *"$output"* ]]; then
echo "'$expected' not found in text:"
echo "'$output'"
check_and_output_long_running_output
echo "=========== END OF THE LONG RUNNING OUTPUT. Terminating..."
exit 1
fi
"#
Expand All @@ -122,6 +127,7 @@ impl Command {
r#"
if [ $? -ne {0} ]; then
echo "Expected exit code {0}, got $?"
check_and_output_long_running_output
exit 1
fi
"#,
Expand All @@ -139,6 +145,19 @@ fn compile_commands_into_bash(cmds: Vec<Command>) -> String {
// Shebang.
writeln!(&mut script, "#!/usr/bin/env bash").unwrap();
writeln!(&mut script, r#"trap 'jobs -p | xargs -r kill' EXIT"#).unwrap();
writeln!(
&mut script,
indoc!(
r#"
check_and_output_long_running_output() {{
if [[ -n "$BASHTESTMD_LONG_RUNNING_OUTPUT" && -f "$BASHTESTMD_LONG_RUNNING_OUTPUT" ]]; then
echo "Output of the long running task:"
cat "$BASHTESTMD_LONG_RUNNING_OUTPUT"
fi
}}
"#
)
).unwrap();

for cmd in cmds {
cmd.compile(&mut script).unwrap();
Expand Down
Loading