Skip to content
Myr

[Fix] `nvm_get_default_packages`: use portable awk patterns

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Replace POSIX `[[:space:]]` character class with `[ \t]` for mawk compatibility on Ubuntu 16.04.

authored by Jordan Harband · committed by Jordan Harband · parent aa1d829 · Browse files · 2 files+60 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+60 added−3 removed

  • tests:+57 added−0 removed
  • other:+3 added−3 removed

2 files+60 added−3 removed

added:test/fast/Unit tests/nvm_get_default_packages mawk compat+57 added
1#!/bin/sh23# Test that nvm_get_default_packages awk patterns work with mawk4# This test runs with mawk explicitly if available, to catch POSIX5# character class compatibility issues (mawk doesn't support [[:space:]])67die () { echo "$@" ; cleanup ; exit 1; }89\. ../../../nvm.sh1011# The awk command from nvm_get_default_packages12AWK_SCRIPT='13 /^[ \t]*#/ { next }14 /^[ \t]*$/ { next }15 /[ \t]/ && !/^[ \t]*#/ {16 print "error" > "/dev/stderr"17 exit 118 }19 {20 if (NR > 1 && !prev_space) printf " "21 printf "%s", $022 prev_space = 023 }24'2526TEST_INPUT="rimraf27object-inspect@1.0.22829# commented-package3031stevemao/left-pad"3233EXPECTED_OUTPUT="rimraf object-inspect@1.0.2 stevemao/left-pad"3435# Test with system awk36OUTPUT="$(printf '%s\n' "${TEST_INPUT}" | awk "${AWK_SCRIPT}")"37[ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "system awk: expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"3839# Test with mawk explicitly if available40if command -v mawk > /dev/null 2>&1; then41 OUTPUT="$(printf '%s\n' "${TEST_INPUT}" | mawk "${AWK_SCRIPT}")"42 [ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "mawk: expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"43 echo "mawk test passed"44else45 echo "mawk not available, skipping mawk-specific test"46fi4748# Test with gawk explicitly if available49if command -v gawk > /dev/null 2>&1; then50 OUTPUT="$(printf '%s\n' "${TEST_INPUT}" | gawk "${AWK_SCRIPT}")"51 [ "${OUTPUT}" = "${EXPECTED_OUTPUT}" ] || die "gawk: expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<"52 echo "gawk test passed"53else54 echo "gawk not available, skipping gawk-specific test"55fi5657echo "All awk compatibility tests passed"
modified:nvm.sh+3 added-3 removed
4528 unmodified lines
4529 local NVM_DEFAULT_PACKAGE_FILE4530 NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"4531 if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then4532 command awk -v filename="${NVM_DEFAULT_PACKAGE_FILE}" '4533 /^[[:space:]]*#/ { next } # Skip lines that begin with #4534 /^[[:space:]]*$/ { next } # Skip empty lines4535 /[[:space:]]/ && !/^[[:space:]]*#/ {4533 /^[ \t]*#/ { next } # Skip lines that begin with #4534 /^[ \t]*$/ { next } # Skip empty lines4535 /[ \t]/ && !/^[ \t]*#/ {4536 print "Only one package per line is allowed in `" filename "`. Please remove any lines with multiple space-separated values." > "/dev/stderr"4537 err = 14538 exit 14539 }
138 unmodified lines