diff --git a/nvm.sh b/nvm.sh index 7c183b83..cee89bd9 100755 --- a/nvm.sh +++ b/nvm.sh @@ -893,9 +893,10 @@ all non-commented content (anything after # is a comment) must be either: additionally, a single bare nvm-recognized version-ish must be present (after stripping comments)." # show control characters (other than tabs and newlines) as `?`, so the parsed content is printed as plain text + # and only show an excerpt: a real .nvmrc is short, and showing all of a long file only helps to disclose it local warn_text warn_text="non-commented content parsed: -$(nvm_echo "${1}" | command tr '\001-\010\013-\037\177' '??????????????????????????????')" +$(nvm_echo "${1-}" | command tr '\001-\010\013-\037\177' '??????????????????????????????' | command awk 'NR > 10 { print "[...]"; exit } { if (length($0) > 100) { print substr($0, 1, 100) "[...]" } else { print } }')" nvm_err "$(nvm_wrap_with_color_code 'r' "${error_text}") diff --git a/test/fast/Unit tests/nvm_nvmrc_invalid_msg shows a bounded excerpt b/test/fast/Unit tests/nvm_nvmrc_invalid_msg shows a bounded excerpt new file mode 100755 index 00000000..d0550bfd --- /dev/null +++ b/test/fast/Unit tests/nvm_nvmrc_invalid_msg shows a bounded excerpt @@ -0,0 +1,38 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +\. ../../common.sh + +LONG_LINE='aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaTAIL' +CONTENT="${LONG_LINE} +2 +3 +4 +5 +6 +7 +8 +9 +10 +eleventh" + +try_err nvm_nvmrc_invalid_msg "${CONTENT}" +EXCERPT="$(nvm_echo "${CAPTURED_STDERR}" | strip_colors | sed -n '/^non-commented content parsed:$/,$p')" +EXPECTED="non-commented content parsed: +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[...] +2 +3 +4 +5 +6 +7 +8 +9 +10 +[...]" + +[ "${EXCERPT}" = "${EXPECTED}" ] || die "expected >${EXPECTED}<; got >${EXCERPT}<"