From f9d8b551dc2afa82e98d0779d1c6e4eebace74f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 24 Dec 2015 12:29:35 +0100 Subject: [PATCH 1/3] Add test for detecting shell when `rbenv init` is called from script References #730 --- test/init.bats | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/init.bats b/test/init.bats index 0a46dd0d..d7005489 100644 --- a/test/init.bats +++ b/test/init.bats @@ -25,12 +25,24 @@ load test_helper } @test "detect parent shell" { - root="$(cd $BATS_TEST_DIRNAME/.. && pwd)" SHELL=/bin/false run rbenv-init - assert_success assert_line "export RBENV_SHELL=bash" } +@test "detect parent shell from script" { + mkdir -p "$RBENV_TEST_DIR" + cd "$RBENV_TEST_DIR" + cat > myscript.sh < Date: Sun, 10 May 2015 16:03:06 +0200 Subject: [PATCH 2/3] Fix shell version when invoked from a script When invoked from a shell script, `$(rbenv init -)` did not get the shell name correct. It needs to look at the `args` value from `ps`. Ref: https://github.com/yyuu/pyenv/issues/373 --- libexec/rbenv-init | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv-init b/libexec/rbenv-init index 8e6ee3d8..9fcfc2f4 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -33,7 +33,7 @@ done shell="$1" if [ -z "$shell" ]; then - shell="$(ps c -p "$PPID" -o 'ucomm=' 2>/dev/null || true)" + shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)" shell="${shell##-}" shell="${shell%% *}" shell="${shell:-$SHELL}" From a9a9636d1e739e047c8089e0319eed5c0cfebc21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 24 Dec 2015 13:23:01 +0100 Subject: [PATCH 3/3] Tweak sanitizing shell name Handles situation when the output is `/bin/shell- args...`. First strip away the arguments, then the trailing dash. --- libexec/rbenv-init | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libexec/rbenv-init b/libexec/rbenv-init index 9fcfc2f4..7ddae5e5 100755 --- a/libexec/rbenv-init +++ b/libexec/rbenv-init @@ -34,8 +34,8 @@ done shell="$1" if [ -z "$shell" ]; then shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)" - shell="${shell##-}" shell="${shell%% *}" + shell="${shell##-}" shell="${shell:-$SHELL}" shell="${shell##*/}" fi