rbenv install: fix substituting $HOME with "~" (#2501)

Depending on bash version, the expression `${var/$HOME\//~/}` will not have
effect because the "~" character in the replacement expression is expanded.

The updated approach is a bit of a mouthful, but it avoids using "~" in a
substitution pattern, while also guarding against values of HOME that are
blank or when HOME is literally just "/".
This commit is contained in:
Mislav Marohnić 2025-01-21 22:11:40 +01:00 committed by GitHub
parent 3643c8edf3
commit fdcfac8d42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 2 deletions

View file

@ -250,8 +250,10 @@ if [ "$STATUS" == "2" ]; then
printf ":\n\n"
echo " brew upgrade ruby-build"
elif [ -d "${here}/.git" ]; then
display_here="$here"
[[ -z "${HOME%/}" || $here != "${HOME}/"* ]] || display_here="~${here#"$HOME"}"
printf ":\n\n"
echo " git -C ${here/${HOME}\//~/} pull"
echo " git -C $display_here pull"
else
printf ".\n"
fi

View file

@ -88,6 +88,11 @@ OUT
}
@test "nonexistent version" {
display_here="${BATS_TEST_DIRNAME}"/..
if [[ -n $HOME && $display_here == "${HOME}/"* ]]; then
display_here="~${display_here#"${HOME}"}"
fi
stub_git_dir=
if [ ! -d "${BATS_TEST_DIRNAME}"/../.git ]; then
stub_git_dir="${BATS_TEST_DIRNAME}"/../.git
@ -113,7 +118,7 @@ See all available versions with \`rbenv install --list-all'.
If the version you need is missing, try upgrading ruby-build:
git -C ${BATS_TEST_DIRNAME/$HOME\//~/}/.. pull
git -C $display_here pull
OUT
unstub brew