Skip to content

Commit

Permalink
Perform an ascending search for .ruby-version (issue #40).
Browse files Browse the repository at this point in the history
* Added @wilmoore's test case.
  • Loading branch information
postmodern committed Dec 17, 2012
1 parent 88b79c8 commit 0a333ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
31 changes: 23 additions & 8 deletions share/chruby/auto.sh
@@ -1,13 +1,28 @@
function chruby_auto() {
if [[ -f .ruby-version ]]; then
chruby $(cat .ruby-version) && export RUBY_VERSIONED_DIR="$PWD"
elif [[ "$PWD" != "$RUBY_VERSIONED_DIR"/* ]]; then
chruby_reset; unset RUBY_VERSIONED_DIR
if [[ -n "$RUBY_VERSIONED_DIR" ]]; then
if [[ "$PWD" != "$RUBY_VERSIONED_DIR"/* ]]; then
chruby_reset
unset RUBY_VERSIONED_DIR
fi
else
local dir="$PWD"
local version_file

until [[ "$dir" == / ]]; do
version_file="$dir/.ruby-version"

if [[ -f "$version_file" ]]; then
chruby $(cat "$version_file") || return 1

export RUBY_VERSIONED_DIR="$dir"
break
fi

dir=$(dirname "$dir")
done
fi
}

if [[ -n "$ZSH_VERSION" ]]; then
precmd_functions+=("chruby_auto")
else
PROMPT_COMMAND="chruby_auto; $PROMPT_COMMAND"
if [[ -n "$ZSH_VERSION" ]]; then precmd_functions+=("chruby_auto")
else PROMPT_COMMAND="chruby_auto; $PROMPT_COMMAND"
fi
9 changes: 5 additions & 4 deletions test/chruby_auto_test.sh
Expand Up @@ -8,6 +8,7 @@ PROJECT_DIR="$PWD/test/project"
setUp()
{
chruby_reset
unset RUBY_VERSIONED_DIR
}

test_chruby_auto_enter_project_dir()
Expand All @@ -18,12 +19,12 @@ test_chruby_auto_enter_project_dir()
"$TEST_RUBY" "$RUBY"
}

test_chruby_auto_skip_project_dir()
test_chruby_auto_enter_subdir_directly()
{
cd "$PROJECT_DIR/dir1" && chruby_auto
cd "$PROJECT_DIR/dir1/dir2" && chruby_auto

assertNull "did switch Ruby when not explicitly entering the project" \
"$RUBY"
assertEquals "did not switch Ruby when directly entering a sub-directory of a versioned directory" \
"$TEST_RUBY" "$RUBY"
}

test_chruby_auto_enter_subdir()
Expand Down

0 comments on commit 0a333ac

Please sign in to comment.