Skip to content
Myr

[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

2 files+52 added−6 removed

modified:test/fast/Unit tests/nvm_ls handles hash in pattern+29 added-1 removed
1#!/bin/sh23die () { echo "$@" ; exit 1; }3die () { echo "$@" ; cleanup ; exit 1; }45cleanup() {6 unset NVMRC_CONTENT OUTPUT EXIT_CODE TEST_DIR7 rm -rf "${TEST_DIR-}"8}910: nvm.sh11\. ../../../nvm.sh1213# Test: nvm_ls with pattern containing # should not cause sed error
12 unmodified lines
2627# 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"29echo "$OUTPUT" | grep -q "N/A" || die "nvm_ls 'foo#bar' should output N/A, got: $OUTPUT"3031TEST_DIR="${PWD}/nvm_ls_hash_pattern_tmp"32mkdir -p "${TEST_DIR}/versions/node/v24.13.0/bin" || die 'failed to create test version dir'33printf '#!/bin/sh\nprintf v24.13.0\\\\n\n' > "${TEST_DIR}/versions/node/v24.13.0/bin/node" || die 'failed to create test node binary'34chmod +x "${TEST_DIR}/versions/node/v24.13.0/bin/node" || die 'failed to chmod test node binary'3536NVM_DIR="${TEST_DIR}"37NVMRC_CONTENT='v24.13.038# krypton is the codename for Node.js v24.x'3940OUTPUT="$(nvm_ls "${NVMRC_CONTENT}" 2>&1)"41EXIT_CODE=$?4243echo "$OUTPUT" | grep -q "unterminated regular expression" && \44 die "nvm_ls with .nvmrc comments caused sed 'unterminated regular expression' error: $OUTPUT"4546[ "$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"4849OUTPUT="$(nvm_version "${NVMRC_CONTENT}" 2>&1)"50[ "$OUTPUT" = "v24.13.0" ] || die "nvm_version with .nvmrc comments should output v24.13.0, got: $OUTPUT"5152cleanup
modified:nvm.sh+23 added-5 removed
558 unmodified lines
559560$(nvm_wrap_with_color_code 'y' "${warn_text}")"561}562563nvm_process_nvmrc() {564 local NVMRC_PATH565 NVMRC_PATH="$1"563nvm_process_nvmrc_content() {564 local NVMRC_CONTENT565 NVMRC_CONTENT="${1-}"566 local lines567568 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 '^$')569570 if [ -z "$lines" ]; then571 nvm_nvmrc_invalid_msg "${lines}"572 return 1
55 unmodified lines
628629 nvm_echo "${unpaired_line}"630}631632nvm_process_nvmrc() {633 local NVMRC_PATH634 NVMRC_PATH="$1"635636 nvm_process_nvmrc_content "$(command cat "${NVMRC_PATH}")"637}638639nvm_rc_version() {640 local NVMRC_PATH641 NVMRC_PATH="$(nvm_find_nvmrc)"642 if [ ! -e "${NVMRC_PATH}" ]; then
849 unmodified lines
14921493nvm_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_current
3223 unmodified lines
4734 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