Merge branch 'shell-fixes'

This commit is contained in:
Mislav Marohnić 2013-09-28 16:15:13 +02:00
commit 71a6d791c2
7 changed files with 163 additions and 18 deletions

22
completions/rbenv.fish Normal file
View file

@ -0,0 +1,22 @@
function __fish_rbenv_needs_command
set cmd (commandline -opc)
if [ (count $cmd) -eq 1 -a $cmd[1] = 'rbenv' ]
return 0
end
return 1
end
function __fish_rbenv_using_command
set cmd (commandline -opc)
if [ (count $cmd) -gt 1 ]
if [ $argv[1] = $cmd[2] ]
return 0
end
end
return 1
end
complete -f -c rbenv -n '__fish_rbenv_needs_command' -a '(rbenv commands)'
for cmd in (rbenv commands)
complete -f -c rbenv -n "__fish_rbenv_using_command $cmd" -a "(rbenv completions $cmd)"
end

View file

@ -62,6 +62,9 @@ if [ -z "$print" ]; then
ksh ) ksh )
profile='~/.profile' profile='~/.profile'
;; ;;
fish )
profile='~/.config/fish/config.fish'
;;
* ) * )
profile='your profile' profile='your profile'
;; ;;
@ -70,7 +73,14 @@ if [ -z "$print" ]; then
{ echo "# Load rbenv automatically by adding" { echo "# Load rbenv automatically by adding"
echo "# the following to ${profile}:" echo "# the following to ${profile}:"
echo echo
case "$shell" in
fish )
echo '. (rbenv init -|psub)'
;;
* )
echo 'eval "$(rbenv init -)"' echo 'eval "$(rbenv init -)"'
;;
esac
echo echo
} >&2 } >&2
@ -80,21 +90,57 @@ fi
mkdir -p "${RBENV_ROOT}/"{shims,versions} mkdir -p "${RBENV_ROOT}/"{shims,versions}
if [[ ":${PATH}:" != *:"${RBENV_ROOT}/shims":* ]]; then if [[ ":${PATH}:" != *:"${RBENV_ROOT}/shims":* ]]; then
case "$shell" in
fish )
echo "setenv PATH '${RBENV_ROOT}/shims' \$PATH"
;;
* )
echo 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' echo 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"'
;;
esac
fi fi
completion="${root}/completions/rbenv.${shell}" completion="${root}/completions/rbenv.${shell}"
[ -r "$completion" ] && echo "source '$completion'" [ -r "$completion" ] && echo ". '$completion'"
if [ -z "$no_rehash" ]; then if [ -z "$no_rehash" ]; then
echo 'rbenv rehash 2>/dev/null' echo 'rbenv rehash 2>/dev/null'
fi fi
commands=(`rbenv-commands --sh`) commands=(`rbenv-commands --sh`)
case "$shell" in
fish )
cat <<EOS
function rbenv
set command \$argv[1]
set -e argv[1]
switch "\$command"
case ${commands[*]}
eval (rbenv "sh-\$command" \$argv)
case '*'
command rbenv "\$command" \$argv
end
end
EOS
exit 0
;;
ksh )
cat <<EOS
function rbenv {
typeset command
EOS
;;
* )
cat <<EOS
rbenv() {
local command
EOS
;;
esac
IFS="|" IFS="|"
cat <<EOS cat <<EOS
rbenv() {
typeset command
command="\$1" command="\$1"
if [ "\$#" -gt 0 ]; then if [ "\$#" -gt 0 ]; then
shift shift

View file

@ -7,7 +7,19 @@ if [ "$1" = "--complete" ]; then
exec rbenv-rehash --complete exec rbenv-rehash --complete
fi fi
shell="$(basename "$SHELL")"
# When rbenv shell integration is enabled, delegate to rbenv-rehash, # When rbenv shell integration is enabled, delegate to rbenv-rehash,
# then tell the shell to empty its command lookup cache. # then tell the shell to empty its command lookup cache.
rbenv-rehash rbenv-rehash
echo "hash -r 2>/dev/null || true"
case "$shell" in
fish )
or='; or'
;;
* )
or='||'
;;
esac
echo "hash -r 2>/dev/null $or true"

View file

@ -24,6 +24,7 @@ if [ "$1" = "--complete" ]; then
fi fi
version="$1" version="$1"
shell="$(basename "$SHELL")"
if [ -z "$version" ]; then if [ -z "$version" ]; then
if [ -z "$RBENV_VERSION" ]; then if [ -z "$RBENV_VERSION" ]; then
@ -36,14 +37,28 @@ if [ -z "$version" ]; then
fi fi
if [ "$version" = "--unset" ]; then if [ "$version" = "--unset" ]; then
case "$shell" in
fish )
echo "set -e RBENV_VERSION"
;;
* )
echo "unset RBENV_VERSION" echo "unset RBENV_VERSION"
;;
esac
exit exit
fi fi
# Make sure the specified version is installed. # Make sure the specified version is installed.
if rbenv-prefix "$version" >/dev/null; then if rbenv-prefix "$version" >/dev/null; then
case "$shell" in
fish )
echo "setenv RBENV_VERSION \"${version}\""
;;
* )
echo "export RBENV_VERSION=\"${version}\"" echo "export RBENV_VERSION=\"${version}\""
;;
esac
else else
echo "return 1" echo "false"
exit 1 exit 1
fi fi

View file

@ -18,11 +18,17 @@ load test_helper
} }
@test "setup shell completions" { @test "setup shell completions" {
export SHELL=/bin/bash
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
run rbenv-init - SHELL=/bin/bash run rbenv-init -
assert_success assert_success
assert_line "source '${root}/libexec/../completions/rbenv.bash'" assert_line ". '${root}/libexec/../completions/rbenv.bash'"
}
@test "setup shell completions (fish)" {
root="$(cd $BATS_TEST_DIRNAME/.. && pwd)"
SHELL=/usr/bin/fish run rbenv-init -
assert_success
assert_line ". '${root}/libexec/../completions/rbenv.fish'"
} }
@test "option to skip rehash" { @test "option to skip rehash" {
@ -33,14 +39,28 @@ load test_helper
@test "adds shims to PATH" { @test "adds shims to PATH" {
export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin" export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin"
run rbenv-init - SHELL=/bin/bash run rbenv-init -
assert_success assert_success
assert_line 0 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' assert_line 0 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"'
} }
@test "adds shims to PATH (fish)" {
export PATH="${BATS_TEST_DIRNAME}/../libexec:/usr/bin:/bin"
SHELL=/usr/bin/fish run rbenv-init -
assert_success
assert_line 0 "setenv PATH '${RBENV_ROOT}/shims' \$PATH"
}
@test "doesn't add shims to PATH more than once" { @test "doesn't add shims to PATH more than once" {
export PATH="${RBENV_ROOT}/shims:$PATH" export PATH="${RBENV_ROOT}/shims:$PATH"
run rbenv-init - SHELL=/bin/bash run rbenv-init -
assert_success assert_success
refute_line 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"' refute_line 'export PATH="'${RBENV_ROOT}'/shims:${PATH}"'
} }
@test "doesn't add shims to PATH more than once (fish)" {
export PATH="${RBENV_ROOT}/shims:$PATH"
SHELL=/usr/bin/fish run rbenv-init -
assert_success
refute_line 'setenv PATH "'${RBENV_ROOT}'/shims" $PATH ;'
}

View file

@ -98,3 +98,17 @@ SH
assert_success assert_success
assert_output "HELLO=:hello:ugly:world:again" assert_output "HELLO=:hello:ugly:world:again"
} }
@test "sh-rehash in bash" {
create_executable "2.0" "ruby"
SHELL=/bin/bash run rbenv-sh-rehash
assert_success "hash -r 2>/dev/null || true"
assert [ -x "${RBENV_ROOT}/shims/ruby" ]
}
@test "sh-rehash in fish" {
create_executable "2.0" "ruby"
SHELL=/usr/bin/fish run rbenv-sh-rehash
assert_success "hash -r 2>/dev/null ; or true"
assert [ -x "${RBENV_ROOT}/shims/ruby" ]
}

View file

@ -11,26 +11,42 @@ load test_helper
} }
@test "shell version" { @test "shell version" {
RBENV_VERSION="1.2.3" run rbenv-sh-shell SHELL=/bin/bash RBENV_VERSION="1.2.3" run rbenv-sh-shell
assert_success 'echo "$RBENV_VERSION"'
}
@test "shell version (fish)" {
SHELL=/usr/bin/fish RBENV_VERSION="1.2.3" run rbenv-sh-shell
assert_success 'echo "$RBENV_VERSION"' assert_success 'echo "$RBENV_VERSION"'
} }
@test "shell unset" { @test "shell unset" {
run rbenv-sh-shell --unset SHELL=/bin/bash run rbenv-sh-shell --unset
assert_success "unset RBENV_VERSION" assert_success "unset RBENV_VERSION"
} }
@test "shell unset (fish)" {
SHELL=/usr/bin/fish run rbenv-sh-shell --unset
assert_success "set -e RBENV_VERSION"
}
@test "shell change invalid version" { @test "shell change invalid version" {
run rbenv-sh-shell 1.2.3 run rbenv-sh-shell 1.2.3
assert_failure assert_failure
assert_output <<SH assert_output <<SH
rbenv: version \`1.2.3' not installed rbenv: version \`1.2.3' not installed
return 1 false
SH SH
} }
@test "shell change version" { @test "shell change version" {
mkdir -p "${RBENV_ROOT}/versions/1.2.3" mkdir -p "${RBENV_ROOT}/versions/1.2.3"
run rbenv-sh-shell 1.2.3 SHELL=/bin/bash run rbenv-sh-shell 1.2.3
assert_success 'export RBENV_VERSION="1.2.3"' assert_success 'export RBENV_VERSION="1.2.3"'
} }
@test "shell change version (fish)" {
mkdir -p "${RBENV_ROOT}/versions/1.2.3"
SHELL=/usr/bin/fish run rbenv-sh-shell 1.2.3
assert_success 'setenv RBENV_VERSION "1.2.3"'
}