From 0c0b3142dc392e1de7134adb16bc98ac265b05f8 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Mon, 2 Sep 2024 09:31:57 +0200 Subject: [PATCH 1/5] Output stdout from long running task --- src/main.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 841baaf..7ffc9a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,21 +58,36 @@ impl Command { shell_escape::escape(format!("Running: '{}'", self.cmd).into()) )?; + writeln!( + w, + 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 + }} + "# + ))?; + if self.long_running { if let Some(wait_until) = &self.wait_until { writeln!( w, indoc!( r#" - output=$(mktemp) - {} &> $output & + long_running_output=$(mktemp) + export BASHTESTMD_LONG_RUNNING_OUTPUT=$long_running_output + {} &> $long_running_output & background_process_pid=$! echo "Waiting for process with PID: $background_process_pid" 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 $long_running_output exit 1 fi echo -n "." @@ -104,6 +119,7 @@ impl Command { if ! [[ $output == *"$expected"* || $expected == *"$output"* ]]; then echo "'$expected' not found in text:" echo "'$output'" + check_and_output_long_running_output exit 1 fi "# @@ -122,6 +138,7 @@ impl Command { r#" if [ $? -ne {0} ]; then echo "Expected exit code {0}, got $?" + check_and_output_long_running_output exit 1 fi "#, From daeb232999a4b6bef8aeeadd95e2d413ccc6a34f Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Mon, 2 Sep 2024 09:41:31 +0200 Subject: [PATCH 2/5] Fixing formatter --- src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7ffc9a8..8a18295 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,7 +61,7 @@ impl Command { writeln!( w, indoc!( - r#" + 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:" @@ -69,7 +69,8 @@ impl Command { fi }} "# - ))?; + ) + )?; if self.long_running { if let Some(wait_until) = &self.wait_until { From 4a6968c25bdeb8f7a779446657000527e4355f60 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Mon, 2 Sep 2024 10:00:20 +0200 Subject: [PATCH 3/5] Output of long running log --- src/main.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8a18295..ca64399 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,20 +58,6 @@ impl Command { shell_escape::escape(format!("Running: '{}'", self.cmd).into()) )?; - writeln!( - w, - 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 - }} - "# - ) - )?; - if self.long_running { if let Some(wait_until) = &self.wait_until { writeln!( @@ -82,8 +68,8 @@ impl Command { export BASHTESTMD_LONG_RUNNING_OUTPUT=$long_running_output {} &> $long_running_output & background_process_pid=$! - echo "Waiting for process with PID: $background_process_pid" - until grep -q -i {} $output + echo "Waiting for process with PID: $background_process_pid to have a match in $long_running_output" + until grep -q -i {} $long_running_output do if ! ps $background_process_pid > /dev/null then @@ -121,6 +107,7 @@ impl Command { 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 "# @@ -157,6 +144,19 @@ fn compile_commands_into_bash(cmds: Vec) -> 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(); From aa01d3a07dc0095fd9c63dd884ef1fab5d0c0ddf Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Mon, 2 Sep 2024 10:02:18 +0200 Subject: [PATCH 4/5] New line after the dot! --- src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.rs b/src/main.rs index ca64399..7031959 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,6 +80,7 @@ impl Command { echo -n "." sleep 5 done + echo "" "# ), self.cmd, From 8c28194aba2c30b6d830568eb2d9f840b217b3c8 Mon Sep 17 00:00:00 2001 From: Nikolai Golub Date: Mon, 2 Sep 2024 10:03:53 +0200 Subject: [PATCH 5/5] We don't need separate variable name --- src/main.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7031959..a871c5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,17 +64,17 @@ impl Command { w, indoc!( r#" - long_running_output=$(mktemp) - export BASHTESTMD_LONG_RUNNING_OUTPUT=$long_running_output - {} &> $long_running_output & + output=$(mktemp) + export BASHTESTMD_LONG_RUNNING_OUTPUT=$output + {} &> $output & background_process_pid=$! - echo "Waiting for process with PID: $background_process_pid to have a match in $long_running_output" - until grep -q -i {} $long_running_output + 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, output:" >&2 - cat $long_running_output + cat $output exit 1 fi echo -n "."