| 1 | #!/usr/bin/env bash |
| 2 | # |
| 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | # |
| 5 | # SPDX-FileCopyrightText: 2022 EfficiOS Inc. |
| 6 | # |
| 7 | |
| 8 | # This file is meant to be sourced at the start of shell script-based tests. |
| 9 | |
| 10 | |
| 11 | # Error out when encountering an undefined variable |
| 12 | set -u |
| 13 | |
| 14 | # If "readlink -f" is available, get a resolved absolute path to the |
| 15 | # tests source dir, otherwise make do with a relative path. |
| 16 | scriptdir="$(dirname "${BASH_SOURCE[0]}")" |
| 17 | if readlink -f "." >/dev/null 2>&1; then |
| 18 | testsdir=$(readlink -f "$scriptdir/..") |
| 19 | else |
| 20 | testsdir="$scriptdir/.." |
| 21 | fi |
| 22 | |
| 23 | # The OS on which we are running. See [1] for possible values of 'uname -s'. |
| 24 | # We do a bit of translation to ease our life down the road for comparison. |
| 25 | # Export it so that called executables can use it. |
| 26 | # [1] https://en.wikipedia.org/wiki/Uname#Examples |
| 27 | if [ "x${URCU_TESTS_OS_TYPE:-}" = "x" ]; then |
| 28 | URCU_TESTS_OS_TYPE="$(uname -s)" |
| 29 | case "$URCU_TESTS_OS_TYPE" in |
| 30 | MINGW*) |
| 31 | URCU_TESTS_OS_TYPE="mingw" |
| 32 | ;; |
| 33 | Darwin) |
| 34 | URCU_TESTS_OS_TYPE="darwin" |
| 35 | ;; |
| 36 | Linux) |
| 37 | URCU_TESTS_OS_TYPE="linux" |
| 38 | ;; |
| 39 | CYGWIN*) |
| 40 | URCU_TESTS_OS_TYPE="cygwin" |
| 41 | ;; |
| 42 | *) |
| 43 | URCU_TESTS_OS_TYPE="unsupported" |
| 44 | ;; |
| 45 | esac |
| 46 | fi |
| 47 | export URCU_TESTS_OS_TYPE |
| 48 | |
| 49 | # Allow overriding the source and build directories |
| 50 | if [ "x${URCU_TESTS_SRCDIR:-}" = "x" ]; then |
| 51 | URCU_TESTS_SRCDIR="$testsdir" |
| 52 | fi |
| 53 | export URCU_TESTS_SRCDIR |
| 54 | |
| 55 | if [ "x${URCU_TESTS_BUILDDIR:-}" = "x" ]; then |
| 56 | URCU_TESTS_BUILDDIR="$testsdir" |
| 57 | fi |
| 58 | export URCU_TESTS_BUILDDIR |
| 59 | |
| 60 | # Source the generated environment file if it's present. |
| 61 | if [ -f "${URCU_TESTS_BUILDDIR}/utils/env.sh" ]; then |
| 62 | # shellcheck source=./env.sh |
| 63 | . "${URCU_TESTS_BUILDDIR}/utils/env.sh" |
| 64 | fi |
| 65 | |
| 66 | |
| 67 | ### External Tools ### |
| 68 | |
| 69 | if [ "x${URCU_TESTS_NPROC_BIN:-}" = "x" ]; then |
| 70 | URCU_TESTS_NPROC_BIN="nproc" |
| 71 | fi |
| 72 | export URCU_TESTS_NPROC_BIN |
| 73 | |
| 74 | if [ "x${URCU_TESTS_GETCONF_BIN:-}" = "x" ]; then |
| 75 | URCU_TESTS_GETCONF_BIN="getconf" |
| 76 | fi |
| 77 | export URCU_TESTS_GETCONF_BIN |
| 78 | |
| 79 | |
| 80 | # By default, it will not source tap.sh. If you to tap output directly from |
| 81 | # the test script, define the 'SH_TAP' variable to '1' before sourcing this |
| 82 | # script. |
| 83 | if [ "x${SH_TAP:-}" = x1 ]; then |
| 84 | # shellcheck source=./tap.sh |
| 85 | . "${URCU_TESTS_SRCDIR}/utils/tap.sh" |
| 86 | fi |
| 87 | |
| 88 | |
| 89 | ### Functions ### |
| 90 | |
| 91 | # Print the number of online CPUs, fallback to '1' if no tools to count CPUs |
| 92 | # are found in the environment. |
| 93 | urcu_nproc() { |
| 94 | if command -v "${URCU_TESTS_NPROC_BIN}" >/dev/null 2>&1; then |
| 95 | "${URCU_TESTS_NPROC_BIN}" |
| 96 | elif command -v "${URCU_TESTS_GETCONF_BIN}" >/dev/null 2>&1; then |
| 97 | "${URCU_TESTS_GETCONF_BIN}" _NPROCESSORS_ONLN |
| 98 | else |
| 99 | # Fallback to '1' |
| 100 | echo 1 |
| 101 | fi |
| 102 | } |
| 103 | |
| 104 | # Shell implementation of the 'seq' command. |
| 105 | urcu_xseq() { |
| 106 | local i=$1 |
| 107 | |
| 108 | while [[ "$i" -le "$2" ]]; do |
| 109 | echo "$i" |
| 110 | i=$(( i + 1 )) |
| 111 | done |
| 112 | } |