Merge branch 'rj/sparse-flags'
[gitweb.git] / t / test-lib-functions.sh
index 6b3bbf99e46cf7cc418eca1eaf768d526ac658aa..92cf8f812cca26c94c8054ccd40af806f6b77310 100644 (file)
@@ -1263,3 +1263,42 @@ test_oid () {
        fi &&
        eval "printf '%s' \"\${$var}\""
 }
+
+# Choose a port number based on the test script's number and store it in
+# the given variable name, unless that variable already contains a number.
+test_set_port () {
+       local var=$1 port
+
+       if test $# -ne 1 || test -z "$var"
+       then
+               BUG "test_set_port requires a variable name"
+       fi
+
+       eval port=\$$var
+       case "$port" in
+       "")
+               # No port is set in the given env var, use the test
+               # number as port number instead.
+               # Remove not only the leading 't', but all leading zeros
+               # as well, so the arithmetic below won't (mis)interpret
+               # a test number like '0123' as an octal value.
+               port=${this_test#${this_test%%[1-9]*}}
+               if test "${port:-0}" -lt 1024
+               then
+                       # root-only port, use a larger one instead.
+                       port=$(($port + 10000))
+               fi
+               ;;
+       *[^0-9]*|0*)
+               error >&7 "invalid port number: $port"
+               ;;
+       *)
+               # The user has specified the port.
+               ;;
+       esac
+
+       # Make sure that parallel '--stress' test jobs get different
+       # ports.
+       port=$(($port + ${GIT_TEST_STRESS_JOB_NR:-0}))
+       eval $var=$port
+}