From 4f6cff8c9c34e151078d5e5e57d37a4118fb0201 Mon Sep 17 00:00:00 2001 From: Rahmat Budiharso Date: Wed, 11 Oct 2017 11:32:27 +0700 Subject: [PATCH] preserve existing environment variable --- wait-for | 10 +++++++++- wait-for.bats | 11 ++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/wait-for b/wait-for index ddfc39e..435e278 100755 --- a/wait-for +++ b/wait-for @@ -20,12 +20,20 @@ USAGE } wait_for() { + _HOST=$HOST + _PORT=$PORT for i in `seq $TIMEOUT` ; do nc -z "$HOST" "$PORT" > /dev/null 2>&1 - + result=$? if [ $result -eq 0 ] ; then if [ $# -gt 0 ] ; then + unset HOST + unset PORT + HOST=$_HOST + PORT=$_PORT + unset _HOST + unset _PORT exec "$@" fi exit 0 diff --git a/wait-for.bats b/wait-for.bats index cbea6a4..c720419 100644 --- a/wait-for.bats +++ b/wait-for.bats @@ -2,7 +2,7 @@ @test "google should be immediately found" { run ./wait-for google.com:80 -- echo 'success' - + [ "$output" = "success" ] } @@ -12,3 +12,12 @@ [ "$status" -ne 0 ] [ "$output" != "success" ] } + +@test "preserve existing environment variable" { + HOST=myweb.com + PORT=8080 + run ./wait-for google.com:80 -- echo 'success' + + [ "$(echo $HOST)" = 'myweb.com' ] + [ "$(echo $PORT)" = '8080' ] +}