From d396181ffba9a4f5addb24900f88c4b9308c8b54 Mon Sep 17 00:00:00 2001 From: nmarghetti Date: Sat, 2 Jan 2021 00:03:24 +0100 Subject: [PATCH] [Refactor] add `nvm_get_artifact_compression` --- nvm.sh | 30 ++++++++++++++----- .../Unit tests/nvm_get_artifact_compression | 16 ++++++++++ test/fast/Unit tests/nvm_get_checksum | 4 +-- 3 files changed, 40 insertions(+), 10 deletions(-) create mode 100755 test/fast/Unit tests/nvm_get_artifact_compression diff --git a/nvm.sh b/nvm.sh index e0eccfe..a50879f 100644 --- a/nvm.sh +++ b/nvm.sh @@ -1559,7 +1559,7 @@ nvm_get_checksum() { SHASUMS_URL="${MIRROR}/${3}/SHASUMS.txt" fi - nvm_download -L -s "${SHASUMS_URL}" -o - | command awk "{ if (\"${4}.tar.${5}\" == \$2) print \$1}" + nvm_download -L -s "${SHASUMS_URL}" -o - | command awk "{ if (\"${4}.${5}\" == \$2) print \$1}" } nvm_print_versions() { @@ -1983,6 +1983,22 @@ nvm_get_download_slug() { fi } +nvm_get_artifact_compression() { + local VERSION + VERSION="${1-}" + + local NVM_OS + NVM_OS="$(nvm_get_os)" + + local COMPRESSION + COMPRESSION='tar.gz' + if nvm_supports_xz "${VERSION}"; then + COMPRESSION='tar.xz' + fi + + nvm_echo "${COMPRESSION}" +} + # args: flavor, kind, type, version nvm_download_artifact() { local FLAVOR @@ -2029,10 +2045,7 @@ nvm_download_artifact() { SLUG="$(nvm_get_download_slug "${FLAVOR}" "${KIND}" "${VERSION}")" local COMPRESSION - COMPRESSION='gz' - if nvm_supports_xz "${VERSION}"; then - COMPRESSION='xz' - fi + COMPRESSION="$(nvm_get_artifact_compression "${VERSION}")" local CHECKSUM CHECKSUM="$(nvm_get_checksum "${FLAVOR}" "${TYPE}" "${VERSION}" "${SLUG}" "${COMPRESSION}")" @@ -2049,13 +2062,13 @@ nvm_download_artifact() { ) local TARBALL - TARBALL="${tmpdir}/${SLUG}.tar.${COMPRESSION}" + TARBALL="${tmpdir}/${SLUG}.${COMPRESSION}" local TARBALL_URL if nvm_version_greater_than_or_equal_to "${VERSION}" 0.1.14; then - TARBALL_URL="${MIRROR}/${VERSION}/${SLUG}.tar.${COMPRESSION}" + TARBALL_URL="${MIRROR}/${VERSION}/${SLUG}.${COMPRESSION}" else # node <= 0.1.13 does not have a directory - TARBALL_URL="${MIRROR}/${SLUG}.tar.${COMPRESSION}" + TARBALL_URL="${MIRROR}/${SLUG}.${COMPRESSION}" fi if [ -r "${TARBALL}" ]; then @@ -3944,6 +3957,7 @@ nvm() { nvm_npmrc_bad_news_bears \ nvm_get_colors nvm_set_colors nvm_print_color_code nvm_format_help_message_colors \ nvm_echo_with_colors nvm_err_with_colors \ + nvm_get_artifact_compression \ >/dev/null 2>&1 unset NVM_RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \ NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \ diff --git a/test/fast/Unit tests/nvm_get_artifact_compression b/test/fast/Unit tests/nvm_get_artifact_compression new file mode 100755 index 0000000..ed7a180 --- /dev/null +++ b/test/fast/Unit tests/nvm_get_artifact_compression @@ -0,0 +1,16 @@ +#!/bin/sh + +cleanup () { + unset -f die cleanup +} +die () { echo "$@" ; cleanup ; exit 1; } + +\. ../../../nvm.sh + +# nvm_get_artifact_compression by default +[ "$(nvm_get_artifact_compression)" = 'tar.gz' ] || die 'nvm_get_artifact_compression should return "tar.gz" by default' + +# nvm_get_artifact_compression with xz +[ "$(nvm_get_artifact_compression "14.0.0")" = 'tar.xz' ] || die 'nvm_get_artifact_compression should return "tar.xz" for this version' + +cleanup diff --git a/test/fast/Unit tests/nvm_get_checksum b/test/fast/Unit tests/nvm_get_checksum index 53e6839..975a3e6 100755 --- a/test/fast/Unit tests/nvm_get_checksum +++ b/test/fast/Unit tests/nvm_get_checksum @@ -31,14 +31,14 @@ nvm_download() { nvm_get_checksum_alg() { echo 'sha-256' } -OUTPUT="$(nvm_get_checksum node std foo bar baz)" +OUTPUT="$(nvm_get_checksum node std foo bar tar.baz)" EXPECTED_OUTPUT="mirror-node-std/foo/SHASUMS256.txt" [ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" nvm_get_checksum_alg() { echo 'sha-1' } -OUTPUT="$(nvm_get_checksum iojs std foo bar baz)" +OUTPUT="$(nvm_get_checksum iojs std foo bar tar.baz)" EXPECTED_OUTPUT="mirror-iojs-std/foo/SHASUMS.txt" [ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"