Skip to content

Commit 2478639

Browse files
committed
Change strict option to loose option to avoid breaking existing behavior
1 parent cd8c81b commit 2478639

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

README.md

+6-7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ When using this tool, you only need to pick the `wait-for` file as part of your
99
## Usage
1010

1111
```
12-
./wait-for host:port [-t timeout] [-- command args]
12+
wait-for host:port [-t timeout] [-- command args]
1313
-q | --quiet Do not output any status messages
14-
-s | --strict Only execute subcommand if the test succeeds
14+
-l | --loose Execute subcommand even if the test times out
1515
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
1616
-- COMMAND ARGS Execute command with args after the test finishes
1717
```
@@ -22,18 +22,17 @@ To check if [eficode.com](https://eficode.com) is available:
2222

2323
```
2424
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
25-
26-
Connection to www.eficode.com port 80 [tcp/http] succeeded!
2725
Eficode site is up
2826
```
2927

3028
The subcommand will be executed regardless if the service is up or not. If you wish to execute the subcommand only if the service is up, add the --strict argument. In this example, we will test port 81 on www.google.com which will fail:
3129

3230
```
33-
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
34-
$ ./wait-for www.google.com:81 --timeout=1 --strict -- echo "google is up"
31+
$ ./wait-for www.google.com:81 --timeout=1 -- echo "google is up"
32+
Operation timed out
33+
$ ./wait-for www.google.com:81 --timeout=1 --loose -- echo "waited for google"
3534
Operation timed out
36-
google is up
35+
waited for google
3736
```
3837

3938
To wait for database container to become available:

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "wait-for",
33
"version": "0.1.0",
44
"scripts": {
5-
"test": "./node_modules/.bin/bats wait-for.bats"
5+
"test": "./wait-for -t 1 -l noserver:9999 -- echo 'passable' ; ./node_modules/.bin/bats wait-for.bats"
66
},
77
"dependencies": {
88
"bats": "^0.4.2"

wait-for

+11-19
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ OLD_TIMEOUT=$TIMEOUT
44
OLD_QUIET=$QUIET
55
OLD_PORT=$PORT
66
OLD_HOST=$HOST
7+
OLD_LOOSE=$LOOSE
78

89
TIMEOUT=15
910
QUIET=0
11+
LOOSE=0
1012

1113
if ! which nc >/dev/null; then
1214
echo "Netcat is not installed. This script requires netcat to work correctly."
@@ -23,7 +25,7 @@ usage() {
2325
Usage:
2426
$(basename $0) host:port [-t timeout] [-- command args]
2527
-q | --quiet Do not output any status messages
26-
-s | --strict Only execute subcommand if the test succeeds
28+
-l | --loose Execute subcommand even if the test times out
2729
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
2830
-- COMMAND ARGS Execute command with args after the test finishes
2931
USAGE
@@ -39,27 +41,19 @@ test_connection() {
3941
}
4042

4143
wait_for() {
44+
local result
4245
for i in `seq $TIMEOUT` ; do
4346
# use a 1-second timeout, but still sleep 0.1 seconds after just to be safe
4447
test_connection "$HOST" "$PORT"
45-
4648
result=$?
47-
if [ $result -eq 0 ] ; then
48-
if [ $# -gt 0 ] ; then
49-
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST exec "$@"
50-
fi
51-
exit 0
52-
fi
49+
if [ $result -eq 0 ] ; then break ; fi
5350
sleep 0.1
5451
done
55-
echo "Operation timed out" >&2
56-
if [ $result -ne 0 ] && [ $STRICT -ne 1 ] ; then
57-
if [ $# -gt 0 ] ; then
58-
exec "$@"
59-
fi
60-
exit 0
52+
[ $result -ne 0 ] && echo "Operation timed out" >&2
53+
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
54+
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
6155
fi
62-
exit 1
56+
exit $result
6357
}
6458

6559
while [ $# -gt 0 ]
@@ -74,8 +68,8 @@ do
7468
QUIET=1
7569
shift 1
7670
;;
77-
-s | --strict)
78-
STRICT=1
71+
-l | --loose)
72+
LOOSE=1
7973
shift 1
8074
;;
8175
-t)
@@ -106,6 +100,4 @@ if [ "$HOST" = "" -o "$PORT" = "" ]; then
106100
usage 2
107101
fi
108102

109-
STRICT=${STRICT:-0}
110-
111103
wait_for "$@"

wait-for.bats

+19-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,25 @@
1313
[ "$output" != "success" ]
1414
}
1515

16-
@test "preserve existing environment variable" {
17-
HOST=myweb.com
18-
PORT=8080
16+
@test "nonexistent server should start command if loose option is specified" {
17+
run ./wait-for -t 1 -l noserver:9999 -- echo 'passable'
18+
19+
[ "$status" -eq 0 ]
20+
[ "$output" = "passable" ]
21+
}
22+
23+
@test "preserve existing environment variables" {
24+
TIMEOUT=mytimeout
25+
QUIET=myquiet
26+
HOST=myhost
27+
PORT=myport
28+
LOOSE=myloose
29+
1930
run ./wait-for google.com:80 -- echo 'success'
2031

21-
[ "$(echo $HOST)" = 'myweb.com' ]
22-
[ "$(echo $PORT)" = '8080' ]
32+
[ "$(echo $TIMEOUT)" = 'mytimeout' ]
33+
[ "$(echo $QUIET)" = 'myquiet' ]
34+
[ "$(echo $HOST)" = 'myhost' ]
35+
[ "$(echo $PORT)" = 'myport' ]
36+
[ "$(echo $LOOSE)" = 'myloose' ]
2337
}

0 commit comments

Comments
 (0)