diff --git a/ChangeLog.md b/ChangeLog.md index e857bbc8..43ed8164 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,3 +1,22 @@ +### 1.0.0 + +#### Upgrade Notes + +* When updating from `chruby` < 1.0 to `chruby` >= 1.0, you will need to + re-install your gems because the gem directories will be different. It is also + recommended to clean old gem directories under `~/.gem` which will no longer + be used to save disk space. To remove them, you can use + `rm -rf ~/.gem/{ruby,jruby,rbx,truffleruby}/[0-9].[0-9].[0-9]` + +#### chruby.sh + +* Use the installed Ruby directory name for setting `GEM_HOME`. (@eregon) + This guarantees a unique `GEM_HOME` per installed Ruby, even if they have + the same `RUBY_VERSION`. This is important for native extensions, which + might compile differently based on build-time flags such as `--enable-shared`. + This also fixes the bug that the `GEM_HOME` for non-MRI Ruby implementations + was shared even for different releases. + ### 0.3.9 / 2014-11-23 #### chruby.sh diff --git a/Makefile b/Makefile index c8502798..84ce578b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME=chruby -VERSION=0.3.9 +VERSION=1.0.0 AUTHOR=postmodern URL=https://github.com/$(AUTHOR)/$(NAME) @@ -22,6 +22,15 @@ all: pkg: mkdir $(PKG_DIR) +share/man/man1/chruby.1: doc/man/chruby.1.md + kramdown-man share/man/man1/chruby.1 + +share/man/man1/chruby-exec.1: doc/man/chruby-exec.1.md + kramdown-man share/man/man1/chruby-exec.1 + +update-man: share/man/man1/chruby.1 share/man/man1/chruby-exec.1 + git commit -m "Updated the man pages" doc/man/chruby.1.md share/man/man1/chruby.1 doc/man/chruby-exec.1.md share/man/man1/chruby-exec.1 + download: pkg wget -O $(PKG) $(URL)/archive/v$(VERSION).tar.gz @@ -80,4 +89,4 @@ uninstall: rm -rf $(DESTDIR)$(PREFIX)/$(DOC_DIR) rmdir $(DESTDIR)$(PREFIX)/$(SHARE_DIR)/chruby -.PHONY: build download sign verify clean check configure_tests test integration_tests gauntlet tag release rpm install uninstall all +.PHONY: build man download sign verify clean check configure_tests test integration_tests gauntlet tag release rpm install uninstall all diff --git a/README.md b/README.md index 107e1a33..99b9d9d8 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ Changes the current Ruby. * Updates `$PATH`. * Also adds RubyGems `bin/` directories to `$PATH`. * Correctly sets `$GEM_HOME` and `$GEM_PATH`. - * Users: gems are installed into `~/.gem/$ruby/$version`. - * Root: gems are installed directly into `/path/to/$ruby/$gemdir`. + * Users: gems are installed into `~/.gem/$ruby_name` (e.g., `~/.gem/ruby-2.6.3` for `~/.rubies/ruby-2.6.3`). + * Root: gems are installed directly into `$ruby_install_dir/$gemdir` (the default). * Additionally sets `$RUBY_ROOT`, `$RUBY_ENGINE`, `$RUBY_VERSION` and `$GEM_ROOT`. * Optionally sets `$RUBYOPT` if second argument is given. @@ -218,13 +218,13 @@ If you have enabled auto-switching, simply create a `.ruby-version` file: ### RubyGems Gems installed as a non-root user via `gem install` will be installed into -`~/.gem/$ruby/X.Y.Z`. By default, RubyGems will use the absolute path to the -currently selected ruby for the shebang of any binstubs it generates. In some -cases, this path may contain extra version information (e.g. +`~/.gem/$ruby_name`. By default, RubyGems will use the +absolute path to the currently selected ruby for the shebang of any binstubs it +generates. In some cases, this path may contain extra version information (e.g. `ruby-2.0.0-p451`). To mitigate potential problems when removing rubies, you can force RubyGems to generate binstubs with shebangs that will search for ruby in your `$PATH` by using `gem install --env-shebang` (or the equivalent -short option `-E`). This parameter can also be added to your gemrc file. +short option `-E`). This parameter can also be added to your `.gemrc` file. ### Integration @@ -269,7 +269,7 @@ Select a Ruby: - ruby - x86_64-linux - GEM PATHS: - - /home/hal/.gem/ruby/1.9.3 + - /home/hal/.gem/ruby-1.9.3-p392 - /opt/rubies/ruby-1.9.3-p392/lib/ruby/gems/1.9.1 - GEM CONFIGURATION: - :update_sources => true @@ -299,7 +299,7 @@ Run a command under a Ruby with `chruby-exec`: Switch to an arbitrary Ruby on the fly: - $ chruby_use /path/to/ruby + $ chruby_set /path/to/ruby ## Uninstall diff --git a/benchmarks/chruby_use.sh b/benchmarks/chruby_set.sh similarity index 90% rename from benchmarks/chruby_use.sh rename to benchmarks/chruby_set.sh index ff60bf7e..40fccf7a 100755 --- a/benchmarks/chruby_use.sh +++ b/benchmarks/chruby_set.sh @@ -9,7 +9,7 @@ for i in {1..3}; do time ( for ((i=0; i<$n; i+=1)); do - chruby_use "$ruby_dir" + chruby_set "$ruby_dir" done ) done diff --git a/doc/man/chruby-exec.1.md b/doc/man/chruby-exec.1.md new file mode 100644 index 00000000..a06cd4fd --- /dev/null +++ b/doc/man/chruby-exec.1.md @@ -0,0 +1,76 @@ +# chruby-exec(1) -- Run a command with selected Ruby. + +## SYNOPSIS + +`chruby-exec` RUBY [RUBYOPTS] -- COMMAND + +## ARGUMENTS + +*RUBY* + Change current Ruby based on fuzzy matching of Ruby by name. + +*RUBYOPTS* + Additional optional arguments to pass to Ruby. + +*COMMAND* + Command to run under the selected Ruby. + +## OPTIONS + +`-h`, `--help` + +`-v`, `--version` + +## DESCRIPTION +Run a command with the selected Ruby version by correctly setting the appropriate environment variables. + +[https://github.com/postmodern/chruby/blob/master/README.md](https://github.com/postmodern/chruby/blob/master/README.md) + +## EXAMPLES + +Run the command `gem update` under JRuby: + $ chruby-exec jruby -- gem update + +## FILES + +*/opt/rubies* + Primary default Ruby install location. + +*~/.rubies/* + Secondary default Ruby install location. + +*/etc/profile.d/chruby.sh* + Application environment settings for chruby. + +*~/.gem/$ruby/$version* + Default gem install location. + +## ENVIRONMENT + +*PATH* + Updates the PATH environment variable to include Rubies and RubyGems bin/ directories. + +*GEM_HOME* + Default repository location for gem installation. + +*GEM_PATH* + A colon-separated list of gem repository directories. + +*GEM_ROOT* + +*RUBY_ROOT* + +*RUBY_ENGINE* + Name of Ruby implementation. + +*RUBY_VERSION* + Ruby version number. + +*RUBYOPT* + Optionally set if additional Ruby options are given. + +## AUTHOR +Postmodern [postmodern.mod3\@gmail.com](mailto:postmodern.mod3\@gmail.com). + +## SEE ALSO +chruby(1), ruby(1), gem(1) diff --git a/doc/man/chruby.1.md b/doc/man/chruby.1.md new file mode 100644 index 00000000..56bd0b79 --- /dev/null +++ b/doc/man/chruby.1.md @@ -0,0 +1,95 @@ +# chruby(1) -- Changes the current Ruby. + +## SYNOPSIS + +`chruby` [OPTIONS | [] | system] + +## ARGUMENTS + +*RUBY|VERSION* + Change current Ruby based on fuzzy matching of Ruby by name. + +*system* + Change current Ruby to system Ruby. + +*RUBY_OPTS* + Additional optional arguments to pass to Ruby. + +## OPTIONS + +`-h`, `--help` + +`-v`, `--version` + +## DESCRIPTION +Changes the current Ruby version by correctly setting the appropriate environment variables. + +[https://github.com/postmodern/chruby/blob/master/README.md](https://github.com/postmodern/chruby/blob/master/README.md) + +## EXAMPLES + +List available Rubies: + $ chruby + ruby-1.9.3-p362 + jruby-1.7.2 + rubinius-2.0.0-rc1 + +Select a Ruby: + $ chruby 1.9.3 + $ chruby + * ruby-1.9.3-p362 + jruby-1.7.2 + rubinius-2.0.0-rc1 + +Switch to JRuby in 1.9 mode: + $ chruby jruby --1.9 + +Switch back to system Ruby: + $ chruby system + +Switch to an arbitrary Ruby on the fly: + $ chruby_set /path/to/ruby + +## FILES + +*/opt/rubies* + Primary default Ruby install location. + +*~/.rubies/* + Secondary default Ruby install location. + +*/etc/profile.d/chruby.sh* + Application environment settings for chruby. + +*~/.gem/$ruby/$version* + Default gem install location. + +## ENVIRONMENT + +*PATH* + Updates the PATH environment variable to include Rubies and RubyGems bin/ directories. + +*GEM_HOME* + Default repository location for gem installation. + +*GEM_PATH* + A colon-separated list of gem repository directories. + +*GEM_ROOT* + +*RUBY_ROOT* + +*RUBY_ENGINE* + Name of Ruby implementation. + +*RUBY_VERSION* + Ruby version number. + +*RUBYOPT* + Optionally set if additional Ruby options are given. + +## AUTHOR +Postmodern [postmodern.mod3\@gmail.com](mailto:postmodern.mod3\@gmail.com). + +## SEE ALSO +chruby-exec(1), ruby(1), gem(1) diff --git a/share/chruby/auto.sh b/share/chruby/auto.sh index 88f3cb34..1fb5247f 100644 --- a/share/chruby/auto.sh +++ b/share/chruby/auto.sh @@ -1,6 +1,7 @@ -unset RUBY_AUTO_VERSION +unset CHRUBY_AUTO_VERSION -function chruby_auto() { +function chruby_auto() +{ local dir="$PWD/" version until [[ -z "$dir" ]]; do @@ -9,18 +10,19 @@ function chruby_auto() { if { read -r version <"$dir/.ruby-version"; } 2>/dev/null || [[ -n "$version" ]]; then version="${version%%[[:space:]]}" - if [[ "$version" == "$RUBY_AUTO_VERSION" ]]; then return + if [[ "$version" == "$CHRUBY_AUTO_VERSION" ]]; then + return else - RUBY_AUTO_VERSION="$version" + CHRUBY_AUTO_VERSION="$version" chruby "$version" return $? fi fi done - if [[ -n "$RUBY_AUTO_VERSION" ]]; then + if [[ -n "$CHRUBY_AUTO_VERSION" ]]; then chruby_reset - unset RUBY_AUTO_VERSION + unset CHRUBY_AUTO_VERSION fi } diff --git a/share/chruby/chruby.sh b/share/chruby/chruby.sh index 7b2b4895..604e388d 100644 --- a/share/chruby/chruby.sh +++ b/share/chruby/chruby.sh @@ -1,10 +1,35 @@ -CHRUBY_VERSION="0.3.9" -RUBIES=() +CHRUBY_VERSION="1.0.0" +CHRUBY_DIRS=("$PREFIX/opt/rubies" "$HOME/.rubies") -for dir in "$PREFIX/opt/rubies" "$HOME/.rubies"; do - [[ -d "$dir" && -n "$(command ls -A "$dir")" ]] && RUBIES+=("$dir"/*) -done -unset dir +function chruby_list() +{ + local rubies_dir ruby_dir + + 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 ruby_dir ruby_name match + + while IFS= read -r ruby_dir; do + ruby_dir="${ruby_dir%%/}" + ruby_name="${ruby_dir##*/}" + + case "$ruby_name" in + "$1") match="$ruby_dir" && break ;; + *"$1"*) match="$ruby_dir" ;; + esac + done <<< "$(chruby_list)" + + echo -n "$match" +} function chruby_reset() { @@ -30,16 +55,21 @@ function chruby_reset() hash -r } -function chruby_use() +function chruby_gem_home() { echo -n "$HOME/.gem/rubies/${RUBY_ROOT##*/}"; } +function chruby_gem_path() { echo -n "$GEM_HOME${GEM_ROOT:+:$GEM_ROOT}${GEM_PATH:+:$GEM_PATH}"; } + +function chruby_set() { - if [[ ! -x "$1/bin/ruby" ]]; then - echo "chruby: $1/bin/ruby not executable" >&2 + local ruby_dir="$1" + + if [[ ! -x "$ruby_dir/bin/ruby" ]]; then + echo "chruby: $ruby_dir/bin/ruby not executable" >&2 return 1 fi [[ -n "$RUBY_ROOT" ]] && chruby_reset - export RUBY_ROOT="$1" + export RUBY_ROOT="$ruby_dir" export RUBYOPT="$2" export PATH="$RUBY_ROOT/bin:$PATH" @@ -52,8 +82,8 @@ EOF export PATH="${GEM_ROOT:+$GEM_ROOT/bin:}$PATH" if (( UID != 0 )); then - export GEM_HOME="$HOME/.gem/$RUBY_ENGINE/$RUBY_VERSION" - export GEM_PATH="$GEM_HOME${GEM_ROOT:+:$GEM_ROOT}${GEM_PATH:+:$GEM_PATH}" + export GEM_HOME="$(chruby_gem_home)" + export GEM_PATH="$(chruby_gem_path)" export PATH="$GEM_HOME/bin:$PATH" fi @@ -64,41 +94,37 @@ function chruby() { case "$1" in -h|--help) - echo "usage: chruby [RUBY|VERSION|system] [RUBYOPT...]" + echo "usage: chruby [RUBY|VERSION [RUBYOPT...] | system]" ;; -V|--version) echo "chruby: $CHRUBY_VERSION" ;; "") - local dir ruby - for dir in "${RUBIES[@]}"; do - dir="${dir%%/}"; ruby="${dir##*/}" - if [[ "$dir" == "$RUBY_ROOT" ]]; then - echo " * ${ruby} ${RUBYOPT}" + local ruby_dir ruby_name + + while IFS= read -r 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 ;; *) - local dir ruby match - for dir in "${RUBIES[@]}"; do - dir="${dir%%/}"; ruby="${dir##*/}" - case "$ruby" in - "$1") match="$dir" && break ;; - *"$1"*) match="$dir" ;; - esac - done - - if [[ -z "$match" ]]; then - echo "chruby: unknown Ruby: $1" >&2 + local ruby="$1" + local ruby_dir="$(chruby_find "$ruby")" + + if [[ -z "$ruby_dir" ]]; then + echo "chruby: unknown Ruby: $ruby" >&2 return 1 fi shift - chruby_use "$match" "$*" + chruby_set "$ruby_dir" "$*" ;; esac } diff --git a/share/man/man1/chruby-exec.1 b/share/man/man1/chruby-exec.1 new file mode 100644 index 00000000..47704d2f --- /dev/null +++ b/share/man/man1/chruby-exec.1 @@ -0,0 +1,78 @@ +.TH +.BR chruby-exec (1) +\-\- Run a command with selected Ruby. +.SH SYNOPSIS +.PP +\fB\fCchruby-exec\fR RUBY [RUBYOPTS] \-\- COMMAND +.SH ARGUMENTS +.PP +\fIRUBY\fP + Change current Ruby based on fuzzy matching of Ruby by name. +.PP +\fIRUBYOPTS\fP + Additional optional arguements to pass to Ruby. +.PP +\fICOMMAND\fP + Command to run under the selected Ruby. +.SH OPTIONS +.PP +\fB\fC-h\fR, \fB\fC--help\fR +.PP +\fB\fC-v\fR, \fB\fC--version\fR +.SH DESCRIPTION +.PP +Run a command with the selected Ruby version by correctly setting the appropriate environment variables. +.PP +https://github.com/postmodern/chruby/blob/master/README.md +.UR https://github.com/postmodern/chruby/blob/master/README.md +.UE +.SH EXAMPLES +.PP +Run the command \fB\fCgem update\fR under JRuby: + $ chruby\-exec jruby \-\- gem update +.SH FILES +.PP +\fI/opt/rubies\fP + Primary default Ruby install location. +.PP +\fI~/.rubies/\fP + Secondary default Ruby install location. +.PP +\fI/etc/profile.d/chruby.sh\fP + Application environment settings for chruby. +.PP +\fI~/.gem/$ruby/$version\fP + Default gem install location. +.SH ENVIRONMENT +.PP +\fIPATH\fP + Updates the PATH environment variable to include Rubies and RubyGems bin/ directories. +.PP +\fIGEM_HOME\fP + Default repository location for gem installation. +.PP +\fIGEM_PATH\fP + A colon\-separated list of gem repository directories. +.PP +\fIGEM_ROOT\fP +.PP +\fIRUBY_ROOT\fP +.PP +\fIRUBY_ENGINE\fP + Name of Ruby implementation. +.PP +\fIRUBY_VERSION\fP + Ruby version number. +.PP +\fIRUBYOPT\fP + Optionally set if additional Ruby options are given. +.SH AUTHOR +.PP +Postmodern postmodern.mod3\@gmail.com +.MT postmodern.mod3@gmail.com +.ME . +.SH SEE ALSO +.PP +.BR chruby (1), +.BR ruby (1), +.BR gem (1) \ No newline at end of file diff --git a/share/man/man1/chruby.1 b/share/man/man1/chruby.1 new file mode 100644 index 00000000..33df162d --- /dev/null +++ b/share/man/man1/chruby.1 @@ -0,0 +1,94 @@ +.TH +.BR chruby (1) +\-\- Changes the current Ruby. +.SH SYNOPSIS +.PP +\fB\fCchruby\fR [] [] +.SH ARGUMENTS +.PP +\fIRUBY|VERSION\fP + Change current Ruby based on fuzzy matching of Ruby by name. +.PP +\fIsystem\fP + Change current Ruby to system Ruby. +.PP +\fIRUBY_OPTS\fP + Additional optional arguements to pass to Ruby. +.SH OPTIONS +.PP +\fB\fC-h\fR, \fB\fC--help\fR +.SH DESCRIPTION +.PP +Changes the current Ruby version by correctly setting the appropriate environment variables. +.PP +https://github.com/postmodern/chruby/blob/master/README.md +.UR https://github.com/postmodern/chruby/blob/master/README.md +.UE +.SH EXAMPLES +.PP +List available Rubies: + $ chruby + ruby\-1.9.3\-p362 + jruby\-1.7.2 + rubinius\-2.0.0\-rc1 +.PP +Select a Ruby: + $ chruby 1.9.3 + $ chruby + * ruby\-1.9.3\-p362 + jruby\-1.7.2 + rubinius\-2.0.0\-rc1 +.PP +Switch to JRuby in 1.9 mode: + $ chruby jruby \-\-1.9 +.PP +Switch back to system Ruby: + $ chruby system +.PP +Switch to an arbitrary Ruby on the fly: + $ chruby_set /path/to/ruby +.SH FILES +.PP +\fI/opt/rubies\fP + Primary default Ruby install location. +.PP +\fI~/.rubies/\fP + Secondary default Ruby install location. +.PP +\fI/etc/profile.d/chruby.sh\fP + Application environment settings for chruby. +.PP +\fI~/.gem/$ruby/$version\fP + Default gem install location. +.SH ENVIRONMENT +.PP +\fIPATH\fP + Updates the PATH environment variable to include Rubies and RubyGems bin/ directories. +.PP +\fIGEM_HOME\fP + Default repository location for gem installation. +.PP +\fIGEM_PATH\fP + A colon\-separated list of gem repository directories. +.PP +\fIGEM_ROOT\fP +.PP +\fIRUBY_ROOT\fP +.PP +\fIRUBY_ENGINE\fP + Name of Ruby implementation. +.PP +\fIRUBY_VERSION\fP + Ruby version number. +.PP +\fIRUBYOPT\fP + Optionally set if additional Ruby options are given. +.SH AUTHOR +.PP +Postmodern postmodern.mod3\@gmail.com +.MT postmodern.mod3@gmail.com +.ME . +.SH SEE ALSO +.PP +.BR ruby (1), +.BR gem (1) diff --git a/test/unit/chruby_auto_test.sh b/test/unit/chruby_auto_test.sh index 46729627..22c6f135 100755 --- a/test/unit/chruby_auto_test.sh +++ b/test/unit/chruby_auto_test.sh @@ -25,7 +25,7 @@ function setUp() echo "system" > "$test_auto_version_dir/sub_versioned/.ruby-version" chruby_reset - unset RUBY_AUTO_VERSION + unset CHRUBY_AUTO_VERSION } function test_chruby_auto_loaded_in_zsh() @@ -61,12 +61,12 @@ function test_chruby_auto_loaded_twice_in_zsh() function test_chruby_auto_loaded_twice() { - RUBY_AUTO_VERSION="dirty" + CHRUBY_AUTO_VERSION="dirty" PROMPT_COMMAND="chruby_auto" . ./share/chruby/auto.sh - assertNull "RUBY_AUTO_VERSION was not unset" "$RUBY_AUTO_VERSION" + assertNull "CHRUBY_AUTO_VERSION was not unset" "$CHRUBY_AUTO_VERSION" } function test_chruby_auto_enter_project_dir() @@ -142,8 +142,8 @@ function test_chruby_auto_unknown_ruby() assertEquals "did not keep the current Ruby when loading an unknown version" \ "$test_ruby_root" "$RUBY_ROOT" - assertEquals "did not set RUBY_AUTO_VERSION" \ - "$expected_auto_version" "$RUBY_AUTO_VERSION" + assertEquals "did not set CHRUBY_AUTO_VERSION" \ + "$expected_auto_version" "$CHRUBY_AUTO_VERSION" } function test_chruby_auto_ruby_version_containing_options() @@ -155,8 +155,8 @@ function test_chruby_auto_ruby_version_containing_options() assertEquals "did not keep the current Ruby when loading an unknown version" \ "$test_ruby_root" "$RUBY_ROOT" - assertEquals "did not set RUBY_AUTO_VERSION" \ - "$expected_auto_version" "$RUBY_AUTO_VERSION" + assertEquals "did not set CHRUBY_AUTO_VERSION" \ + "$expected_auto_version" "$CHRUBY_AUTO_VERSION" } function tearDown() diff --git a/test/unit/chruby_find_test.sh b/test/unit/chruby_find_test.sh new file mode 100755 index 00000000..2bf9d188 --- /dev/null +++ b/test/unit/chruby_find_test.sh @@ -0,0 +1,66 @@ +. ./test/unit/helper.sh + +function setUp() +{ + test_ruby1="ruby1" + test_ruby2="ruby2" + + 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_ruby2}" + local result="$(chruby_find "$exact_match")" + + assertEquals "did not return the correct ruby dir" \ + "$test_ruby_path2" "$result" +} + +function test_chruby_find_with_exact_name_but_there_are_multiple_matches() +{ + mkdir -p "${test_root_dir}${test_ruby_path1}-foo" + mkdir -p "${test_home_dir}${test_ruby_path2}-bar" + + 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() +{ + local result="$(chruby_find "2")" + + assertEquals "did not return the correct ruby dir" \ + "$test_ruby_path2" "$result" +} + +function test_chruby_find_when_there_are_multiple_matches() +{ + local result="$(chruby_find "ruby")" + + assertEquals "did not use the last match" "$test_ruby_path2" "$result" +} + +function test_chruby_find_when_cannot_find_match() +{ + local result="$(chruby_find "foo")" + + assertEquals "did not return an empty string" "" "$result" +} + +function tearDown() +{ + rmdir "${test_ruby_path1}" + rmdir "${test_ruby_path2}" +} + +SHUNIT_PARENT=$0 . $SHUNIT2 diff --git a/test/unit/chruby_reset_test.sh b/test/unit/chruby_reset_test.sh index 5e19ed60..ee2f5422 100755 --- a/test/unit/chruby_reset_test.sh +++ b/test/unit/chruby_reset_test.sh @@ -2,7 +2,7 @@ function setUp() { - chruby_use "$test_ruby_root" >/dev/null + chruby_set "$test_ruby_root" >/dev/null export PATH="$GEM_HOME/bin:$GEM_ROOT/bin:$RUBY_ROOT/bin:$original_path" } diff --git a/test/unit/chruby_use_test.sh b/test/unit/chruby_set_test.sh similarity index 73% rename from test/unit/chruby_use_test.sh rename to test/unit/chruby_set_test.sh index 7cd8e049..957f4c42 100755 --- a/test/unit/chruby_use_test.sh +++ b/test/unit/chruby_set_test.sh @@ -1,23 +1,35 @@ . ./test/unit/helper.sh +function setUp() +{ + chruby_set "$test_ruby_root" >/dev/null +} + function test_chruby_clears_hash_table() { if [[ -n "$ZSH_VERSION" ]]; then - chruby_use "$test_ruby_root" >/dev/null + chruby_set "$test_ruby_root" >/dev/null assertEquals "did not clear the path table" \ "" "$(hash)" elif [[ -n "$BASH_VERSION" ]]; then - chruby_use "$test_ruby_root" >/dev/null + chruby_set "$test_ruby_root" >/dev/null assertEquals "did not clear the path table" \ "hash: hash table empty" "$(hash)" fi } -function test_chruby_use_env_variables() +function test_chruby_set_with_invalid_ruby_path() +{ + chruby_set "/path/to/fake/ruby" 2>/dev/null + + assertEquals "did not return an error code" 1 $? +} + +function test_chruby_set_env_variables() { - chruby_use "$test_ruby_root" >/dev/null + chruby_set "$test_ruby_root" >/dev/null assertEquals "invalid RUBY_ROOT" "$test_ruby_root" "$RUBY_ROOT" assertEquals "invalid RUBY_ENGINE" "$test_ruby_engine" "$RUBY_ENGINE" diff --git a/test/unit/chruby_test.sh b/test/unit/chruby_test.sh index e466fa58..1124ec31 100755 --- a/test/unit/chruby_test.sh +++ b/test/unit/chruby_test.sh @@ -1,45 +1,52 @@ . ./test/unit/helper.sh -function setUp() +function test_chruby_list_rubies() { - original_rubies=(${RUBIES[@]}) -} + local expected=" ${test_ruby_engine}-${test_ruby_version}" + local output="$(chruby)" -function test_chruby_default_RUBIES() -{ - assertEquals "did not correctly populate RUBIES" \ - "$test_ruby_root" \ - "${RUBIES[@]}" + assertEquals "did not correctly list the rubies in CHRUBY_DIRS" \ + "$expected" "$output" } -function test_chruby_X_Y() +function test_chruby_list_rubies_when_a_ruby_is_active() { - chruby "$test_ruby_version_x_y" >/dev/null + chruby "${test_ruby_engine}-${test_ruby_version}" - assertEquals "did not match $test_ruby_version_x_y" "$test_ruby_root" "$RUBY_ROOT" + local expected=" * ${test_ruby_engine}-${test_ruby_version}" + local output="$(chruby)" + + assertEquals "did not correctly list the rubies in CHRUBY_DIRS" \ + "$expected" "$output" } -function test_chruby_multiple_matches() +function test_chruby_list_rubies_when_one_contains_a_space() { - local fake_ruby="/path/to/ruby-${test_ruby_version_x_y}" + local ruby_name="ruby with spaces" + local ruby_dir="$test_home_dir/.rubies/$ruby_name" + mkdir -p "$ruby_dir" - RUBIES=("$fake_ruby" "$test_ruby_root") + local output="$(chruby)" + local expected="$(cat </dev/null + assertEquals "did not correctly handle paths containing spaces" \ + "$expected" "$output" - assertEquals "did not use the last match" "$test_ruby_root" "$RUBY_ROOT" + rmdir "$ruby_dir" } -function test_chruby_exact_match_first() +function test_chruby_with_X_Y() { - RUBIES=("$test_ruby_root" "${test_ruby_root}-rc1") - - chruby "${test_ruby_root##*/}" + chruby "$test_ruby_version_x_y" >/dev/null - assertEquals "did not use the exact match" "$test_ruby_root" "$RUBY_ROOT" + assertEquals "did not match $test_ruby_version_x_y" "$test_ruby_root" "$RUBY_ROOT" } -function test_chruby_system() +function test_chruby_with_system() { chruby "$test_ruby_version" >/dev/null chruby system @@ -47,27 +54,42 @@ function test_chruby_system() assertNull "did not reset the Ruby" "$RUBY_ROOT" } -function test_chruby_unknown() +function test_chruby_with_unknown_ruby() { chruby "does_not_exist" 2>/dev/null assertEquals "did not return 1" 1 $? } -function test_chruby_invalid_ruby() +function test_chruby_with_invalid_ruby() { - RUBIES=(/does/not/exist/jruby) + CHRUBY_RUBIES=(/does/not/exist/jruby) chruby "jruby" 2>/dev/null assertEquals "did not return 1" 1 $? } +function test_chruby_help() +{ + local usage="usage: chruby [RUBY|VERSION [RUBYOPT...] | system]" + local output="$(chruby --help)" + + assertEquals "did not output the chruby usage string" \ + "$usage" "$output" +} + +function test_chruby_version() +{ + local output="$(chruby --version)" + + assertEquals "did not output the chruby version" \ + "chruby: $CHRUBY_VERSION" "$output" +} + function tearDown() { chruby_reset - - RUBIES=(${original_rubies[@]}) } SHUNIT_PARENT=$0 . $SHUNIT2 diff --git a/test/unit/config.sh b/test/unit/config.sh index f5a5370e..d54f5d81 100644 --- a/test/unit/config.sh +++ b/test/unit/config.sh @@ -11,7 +11,7 @@ EOF )" else test_ruby_engine="${TEST_RUBY_ENGINE:-ruby}" - test_ruby_version="${TEST_RUBY_VERSION:-2.2.5}" + test_ruby_version="${TEST_RUBY_VERSION:-3.0.0}" test_ruby_root="$test_fixtures_dir/root/opt/rubies/$test_ruby_engine-$test_ruby_version" test_ruby_api="${TEST_RUBY_API:-${test_ruby_version%.*}.0}" test_gem_root="$test_ruby_root/lib/ruby/gems/$test_ruby_api" @@ -19,4 +19,4 @@ fi test_ruby_version_x_y="${test_ruby_version%.*}" -test_gem_home="$HOME/.gem/$test_ruby_engine/$test_ruby_version" +test_gem_home="$HOME/.gem/rubies/$test_ruby_engine-$test_ruby_version" diff --git a/test/unit/configure b/test/unit/configure index a08ee75c..01f85d00 100755 --- a/test/unit/configure +++ b/test/unit/configure @@ -31,7 +31,7 @@ if [[ "\$1" == "-e" ]]; then echo \$PPID ;; "print RUBY_VERSION") - echo -n 2.2.5 + echo -n 3.0.0 ;; *) echo "error: no stub for ruby expression: '\$2'" >&2 diff --git a/test/unit/helper.sh b/test/unit/helper.sh index 218c89ec..95c67354 100644 --- a/test/unit/helper.sh +++ b/test/unit/helper.sh @@ -4,12 +4,15 @@ 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 +test_path="$PATH" + unset GEM_HOME GEM_PATH . ./test/unit/config.sh