From eea2a555331a1c5c488f5af392b01a87a9641e65 Mon Sep 17 00:00:00 2001 From: John Gee Date: Mon, 28 Oct 2024 14:58:34 +1300 Subject: [PATCH] Add support for trailing comment in .nvmrc (#820) --- bin/n | 2 ++ test/tests/version-resolve-auto-nvmrc.bats | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/bin/n b/bin/n index 675b88c..cd31052 100755 --- a/bin/n +++ b/bin/n @@ -1147,6 +1147,8 @@ function get_nvmrc_version() { verbose_log "found" "${filepath}" local version <"${filepath}" read -r version + # remove trailing comment, after # + version="$(echo "${version}" | sed 's/[[:space:]]*#.*//')" verbose_log "read" "${version}" # Translate from nvm aliases case "${version}" in diff --git a/test/tests/version-resolve-auto-nvmrc.bats b/test/tests/version-resolve-auto-nvmrc.bats index a99c7fa..fbe72b7 100644 --- a/test/tests/version-resolve-auto-nvmrc.bats +++ b/test/tests/version-resolve-auto-nvmrc.bats @@ -68,3 +68,11 @@ function setup() { output="$(n N_TEST_DISPLAY_LATEST_RESOLVED_VERSION auto)" assert_equal "${output}" "8.11.1" } + +@test "auto .nvmrc, trailing comment" { + local TARGET_VERSION="8.10.0" + cd "${MY_DIR}" + printf "${TARGET_VERSION} # comment" > .nvmrc + output="$(n N_TEST_DISPLAY_LATEST_RESOLVED_VERSION auto)" + assert_equal "${output}" "${TARGET_VERSION}" +}