diff --git a/nvm.sh b/nvm.sh index 787a9531..6b7ad5ec 100755 --- a/nvm.sh +++ b/nvm.sh @@ -368,7 +368,7 @@ nvm_wget_with_auth_header() { nvm_echo fi nvm_echo "header = Authorization: ${1-}" - } > "${NVM_WGETRC}" || exit 1 + } >| "${NVM_WGETRC}" || exit 1 shift WGETRC="${NVM_WGETRC}" export WGETRC @@ -3076,7 +3076,7 @@ nvm_record_lts_status() { } for (i = 1; i <= rows; i++) { print versions[i], lts[versions[i]] } } - ' > "${NVM_LTS_STATUS_TMP}"; then + ' >| "${NVM_LTS_STATUS_TMP}"; then command mv -f "${NVM_LTS_STATUS_TMP}" "${NVM_LTS_STATUS_PATH}" 2>/dev/null && return 0 fi command rm -f "${NVM_LTS_STATUS_TMP}" diff --git a/test/fast/Unit tests/nvm_record_lts_status with noclobber b/test/fast/Unit tests/nvm_record_lts_status with noclobber new file mode 100755 index 00000000..63154dd3 --- /dev/null +++ b/test/fast/Unit tests/nvm_record_lts_status with noclobber @@ -0,0 +1,47 @@ +#!/bin/sh + +ORIG_NVM_DIR="${NVM_DIR}" +TMP_DIR='' + +cleanup () { + set +o noclobber + case "${TMP_DIR}" in + /*) rm -rf "${TMP_DIR}" ;; + esac + NVM_DIR="${ORIG_NVM_DIR}" + export NVM_DIR +} + +die () { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh + +TMP_DIR="$(mktemp -d)" +NVM_DIR="${TMP_DIR}/nvm" +export NVM_DIR +mkdir -p "${NVM_DIR}" || die 'could not create NVM_DIR' + +row () { + printf '%s\t2024-01-01\tsrc\t-\t-\t-\t-\t-\t-\t%s\t-\n' "$1" "$2" +} + +recorded () { + [ -f "$(nvm_lts_status_path)" ] || die "no LTS status is recorded, so $1 has none" + OUTPUT="$(command awk -v version="$1" '$1 == version { lts = $2 } END { print (lts == "" ? "-" : lts) }' "$(nvm_lts_status_path)")" + [ "_${OUTPUT}" = "_$2" ] || die "the recorded LTS status of $1 is >${OUTPUT}<, expected >$2<" +} + +# mktemp creates the file that the record is written to, which noclobber would refuse to overwrite +set -o noclobber + +row v24.11.0 Krypton | nvm_record_lts_status || die 'nvm_record_lts_status should start a record with noclobber set' +recorded v24.11.0 krypton + +row v26.0.0 - | nvm_record_lts_status || die 'nvm_record_lts_status should merge into a record with noclobber set' +recorded v26.0.0 - +recorded v24.11.0 krypton + +[ -z "$(command find "${NVM_DIR}" -name '.lts-status.*' -print)" ] || die "temporary files were left behind: $(command find "${NVM_DIR}" -name '.lts-status.*' -print)" + +cleanup diff --git a/test/fast/Unit tests/nvm_wget_with_auth_header with noclobber b/test/fast/Unit tests/nvm_wget_with_auth_header with noclobber new file mode 100755 index 00000000..a52dd4a9 --- /dev/null +++ b/test/fast/Unit tests/nvm_wget_with_auth_header with noclobber @@ -0,0 +1,52 @@ +#!/bin/sh + +WORK="$PWD/nvm_wget_with_auth_header-noclobber-work.$$" +OLDHOME="$HOME" +OLDTMPDIR="${TMPDIR-}" + +cleanup() { + set +o noclobber + unset -f nvm_wget_same_origin + unset WGETRC + rm -rf "$WORK" + HOME="$OLDHOME" + export HOME + TMPDIR="$OLDTMPDIR" + export TMPDIR +} +die () { echo "$@" ; cleanup ; exit 1; } + +\. ../../../nvm.sh + +mkdir -p "$WORK/home" "$WORK/tmp" || die 'unable to create the work directory' +HOME="$WORK/home" +export HOME +TMPDIR="$WORK/tmp" +export TMPDIR +unset WGETRC + +# records the config wget would be run with; the real one runs `command wget` +nvm_wget_same_origin() { + printf '%s\n' "${WGETRC-}" >| "$WORK/wgetrc-path" + if [ -n "${WGETRC-}" ] && [ -f "${WGETRC}" ]; then + cat "${WGETRC}" >| "$WORK/wgetrc" + else + : >| "$WORK/wgetrc" + fi +} + +# mktemp creates the config file, which noclobber would refuse to overwrite +set -o noclobber + +nvm_wget_with_auth_header 'Bearer test-token' 'https://example.com/dist/index.tab' || die 'no wgetrc: expected exit code 0, got nonzero' +[ "$(cat "$WORK/wgetrc")" = 'header = Authorization: Bearer test-token' ] || die "no wgetrc: expected only the header in the config, got >$(cat "$WORK/wgetrc")<" +[ ! -e "$(cat "$WORK/wgetrc-path")" ] || die "no wgetrc: expected the config file to be removed, got $(cat "$WORK/wgetrc-path") left behind" + +printf 'tries = 7' >| "$HOME/.wgetrc" +nvm_wget_with_auth_header 'Bearer test-token' 'https://example.com/dist/index.tab' || die 'with ~/.wgetrc: expected exit code 0, got nonzero' +EXPECTED_RC='tries = 7 +header = Authorization: Bearer test-token' +[ "$(cat "$WORK/wgetrc")" = "$EXPECTED_RC" ] || die "with ~/.wgetrc: expected >$EXPECTED_RC<, got >$(cat "$WORK/wgetrc")<" +[ -z "$(ls -A "$WORK/tmp")" ] || die "expected \$TMPDIR to be empty, got: $(ls -A "$WORK/tmp")" + +cleanup