Skip to content

Natural sort RUBIES by version. #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ install:
- sudo apt-get install shunit2 zsh ruby

before_script:
- echo yes | rvm implode
- sudo rm -rf $HOME/.rvm $HOME/.rvmrc /etc/rvmrc /etc/profile.d/rvm.sh /usr/local/rvm /usr/local/bin/rvm
- __rvm_unload

script: make test
19 changes: 14 additions & 5 deletions share/chruby/chruby.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
CHRUBY_VERSION="0.3.8"
RUBIES=()

for dir in "$PREFIX/opt/rubies" "$HOME/.rubies"; do
[[ -d "$dir" && -n "$(ls -A "$dir")" ]] && RUBIES+=("$dir"/*)
done
unset dir
function chruby_rubies()
{
local rubies dir ruby version
rubies=()
for dir in "$@"; do
[[ -d "$dir" && -n "$(ls -A "$dir")" ]] && for ruby in "$dir"/*; do
rubies+=("$(basename "$ruby" | tr "-" ".")\t$ruby")
done
done

printf "%b\n" "${rubies[@]}" | sort -t "." -k "1,1" -k "2,2n" -k "3,3n" -k "4,4n" | cut -f 2
}

RUBIES=($(chruby_rubies "$PREFIX/opt/rubies" "$HOME/.rubies"))

function chruby_reset()
{
Expand Down
19 changes: 15 additions & 4 deletions test/chruby_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ function tearDown()
RUBIES=(${original_rubies[@]})
}

function test_chruby_default_RUBIES()
function test_chruby_rubies_sort_order()
{
assertEquals "did not correctly populate RUBIES" \
"$test_ruby_root" \
"${RUBIES[*]}"
actual_order=""
for ruby in "${RUBIES[@]}"; do
actual_order="$actual_order ${ruby##*/}"
done
actual_order="${actual_order##?}"

assertEquals "did not natural version sort" "$test_rubies_order" "$actual_order"
}

function test_chruby_rubies_spaces()
{
detected_ruby=$(chruby_rubies "${test_ruby_dir%/*}/r u b i e s")

assertEquals "did not work with spaces in RUBIES path" "ruby-2.1.2" "${detected_ruby##*/}"
}

function test_chruby_2_0()
Expand Down
4 changes: 3 additions & 1 deletion test/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ test_ruby_engine="ruby"
test_ruby_version="2.0.0"
test_ruby_patchlevel="353"
test_ruby_api="2.0.0"
test_ruby_root="$PWD/test/opt/rubies/$test_ruby_engine-$test_ruby_version-p$test_ruby_patchlevel"
test_ruby_dir="$PWD/test/opt/rubies"
test_ruby_root="$test_ruby_dir/$test_ruby_engine-$test_ruby_version-p$test_ruby_patchlevel"
test_rubies_order="jruby-1.7.9 jruby-1.7.10 rbx-2.2.9 rbx-2.2.10 rbx-10.0.0 ruby-1.9.3-p547 ruby-2.0.0-p0 ruby-2.0.0-p195 ruby-2.0.0-p247 ruby-2.0.0-p353 ruby-2.1.0 ruby-2.1.1 ruby-2.1.2"

test_path="$PATH"
test_gem_home="$HOME/.gem/$test_ruby_engine/$test_ruby_version"
Expand Down
6 changes: 6 additions & 0 deletions test/setup
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ test_ruby_root="$test_ruby_engine-$test_ruby_version-p$test_ruby_patchlevel"
mkdir -p "$PREFIX/opt/rubies"
cd "$PREFIX/opt/rubies"

mkdir -p "jruby-1.7.9" "jruby-1.7.10" "rbx-2.2.9" "rbx-2.2.10" "rbx-10.0.0" \
"ruby-1.9.3-p547" "ruby-2.0.0-p0" "ruby-2.0.0-p195" "ruby-2.0.0-p247" \
"ruby-2.0.0-p353" "ruby-2.1.0" "ruby-2.1.1" "ruby-2.1.2"

mkdir -p "$PREFIX/opt/r u b i e s/ruby-2.1.2"

log "Downloading $test_ruby_url ..."
download "$test_ruby_url" "$test_ruby_archive" || fail "Download failed"

Expand Down