Skip to content

Commit db19450

Browse files
committed
[Fix] default_packages: work when the file lacks a trailing newline
Fixes #1995.
1 parent 5c117e6 commit db19450

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

nvm.sh

+8-2
Original file line numberDiff line numberDiff line change
@@ -3495,12 +3495,18 @@ nvm() {
34953495
}
34963496

34973497
nvm_get_default_packages() {
3498-
if [ -f "${NVM_DIR}/default-packages" ]; then
3498+
local NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"
3499+
if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then
34993500
local DEFAULT_PACKAGES
35003501
DEFAULT_PACKAGES=''
35013502

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

35193525
DEFAULT_PACKAGES="${DEFAULT_PACKAGES}${line} "
3520-
done < "${NVM_DIR}/default-packages"
3526+
done < "${WORK}/default-packages"
35213527
echo "${DEFAULT_PACKAGES}" | xargs
35223528
fi
35233529
}

test/fast/Unit tests/nvm_get_default_packages

+17-2
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,29 @@ cleanup
5252

5353
setup
5454

55+
cat > $FILE << EOF
56+
rimraf
57+
not~a~package~name
58+
mkdirp
59+
EOF
60+
printf %s "$(cat "${FILE}")" > $FILE # strip trailing newline
61+
62+
DEFAULT_PKGS="$(nvm_get_default_packages)"
63+
EXPECTED_PKGS='rimraf not~a~package~name mkdirp'
64+
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "3: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
65+
66+
cleanup
67+
68+
setup
69+
5570
cat > $FILE << EOF
5671
object-inspect @ 1.0.2
5772
rimraf
5873
EOF
5974

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

6479
cleanup
6580

@@ -69,7 +84,7 @@ rm -rf $FILE
6984

7085
DEFAULT_PKGS="$(nvm_get_default_packages)"
7186
EXPECTED_PKGS=''
72-
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
87+
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "5: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
7388

7489
touch $FILE
7590

test/installation_node/default-packages

+8
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ [email protected]
3030
# commented-package
3131
3232
stevemao/left-pad
33+
daytime
3334
EOF
3435

36+
printf %s "$(cat "${FILE}")" > $FILE # strip trailing newline
37+
3538
nvm install v6.10.1 2>&1
3639
EXIT_CODE=$?
3740
[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm install v6.10.1' to exit with 0, got $EXIT_CODE"
@@ -41,6 +44,11 @@ if [ -z "$?" ]; then
4144
die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'rimraf'' to exit with 0, got $?"
4245
fi
4346

47+
nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'daytime'
48+
if [ -z "$?" ]; then
49+
die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'daytime'' to exit with 0, got $?"
50+
fi
51+
4452
cleanup
4553

4654
setup

0 commit comments

Comments
 (0)