[Fix] Parse commented nvmrc content in nvm_ls
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #3761
authored by jinhyuk9714 · committed by Jordan Harband · parent ab77712 · Browse files · 2 files·+52 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: jinhyuk9714 <jinhyuk9714@gmail.com>
Diff stats
2 files+52 added−6 removed
- tests:+29 added−1 removed
- other:+23 added−5 removed
Provenance
- Commit
82315e2- Tree
dc99403- Author
jinhyuk9714 <jinhyuk9714@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent
ab77712
2 files changed+52 added−6 removed
modified:test/fast/Unit tests/nvm_ls handles hash in pattern+29 added-1 removed
1 #!/bin/sh2 3-die () { echo "$@" ; exit 1; }3+die () { echo "$@" ; cleanup ; exit 1; }4 5+cleanup() {6+ unset NVMRC_CONTENT OUTPUT EXIT_CODE TEST_DIR7+ rm -rf "${TEST_DIR-}"8+}9+10 : nvm.sh11 \. ../../../nvm.sh12 13 # Test: nvm_ls with pattern containing # should not cause sed error12 unmodified lines26 27 # Should return N/A with exit code 3 (not found)28 [ "$EXIT_CODE" = "3" ] || die "nvm_ls 'foo#bar' should exit with code 3, got $EXIT_CODE"29 echo "$OUTPUT" | grep -q "N/A" || die "nvm_ls 'foo#bar' should output N/A, got: $OUTPUT"30+31+TEST_DIR="${PWD}/nvm_ls_hash_pattern_tmp"32+mkdir -p "${TEST_DIR}/versions/node/v24.13.0/bin" || die 'failed to create test version dir'33+printf '#!/bin/sh\nprintf v24.13.0\\\\n\n' > "${TEST_DIR}/versions/node/v24.13.0/bin/node" || die 'failed to create test node binary'34+chmod +x "${TEST_DIR}/versions/node/v24.13.0/bin/node" || die 'failed to chmod test node binary'35+36+NVM_DIR="${TEST_DIR}"37+NVMRC_CONTENT='v24.13.038+# krypton is the codename for Node.js v24.x'39+40+OUTPUT="$(nvm_ls "${NVMRC_CONTENT}" 2>&1)"41+EXIT_CODE=$?42+43+echo "$OUTPUT" | grep -q "unterminated regular expression" && \44+ die "nvm_ls with .nvmrc comments caused sed 'unterminated regular expression' error: $OUTPUT"45+46+[ "$EXIT_CODE" = "0" ] || die "nvm_ls with .nvmrc comments should exit with code 0, got $EXIT_CODE"47+[ "$OUTPUT" = "v24.13.0" ] || die "nvm_ls with .nvmrc comments should output v24.13.0, got: $OUTPUT"48+49+OUTPUT="$(nvm_version "${NVMRC_CONTENT}" 2>&1)"50+[ "$OUTPUT" = "v24.13.0" ] || die "nvm_version with .nvmrc comments should output v24.13.0, got: $OUTPUT"51+52+cleanupmodified:nvm.sh+23 added-5 removed
558 unmodified lines559 560 $(nvm_wrap_with_color_code 'y' "${warn_text}")"561 }562 563-nvm_process_nvmrc() {564- local NVMRC_PATH565- NVMRC_PATH="$1"563+nvm_process_nvmrc_content() {564+ local NVMRC_CONTENT565+ NVMRC_CONTENT="${1-}"566 local lines567 568- lines=$(command sed 's/#.*//' "$NVMRC_PATH" | command sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | nvm_grep -v '^$')568+ lines=$(nvm_echo "${NVMRC_CONTENT}" | command sed 's/#.*//' | command sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | nvm_grep -v '^$')569 570 if [ -z "$lines" ]; then571 nvm_nvmrc_invalid_msg "${lines}"572 return 155 unmodified lines628 629 nvm_echo "${unpaired_line}"630 }631 632+nvm_process_nvmrc() {633+ local NVMRC_PATH634+ NVMRC_PATH="$1"635+636+ nvm_process_nvmrc_content "$(command cat "${NVMRC_PATH}")"637+}638+639 nvm_rc_version() {640 local NVMRC_PATH641 NVMRC_PATH="$(nvm_find_nvmrc)"642 if [ ! -e "${NVMRC_PATH}" ]; then849 unmodified lines1492 1493 nvm_ls() {1494 local PATTERN1495 PATTERN="${1-}"1496+ case "${PATTERN}" in1497+ *'#'* | *'1498+'*)1499+ local NVMRC_PATTERN1500+ if ! NVMRC_PATTERN="$(nvm_process_nvmrc_content "${PATTERN}" 2>/dev/null)"; then1501+ nvm_echo 'N/A'1502+ return 31503+ fi1504+ PATTERN="${NVMRC_PATTERN}"1505+ ;;1506+ esac1507 local VERSIONS1508 VERSIONS=''1509 if [ "${PATTERN}" = 'current' ]; then1510 nvm_ls_current3223 unmodified lines4734 nvm_npmrc_bad_news_bears nvm_sanitize_auth_header \4735 nvm_get_colors nvm_set_colors nvm_print_color_code nvm_wrap_with_color_code nvm_format_help_message_colors \4736 nvm_echo_with_colors nvm_err_with_colors \4737 nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \4720- nvm_process_nvmrc nvm_nvmrc_invalid_msg \4738+ nvm_process_nvmrc nvm_process_nvmrc_content nvm_nvmrc_invalid_msg \4739 nvm_write_nvmrc \4740 >/dev/null 2>&14741 unset NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \4742 NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS \171 unmodified lines