Skip to content

Commit

Permalink
Changed chruby_find to search CHRUBY_DIRS each time.
Browse files Browse the repository at this point in the history
* Added `chruby_list` to list the contents of `CHRUBY_DIRS`.
* Removed `chruby_init` and the `--reload` option.
* This prevents having to restart the shell after installing a
  new ruby.
  • Loading branch information
postmodern committed Jan 19, 2023
1 parent 355ad07 commit c87f4cf
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 109 deletions.
3 changes: 0 additions & 3 deletions doc/man/chruby.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

## OPTIONS

`--reload`
Reloads the list of available Rubies

`-h`, `--help`

`-v`, `--version`
Expand Down
52 changes: 27 additions & 25 deletions share/chruby/chruby.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
CHRUBY_VERSION="1.0.0"
CHRUBY_DIRS=("$PREFIX/opt/rubies" "$HOME/.rubies")

function chruby_init()
function chruby_list()
{
local dir
local rubies_dir ruby_dir

CHRUBY_RUBIES=()
for dir in "${CHRUBY_DIRS[@]}"; do
if [[ -d "$dir" ]] && [[ -n "$(ls -A "$dir")" ]]; then
CHRUBY_RUBIES+=("$dir"/*)
fi
for rubies_dir in "${CHRUBY_DIRS[@]}"; do
for ruby_dir in "${rubies_dir}"/*; do
if [[ -d "$ruby_dir" ]]; then
echo "$ruby_dir"
fi
done
done
}

function chruby_find()
{
local dir ruby match
local ruby_dir ruby_name match

while IFS= read ruby_dir; do
ruby_dir="${ruby_dir%%/}"
ruby_name="${ruby_dir##*/}"

for dir in "${CHRUBY_RUBIES[@]}"; do
dir="${dir%%/}"; ruby="${dir##*/}"
case "$ruby" in
"$1") match="$dir" && break ;;
*"$1"*) match="$dir" ;;
case "$ruby_name" in
"$1") match="$ruby_dir" && break ;;
*"$1"*) match="$ruby_dir" ;;
esac
done
done <<< $(chruby_list)

echo -n "$match"
}
Expand Down Expand Up @@ -91,23 +94,24 @@ function chruby()
{
case "$1" in
-h|--help)
echo "usage: chruby [--reload | RUBY|VERSION [RUBYOPT...] | system]"
echo "usage: chruby [RUBY|VERSION [RUBYOPT...] | system]"
;;
-V|--version)
echo "chruby: $CHRUBY_VERSION"
;;
--reload) chruby_init ;;
"")
local dir ruby
local ruby_dir ruby_name

for dir in "${CHRUBY_RUBIES[@]}"; do
dir="${dir%%/}"; ruby="${dir##*/}"
if [[ "$dir" == "$RUBY_ROOT" ]]; then
echo " * ${ruby}${RUBYOPT:+ $RUBYOPT}"
while IFS= read ruby_dir; do
ruby_dir="${ruby_dir%%/}"
ruby_name="${ruby_dir##*/}"

if [[ "$ruby_dir" == "$RUBY_ROOT" ]]; then
echo " * ${ruby_name}${RUBYOPT:+ $RUBYOPT}"
else
echo " ${ruby}"
echo " ${ruby_name}"
fi
done
done <<< $(chruby_list)
;;
system) chruby_reset ;;
*)
Expand All @@ -124,5 +128,3 @@ function chruby()
;;
esac
}

chruby_init
31 changes: 20 additions & 11 deletions test/unit/chruby_find_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

function setUp()
{
test_ruby_path1="/path/to/ruby1"
test_ruby_path2="/path/to/ruby2"
test_ruby1="ruby1"
test_ruby2="ruby2"

CHRUBY_RUBIES=("$test_ruby_path1" "$test_ruby_path2")
test_ruby_path1="${test_root_dir}/opt/rubies/${test_ruby1}"
test_ruby_path2="${test_home_dir}/.rubies/${test_ruby2}"

mkdir -p "$test_ruby_path1"
mkdir -p "$test_ruby_path2"
}

function test_chruby_find_with_exact_name()
{
local exact_match="${test_ruby_path2##*/}"
local exact_match="${test_ruby2}"
local result="$(chruby_find "$exact_match")"

assertEquals "did not return the correct ruby dir" \
Expand All @@ -19,17 +23,16 @@ function test_chruby_find_with_exact_name()

function test_chruby_find_with_exact_name_but_there_are_multiple_matches()
{
CHRUBY_RUBIES=(
"$test_ruby_path1"
"${test_ruby_path1}-foo"
"$test_ruby_path2"
"${test_ruby_path2}-bar"
)
mkdir -p "${test_root_dir}${test_ruby_path1}-foo"
mkdir -p "${test_home_dir}${test_ruby_path2}-bar"

local result="$(chruby_find "${test_ruby_path2##*/}")"
local result="$(chruby_find "$test_ruby2")"

assertEquals "did not use the exact match first" \
"$test_ruby_path2" "$result"

rmdir "${test_root_dir}${test_ruby_path1}-foo"
rmdir "${test_home_dir}${test_ruby_path2}-bar"
}

function test_chruby_find_with_a_substring()
Expand All @@ -54,4 +57,10 @@ function test_chruby_find_when_cannot_find_match()
assertEquals "did not return an empty string" "" "$result"
}

function tearDown()
{
rmdir "${test_ruby_path1}"
rmdir "${test_ruby_path2}"
}

SHUNIT_PARENT=$0 . $SHUNIT2
37 changes: 0 additions & 37 deletions test/unit/chruby_init_test.sh

This file was deleted.

46 changes: 14 additions & 32 deletions test/unit/chruby_test.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
. ./test/unit/helper.sh

function setUp()
{
original_rubies=("${CHRUBY_RUBIES[@]}")
}

function test_chruby_list_rubies()
{
local expected=" ${test_ruby_engine}-${test_ruby_version}"
local output="$(chruby)"

assertEquals "did not correctly list CHRUBY_RUBIES" \
assertEquals "did not correctly list the rubies in CHRUBY_DIRS" \
"$expected" "$output"
}

Expand All @@ -21,20 +16,27 @@ function test_chruby_list_rubies_when_a_ruby_is_active()
local expected=" * ${test_ruby_engine}-${test_ruby_version}"
local output="$(chruby)"

assertEquals "did not correctly list CHRUBY_RUBIES" \
assertEquals "did not correctly list the rubies in CHRUBY_DIRS" \
"$expected" "$output"
}

function test_chruby_list_rubies_when_one_contains_a_space()
{
local ruby_name="ruby"
local path_with_spaces="/path/with spaces/$ruby_name"
local ruby_name="ruby with spaces"
local ruby_dir="$test_home_dir/.rubies/$ruby_name"
mkdir -p "$ruby_dir"

CHRUBY_RUBIES=("$path_with_spaces")
local output="$(chruby)"
local expected="$(cat <<EOS
${test_ruby_engine}-${test_ruby_version}
${ruby_name}
EOS
)"

assertEquals "did not correctly handle paths containing spaces" \
" $ruby_name" "$output"
"$expected" "$output"

rmdir "$ruby_dir"
}

function test_chruby_with_X_Y()
Expand Down Expand Up @@ -68,27 +70,9 @@ function test_chruby_with_invalid_ruby()
assertEquals "did not return 1" 1 $?
}

function test_chruby_reload()
{
CHRUBY_RUBIES=()

chruby --reload

assertEquals "did not return 0" 0 $?
assertEquals "did not re-populate CHRUBY_RUBIES" 1 ${#CHRUBY_RUBIES[@]}

if [[ -n "$ZSH_VERSION" ]]; then
assertEquals "did not detect rubies in \$PREFIX/opt/rubies" \
"$test_ruby_root" "${CHRUBY_RUBIES[1]}"
else
assertEquals "did not detect rubies in \$PREFIX/opt/rubies" \
"$test_ruby_root" "${CHRUBY_RUBIES[0]}"
fi
}

function test_chruby_help()
{
local usage="usage: chruby [--reload | RUBY|VERSION [RUBYOPT...] | system]"
local usage="usage: chruby [RUBY|VERSION [RUBYOPT...] | system]"
local output="$(chruby --help)"

assertEquals "did not output the chruby usage string" \
Expand All @@ -106,8 +90,6 @@ function test_chruby_version()
function tearDown()
{
chruby_reset

CHRUBY_RUBIES=("${original_rubies[@]}")
}

SHUNIT_PARENT=$0 . $SHUNIT2
3 changes: 2 additions & 1 deletion test/unit/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
test_dir="$PWD/test/unit"
test_fixtures_dir="$test_dir/fixtures"
test_root_dir="$test_fixtures_dir/root"
test_home_dir="$test_root_dir/home"

export PREFIX="$test_root_dir"
export HOME="$test_root_dir/home"
export HOME="$test_home_dir"
export PATH="$PWD/bin:$PATH"
hash -r

Expand Down

0 comments on commit c87f4cf

Please sign in to comment.