From 38ffc713a72682e3bd9fdb478ac44f550975a8ff Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 26 Jan 2026 16:59:11 -0800 Subject: [PATCH] [Fix] `nvm_get_default_packages`: use portable awk patterns Replace POSIX `[[:space:]]` character class with `[ \t]` for mawk compatibility on Ubuntu 16.04. --- nvm.sh | 6 +- .../nvm_get_default_packages mawk compat | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100755 test/fast/Unit tests/nvm_get_default_packages mawk compat diff --git a/nvm.sh b/nvm.sh index fe95895..effb506 100755 --- a/nvm.sh +++ b/nvm.sh @@ -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 diff --git a/test/fast/Unit tests/nvm_get_default_packages mawk compat b/test/fast/Unit tests/nvm_get_default_packages mawk compat new file mode 100755 index 0000000..f6fa5d7 --- /dev/null +++ b/test/fast/Unit tests/nvm_get_default_packages mawk compat @@ -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"