Skip to content

Commit c309710

Browse files
committed
add more verbosity if not in quiet mode, enforce it if we are
1 parent 8f426bb commit c309710

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

wait-for

+13-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ echoerr() {
1919
if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi
2020
}
2121

22+
conditionally_output() {
23+
if [ "$QUIET" -ne 1 ]; then
24+
"$@"
25+
else
26+
"$@" > /dev/null 2>&1
27+
fi
28+
}
29+
2230
usage() {
2331
exitcode="$1"
2432
cat << USAGE >&2
@@ -33,11 +41,13 @@ USAGE
3341
}
3442

3543
test_connection() {
44+
conditionally_output echo "Testing connection to $1:$2..."
45+
3646
# force a 1-second timeout on darwin (https://stackoverflow.com/a/20460402/2063546)
3747
if [ -z "${OSTYPE##*darwin*}" ] ; then
38-
nc -z -w 1 -G 1 "$1" "$2" > /dev/null 2>&1
48+
conditionally_output nc -z -w 1 -G 1 "$1" "$2"
3949
else
40-
nc -z -w 1 "$1" "$2" > /dev/null 2>&1
50+
conditionally_output nc -z -w 1 "$1" "$2"
4151
fi
4252
}
4353

@@ -50,7 +60,7 @@ wait_for() {
5060
if [ $result -eq 0 ] ; then break ; fi
5161
sleep 1
5262
done
53-
[ $result -ne 0 ] && echo "Operation timed out" >&2
63+
[ $result -ne 0 ] && echoerr "Operation timed out"
5464
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
5565
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
5666
fi

wait-for.bats

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bats
22

3-
@test "google should be immediately found" {
4-
run ./wait-for google.com:80 -- echo 'success'
3+
@test "google should be immediately found, no output other than our own" {
4+
run ./wait-for -q google.com:80 -- echo 'success'
55

66
[ "$output" = "success" ]
77
}
@@ -14,12 +14,11 @@
1414
}
1515

1616
@test "nonexistent server should start command if loose option is specified" {
17-
run ./wait-for -t 1 -l noserver:9999 -- echo 'passable' 2>&1
17+
run ./wait-for -q -t 1 -l noserver:9999 -- echo 'passable' 2>&1
1818

1919
[ "$status" -eq 0 ]
2020

21-
[ "${lines[0]}" = "Operation timed out" ]
22-
[ "${lines[1]}" = "passable" ]
21+
[ "$output" = "passable" ]
2322
}
2423

2524
@test "preserve existing environment variables" {

0 commit comments

Comments
 (0)