Adjust shell scripts to allow Bash in other locations
[urcu.git] / tests / utils / utils.sh
CommitLineData
da56d5ca 1#!/usr/bin/env bash
de6cb9d5
MJ
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
12set -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.
16scriptdir="$(dirname "${BASH_SOURCE[0]}")"
17if readlink -f "." >/dev/null 2>&1; then
18 testsdir=$(readlink -f "$scriptdir/..")
19else
20 testsdir="$scriptdir/.."
21fi
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
27if [ "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
46fi
47export URCU_TESTS_OS_TYPE
48
49# Allow overriding the source and build directories
50if [ "x${URCU_TESTS_SRCDIR:-}" = "x" ]; then
51 URCU_TESTS_SRCDIR="$testsdir"
52fi
53export URCU_TESTS_SRCDIR
54
55if [ "x${URCU_TESTS_BUILDDIR:-}" = "x" ]; then
56 URCU_TESTS_BUILDDIR="$testsdir"
57fi
58export URCU_TESTS_BUILDDIR
59
60# Source the generated environment file if it's present.
61if [ -f "${URCU_TESTS_BUILDDIR}/utils/env.sh" ]; then
62 # shellcheck source=./env.sh
63 . "${URCU_TESTS_BUILDDIR}/utils/env.sh"
64fi
65
66
67### External Tools ###
68
69if [ "x${URCU_TESTS_NPROC_BIN:-}" = "x" ]; then
70 URCU_TESTS_NPROC_BIN="nproc"
71fi
72export URCU_TESTS_NPROC_BIN
73
74if [ "x${URCU_TESTS_GETCONF_BIN:-}" = "x" ]; then
75 URCU_TESTS_GETCONF_BIN="getconf"
76fi
77export 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.
83if [ "x${SH_TAP:-}" = x1 ]; then
84 # shellcheck source=./tap.sh
85 . "${URCU_TESTS_SRCDIR}/utils/tap.sh"
86fi
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.
93urcu_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.
105urcu_xseq() {
106 local i=$1
107
108 while [[ "$i" -le "$2" ]]; do
109 echo "$i"
110 i=$(( i + 1 ))
111 done
112}
This page took 0.026803 seconds and 4 git commands to generate.