From fcf35b292942074ba8eff788cd242f4ea2388916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20S=C3=BAkup?= Date: Fri, 24 Nov 2023 13:20:11 +0100 Subject: [PATCH 1/2] Add testcase for succes on last retry --- test/01-retry.t | 5 ++++- test/success_on_third.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100755 test/success_on_third.sh diff --git a/test/01-retry.t b/test/01-retry.t index 1ae1a68..afe5cb7 100755 --- a/test/01-retry.t +++ b/test/01-retry.t @@ -13,7 +13,7 @@ PATH=$BASHLIB$PATH source bash+ :std use Test::More -plan tests 14 +plan tests 15 PATH=$dir/..:$PATH @@ -40,3 +40,6 @@ set +e; out=$(retry -r 0 false 2>&1); rc=$?; set -e is $rc 1 'failing command without retry returns no success' set +e; out=$(retry -r 0 true 2>&1); rc=$?; set -e is $rc 0 'passing command without retry returns no error' +trap "rm -f 'run_count.txt'" EXIT +set +e; out=$(retry -r 2 $dir/success_on_third.sh 2>&1); rc=$?; set -e +is $rc 0 'pass on last run is success' diff --git a/test/success_on_third.sh b/test/success_on_third.sh new file mode 100755 index 0000000..8379632 --- /dev/null +++ b/test/success_on_third.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Set the path to the file that will store the run count in /var/run directory +count_file="run_count.txt" + +# Initialize the count file if it doesn't exist +if [ ! -f "$count_file" ]; then + echo "0" > "$count_file" +fi + +# Read the current run count from the file +run_count=$(<"$count_file") + +# Increment the run count +((run_count++)) + +# Write the updated run count back to the file +echo "$run_count" > "$count_file" + +# Check if it's the third run and exit with an error if true +if [ "$((run_count % 3))" -gt 0 ]; then + echo "Failing on the run!" + exit 1 +fi + +# Your script logic goes here +echo "Script is running successfully on run $run_count" From 36a6471ee5ce650cc11dbfeb8840d0ed14f38c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20S=C3=BAkup?= Date: Fri, 24 Nov 2023 15:21:59 +0100 Subject: [PATCH 2/2] Add explicit -s 0 to test commands --- test/01-retry.t | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/01-retry.t b/test/01-retry.t index afe5cb7..f6c5cd9 100755 --- a/test/01-retry.t +++ b/test/01-retry.t @@ -26,9 +26,9 @@ ok $? 'calling help returns success' set +e; out=$(retry --unknown-option 2>&1); rc=$?; set -e is $rc 1 'calling with unknown option returns failure' like "$out" 'unrecognized option.*usage:' 'retry help is shown for unknown option' -ok "$(retry true)" 'successful command returns success' -is "$(retry true)" '' 'successful command does not show any output by default' -set +e; out=$(retry false 2>&1); rc=$?; set -e +ok "$(retry -s 0 true)" 'successful command returns success' +is "$(retry -s 0 true)" '' 'successful command does not show any output by default' +set +e; out=$(retry -s 0 false 2>&1); rc=$?; set -e like "$out" 'Retrying up to 3 more.*Retrying up to 1' 'failing command retries' is $rc 1 'failing command returns no success' set +e; out=$(retry -s 1 -e -r 2 false 2>&1); rc=$?; set -e @@ -36,10 +36,10 @@ like "$out" 'sleeping 1s.*sleeping 2s' 'sleep amount doubles' is $rc 1 'failing command returns no success' set +e; out=$(retry -r 1 -- sh -c 'echo -n .; false' 2>/dev/null); set -e is "$out" '..' 'specified number of tries (1+retries)' -set +e; out=$(retry -r 0 false 2>&1); rc=$?; set -e +set +e; out=$(retry -r 0 -s 0 false 2>&1); rc=$?; set -e is $rc 1 'failing command without retry returns no success' -set +e; out=$(retry -r 0 true 2>&1); rc=$?; set -e +set +e; out=$(retry -r 0 -s 0 true 2>&1); rc=$?; set -e is $rc 0 'passing command without retry returns no error' trap "rm -f 'run_count.txt'" EXIT -set +e; out=$(retry -r 2 $dir/success_on_third.sh 2>&1); rc=$?; set -e +set +e; out=$(retry -r 2 -s 0 $dir/success_on_third.sh 2>&1); rc=$?; set -e is $rc 0 'pass on last run is success'