Extract has_checksum_support predicate function

- Remove unnecessary HAS_X_SUPPORT variables
- Merge conditional for unsetting mirror-url
- Memoize has_checksum_support function
This commit is contained in:
Jason Karns 2016-01-18 11:44:14 -05:00
parent a187b07dab
commit 14750a0b93
3 changed files with 18 additions and 36 deletions

View file

@ -246,6 +246,12 @@ compute_md5() {
fi fi
} }
declare -a HAS_CHECKSUM_SUPPORT
has_checksum_support() {
local checksum_command="$1"
return ${HAS_CHECKSUM_SUPPORT[$checksum_command]:=$(echo test | "$checksum_command" >/dev/null; echo $?)}
}
verify_checksum() { verify_checksum() {
local checksum_command local checksum_command
local filename="$1" local filename="$1"
@ -255,22 +261,19 @@ verify_checksum() {
[ -e "$filename" ] || return 0 [ -e "$filename" ] || return 0
case "${#expected_checksum}" in case "${#expected_checksum}" in
0) # If there's no expected checksum, return success # If expected checksum is empty, return success
return 0 0) return 0 ;;
;; # MD5
32) # MD5 32) checksum_command="compute_md5" ;;
[ -n "$HAS_MD5_SUPPORT" ] || return 0 # SHA2 256
checksum_command="compute_md5" 64) checksum_command="compute_sha2" ;;
;; # unknown checksum algorithm, return failure
64) # SHA2 256 *) return 1 ;;
[ -n "$HAS_SHA2_SUPPORT" ] || return 0
checksum_command="compute_sha2"
;;
*) # unknown checksum algorithm, return failure
return 1
;;
esac esac
# If chosen provided checksum algorithm isn't supported, return success
has_checksum_support "$checksum_command" || return 0
# If the computed checksum is empty, return failure # If the computed checksum is empty, return failure
local computed_checksum=`echo "$($checksum_command < "$filename")" | tr [A-Z] [a-z]` local computed_checksum=`echo "$($checksum_command < "$filename")" | tr [A-Z] [a-z]`
[ -n "$computed_checksum" ] || return 1 [ -n "$computed_checksum" ] || return 1
@ -1225,23 +1228,10 @@ else
RUBY_BUILD_DEFAULT_MIRROR= RUBY_BUILD_DEFAULT_MIRROR=
fi fi
if [ -n "$RUBY_BUILD_SKIP_MIRROR" ]; then if [ -n "$RUBY_BUILD_SKIP_MIRROR" ] || ! has_checksum_support compute_sha2; then
unset RUBY_BUILD_MIRROR_URL unset RUBY_BUILD_MIRROR_URL
fi fi
if echo test | compute_sha2 >/dev/null; then
HAS_SHA2_SUPPORT=1
else
unset HAS_SHA2_SUPPORT
unset RUBY_BUILD_MIRROR_URL
fi
if echo test | compute_md5 >/dev/null; then
HAS_MD5_SUPPORT=1
else
unset HAS_MD5_SUPPORT
fi
SEED="$(date "+%Y%m%d%H%M%S").$$" SEED="$(date "+%Y%m%d%H%M%S").$$"
LOG_PATH="${TMP}/ruby-build.${SEED}.log" LOG_PATH="${TMP}/ruby-build.${SEED}.log"
RUBY_BIN="${PREFIX_PATH}/bin/ruby" RUBY_BIN="${PREFIX_PATH}/bin/ruby"

View file

@ -10,7 +10,6 @@ setup() {
@test "packages are saved to download cache" { @test "packages are saved to download cache" {
stub shasum true
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3" stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
install_fixture definitions/without-checksum install_fixture definitions/without-checksum
@ -19,12 +18,10 @@ setup() {
[ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ] [ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]
unstub curl unstub curl
unstub shasum
} }
@test "cached package without checksum" { @test "cached package without checksum" {
stub shasum true
stub curl stub curl
cp "${FIXTURE_ROOT}/package-1.0.0.tar.gz" "$RUBY_BUILD_CACHE_PATH" cp "${FIXTURE_ROOT}/package-1.0.0.tar.gz" "$RUBY_BUILD_CACHE_PATH"
@ -35,7 +32,6 @@ setup() {
[ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ] [ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]
unstub curl unstub curl
unstub shasum
} }
@ -79,7 +75,6 @@ setup() {
@test "nonexistent cache directory is ignored" { @test "nonexistent cache directory is ignored" {
stub shasum true
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3" stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
export RUBY_BUILD_CACHE_PATH="${TMP}/nonexistent" export RUBY_BUILD_CACHE_PATH="${TMP}/nonexistent"
@ -91,5 +86,4 @@ setup() {
[ ! -d "$RUBY_BUILD_CACHE_PATH" ] [ ! -d "$RUBY_BUILD_CACHE_PATH" ]
unstub curl unstub curl
unstub shasum
} }

View file

@ -6,7 +6,6 @@ export RUBY_BUILD_CACHE_PATH=
@test "package URL without checksum" { @test "package URL without checksum" {
stub shasum true
stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3" stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"
install_fixture definitions/without-checksum install_fixture definitions/without-checksum
@ -14,7 +13,6 @@ export RUBY_BUILD_CACHE_PATH=
[ -x "${INSTALL_ROOT}/bin/package" ] [ -x "${INSTALL_ROOT}/bin/package" ]
unstub curl unstub curl
unstub shasum
} }