1
0
mirror of https://github.com/pyenv/pyenv.git synced 2025-11-14 22:33:52 -05:00

rbenv init: modify shell config files instead of printing instructions (#1568)

When running `rbenv init`, typically during rbenv setup, users expected
their shell environment to be modified permanently. Instead, what the
command would do is print the instructions to the user and expect them
to edit their shell initialization files accordingly. This proved to
be unintuitive.

Now, running `rbenv init <shells>...` will modify the shell initialization
files of the following shells:

- bash: `~/.bash_profile` or `~/.bashrc` if the latter exists but the former does not
- zsh: `~/.zprofile` or `~/.zshrc` if the latter exists and mentions "rbenv"
- fish: `~/.config/fish/config.fish`

If no shells were specified on the command line, rbenv will try to detect
the current shell.

It should be safe to run `rbenv init` multiple times, as the command will
avoid modifying any shell startup file that already mentions "rbenv init".
This commit is contained in:
Mislav Marohnić
2024-05-03 16:59:39 +02:00
committed by GitHub
parent a3b98a4223
commit c3ba994ec2
3 changed files with 151 additions and 81 deletions

View File

@@ -51,18 +51,67 @@ OUT
[ -z "$line" ] || flunk "did not expect line: $line"
}
@test "posix shell instructions" {
@test "set up bash" {
assert [ ! -e ~/.bash_profile ]
run rbenv-init bash
assert [ "$status" -eq 1 ]
assert_success "writing ~/.bash_profile: now configured for rbenv."
run cat ~/.bash_profile
# shellcheck disable=SC2016
assert_line 'eval "$(rbenv init - bash)"'
}
@test "fish instructions" {
@test "set up bash (bashrc)" {
mkdir -p "$HOME"
touch ~/.bashrc
assert [ ! -e ~/.bash_profile ]
run rbenv-init bash
assert_success "writing ~/.bashrc: now configured for rbenv."
run cat ~/.bashrc
# shellcheck disable=SC2016
assert_line 'eval "$(rbenv init - bash)"'
}
@test "set up zsh" {
unset ZDOTDIR
assert [ ! -e ~/.zprofile ]
run rbenv-init zsh
assert_success "writing ~/.zprofile: now configured for rbenv."
run cat ~/.zprofile
# shellcheck disable=SC2016
assert_line 'eval "$(rbenv init - zsh)"'
}
@test "set up zsh (zshrc)" {
unset ZDOTDIR
mkdir -p "$HOME"
cat > ~/.zshrc <<<"# rbenv"
run rbenv-init zsh
assert_success "writing ~/.zshrc: now configured for rbenv."
run cat ~/.zshrc
# shellcheck disable=SC2016
assert_line 'eval "$(rbenv init - zsh)"'
}
@test "set up fish" {
unset XDG_CONFIG_HOME
run rbenv-init fish
assert [ "$status" -eq 1 ]
assert_success "writing ~/.config/fish/config.fish: now configured for rbenv."
run cat ~/.config/fish/config.fish
assert_line 'status --is-interactive; and rbenv init - fish | source'
}
@test "set up multiple shells at once" {
unset ZDOTDIR
unset XDG_CONFIG_HOME
run rbenv-init bash zsh fish
assert_success
assert_output <<OUT
writing ~/.bash_profile: now configured for rbenv.
writing ~/.zprofile: now configured for rbenv.
writing ~/.config/fish/config.fish: now configured for rbenv.
OUT
}
@test "option to skip rehash" {
run rbenv-init - --no-rehash
assert_success