Skip to content

Commit

Permalink
Improve cached_tarball test helper
Browse files Browse the repository at this point in the history
- Now only generates a `configure` script if explicitly specified
- Add ability to add arbitrary files to the tarball
- Cache most common tarballs in fixtures directory to speed up tests
  • Loading branch information
mislav committed Oct 17, 2023
1 parent fe933ab commit a559b30
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 67 deletions.
14 changes: 6 additions & 8 deletions test/arguments.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
load test_helper

@test "not enough arguments for ruby-build" {
mkdir -p "$TMP"
# use empty inline definition so nothing gets built anyway
local definition="${TMP}/build-definition"
echo '' > "$definition"

run ruby-build "$definition"
touch "${TMP}/empty-definition"
run ruby-build "${TMP}/empty-definition"
assert_failure
assert_output_contains 'Usage: ruby-build'
}

@test "extra arguments for ruby-build" {
mkdir -p "$TMP"
# use empty inline definition so nothing gets built anyway
local definition="${TMP}/build-definition"
echo '' > "$definition"

run ruby-build "$definition" "${TMP}/install" ""
touch "${TMP}/empty-definition"
run ruby-build "${TMP}/empty-definition" "${TMP}/install" ""
assert_failure
assert_output_contains 'Usage: ruby-build'
}
136 changes: 84 additions & 52 deletions test/build.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,63 @@ executable() {
}

cached_tarball() {
mkdir -p "$RUBY_BUILD_CACHE_PATH"
pushd "$RUBY_BUILD_CACHE_PATH" >/dev/null
tarball "$@"
popd >/dev/null
local save_to_fixtures
case "$*" in
"ruby-2.0.0 configure" | "yaml-0.1.6 configure" | "jruby-9000.dev bin/jruby" )
save_to_fixtures=1
;;
esac

local tarball="${1}.tar.gz"
local fixture_tarball="${FIXTURE_ROOT}/${tarball}"
local cached_tarball="${RUBY_BUILD_CACHE_PATH}/${tarball}"
shift 1

if [ -n "$save_to_fixtures" ] && [ -e "$fixture_tarball" ]; then
mkdir -p "$(dirname "$cached_tarball")"
cp "$fixture_tarball" "$cached_tarball"
return 0
fi

generate_tarball "$cached_tarball" "$@"
[ -z "$save_to_fixtures" ] || cp "$cached_tarball" "$fixture_tarball"
}

tarball() {
local name="$1"
local path="$PWD/$name"
local configure="$path/configure"
generate_tarball() {
local tarfile="$1"
shift 1
local name path
name="$(basename "${tarfile%.tar.gz}")"
path="$(mktemp -d "$TMP/tarball.XXXXX")/${name}"

executable "$configure" <<OUT
local file target
for file; do
case "$file" in
config | configure )
mkdir -p "$(dirname "${path}/${file}")"
cat > "${path}/${file}" <<OUT
#!$BASH
IFS=,
echo "$name: [\$*]" \${RUBYOPT:+RUBYOPT=\$RUBYOPT} >> build.log
OUT

for file; do
mkdir -p "$(dirname "${path}/${file}")"
touch "${path}/${file}"
chmod +x "${path}/${file}"
;;
*:* )
target="${file#*:}"
file="${file%:*}"
mkdir -p "$(dirname "${path}/${file}")"
cp "$target" "${path}/${file}"
;;
* )
mkdir -p "$(dirname "${path}/${file}")"
touch "${path}/${file}"
;;
esac
done

tar czf "${path}.tar.gz" -C "${path%/*}" "$name"
mkdir -p "$(dirname "$tarfile")"
tar czf "$tarfile" -C "${path%/*}" "$name"
rm -rf "$path"
}

stub_make_install() {
Expand All @@ -59,8 +92,8 @@ assert_build_log() {
}

@test "yaml is installed for ruby" {
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"
cached_tarball "yaml-0.1.6" configure
cached_tarball "ruby-2.0.0" configure

stub_repeated uname '-s : echo Linux'
stub_repeated brew false
Expand All @@ -85,8 +118,8 @@ OUT
}

@test "apply ruby patch before building" {
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"
cached_tarball "yaml-0.1.6" configure
cached_tarball "ruby-2.0.0" configure

stub_repeated uname '-s : echo Linux'
stub_repeated brew false
Expand Down Expand Up @@ -118,8 +151,8 @@ OUT
}

@test "striplevel ruby patch before building" {
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"
cached_tarball "yaml-0.1.6" configure
cached_tarball "ruby-2.0.0" configure

stub_repeated uname '-s : echo Linux'
stub_repeated brew false
Expand Down Expand Up @@ -151,8 +184,8 @@ OUT
}

@test "apply ruby patch from git diff before building" {
cached_tarball "yaml-0.1.6"
cached_tarball "ruby-2.0.0"
cached_tarball "yaml-0.1.6" configure
cached_tarball "ruby-2.0.0" configure

stub_repeated uname '-s : echo Linux'
stub_repeated brew false
Expand Down Expand Up @@ -185,7 +218,7 @@ OUT
}

@test "yaml is linked from Homebrew" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

brew_libdir="$TMP/homebrew-yaml"
mkdir -p "$brew_libdir"
Expand All @@ -211,7 +244,7 @@ OUT
}

@test "gmp is linked from Homebrew" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

gmp_libdir="$TMP/homebrew-gmp"
mkdir -p "$gmp_libdir"
Expand All @@ -235,7 +268,7 @@ OUT
}

@test "readline is linked from Homebrew" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

readline_libdir="$TMP/homebrew-readline"
mkdir -p "$readline_libdir"
Expand All @@ -259,7 +292,7 @@ OUT
}

@test "readline is not linked from Homebrew when explicitly defined" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

readline_libdir="$TMP/homebrew-readline"
mkdir -p "$readline_libdir"
Expand All @@ -284,7 +317,7 @@ OUT
}

@test "forward extra command-line arguments as configure flags" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

stub_repeated brew false
stub_make_install
Expand All @@ -307,7 +340,7 @@ OUT
}

@test "number of CPU cores defaults to 2" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

stub_repeated uname '-s : echo Darwin'
stub sysctl false
Expand All @@ -330,7 +363,7 @@ OUT
}

@test "number of CPU cores is detected on Mac" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

stub_repeated uname '-s : echo Darwin'
stub sysctl '-n hw.ncpu : echo 4'
Expand All @@ -354,7 +387,7 @@ OUT
}

@test "number of CPU cores is detected on FreeBSD" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

stub_repeated uname '-s : echo FreeBSD'
stub sysctl '-n hw.ncpu : echo 1'
Expand All @@ -379,7 +412,7 @@ OUT
}

@test "using MAKE_INSTALL_OPTS" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

stub_repeated uname '-s : echo Linux'
stub_make_install
Expand Down Expand Up @@ -411,7 +444,7 @@ OUT
}

@test "can use RUBY_CONFIGURE to apply a patch" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

executable "${TMP}/custom-configure" <<CONF
#!$BASH
Expand Down Expand Up @@ -456,8 +489,7 @@ OUT
}

@test "mruby strategy" {
package="$TMP/mruby-1.0"
executable "$package/minirake" <<OUT
executable "$TMP/minirake" <<OUT
#!$BASH
set -e
IFS=,
Expand All @@ -466,11 +498,7 @@ mkdir -p build/host/bin
touch build/host/bin/{mruby,mirb}
chmod +x build/host/bin/{mruby,mirb}
OUT
mkdir -p "$package/include"
touch "$package/include/mruby.h"
mkdir -p "$RUBY_BUILD_CACHE_PATH"
tar czf "$RUBY_BUILD_CACHE_PATH/${package##*/}.tar.gz" -C "${package%/*}" "${package##*/}"
rm -rf "$package"
cached_tarball "mruby-1.0" "minirake:$TMP/minirake" include/mruby.h

stub gem false
stub rake false
Expand All @@ -494,7 +522,7 @@ OUT
}

@test "rbx uses bundle then rake" {
cached_tarball "rubinius-2.0.0" "Gemfile"
cached_tarball "rubinius-2.0.0" Gemfile configure

stub gem false
stub rake false
Expand All @@ -519,15 +547,17 @@ OUT
}

@test "fixes rbx binstubs" {
executable "${RUBY_BUILD_CACHE_PATH}/rubinius-2.0.0/gems/bin/rake" <<OUT
executable "${TMP}/rbx-rake" <<OUT
#!rbx
puts 'rake'
OUT
executable "${RUBY_BUILD_CACHE_PATH}/rubinius-2.0.0/gems/bin/irb" <<OUT
executable "${TMP}/rbx-irb" <<OUT
#!rbx
print '>>'
OUT
cached_tarball "rubinius-2.0.0" bin/ruby
cached_tarball "rubinius-2.0.0" configure bin/ruby \
gems/bin/rake:"$TMP"/rbx-rake \
gems/bin/irb:"$TMP"/rbx-irb

stub bundle false
stub rake \
Expand Down Expand Up @@ -567,16 +597,18 @@ OUT
}

@test "JRuby build" {
executable "${RUBY_BUILD_CACHE_PATH}/jruby-1.7.9/bin/jruby" <<OUT
executable "${TMP}/jruby-bin" <<OUT
#!${BASH}
IFS=,
echo "jruby [\$*]" >> ../build.log
OUT
executable "${RUBY_BUILD_CACHE_PATH}/jruby-1.7.9/bin/gem" <<OUT
executable "${TMP}/jruby-gem" <<OUT
#!/usr/bin/env jruby
nice gem things
OUT
cached_tarball "jruby-1.7.9" bin/foo.exe bin/bar.dll bin/baz.bat
cached_tarball "jruby-1.7.9" bin/foo.exe bin/bar.dll bin/baz.bat \
bin/jruby:"$TMP"/jruby-bin \
bin/gem:"$TMP"/jruby-gem

run_inline_definition <<DEF
install_package "jruby-1.7.9" "http://jruby.org/downloads/jruby-bin-1.7.9.tar.gz" jruby
Expand Down Expand Up @@ -715,17 +747,17 @@ DEF
}

@test "TruffleRuby post-install hook" {
rmdir "$INSTALL_ROOT"
executable "${RUBY_BUILD_CACHE_PATH}/truffleruby-test/lib/truffle/post_install_hook.sh" <<OUT
echo Running post-install hook
executable "${TMP}/hook.sh" <<OUT
echo Running post-install hook >> build.log
OUT
cached_tarball "truffleruby-test" bin/truffleruby
cached_tarball "truffleruby-test" bin/truffleruby lib/truffle/post_install_hook.sh:"$TMP"/hook.sh

run_inline_definition <<DEF
install_package "truffleruby-test" "URL" truffleruby
DEF
assert_success
assert_output_contains "Running post-install hook"
run cat "$INSTALL_ROOT"/build.log
assert_success "Running post-install hook"
}

@test "non-writable TMPDIR aborts build" {
Expand All @@ -749,7 +781,7 @@ DEF
}

@test "does not initialize LDFLAGS directories" {
cached_tarball "ruby-2.0.0"
cached_tarball "ruby-2.0.0" configure

export LDFLAGS="-L ${BATS_TEST_DIRNAME}/what/evs"
run_inline_definition <<DEF
Expand Down
9 changes: 4 additions & 5 deletions test/cache.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ load test_helper
export RUBY_BUILD_SKIP_MIRROR=1
export RUBY_BUILD_CACHE_PATH="$TMP/cache"

setup() {
mkdir "$RUBY_BUILD_CACHE_PATH"
}


@test "packages are saved to download cache" {
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"

mkdir -p "$RUBY_BUILD_CACHE_PATH"
install_fixture definitions/without-checksum

assert_success
Expand All @@ -24,6 +20,7 @@ setup() {
@test "cached package without checksum" {
stub curl

mkdir -p "$RUBY_BUILD_CACHE_PATH"
cp "${FIXTURE_ROOT}/package-1.0.0.tar.gz" "$RUBY_BUILD_CACHE_PATH"

install_fixture definitions/without-checksum
Expand All @@ -39,6 +36,7 @@ setup() {
stub shasum true "echo ba988b1bb4250dee0b9dd3d4d722f9c64b2bacfc805d1b6eba7426bda72dd3c5"
stub curl

mkdir -p "$RUBY_BUILD_CACHE_PATH"
cp "${FIXTURE_ROOT}/package-1.0.0.tar.gz" "$RUBY_BUILD_CACHE_PATH"

install_fixture definitions/with-checksum
Expand All @@ -60,6 +58,7 @@ setup() {
stub curl "-*I* : true" \
"-q -o * -*S* https://?*/$checksum : cp $FIXTURE_ROOT/package-1.0.0.tar.gz \$3"

mkdir -p "$RUBY_BUILD_CACHE_PATH"
touch "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz"

install_fixture definitions/with-checksum
Expand Down
Binary file added test/fixtures/jruby-9000.dev.tar.gz
Binary file not shown.
Binary file added test/fixtures/ruby-2.0.0.tar.gz
Binary file not shown.
Binary file added test/fixtures/yaml-0.1.6.tar.gz
Binary file not shown.
Loading

0 comments on commit a559b30

Please sign in to comment.