Skip to content

Commit

Permalink
add stub_repeated test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Mar 6, 2023
1 parent 0e92f8b commit 6d63f05
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
44 changes: 24 additions & 20 deletions test/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ assert_build_log() {
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub brew false
stub_repeated uname '-s : echo Linux'
stub_repeated brew false
stub_make_install
stub_make_install

install_fixture definitions/needs-yaml
assert_success

unstub uname
unstub brew
unstub make

assert_build_log <<OUT
Expand All @@ -86,8 +87,8 @@ OUT
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub brew false
stub_repeated uname '-s : echo Linux'
stub_repeated brew false
stub_make_install
stub_make_install
stub patch ' : echo patch "$@" | sed -E "s/\.[[:alnum:]]+$/.XXX/" >> build.log'
Expand All @@ -100,6 +101,7 @@ PATCH
assert_success

unstub uname
unstub brew
unstub make
unstub patch

Expand All @@ -118,8 +120,8 @@ OUT
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub brew false
stub_repeated uname '-s : echo Linux'
stub_repeated brew false
stub_make_install
stub_make_install
stub patch ' : echo patch "$@" | sed -E "s/\.[[:alnum:]]+$/.XXX/" >> build.log'
Expand All @@ -132,6 +134,7 @@ PATCH
assert_success

unstub uname
unstub brew
unstub make
unstub patch

Expand All @@ -150,8 +153,8 @@ OUT
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub brew false
stub_repeated uname '-s : echo Linux'
stub_repeated brew false
stub_make_install
stub_make_install
stub patch ' : echo patch "$@" | sed -E "s/\.[[:alnum:]]+$/.XXX/" >> build.log'
Expand All @@ -165,6 +168,7 @@ PATCH
assert_success

unstub uname
unstub brew
unstub make
unstub patch

Expand All @@ -185,8 +189,8 @@ OUT
brew_libdir="$TMP/homebrew-yaml"
mkdir -p "$brew_libdir"

stub uname '-s : echo Linux'
stub brew false "--prefix libyaml : echo '$brew_libdir'" false
stub_repeated uname '-s : echo Linux'
stub_repeated brew "--prefix libyaml : echo '$brew_libdir'"
stub_make_install

run_inline_definition <<DEF
Expand All @@ -211,7 +215,7 @@ OUT
gmp_libdir="$TMP/homebrew-gmp"
mkdir -p "$gmp_libdir"

stub brew false "--prefix gmp : echo '$gmp_libdir'"
stub_repeated brew "--prefix gmp : echo '$gmp_libdir'"
stub_make_install

run_inline_definition <<DEF
Expand All @@ -235,7 +239,7 @@ OUT
readline_libdir="$TMP/homebrew-readline"
mkdir -p "$readline_libdir"

stub brew "--prefix readline : echo '$readline_libdir'" false
stub_repeated brew "--prefix readline : echo '$readline_libdir'"
stub_make_install

run_inline_definition <<DEF
Expand All @@ -256,7 +260,7 @@ OUT
@test "readline is not linked from Homebrew when explicitly defined" {
cached_tarball "ruby-2.0.0"

stub brew false
stub_repeated brew false
stub_make_install

export RUBY_CONFIGURE_OPTS='--with-readline-dir=/custom'
Expand All @@ -278,7 +282,7 @@ OUT
@test "number of CPU cores defaults to 2" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Darwin' false
stub_repeated uname '-s : echo Darwin'
stub sysctl false
stub_make_install

Expand All @@ -301,7 +305,7 @@ OUT
@test "number of CPU cores is detected on Mac" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Darwin' false
stub_repeated uname '-s : echo Darwin'
stub sysctl '-n hw.ncpu : echo 4'
stub_make_install

Expand All @@ -325,7 +329,7 @@ OUT
@test "number of CPU cores is detected on FreeBSD" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo FreeBSD' false
stub_repeated uname '-s : echo FreeBSD'
stub sysctl '-n hw.ncpu : echo 1'
stub_make_install

Expand All @@ -349,7 +353,7 @@ OUT
@test "setting RUBY_MAKE_INSTALL_OPTS to a multi-word string" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub_repeated uname '-s : echo Linux'
stub_make_install

export RUBY_MAKE_INSTALL_OPTS="DOGE=\"such wow\""
Expand All @@ -371,7 +375,7 @@ OUT
@test "setting MAKE_INSTALL_OPTS to a multi-word string" {
cached_tarball "ruby-2.0.0"

stub uname '-s : echo Linux'
stub_repeated uname '-s : echo Linux'
stub_make_install

export MAKE_INSTALL_OPTS="DOGE=\"such wow\""
Expand Down Expand Up @@ -402,7 +406,7 @@ OUT
@test "make on FreeBSD defaults to gmake" {
cached_tarball "ruby-2.0.0"

stub uname "-s : echo FreeBSD" false
stub_repeated uname "-s : echo FreeBSD"
MAKE=gmake stub_make_install

MAKE= install_fixture definitions/vanilla-ruby
Expand All @@ -421,7 +425,7 @@ apply -p1 -i /my/patch.diff
exec ./configure "\$@"
CONF

stub uname '-s : echo Linux'
stub_repeated uname '-s : echo Linux'
stub apply 'echo apply "$@" >> build.log'
stub_make_install

Expand Down
30 changes: 20 additions & 10 deletions test/compiler.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export -n CC
export -n RUBY_CONFIGURE_OPTS

@test "require_gcc on OS X 10.9" {
stub uname '-s : echo Darwin'
stub_repeated uname '-s : echo Darwin'
stub sw_vers '-productVersion : echo 10.9.5'
stub gcc '--version : echo 4.2.1' '--version : echo 4.2.1'
stub_repeated gcc '--version : echo 4.2.1'

run_inline_definition <<DEF
require_gcc
Expand All @@ -22,12 +22,16 @@ DEF
CC=${TMP}/bin/gcc
MACOSX_DEPLOYMENT_TARGET=no
OUT

unstub uname
unstub sw_vers
unstub gcc
}

@test "require_gcc on OS X 10.10" {
stub uname '-s : echo Darwin'
stub_repeated uname '-s : echo Darwin'
stub sw_vers '-productVersion : echo 10.10'
stub gcc '--version : echo 4.2.1' '--version : echo 4.2.1'
stub_repeated gcc '--version : echo 4.2.1'

run_inline_definition <<DEF
require_gcc
Expand All @@ -39,6 +43,10 @@ DEF
CC=${TMP}/bin/gcc
MACOSX_DEPLOYMENT_TARGET=10.9
OUT

unstub uname
unstub sw_vers
unstub gcc
}

@test "require_gcc silences warnings" {
Expand All @@ -55,13 +63,10 @@ DEF
mkdir -p "$INSTALL_ROOT"
cd "$INSTALL_ROOT"

stub uname '-s : echo Darwin' '-s : echo Darwin'
stub_repeated uname '-s : echo Darwin'
stub sw_vers '-productVersion : echo 10.10'
stub cc 'false'
stub brew 'false'
stub make \
'echo make $@' \
'echo make $@'
stub_repeated brew 'false'
stub_repeated make 'echo make $@'

cat > ./configure <<CON
#!${BASH}
Expand All @@ -83,4 +88,9 @@ CFLAGS=no
make -j 2
make install
OUT

unstub uname
unstub sw_vers
unstub brew
unstub make
}
3 changes: 2 additions & 1 deletion test/rbenv.bats
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ OUT
}

@test "nonexistent version" {
stub brew false
stub_repeated brew false
stub_ruby_build 'echo ERROR >&2 && exit 2' \
"--definitions : echo 1.8.7 1.9.3-p0 1.9.3-p194 2.1.2 | tr ' ' $'\\n'"

Expand All @@ -97,6 +97,7 @@ If the version you need is missing, try upgrading ruby-build:
git -C ${BATS_TEST_DIRNAME}/.. pull
OUT

unstub brew
unstub ruby-build
}

Expand Down
10 changes: 6 additions & 4 deletions test/stubs/stub
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bash
set -e

status=0
status=127
program="${0##*/}"
PROGRAM="$(echo "$program" | tr a-z- A-Z_)"
[ -n "$TMPDIR" ] || TMPDIR="/tmp"

_STUB_PLAN="${PROGRAM}_STUB_PLAN"
_STUB_RUN="${PROGRAM}_STUB_RUN"
_STUB_INDEX="${PROGRAM}_STUB_INDEX"
_STUB_NOINDEX="${PROGRAM}_STUB_NOINDEX"
_STUB_RESULT="${PROGRAM}_STUB_RESULT"
_STUB_END="${PROGRAM}_STUB_END"
_STUB_DEBUG="${PROGRAM}_STUB_DEBUG"
Expand All @@ -32,7 +33,7 @@ index=0
while IFS= read -r line; do
index=$(($index + 1))

if [ -z "${!_STUB_END}" ] && [ $index -eq "${!_STUB_INDEX}" ]; then
if [[ -z "${!_STUB_END}" && -n "${!_STUB_NOINDEX}" || $index -eq "${!_STUB_INDEX}" ]]; then
# We found the plan line we're interested in.
# Start off by assuming success.
result=0
Expand Down Expand Up @@ -72,7 +73,8 @@ while IFS= read -r line; do
( eval "$command" )
status="$?"
set -e
else
[ -z "${!_STUB_NOINDEX}" ] || break
elif [ -z "${!_STUB_NOINDEX}" ]; then
eval "${_STUB_RESULT}"=1
fi
fi
Expand All @@ -95,7 +97,7 @@ if [ -n "${!_STUB_END}" ]; then
else
# If the requested index is larger than the number
# of lines in the plan file, we failed.
if [ "${!_STUB_INDEX}" -gt $index ]; then
if [[ -z "${!_STUB_NOINDEX}" && "${!_STUB_INDEX}" -gt $index ]]; then
eval "${_STUB_RESULT}"=1
fi

Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ stub() {
for arg in "$@"; do printf "%s\n" "$arg" >> "${TMP}/${program}-stub-plan"; done
}

stub_repeated() {
local program="$1"
local prefix="$(echo "$program" | tr a-z- A-Z_)"
export "${prefix}_STUB_NOINDEX"=1
stub "$@"
}

unstub() {
local program="$1"
local prefix="$(echo "$program" | tr a-z- A-Z_)"
Expand Down

0 comments on commit 6d63f05

Please sign in to comment.