2023-10-17 00:18:14 +02:00
|
|
|
export TMP="$BATS_TMPDIR"/ruby-build-test
|
2018-04-26 04:07:32 +02:00
|
|
|
export RUBY_BUILD_CURL_OPTS=
|
|
|
|
export RUBY_BUILD_HTTP_CLIENT="curl"
|
2013-10-25 02:01:50 +02:00
|
|
|
|
|
|
|
if [ "$FIXTURE_ROOT" != "$BATS_TEST_DIRNAME/fixtures" ]; then
|
|
|
|
export FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures"
|
|
|
|
export INSTALL_ROOT="$TMP/install"
|
Only FreeBSD needs extra PATH for bash
For the tests to run on FreeBSD, the bash executable needs to be
available in PATH.
The test_helper PATH was modified by
49a9471758318074109a740c85bc91de79488c55 and f70d958f470e1c27041ea0ee7695c5aca28711ee
in order to be sure that bash is in PATH on FreeBSD.
(The default location for packages on FreeBSD is /usr/local/bin, and
since bash isn't provided by FreeBSD itself, that means /usr/local/bin
is the most common, if not only, location to find bash on FreeBSD.)
However, adding /usr/local/bin to PATH means that homebrew-managed
packages on macOS are now in PATH for the test suite. This is especially
important due to packages like readline. Since ruby-build prefers
homebrew readline when available, this makes the tests fail on macOS
(because the tests are written assuming no readline configuration.)
While the tests could stub brew in all cases, to ensure the readline
package is ignored by the tests, it seems more prudent to revert the
PATH to be as minimal as possible, and to only include /usr/local/bin by
precisely the OS that depends on it.
2018-11-12 20:14:21 +01:00
|
|
|
PATH="/usr/bin:/bin:/usr/sbin:/sbin"
|
|
|
|
if [ "FreeBSD" = "$(uname -s)" ]; then
|
|
|
|
PATH="/usr/local/bin:$PATH"
|
|
|
|
fi
|
2013-10-26 17:39:55 +02:00
|
|
|
PATH="$BATS_TEST_DIRNAME/../bin:$PATH"
|
|
|
|
PATH="$TMP/bin:$PATH"
|
|
|
|
export PATH
|
2013-10-25 02:01:50 +02:00
|
|
|
fi
|
2012-11-20 00:53:32 +01:00
|
|
|
|
2019-11-04 14:44:03 +01:00
|
|
|
remove_commands_from_path() {
|
|
|
|
local path cmd
|
|
|
|
local NEWPATH=":$PATH:"
|
2021-07-07 19:17:45 +02:00
|
|
|
while PATH="${NEWPATH#:}" command -v "$@" >/dev/null; do
|
|
|
|
local paths=( $(PATH="${NEWPATH#:}" command -v "$@" | sed 's!/[^/]*$!!' | sort -u) )
|
|
|
|
for path in "${paths[@]}"; do
|
|
|
|
local tmp_path="$(mktemp -d "$TMP/path.XXXXX")"
|
|
|
|
ln -fs "$path"/* "$tmp_path/"
|
|
|
|
for cmd; do rm -f "$tmp_path/$cmd"; done
|
|
|
|
NEWPATH="${NEWPATH/:$path:/:$tmp_path:}"
|
|
|
|
done
|
2019-11-04 14:44:03 +01:00
|
|
|
done
|
|
|
|
echo "${NEWPATH#:}"
|
|
|
|
}
|
|
|
|
|
2012-11-20 00:53:32 +01:00
|
|
|
teardown() {
|
2023-10-17 00:18:14 +02:00
|
|
|
rm -fr "${TMP:?}"
|
2012-11-20 00:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stub() {
|
|
|
|
local program="$1"
|
2023-03-06 19:19:31 +01:00
|
|
|
# shellcheck disable=SC2155
|
2014-01-04 14:43:25 +01:00
|
|
|
local prefix="$(echo "$program" | tr a-z- A-Z_)"
|
2012-11-20 00:53:32 +01:00
|
|
|
shift
|
|
|
|
|
|
|
|
export "${prefix}_STUB_PLAN"="${TMP}/${program}-stub-plan"
|
|
|
|
export "${prefix}_STUB_RUN"="${TMP}/${program}-stub-run"
|
|
|
|
export "${prefix}_STUB_END"=
|
|
|
|
|
2013-10-25 02:01:50 +02:00
|
|
|
mkdir -p "${TMP}/bin"
|
2013-10-25 04:21:34 +02:00
|
|
|
ln -sf "${BATS_TEST_DIRNAME}/stubs/stub" "${TMP}/bin/${program}"
|
2012-11-20 00:53:32 +01:00
|
|
|
|
2012-11-20 06:45:00 +01:00
|
|
|
touch "${TMP}/${program}-stub-plan"
|
2012-11-20 00:53:32 +01:00
|
|
|
for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done
|
|
|
|
}
|
|
|
|
|
2023-03-06 18:58:00 +01:00
|
|
|
stub_repeated() {
|
|
|
|
local program="$1"
|
2023-03-06 19:19:31 +01:00
|
|
|
# shellcheck disable=SC2155
|
2023-03-06 18:58:00 +01:00
|
|
|
local prefix="$(echo "$program" | tr a-z- A-Z_)"
|
|
|
|
export "${prefix}_STUB_NOINDEX"=1
|
|
|
|
stub "$@"
|
|
|
|
}
|
|
|
|
|
2012-11-20 00:53:32 +01:00
|
|
|
unstub() {
|
|
|
|
local program="$1"
|
2023-03-06 19:19:31 +01:00
|
|
|
# shellcheck disable=SC2155
|
2014-01-04 14:43:25 +01:00
|
|
|
local prefix="$(echo "$program" | tr a-z- A-Z_)"
|
2013-10-25 02:01:50 +02:00
|
|
|
local path="${TMP}/bin/${program}"
|
2012-11-20 00:53:32 +01:00
|
|
|
|
|
|
|
export "${prefix}_STUB_END"=1
|
|
|
|
|
2013-10-26 17:43:54 +02:00
|
|
|
local STATUS=0
|
|
|
|
"$path" || STATUS="$?"
|
|
|
|
|
2013-10-25 02:01:50 +02:00
|
|
|
rm -f "$path"
|
2013-10-25 04:17:53 +02:00
|
|
|
rm -f "${TMP}/${program}-stub-plan" "${TMP}/${program}-stub-run"
|
2013-10-26 17:43:54 +02:00
|
|
|
return "$STATUS"
|
2012-11-20 00:53:32 +01:00
|
|
|
}
|
|
|
|
|
2013-10-26 17:49:43 +02:00
|
|
|
run_inline_definition() {
|
|
|
|
local definition="${TMP}/build-definition"
|
|
|
|
cat > "$definition"
|
|
|
|
run ruby-build "$definition" "${1:-$INSTALL_ROOT}"
|
|
|
|
}
|
|
|
|
|
2012-11-20 00:53:32 +01:00
|
|
|
install_fixture() {
|
2013-12-11 21:16:47 +01:00
|
|
|
local args
|
|
|
|
|
|
|
|
while [ "${1#-}" != "$1" ]; do
|
|
|
|
args="$args $1"
|
|
|
|
shift 1
|
|
|
|
done
|
|
|
|
|
2012-11-20 00:53:32 +01:00
|
|
|
local name="$1"
|
|
|
|
local destination="$2"
|
|
|
|
[ -n "$destination" ] || destination="$INSTALL_ROOT"
|
|
|
|
|
2013-12-11 21:16:47 +01:00
|
|
|
run ruby-build $args "$FIXTURE_ROOT/$name" "$destination"
|
2012-11-20 00:53:32 +01:00
|
|
|
}
|
2013-10-25 04:17:53 +02:00
|
|
|
|
|
|
|
assert() {
|
|
|
|
if ! "$@"; then
|
|
|
|
flunk "failed: $@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-10-29 15:40:32 +01:00
|
|
|
refute() {
|
|
|
|
if "$@"; then
|
|
|
|
flunk "expected to fail: $@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2013-10-25 04:17:53 +02:00
|
|
|
flunk() {
|
|
|
|
{ if [ "$#" -eq 0 ]; then cat -
|
|
|
|
else echo "$@"
|
|
|
|
fi
|
|
|
|
} | sed "s:${TMP}:\${TMP}:g" >&2
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_success() {
|
|
|
|
if [ "$status" -ne 0 ]; then
|
|
|
|
{ echo "command failed with exit status $status"
|
|
|
|
echo "output: $output"
|
|
|
|
} | flunk
|
|
|
|
elif [ "$#" -gt 0 ]; then
|
|
|
|
assert_output "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_failure() {
|
|
|
|
if [ "$status" -eq 0 ]; then
|
|
|
|
flunk "expected failed exit status"
|
|
|
|
elif [ "$#" -gt 0 ]; then
|
|
|
|
assert_output "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_equal() {
|
|
|
|
if [ "$1" != "$2" ]; then
|
|
|
|
{ echo "expected: $1"
|
|
|
|
echo "actual: $2"
|
|
|
|
} | flunk
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_output() {
|
|
|
|
local expected
|
|
|
|
if [ $# -eq 0 ]; then expected="$(cat -)"
|
|
|
|
else expected="$1"
|
|
|
|
fi
|
|
|
|
assert_equal "$expected" "$output"
|
|
|
|
}
|
2013-10-26 06:32:03 +02:00
|
|
|
|
|
|
|
assert_output_contains() {
|
|
|
|
local expected="$1"
|
2014-11-18 00:07:40 +01:00
|
|
|
if [ -z "$expected" ]; then
|
|
|
|
echo "assert_output_contains needs an argument" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
2014-04-18 02:51:26 +02:00
|
|
|
echo "$output" | $(type -p ggrep grep | head -1) -F "$expected" >/dev/null || {
|
2013-10-26 06:32:03 +02:00
|
|
|
{ echo "expected output to contain $expected"
|
|
|
|
echo "actual: $output"
|
|
|
|
} | flunk
|
|
|
|
}
|
|
|
|
}
|