Debian Codegraph @

tests/testlib/wait-on-file

raw
1 #!/bin/sh
2 #
3 # wait up to TIMEOUT seconds until a WAIT_ON_FILE is created.
4 #
5 # In addition, this script can create CREATE_FILE once it is ready to wait.
6
7 if [ $# -lt 2 ] || [ $# -gt 3 ]; then
8 echo $#
9 echo "USAGE: $0 TIMEOUT WAIT_ON_FILE [CREATE_FILE]"
10 fi
11
12 timer="$1"
13
14 # If the test timeout have been extended, also scale the timer relative
15 # to the normal timing.
16 if [ "$HGTEST_TIMEOUT_DEFAULT" -lt "$HGTEST_TIMEOUT" ]; then
17 timer=$(( ( $timer * $HGTEST_TIMEOUT) / $HGTEST_TIMEOUT_DEFAULT ))
18 fi
19
20 max_time=$timer
21
22 # Scale the timeout to match the sleep steps below, i.e. 1/0.02.
23 timer=$(( 50 * $timer ))
24
25 wait_on="$2"
26 create=""
27 if [ $# -eq 3 ]; then
28 create="$3"
29 fi
30
31 if [ -n "$create" ]; then
32 touch "$create"
33 create=""
34 fi
35 while [ "$timer" -gt 0 ] && !([ -e "$wait_on" ] || [ -L "$wait_on" ]) ; do
36 timer=$(( $timer - 1))
37 sleep 0.02
38 done
39 if [ "$timer" -le 0 ]; then
40 echo "file not created after $max_time seconds: $wait_on" >&2
41 exit 1
42 fi