Skip to content

nodejs: improve nvm error handling #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion nodejs/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,24 @@ fi
export NVM_DIR=${HOME}/.nvm
[ ! -e ${NVM_DIR} ] && mkdir -p ${NVM_DIR}

set +e
. /etc/nvm/nvm.sh
nvm_source_exit_code="$?"
set -e

nvm install ${NODE_VERSION}
if [[ "$nvm_source_exit_code" != "0" ]]; then
echo "WARNING: sourcing nvm.sh returned exit status ${nvm_source_exit_code}. This may not be a problem but report this message if the deploy fails."
fi

set +e
nvm install "${NODE_VERSION}"
nvm_install_exit_code="$?"
set -e

if [[ "$nvm_install_exit_code" != "0" ]]; then
echo "ERROR: \`nvm install \"${NODE_VERSION}\"\` returned exit status ${nvm_install_exit_code}."
exit "${nvm_install_exit_code}"
fi

rm -f ~/.nvm_bin
ln -s $NVM_BIN ~/.nvm_bin
Expand Down
6 changes: 1 addition & 5 deletions nodejs/install
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
SOURCE_DIR=/var/lib/tsuru
source ${SOURCE_DIR}/base/rc/config

apt-get update
apt-get install -y --no-install-recommends git

git clone https://github.com/creationix/nvm.git /etc/nvm
cd /etc/nvm && git checkout `git describe --abbrev=0 --tags`
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.7/install.sh | NVM_DIR=/etc/nvm bash

cat >> ${HOME}/.profile <<EOF
if [ -e ${HOME}/.nvm_bin ]; then
Expand Down
46 changes: 46 additions & 0 deletions tests/nodejs/tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,49 @@ EOF
run /home/ubuntu/.nvm_bin/yarn --version
[[ "$output" == *"0.17.0"* ]]
}

@test "reads node version from .nvmrc" {
echo "v8.9.0" >${CURRENT_DIR}/.nvmrc
touch ${CURRENT_DIR}/yarn.lock
run /var/lib/tsuru/deploy
[ "$status" -eq 0 ]
run node --version
[[ "$output" == "v8.9.0" ]]
}

@test "reads node version from .node-version" {
echo "v8.9.1" >${CURRENT_DIR}/.node-version
touch ${CURRENT_DIR}/yarn.lock
run /var/lib/tsuru/deploy
[ "$status" -eq 0 ]
run node --version
[[ "$output" == "v8.9.1" ]]
}

@test "reads node version from package.json" {
cat <<EOF>>${CURRENT_DIR}/package.json
{
"name": "hello-world",
"description": "hello world test on tsuru",
"version": "0.0.1",
"private": true,
"engines": {
"node": "v8.9.2"
}
}
EOF
touch ${CURRENT_DIR}/yarn.lock
run /var/lib/tsuru/deploy
[ "$status" -eq 0 ]
run node --version
[[ "$output" == "v8.9.2" ]]
}

@test "breaks with invalid node version" {
echo "myinvalidversion" >${CURRENT_DIR}/.node-version
touch ${CURRENT_DIR}/yarn.lock
run /var/lib/tsuru/deploy
[ "$status" -ne 0 ]
[[ "$output" == *"Version 'myinvalidversion' not found"* ]]
[[ "$output" == *"ERROR: \`nvm install \"myinvalidversion\"\` returned exit status"* ]]
}
5 changes: 3 additions & 2 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ parent_path=$( cd "$(dirname "${BASH_SOURCE}")" ; pwd -P )

cd "$parent_path"

has_parallel=$(which parallel)

if [ ! -z $1 ]; then
platforms=$1
has_parallel=""
else
platforms=$(ls -d */ | cut -f1 -d'/' | grep -v common)
fi
Expand All @@ -29,8 +32,6 @@ function run_test {
}
export -f run_test

has_parallel=$(which parallel)

for plat in $platforms; do
if [ "${has_parallel}" ]; then
parallel --semaphore -j 10 run_test $plat
Expand Down