Skip to content
Myr

nvm_do_install_printf_format

This file

Path

test/install_script/nvm_do_install_printf_format

70 lines · 1.8 KiB

Last changed by

[Fix] `install.sh`: fix POSIX compliance, printf format strings, and profile detectionJordan Harbandbefore adoption581317d

History of this file

Take it

1#!/bin/sh23die () { echo "$@" ; cleanup ; exit 1; }45cleanup() {6  unset -f install_nvm_from_git install_nvm_as_script nvm_detect_profile nvm_has nvm_install_dir7  unset -f nvm_check_global_modules nvm_install_node8  unset -f setup cleanup die9  unset NVM_ENV METHOD PROFILE10  rm -rf "${TMPDIR_FOR_TEST-}" 2>/dev/null11}1213setup() {14  NVM_ENV=testing \. ../../install.sh1516  # Mock installation functions to do nothing17  install_nvm_from_git() { :; }18  install_nvm_as_script() { :; }19  nvm_check_global_modules() { :; }20  nvm_install_node() { :; }2122  # Mock nvm_has to return true for git23  nvm_has() {24    case "$1" in25      git) return 0 ;;26      xcode-select) return 1 ;;27      *) return 1 ;;28    esac29  }30}3132setup3334#35# Test: printf calls in nvm_do_install should not interpret % in paths36# When NVM_DIR contains printf format specifiers like %s or %d,37# the output should contain them literally, not interpret them.38#3940TMPDIR_FOR_TEST="$(mktemp -d)"41PERCENT_DIR="${TMPDIR_FOR_TEST}/nvm_%s_test"42mkdir -p "${PERCENT_DIR}"4344# Copy nvm.sh to the temp dir so sourcing succeeds45cp ../../nvm.sh "${PERCENT_DIR}/nvm.sh"4647# Mock nvm_install_dir to return our percent-containing path48nvm_install_dir() {49  nvm_echo "${PERCENT_DIR}"50}5152# Mock nvm_detect_profile to return empty so we hit the "Profile not found" branch53# which calls: command printf '%b' "${SOURCE_STR}"54nvm_detect_profile() {55  echo ""56}5758OUTPUT="$(PROFILE='' METHOD='' NVM_DIR="${PERCENT_DIR}" nvm_do_install 2>&1)"5960# The SOURCE_STR should contain the %s from the path literally61if ! echo "${OUTPUT}" | grep -q '%s'; then62  die "printf should not have consumed the %s in the path. Output: ${OUTPUT}"63fi6465# Also verify via the "Profile not found" branch that SOURCE_STR was printed correctly66if ! echo "${OUTPUT}" | grep -q 'nvm_%s_test'; then67  die "Expected nvm_%s_test in output but got: ${OUTPUT}"68fi6970cleanup