Skip to content

Commit

Permalink
Merge pull request #3 from mimi1vx/poo_138491_2
Browse files Browse the repository at this point in the history
Add test case for success on last retry
  • Loading branch information
mergify[bot] authored Nov 24, 2023
2 parents dcba6e2 + 36a6471 commit 829db9e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
15 changes: 9 additions & 6 deletions test/01-retry.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PATH=$BASHLIB$PATH

source bash+ :std
use Test::More
plan tests 14
plan tests 15

PATH=$dir/..:$PATH

Expand All @@ -26,17 +26,20 @@ 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
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 -s 0 $dir/success_on_third.sh 2>&1); rc=$?; set -e
is $rc 0 'pass on last run is success'
27 changes: 27 additions & 0 deletions test/success_on_third.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 829db9e

Please sign in to comment.