[Fix] nvm_get_default_packages: use portable awk patterns

Replace POSIX `[[:space:]]` character class with `[ \t]` for
mawk compatibility on Ubuntu 16.04.
This commit is contained in:
Jordan Harband
2026-01-26 16:59:11 -08:00
parent f05be89217
commit 92e0ec2ac2
2 changed files with 60 additions and 3 deletions

6
nvm.sh
View File

@@ -4530,9 +4530,9 @@ nvm_get_default_packages() {
NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"
if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then
command awk -v filename="${NVM_DEFAULT_PACKAGE_FILE}" '
/^[[:space:]]*#/ { next } # Skip lines that begin with #
/^[[:space:]]*$/ { next } # Skip empty lines
/[[:space:]]/ && !/^[[:space:]]*#/ {
/^[ \t]*#/ { next } # Skip lines that begin with #
/^[ \t]*$/ { next } # Skip empty lines
/[ \t]/ && !/^[ \t]*#/ {
print "Only one package per line is allowed in `" filename "`. Please remove any lines with multiple space-separated values." > "/dev/stderr"
err = 1
exit 1

View File

@@ -0,0 +1,57 @@
#!/bin/sh
# Test that nvm_get_default_packages awk patterns work with mawk
# This test runs with mawk explicitly if available, to catch POSIX
# character class compatibility issues (mawk doesn't support [[:space:]])
die () { echo "$@" ; cleanup ; exit 1; }
\. ../../../nvm.sh
# The awk command from nvm_get_default_packages
AWK_SCRIPT='
/^[ \t]*#/ { next }
/^[ \t]*$/ { next }
/[ \t]/ && !/^[ \t]*#/ {
print "error" > "/dev/stderr"
exit 1
}
{
if (NR > 1 && !prev_space) printf " "
printf "%s", $0
prev_space = 0
}
'
TEST_INPUT="rimraf
object-inspect@1.0.2
# commented-package
stevemao/left-pad"
EXPECTED_OUTPUT="rimraf object-inspect@1.0.2 stevemao/left-pad"
# Test with system awk
OUTPUT="$(printf '%s\n' "${TEST_INPUT}" | awk "${AWK_SCRIPT}")"
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "system awk: expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"
# Test with mawk explicitly if available
if command -v mawk > /dev/null 2>&1; then
OUTPUT="$(printf '%s\n' "${TEST_INPUT}" | mawk "${AWK_SCRIPT}")"
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "mawk: expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"
echo "mawk test passed"
else
echo "mawk not available, skipping mawk-specific test"
fi
# Test with gawk explicitly if available
if command -v gawk > /dev/null 2>&1; then
OUTPUT="$(printf '%s\n' "${TEST_INPUT}" | gawk "${AWK_SCRIPT}")"
[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "gawk: expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"
echo "gawk test passed"
else
echo "gawk not available, skipping gawk-specific test"
fi
echo "All awk compatibility tests passed"