Skip to content

Commit 96511d6

Browse files
committed
add more verbosity if not in quiet mode, enforce it if we are
1 parent 9102543 commit 96511d6

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,12 +41,14 @@ 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
# POSIX-compliant string inclusion test https://stackoverflow.com/a/8811800/2063546
3848
if [ "${OSTYPE#*darwin*}" != "$OSTYPE" ] ; then
39-
nc -z -w 1 -G 1 "$1" "$2"
49+
conditionally_output nc -z -w 1 -G 1 "$1" "$2"
4050
else
41-
nc -z -w 1 "$1" "$2" > /dev/null 2>&1
51+
conditionally_output nc -z -w 1 "$1" "$2"
4252
fi
4353
}
4454

@@ -51,7 +61,7 @@ wait_for() {
5161
if [ $result -eq 0 ] ; then break ; fi
5262
sleep 1
5363
done
54-
[ $result -ne 0 ] && echo "Operation timed out" >&2
64+
[ $result -ne 0 ] && echoerr "Operation timed out"
5565
if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then
5666
TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@"
5767
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)