mirror of
https://github.com/apisnetworks/pyenv.git
synced 2026-11-30 19:59:01 +00:00
init: explain what --install does and check every file before changing any (#3644)
This commit is contained in:
1 parent
186b350c85
commit
fc6046ccd0
2 files changed
+226
-28
No files matched your search
+80
-14
@@ -67,6 +67,11 @@ if [ -z "$shell" ]; then
|
||||
shell="${shell:-${SHELL:-}}"
|
||||
shell="${shell##*/}"
|
||||
shell="${shell%%-*}"
|
||||
if [ "$mode" = "install" ]; then
|
||||
echo "pyenv: shell detected as $shell" >&2
|
||||
fi
|
||||
elif [ "$mode" = "install" ]; then
|
||||
echo "pyenv: configuring for $shell" >&2
|
||||
fi
|
||||
|
||||
profile=
|
||||
@@ -336,45 +341,106 @@ function install_shell_startup_files() {
|
||||
;;
|
||||
esac
|
||||
|
||||
local index
|
||||
# Check every file before changing any of them, and report all problems at once.
|
||||
local index failed=
|
||||
for ((index = 0; index < ${#files[@]}; index++)); do
|
||||
check_startup_file "${files[$index]}" || return 1
|
||||
check_startup_file "${files[$index]}" || failed=1
|
||||
done
|
||||
if [[ -n $failed ]]; then
|
||||
echo "pyenv: no files were changed." >&2
|
||||
echo "pyenv: review the files listed above and apply changes manually if necessary." >&2
|
||||
echo "pyenv: run \`pyenv init $shell\` to see the suggested setup." >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [[ $shell == fish ]]; then
|
||||
echo "pyenv: setting fish universal variables for PYENV_ROOT and PATH" >&2
|
||||
install_fish_user_paths || return 1
|
||||
fi
|
||||
|
||||
for ((index = 0; index < ${#files[@]}; index++)); do
|
||||
echo "pyenv: adding configuration to ${files[$index]}" >&2
|
||||
append_lines "${files[$index]}" "${lines[$index]}"
|
||||
done
|
||||
}
|
||||
|
||||
# Report (and return non-zero) if a startup file can't be safely appended to.
|
||||
function check_startup_file() {
|
||||
local file="$1"
|
||||
local grep_status
|
||||
|
||||
# A broken symbolic link is the same as a missing file.
|
||||
if [[ ! -e $file ]]; then
|
||||
return 0
|
||||
check_startup_file_creatable "$file"
|
||||
return
|
||||
fi
|
||||
|
||||
if [[ ! -f $file || ! -r $file ]]; then
|
||||
echo "pyenv: failed to inspect $file" >&2
|
||||
# Some unusual file types (e.g. a pipe) can cause grep to hang;
|
||||
# Busybox grep reports no match for a directory instead of an error.
|
||||
# So need to check the file type before reading.
|
||||
# A symlink to a regular file also passes `[[ -f ]]`.
|
||||
if [[ ! -f $file ]]; then
|
||||
echo "pyenv: $file is not a regular file" >&2
|
||||
return 1
|
||||
fi
|
||||
|
||||
if grep -Fi pyenv "$file" >/dev/null; then
|
||||
elif grep -qFi pyenv "$file"; then
|
||||
echo "pyenv: cannot automatically apply changes to $file: it appears to already contain Pyenv-related code." >&2
|
||||
echo "pyenv: review the file's contents and apply changes manually if necessary." >&2
|
||||
echo "pyenv: run \`pyenv init $shell\` to see the suggested setup." >&2
|
||||
return 1
|
||||
else
|
||||
grep_status=$?
|
||||
if [[ $grep_status == 1 ]]; then
|
||||
return 0
|
||||
# Some greps report no match for a directory instead of an error.
|
||||
if [[ $grep_status != 1 ]]; then
|
||||
# assuming grep has printed its own message about the underlying problem
|
||||
echo "pyenv: failed to inspect $file" >&2
|
||||
return "$grep_status"
|
||||
fi
|
||||
echo "pyenv: failed to inspect $file" >&2
|
||||
return "$grep_status"
|
||||
fi
|
||||
|
||||
if [[ ! -w $file ]]; then
|
||||
echo "pyenv: $file is not writable" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# A missing file is created by `append_lines` with `mkdir -p` and `>>`,
|
||||
# so its closest existing parent directory must be writable.
|
||||
# Through a broken symbolic link, `>>` creates the file the link points to,
|
||||
# but not that file's directory, so that directory must already exist.
|
||||
function check_startup_file_creatable() {
|
||||
local file="$1"
|
||||
local target="$1"
|
||||
local dir link depth=0
|
||||
|
||||
while [[ -L $target ]] && (( depth++ < 40 )); do
|
||||
link="$(readlink "$target")" || break
|
||||
if [[ $link != /* && $target == */* ]]; then
|
||||
link="${target%/*}/$link"
|
||||
fi
|
||||
target="$link"
|
||||
done
|
||||
|
||||
if [[ $target != "$file" ]]; then
|
||||
dir=.
|
||||
if [[ $target == */* ]]; then
|
||||
dir="${target%/*}"
|
||||
dir="${dir:-/}"
|
||||
fi
|
||||
else
|
||||
dir="$file"
|
||||
while [[ $dir == */* ]]; do
|
||||
dir="${dir%/*}"
|
||||
dir="${dir:-/}"
|
||||
if [[ -e $dir || -L $dir ]]; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [[ ! -e $dir && ! -L $dir ]]; then
|
||||
dir=.
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -L $target || ! -d $dir || ! -w $dir || ! -x $dir ]]; then
|
||||
echo "pyenv: cannot create $file" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
+146
-14
@@ -99,6 +99,11 @@ OUT
|
||||
|
||||
run pyenv-init --install
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
pyenv: shell detected as bash
|
||||
pyenv: adding configuration to $HOME/.bashrc
|
||||
pyenv: adding configuration to $HOME/.profile
|
||||
OUT
|
||||
|
||||
expected_setup=$'export PYENV_ROOT="$HOME/.pyenv"\n[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"\neval "$(pyenv init - bash)"'
|
||||
assert_equal "$expected_setup" "$(cat "$HOME/.bashrc")"
|
||||
@@ -125,6 +130,11 @@ OUT
|
||||
|
||||
run pyenv-init --install zsh
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
pyenv: configuring for zsh
|
||||
pyenv: adding configuration to $HOME/.zshrc
|
||||
pyenv: adding configuration to $HOME/.zprofile
|
||||
OUT
|
||||
|
||||
expected_setup=$'export PYENV_ROOT="$HOME/.pyenv"\n[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"\neval "$(pyenv init - zsh)"'
|
||||
assert_equal "$expected_setup" "$(cat "$HOME/.zshrc")"
|
||||
@@ -140,6 +150,11 @@ OUT
|
||||
|
||||
run pyenv-init --install fish
|
||||
assert_success
|
||||
assert_output <<OUT
|
||||
pyenv: configuring for fish
|
||||
pyenv: setting fish universal variables for PYENV_ROOT and PATH
|
||||
pyenv: adding configuration to $HOME/.config/fish/config.fish
|
||||
OUT
|
||||
|
||||
expected_fish_script=$'set -Ux PYENV_ROOT $HOME/.pyenv\nif functions -q fish_add_path\n test -d $PYENV_ROOT/bin; and fish_add_path $PYENV_ROOT/bin\nelse\n test -d $PYENV_ROOT/bin; and set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths\nend'
|
||||
expected_setup='pyenv init - fish | source'
|
||||
@@ -180,14 +195,137 @@ OUT
|
||||
|
||||
run pyenv-init --install bash
|
||||
assert_failure
|
||||
assert_line "pyenv: cannot automatically apply changes to $HOME/.bashrc: it appears to already contain Pyenv-related code."
|
||||
assert_line "pyenv: review the file's contents and apply changes manually if necessary."
|
||||
assert_line "pyenv: run \`pyenv init bash\` to see the suggested setup."
|
||||
assert_output <<OUT
|
||||
pyenv: configuring for bash
|
||||
pyenv: cannot automatically apply changes to $HOME/.bashrc: it appears to already contain Pyenv-related code.
|
||||
pyenv: no files were changed.
|
||||
pyenv: review the files listed above and apply changes manually if necessary.
|
||||
pyenv: run \`pyenv init bash\` to see the suggested setup.
|
||||
OUT
|
||||
|
||||
assert_equal 'eval "$(pyenv init -)"' "$(cat "$HOME/.bashrc")"
|
||||
assert [ ! -e "$HOME/.profile" ]
|
||||
}
|
||||
|
||||
@test "install checks the second startup file before changing the first" {
|
||||
mkdir -p "$HOME"
|
||||
echo 'eval "$(pyenv init -)"' > "$HOME/.profile"
|
||||
|
||||
run pyenv-init --install bash
|
||||
assert_failure
|
||||
assert_line "pyenv: cannot automatically apply changes to $HOME/.profile: it appears to already contain Pyenv-related code."
|
||||
assert_line "pyenv: no files were changed."
|
||||
|
||||
assert [ ! -e "$HOME/.bashrc" ]
|
||||
assert_equal 'eval "$(pyenv init -)"' "$(cat "$HOME/.profile")"
|
||||
}
|
||||
|
||||
@test "install lists every startup file with pyenv-related code" {
|
||||
mkdir -p "$HOME"
|
||||
echo 'eval "$(pyenv init -)"' > "$HOME/.zshrc"
|
||||
echo 'export PYENV_ROOT="$HOME/.pyenv"' > "$HOME/.zprofile"
|
||||
|
||||
run pyenv-init --install zsh
|
||||
assert_failure
|
||||
assert_output <<OUT
|
||||
pyenv: configuring for zsh
|
||||
pyenv: cannot automatically apply changes to $HOME/.zshrc: it appears to already contain Pyenv-related code.
|
||||
pyenv: cannot automatically apply changes to $HOME/.zprofile: it appears to already contain Pyenv-related code.
|
||||
pyenv: no files were changed.
|
||||
pyenv: review the files listed above and apply changes manually if necessary.
|
||||
pyenv: run \`pyenv init zsh\` to see the suggested setup.
|
||||
OUT
|
||||
|
||||
assert_equal 'eval "$(pyenv init -)"' "$(cat "$HOME/.zshrc")"
|
||||
assert_equal 'export PYENV_ROOT="$HOME/.pyenv"' "$(cat "$HOME/.zprofile")"
|
||||
}
|
||||
|
||||
@test "install refuses a non-regular file without partial writes" {
|
||||
mkdir -p "$HOME"
|
||||
mkfifo "$HOME/.profile"
|
||||
|
||||
run pyenv-init --install bash
|
||||
assert_failure
|
||||
assert_line "pyenv: $HOME/.profile is not a regular file"
|
||||
assert_line "pyenv: no files were changed."
|
||||
|
||||
assert [ ! -e "$HOME/.bashrc" ]
|
||||
assert [ -p "$HOME/.profile" ]
|
||||
}
|
||||
|
||||
@test "install refuses unreadable startup file without partial writes" {
|
||||
mkdir -p "$HOME"
|
||||
touch "$HOME/.bashrc"
|
||||
chmod a-r "$HOME/.bashrc"
|
||||
skip_if_nonposix_security -r "$HOME/.bashrc"
|
||||
|
||||
run pyenv-init --install bash
|
||||
assert_failure
|
||||
assert_line "pyenv: failed to inspect $HOME/.bashrc"
|
||||
|
||||
assert [ ! -e "$HOME/.profile" ]
|
||||
assert [ ! -s "$HOME/.bashrc" ]
|
||||
}
|
||||
|
||||
@test "install refuses a non-writable startup file without partial writes" {
|
||||
mkdir -p "$HOME"
|
||||
touch "$HOME/.profile"
|
||||
chmod a-w "$HOME/.profile"
|
||||
skip_if_nonposix_security -w "$HOME/.profile"
|
||||
|
||||
run pyenv-init --install bash
|
||||
assert_failure
|
||||
assert_line "pyenv: $HOME/.profile is not writable"
|
||||
assert_line "pyenv: no files were changed."
|
||||
|
||||
assert [ ! -e "$HOME/.bashrc" ]
|
||||
assert [ ! -s "$HOME/.profile" ]
|
||||
}
|
||||
|
||||
@test "install refuses a startup file it cannot create, before setting fish variables" {
|
||||
mkdir -p "$HOME"
|
||||
touch "$HOME/.config"
|
||||
create_stub fish <<OUT
|
||||
touch "$PYENV_TEST_DIR/fish-called"
|
||||
OUT
|
||||
|
||||
run pyenv-init --install fish
|
||||
assert_failure
|
||||
assert_line "pyenv: cannot create $HOME/.config/fish/config.fish"
|
||||
assert_line "pyenv: no files were changed."
|
||||
|
||||
assert [ ! -e "$PYENV_TEST_DIR/fish-called" ]
|
||||
assert [ ! -s "$HOME/.config" ]
|
||||
}
|
||||
|
||||
@test "install treats a broken symlink as a missing startup file" {
|
||||
export PYENV_ROOT="$HOME/.pyenv"
|
||||
mkdir -p "$PYENV_ROOT" "$HOME/dotfiles"
|
||||
ln -s "$HOME/dotfiles/profile" "$HOME/.profile"
|
||||
|
||||
run pyenv-init --install bash
|
||||
assert_success
|
||||
|
||||
expected_setup=$'export PYENV_ROOT="$HOME/.pyenv"\n[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"\neval "$(pyenv init - bash)"'
|
||||
assert [ -L "$HOME/.profile" ]
|
||||
assert_equal "$expected_setup" "$(cat "$HOME/dotfiles/profile")"
|
||||
}
|
||||
|
||||
@test "install refuses a broken symlink whose target directory is missing" {
|
||||
mkdir -p "$HOME"
|
||||
ln -s "$HOME/missing/profile" "$HOME/.profile"
|
||||
|
||||
run pyenv-init --install bash
|
||||
assert_failure
|
||||
assert_line "pyenv: cannot create $HOME/.profile"
|
||||
assert_line "pyenv: no files were changed."
|
||||
|
||||
assert [ ! -e "$HOME/.bashrc" ]
|
||||
assert [ ! -e "$HOME/missing" ]
|
||||
#assert the symlink is untouched
|
||||
assert_equal "$HOME/missing/profile" "$(readlink "$HOME/.profile")"
|
||||
}
|
||||
|
||||
@test "install treats PYENV_ROOT as pyenv-related code" {
|
||||
mkdir -p "$HOME"
|
||||
echo 'export PYENV_ROOT="$HOME/tools/python-env"' > "$HOME/.bashrc"
|
||||
@@ -197,16 +335,6 @@ OUT
|
||||
assert_line "pyenv: cannot automatically apply changes to $HOME/.bashrc: it appears to already contain Pyenv-related code."
|
||||
}
|
||||
|
||||
@test "install refuses unreadable startup file without partial writes" {
|
||||
mkdir -p "$HOME/.bashrc"
|
||||
|
||||
run pyenv-init --install bash
|
||||
assert_failure
|
||||
assert_line "pyenv: failed to inspect $HOME/.bashrc"
|
||||
|
||||
assert [ ! -e "$HOME/.profile" ]
|
||||
}
|
||||
|
||||
@test "install setup keeps fish block intact when generic lines already exist" {
|
||||
mkdir -p "$HOME/.config/fish"
|
||||
create_stub fish <<OUT
|
||||
@@ -225,7 +353,11 @@ OUT
|
||||
mkdir -p "$HOME"
|
||||
|
||||
run pyenv-init --install nu
|
||||
assert_failure "pyenv: cannot automatically configure startup files for nu"
|
||||
assert_failure
|
||||
assert_output <<'OUT'
|
||||
pyenv: configuring for nu
|
||||
pyenv: cannot automatically configure startup files for nu
|
||||
OUT
|
||||
}
|
||||
|
||||
@test "option to skip rehash" {
|
||||
|
||||
Reference in new issue
Block a user