Skip to content

Commit

Permalink
[Fix] default_packages: work when the file lacks a trailing newline
Browse files Browse the repository at this point in the history
Fixes #1995.
  • Loading branch information
ljharb committed Feb 2, 2019
1 parent 5c117e6 commit db19450
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
10 changes: 8 additions & 2 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3495,12 +3495,18 @@ nvm() {
}

nvm_get_default_packages() {
if [ -f "${NVM_DIR}/default-packages" ]; then
local NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"
if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then
local DEFAULT_PACKAGES
DEFAULT_PACKAGES=''

# Read lines from $NVM_DIR/default-packages
local line
# ensure a trailing newline
WORK=$(mktemp -d) || exit $?
trap "rm -rf '$WORK'" EXIT
# shellcheck disable=SC1003
sed -e '$a\' "${NVM_DEFAULT_PACKAGE_FILE}" > "${WORK}/default-packages"
while IFS=' ' read -r line; do
# Skip empty lines.
[ -n "${line-}" ] || continue
Expand All @@ -3517,7 +3523,7 @@ nvm_get_default_packages() {
esac

DEFAULT_PACKAGES="${DEFAULT_PACKAGES}${line} "
done < "${NVM_DIR}/default-packages"
done < "${WORK}/default-packages"
echo "${DEFAULT_PACKAGES}" | xargs
fi
}
Expand Down
19 changes: 17 additions & 2 deletions test/fast/Unit tests/nvm_get_default_packages
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,29 @@ cleanup

setup

cat > $FILE << EOF
rimraf
not~a~package~name
mkdirp
EOF
printf %s "$(cat "${FILE}")" > $FILE # strip trailing newline

DEFAULT_PKGS="$(nvm_get_default_packages)"
EXPECTED_PKGS='rimraf not~a~package~name mkdirp'
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "3: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"

cleanup

setup

cat > $FILE << EOF
object-inspect @ 1.0.2
rimraf
EOF

DEFAULT_PKGS="$(nvm_get_default_packages 2>&1 >/dev/null)"
EXPECTED_PKGS="Only one package per line is allowed in the $FILE file. Please remove any lines with multiple space-separated values."
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "3: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"

cleanup

Expand All @@ -69,7 +84,7 @@ rm -rf $FILE

DEFAULT_PKGS="$(nvm_get_default_packages)"
EXPECTED_PKGS=''
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "5: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"

touch $FILE

Expand Down
8 changes: 8 additions & 0 deletions test/installation_node/default-packages
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ object-inspect@1.0.2
# commented-package
stevemao/left-pad
daytime
EOF

printf %s "$(cat "${FILE}")" > $FILE # strip trailing newline

nvm install v6.10.1 2>&1
EXIT_CODE=$?
[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm install v6.10.1' to exit with 0, got $EXIT_CODE"
Expand All @@ -41,6 +44,11 @@ if [ -z "$?" ]; then
die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'rimraf'' to exit with 0, got $?"
fi

nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'daytime'
if [ -z "$?" ]; then
die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'daytime'' to exit with 0, got $?"
fi

cleanup

setup
Expand Down

0 comments on commit db19450

Please sign in to comment.