Skip to content

Commit

Permalink
[Tests] make “nvm use iojs” test actually test that thing.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 7, 2016
1 parent 803adac commit 186eb88
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 22 deletions.
46 changes: 46 additions & 0 deletions test/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,49 @@ strip_colors() {
echo "$line" | LC_ALL=C command sed 's/\[[ -?]*[@-~]//g'
done
}

make_echo() {
echo "#!/bin/sh" > "$1"
echo "echo \"${2}\"" > "$1"
chmod a+x "$1"
}

make_fake_node() {
local VERSION
VERSION="${1-}"
[ -n "${VERSION}" ] || return 1

local BIN_PATH
BIN_PATH="$(nvm_version_path "${VERSION}")/bin"
mkdir -p "${BIN_PATH}" || {
echo >&2 'unable to make bin dir'
return 2
}

make_echo "${BIN_PATH}/node" "${VERSION}" || {
echo >&2 'unable to make fake node bin'
return 3
}
}

make_fake_iojs() {
local VERSION
VERSION="${1-}"
[ -n "${VERSION}" ] || return 1

local BIN_PATH
BIN_PATH="$(nvm_version_path "iojs-${VERSION}")/bin"
mkdir -p "${BIN_PATH}" || {
echo >&2 'unable to make bin dir'
return 2
}

make_echo "${BIN_PATH}/node" "${VERSION}" || {
echo >&2 'unable to make fake node bin'
return 3
}
make_echo "${BIN_PATH}/iojs" "${VERSION}" || {
echo >&2 'unable to make fake iojs bin'
return 3
}
}
43 changes: 21 additions & 22 deletions test/fast/Running "nvm use iojs" uses latest io.js version
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
#!/bin/sh

set -ex

. ../common.sh

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

VERSION="v3.99.0"

cleanup() {
rm -rf ../../alias/foo
unset -f make_echo cleanup
rm -rf "$(nvm_version_path "iojs-${VERSION}")"
}

set +ex # TODO
. ../../nvm.sh
set -ex

echo 'foo' > ../../alias/foo

OUTPUT="$(nvm use foo 2>&1)"
EXPECTED_OUTPUT='The alias "foo" leads to an infinite loop. Aborting.'
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "'nvm use foo' did not output '$EXPECTED_OUTPUT'; got '$OUTPUT'"
nvm deactivate || die "unable to deactivate; current: >$(nvm current)<"

EXIT_CODE="$(nvm use foo 2>/dev/null ; echo $?)"
[ "_$EXIT_CODE" = "_8" ] || die "Expected exit code 8; got $EXIT_CODE"
make_fake_iojs "${VERSION}" || die "unable to make_fake_iojs ${VERSION}"

OUTPUT="$(nvm use --silent foo 2>&1)"
EXPECTED_OUTPUT=''
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "'nvm use --silent foo' did not output '$EXPECTED_OUTPUT'; got '$OUTPUT'"
IOJS_VERSION="$(nvm_version iojs)"
[ -n "${IOJS_VERSION}" ] || die 'expected an io.js version; got none'

OUTPUT="$(nvm use foo --silent 2>&1)"
EXPECTED_OUTPUT=''
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "'nvm use foo --silent' did not output '$EXPECTED_OUTPUT'; got '$OUTPUT'"
EXPECTED_OUTPUT="$(nvm_add_iojs_prefix ${VERSION})"
[ "${IOJS_VERSION}" = "${EXPECTED_OUTPUT}" ] || die "iojs version was not >${EXPECTED_OUTPUT}; got >${IOJS_VERSION}<"

EXIT_CODE="$(nvm use --silent foo 2>/dev/null ; echo $?)"
[ "_$EXIT_CODE" = "_8" ] || die "Expected exit code 8 from 'nvm use --silent foo'; got $EXIT_CODE"
nvm use --delete-prefix iojs || die '`nvm use iojs` failed'

EXIT_CODE="$(nvm use foo --silent 2>/dev/null ; echo $?)"
[ "_$EXIT_CODE" = "_8" ] || die "Expected exit code 8 from 'nvm use foo --silent'; got $EXIT_CODE"
CURRENT="$(nvm current)"
echo "current: ${CURRENT}"

cleanup;
[ "${CURRENT}" = "${IOJS_VERSION}" ] || die "expected >${IOJS_VERSION}<; got >${CURRENT}<"

cleanup

0 comments on commit 186eb88

Please sign in to comment.