Skip to content

Commit c7631e5

Browse files
sd-yipAddono
authored andcommitted
fix(command): Restore environment variables before calling exec
Previously, the command would overwrite the HOST and PORT variables based on the hostname and port of the service which we would check for being available. This change prevents these variables from being overwritten, which allows you to set them outside the command. BREAKING CHANGE: HOST, PORT and other internally used environment variables are not overwritten anymore. If you use these, then you need to manually supply them.
1 parent ece51c2 commit c7631e5

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

wait-for

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
# SOFTWARE.
2424

25+
set -- "$@" -- "$TIMEOUT" "$QUIET" "$HOST" "$PORT" "$result"
2526
TIMEOUT=15
2627
QUIET=0
2728

@@ -52,7 +53,15 @@ wait_for() {
5253

5354
result=$?
5455
if [ $result -eq 0 ] ; then
55-
if [ $# -gt 0 ] ; then
56+
if [ $# -gt 6 ] ; then
57+
for result in $(seq $(($# - 6))); do
58+
result=$1
59+
shift
60+
set -- "$@" "$result"
61+
done
62+
63+
TIMEOUT=$2 QUIET=$3 HOST=$4 PORT=$5 result=$6
64+
shift 6
5665
exec "$@"
5766
fi
5867
exit 0
@@ -69,8 +78,7 @@ wait_for() {
6978
exit 1
7079
}
7180

72-
while [ $# -gt 0 ]
73-
do
81+
while :; do
7482
case "$1" in
7583
*:* )
7684
HOST=$(printf "%s\n" "$1"| cut -d : -f 1)

wait-for.bats

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,16 @@
3232
[ "$status" -ne 0 ]
3333
[ "$output" != "success" ]
3434
}
35+
36+
@test "environment variables should be restored for command invocation" {
37+
HOST=success run ./wait-for -t 1 google.com:80 -- sh -c 'echo "$HOST"'
38+
39+
[ "$output" = "success" ]
40+
}
41+
42+
@test "unset environment variables should be restored as unset for command invocation" {
43+
run ./wait-for -t 1 google.com:80 -- sh -uc 'echo "$HOST"'
44+
45+
[ "$status" -ne 0 ]
46+
[ "$output" != "google.com" ]
47+
}

0 commit comments

Comments
 (0)