Commit 14750a0b authored by Jason Karns's avatar Jason Karns
Browse files

Extract has_checksum_support predicate function

- Remove unnecessary HAS_X_SUPPORT variables
- Merge conditional for unsetting mirror-url
- Memoize has_checksum_support function
parent a187b07d
Loading
Loading
Loading
Loading
+18 −28
Original line number Diff line number Diff line
@@ -246,6 +246,12 @@ compute_md5() {
  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() {
  local checksum_command
  local filename="$1"
@@ -255,22 +261,19 @@ verify_checksum() {
  [ -e "$filename" ] || return 0

  case "${#expected_checksum}" in
  0) # If there's no expected checksum, return success
    return 0
    ;;
  32) # MD5
    [ -n "$HAS_MD5_SUPPORT" ] || return 0
    checksum_command="compute_md5"
    ;;
  64) # SHA2 256
    [ -n "$HAS_SHA2_SUPPORT" ] || return 0
    checksum_command="compute_sha2"
    ;;
  *) # unknown checksum algorithm, return failure
    return 1
    ;;
  # If expected checksum is empty, return success
  0) return 0 ;;
  # MD5
  32) checksum_command="compute_md5" ;;
  # SHA2 256
  64) checksum_command="compute_sha2" ;;
  # unknown checksum algorithm, return failure
  *) return 1 ;;
  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
  local computed_checksum=`echo "$($checksum_command < "$filename")" | tr [A-Z] [a-z]`
  [ -n "$computed_checksum" ] || return 1
@@ -1225,23 +1228,10 @@ else
  RUBY_BUILD_DEFAULT_MIRROR=
fi

if [ -n "$RUBY_BUILD_SKIP_MIRROR" ]; then
  unset RUBY_BUILD_MIRROR_URL
fi

if echo test | compute_sha2 >/dev/null; then
  HAS_SHA2_SUPPORT=1
else
  unset HAS_SHA2_SUPPORT
if [ -n "$RUBY_BUILD_SKIP_MIRROR" ] || ! has_checksum_support compute_sha2; then
  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").$$"
LOG_PATH="${TMP}/ruby-build.${SEED}.log"
RUBY_BIN="${PREFIX_PATH}/bin/ruby"
+0 −6
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@ setup() {


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

  install_fixture definitions/without-checksum
@@ -19,12 +18,10 @@ setup() {
  [ -e "${RUBY_BUILD_CACHE_PATH}/package-1.0.0.tar.gz" ]

  unstub curl
  unstub shasum
}


@test "cached package without checksum" {
  stub shasum true
  stub curl

  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" ]

  unstub curl
  unstub shasum
}


@@ -79,7 +75,6 @@ setup() {


@test "nonexistent cache directory is ignored" {
  stub shasum true
  stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"

  export RUBY_BUILD_CACHE_PATH="${TMP}/nonexistent"
@@ -91,5 +86,4 @@ setup() {
  [ ! -d "$RUBY_BUILD_CACHE_PATH" ]

  unstub curl
  unstub shasum
}
+0 −2
Original line number Diff line number Diff line
@@ -6,7 +6,6 @@ export RUBY_BUILD_CACHE_PATH=


@test "package URL without checksum" {
  stub shasum true
  stub curl "-q -o * -*S* http://example.com/* : cp $FIXTURE_ROOT/\${5##*/} \$3"

  install_fixture definitions/without-checksum
@@ -14,7 +13,6 @@ export RUBY_BUILD_CACHE_PATH=
  [ -x "${INSTALL_ROOT}/bin/package" ]

  unstub curl
  unstub shasum
}