diff --git a/test/common.sh b/test/common.sh index eba424d..a7ca5e0 100644 --- a/test/common.sh +++ b/test/common.sh @@ -19,3 +19,17 @@ exit 1 } trap 'die $LINENO' ERR + +# Get a random port for the test to use +# This isn't foolproof but should fail less than handcrafted approaches +function getRandomPort() { + # Get the PID of this script as a way to get a random port, + # and make sure the value is > 1024, as ports < 1024 are priviledged + # and require root priviledges. + # We also need to make sure the value is not > ushort.max + PORT=$(($$ % 65536)) + if [ $PORT -le 1024 ]; then + PORT=$(($PORT + 1025)) + fi + echo $PORT +} diff --git a/test/fetchzip.sh b/test/fetchzip.sh index 723e698..15b906e 100755 --- a/test/fetchzip.sh +++ b/test/fetchzip.sh @@ -3,7 +3,7 @@ . "$DIR"/common.sh -PORT=$(($$ + 1024)) # PID + 1024 +PORT=$(getRandomPort) dub remove gitcompatibledubpackage --non-interactive --version=* 2>/dev/null || true diff --git a/test/issue1180-local-cache-broken.sh b/test/issue1180-local-cache-broken.sh index e46970a..0a83177 100755 --- a/test/issue1180-local-cache-broken.sh +++ b/test/issue1180-local-cache-broken.sh @@ -3,7 +3,8 @@ . "$DIR"/common.sh -PORT=$(($$ + 1024)) # PID + 1024 +PORT=$(getRandomPort) + "$DUB" remove maven-dubpackage --root="$DIR/issue1180-local-cache-broken" --non-interactive --version=* 2>/dev/null || true "$DUB" build --single "$DIR"/test_registry.d diff --git a/test/issue1416-maven-repo-pkg-supplier.sh b/test/issue1416-maven-repo-pkg-supplier.sh index 636ef31..3aca25b 100755 --- a/test/issue1416-maven-repo-pkg-supplier.sh +++ b/test/issue1416-maven-repo-pkg-supplier.sh @@ -3,7 +3,7 @@ . "$DIR"/common.sh -PORT=$(($$ + 1024)) # PID + 1024 +PORT=$(getRandomPort) dub remove maven-dubpackage --non-interactive --version=* 2>/dev/null || true diff --git a/test/issue1524-maven-upgrade-dependency-tree.sh b/test/issue1524-maven-upgrade-dependency-tree.sh index 711cded..24f5f65 100755 --- a/test/issue1524-maven-upgrade-dependency-tree.sh +++ b/test/issue1524-maven-upgrade-dependency-tree.sh @@ -3,7 +3,7 @@ . "$DIR"/common.sh -PORT=$(($$ + 1024)) # PID + 1024 +PORT=$(getRandomPort) dub remove maven-dubpackage-a --non-interactive --version=* 2>/dev/null || true dub remove maven-dubpackage-b --non-interactive --version=* 2>/dev/null || true diff --git a/test/issue1574-addcommand.sh b/test/issue1574-addcommand.sh index dddedca..94d90f1 100755 --- a/test/issue1574-addcommand.sh +++ b/test/issue1574-addcommand.sh @@ -3,7 +3,7 @@ . "$DIR"/common.sh -PORT=$(($$ + 1024)) # PID + 1024 +PORT=$(getRandomPort) tempDir="issue1574-addcommand" "$DUB" build --single "$DIR"/test_registry.d diff --git a/test/timeout.sh b/test/timeout.sh index 3e841c9..160e78b 100755 --- a/test/timeout.sh +++ b/test/timeout.sh @@ -2,7 +2,7 @@ . $(dirname "${BASH_SOURCE[0]}")/common.sh -PORT=$(($$ + 1024)) # PID + 1024 +PORT=$(getRandomPort) log ' Testing unconnectable registry' if timeout 1s $DUB fetch dub --skip-registry=all --registry=http://localhost:$PORT; then