From 96511d65c6578d4866591283fdec6e2fba7e6770 Mon Sep 17 00:00:00 2001 From: Ian Katz Date: Wed, 31 Oct 2018 14:37:52 -0400 Subject: [PATCH] add more verbosity if not in quiet mode, enforce it if we are --- wait-for | 16 +++++++++++++--- wait-for.bats | 9 ++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/wait-for b/wait-for index a8f6708..3b2241c 100755 --- a/wait-for +++ b/wait-for @@ -19,6 +19,14 @@ echoerr() { if [ "$QUIET" -ne 1 ]; then printf "%s\n" "$*" 1>&2; fi } +conditionally_output() { + if [ "$QUIET" -ne 1 ]; then + "$@" + else + "$@" > /dev/null 2>&1 + fi +} + usage() { exitcode="$1" cat << USAGE >&2 @@ -33,12 +41,14 @@ USAGE } test_connection() { + conditionally_output echo "Testing connection to $1:$2..." + # force a 1-second timeout on darwin (https://stackoverflow.com/a/20460402/2063546) # POSIX-compliant string inclusion test https://stackoverflow.com/a/8811800/2063546 if [ "${OSTYPE#*darwin*}" != "$OSTYPE" ] ; then - nc -z -w 1 -G 1 "$1" "$2" + conditionally_output nc -z -w 1 -G 1 "$1" "$2" else - nc -z -w 1 "$1" "$2" > /dev/null 2>&1 + conditionally_output nc -z -w 1 "$1" "$2" fi } @@ -51,7 +61,7 @@ wait_for() { if [ $result -eq 0 ] ; then break ; fi sleep 1 done - [ $result -ne 0 ] && echo "Operation timed out" >&2 + [ $result -ne 0 ] && echoerr "Operation timed out" if [ $result -eq 0 -o $LOOSE -eq 1 -a $# -gt 0 ] ; then TIMEOUT=$OLD_TIMEOUT QUIET=$OLD_QUIET PORT=$OLD_PORT HOST=$OLD_HOST LOOSE=$OLD_LOOSE exec "$@" fi diff --git a/wait-for.bats b/wait-for.bats index fa873ac..a95f889 100644 --- a/wait-for.bats +++ b/wait-for.bats @@ -1,7 +1,7 @@ #!/usr/bin/env bats -@test "google should be immediately found" { - run ./wait-for google.com:80 -- echo 'success' +@test "google should be immediately found, no output other than our own" { + run ./wait-for -q google.com:80 -- echo 'success' [ "$output" = "success" ] } @@ -14,12 +14,11 @@ } @test "nonexistent server should start command if loose option is specified" { - run ./wait-for -t 1 -l noserver:9999 -- echo 'passable' 2>&1 + run ./wait-for -q -t 1 -l noserver:9999 -- echo 'passable' 2>&1 [ "$status" -eq 0 ] - [ "${lines[0]}" = "Operation timed out" ] - [ "${lines[1]}" = "passable" ] + [ "$output" = "passable" ] } @test "preserve existing environment variables" {