Skip to content

Commit

Permalink
Merge pull request #599 from ljharb/nvm_get_latest
Browse files Browse the repository at this point in the history
Add `nvm_get_latest`

This relates to #522, #561, #400.

`nvm_get_latest` uses http://latest.nvm.sh which redirects to https://github.com/creationix/nvm/releases/latest which in turn redirects to the latest release tag.
  • Loading branch information
ljharb committed Dec 22, 2014
2 parents c0a20f2 + 25c6159 commit 0cb8c9a
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
18 changes: 18 additions & 0 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ nvm_has() {
type "$1" > /dev/null 2>&1
}

nvm_get_latest() {
local NVM_LATEST_URL
if nvm_has "curl"; then
NVM_LATEST_URL="$(curl -w "%{url_effective}\n" -L -s -S http://latest.nvm.sh -o /dev/null)"
elif nvm_has "wget"; then
NVM_LATEST_URL="$(wget http://latest.nvm.sh --server-response -O /dev/null 2>&1 | awk '/^ Location: /{DEST=$2} END{ print DEST }')"
else
>&2 echo 'nvm needs curl or wget to proceed.'
return 1
fi
if [ "_$NVM_LATEST_URL" = "_" ]; then
>&2 echo "http://latest.nvm.sh did not redirect to the latest release on Github"
return 2
else
echo "$NVM_LATEST_URL" | awk -F'/' '{print $NF}'
fi
}

nvm_download() {
if nvm_has "curl"; then
curl $*
Expand Down
20 changes: 20 additions & 0 deletions test/fast/Unit tests/nvm_get_latest missing curl or wget
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

die () { echo $@ ; cleanup ; exit 1; }

cleanup() {
unset -f nvm_has
}

. ../../../nvm.sh

nvm_has() { return 1 ; }

OUTPUT="$(nvm_get_latest 2>&1)"
EXIT_CODE="$(nvm_get_latest >/dev/null 2>&1 ; echo $?)"
[ "_$OUTPUT" = "_nvm needs curl or wget to proceed." ] \
|| die "no curl/wget did not report correct error message, got '$OUTPUT'"
[ "_$EXIT_CODE" = "_1" ] \
|| die "no curl/wget did not exit with code 1, got $EXIT_CODE"

cleanup
117 changes: 117 additions & 0 deletions test/installation/nvm_get_latest/nvm_get_latest
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/bin/sh

die () { echo $@ ; cleanup ; exit 1; }

cleanup() {
unset -f curl wget nvm_has
}

. ../../../nvm.sh

EXPECTED_VERSION="v12.3.456"
URL="https://github.com/creationix/nvm/releases/tag/$EXPECTED_VERSION"
EXPECTED_CURL_ARGS="-w %{url_effective}\n -L -s -S http://latest.nvm.sh -o /dev/null"
EXPECTED_WGET_ARGS="http://latest.nvm.sh --server-response -O /dev/null"

curl() {
if [ "_$*" != "_$EXPECTED_CURL_ARGS" ]; then
echo 2>& "expected args ($EXPECTED_CURL_ARGS), got ($*)"
return 1
else
echo $URL
fi
}
wget() {
if [ "_$*" != "_$EXPECTED_WGET_ARGS" ]; then
echo 2>& "expected args ($EXPECTED_WGET_ARGS), got ($*)"
return 1
else
local WGET_CONTENTS
WGET_CONTENTS="
--2014-12-21 18:11:14-- http://latest.nvm.sh/
Resolving latest.nvm.sh... 50.31.209.229
Connecting to latest.nvm.sh|50.31.209.229|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 301 Moved Permanently
Location: https://github.com/creationix/nvm/releases/latest
Content-Type: text/html; charset=utf-8
Content-Length: 84
Date: Mon, 22 Dec 2014 02:11:15 GMT
Location: https://github.com/creationix/nvm/releases/latest [following]
--2014-12-21 18:11:15-- https://github.com/creationix/nvm/releases/latest
Resolving github.com... 192.30.252.130
Connecting to github.com|192.30.252.130|:443... connected.
HTTP request sent, awaiting response...
HTTP/1.1 302 Found
Server: GitHub.com
Date: Mon, 22 Dec 2014 02:11:15 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Status: 302 Found
X-XSS-Protection: 1; mode=block
X-Frame-Options: deny
Content-Security-Policy: default-src *; script-src assets-cdn.github.com collector-cdn.github.com; object-src assets-cdn.github.com; style-src 'self' 'unsafe-inline' 'unsafe-eval' assets-cdn.github.com; img-src 'self' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.githubusercontent.com *.gravatar.com *.wp.com; media-src 'none'; frame-src 'self' render.githubusercontent.com gist.github.com www.youtube.com player.vimeo.com checkout.paypal.com; font-src assets-cdn.github.com; connect-src 'self' ghconduit.com:25035 live.github.com uploads.github.com www.google-analytics.com s3.amazonaws.com
Cache-Control: no-cache
Vary: X-PJAX
Location: $URL
X-UA-Compatible: IE=Edge,chrome=1
Set-Cookie: logged_in=no; domain=.github.com; path=/; expires=Fri, 22-Dec-2034 02:11:15 GMT; secure; HttpOnly
Set-Cookie: _gh_sess=eyJzZXNzaW9uX2lkIjoiNTMzNGNjZWUxM2VhZjNhN2M3MzIwZWUxNGYwNzhmNDkiLCJzcHlfcmVwbyI6ImNyZWF0aW9uaXgvbnZtIiwic3B5X3JlcG9fYXQiOjE0MTkyMTQyNzV9--e2fa4cf5305d61aa58c0e0bf21fdb335a9660dcf; path=/; secure; HttpOnly
X-Request-Id: 4b99bf200157dd845f76ab83e4093acc
X-Runtime: 0.030872
X-Rack-Cache: miss
X-GitHub-Request-Id: 45B56780:3913:880EF6:54977DC3
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Content-Type-Options: nosniff
Vary: Accept-Encoding
X-Served-By: ef97014f01ea59c1ef337fe51a4d0331
Location: $URL [following]
--2014-12-21 18:11:15-- $URL
Reusing existing connection to github.com:443.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Server: GitHub.com
Date: Mon, 22 Dec 2014 02:11:15 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Status: 200 OK
X-XSS-Protection: 1; mode=block
X-Frame-Options: deny
Content-Security-Policy: default-src *; script-src assets-cdn.github.com collector-cdn.github.com; object-src assets-cdn.github.com; style-src 'self' 'unsafe-inline' 'unsafe-eval' assets-cdn.github.com; img-src 'self' data: assets-cdn.github.com identicons.github.com www.google-analytics.com collector.githubapp.com *.githubusercontent.com *.gravatar.com *.wp.com; media-src 'none'; frame-src 'self' render.githubusercontent.com gist.github.com www.youtube.com player.vimeo.com checkout.paypal.com; font-src assets-cdn.github.com; connect-src 'self' ghconduit.com:25035 live.github.com uploads.github.com www.google-analytics.com s3.amazonaws.com
Cache-Control: no-cache, private
Vary: X-PJAX
X-UA-Compatible: IE=Edge,chrome=1
Set-Cookie: _gh_sess=eyJzZXNzaW9uX2lkIjoiNTMzNGNjZWUxM2VhZjNhN2M3MzIwZWUxNGYwNzhmNDkiLCJzcHlfcmVwbyI6ImNyZWF0aW9uaXgvbnZtIiwic3B5X3JlcG9fYXQiOjE0MTkyMTQyNzUsIl9jc3JmX3Rva2VuIjoiemZTVDNIRGo0QzF0dzkyNXp6NFBRdGJVbTl4NSsxTGo1cngwVFQ3NDVwdz0ifQ%3D%3D--b72bfd5241907dcf557b226e74351ff39f0e9ede; path=/; secure; HttpOnly
X-Request-Id: 305b0d158bf8c0b3fa488a33d7687091
X-Runtime: 0.038544
X-Rack-Cache: miss
X-GitHub-Request-Id: 45B56780:3913:880F19:54977DC3
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Content-Type-Options: nosniff
Vary: Accept-Encoding
X-Served-By: 926b734ea1992f8ee1f88ab967a93dac
Length: unspecified [text/html]
Saving to: ‘/dev/null’
0K .......... ......... 225K=0.09s
2014-12-21 18:11:15 (225 KB/s) - ‘/dev/null’ saved [20298]
"
"$WGET_CONTENTS" | while read line
do
2>& echo "$line"
done
fi
}

OUTPUT="$(nvm_get_latest)"
EXIT_CODE="$(nvm_get_latest >/dev/null 2>&1 ; echo $?)"
[ "_$OUTPUT" = "_$EXPECTED_VERSION" ] \
|| die "success path did not return version '$EXPECTED_VERSION', got '$OUTPUT'"
[ "_$EXIT_CODE" = "_0" ] \
|| die "success path did not exit with code 0, got $EXIT_CODE"


cleanup

26 changes: 26 additions & 0 deletions test/installation/nvm_get_latest/nvm_get_latest failed redirect
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

die () { echo $@ ; cleanup ; exit 1; }

cleanup() {
unset -f curl wget
}

. ../../../nvm.sh

curl() {
return 1
}
wget() {
return 1
}

OUTPUT="$(nvm_get_latest 2>&1)"
EXIT_CODE="$(nvm_get_latest >/dev/null 2>&1 ; echo $?)"
[ "_$OUTPUT" = "_http://latest.nvm.sh did not redirect to the latest release on Github" ] \
|| die "failed redirect did not report correct error message, got '$OUTPUT'"
[ "_$EXIT_CODE" = "_2" ] \
|| die "failed redirect did not exit with code 2, got $EXIT_CODE"

cleanup

0 comments on commit 0cb8c9a

Please sign in to comment.