├── .gitignore ├── .travis.yml ├── CONTRIBUTING.markdown ├── MIT-LICENSE ├── README.markdown ├── bin ├── rbenv-alias └── rbenv-unalias ├── etc └── rbenv.d │ ├── install │ └── autoalias.bash │ └── uninstall │ └── autoalias.bash └── test ├── alias.bats ├── bin ├── nodenv-uninstall ├── rbenv-install └── rbenv-uninstall ├── commands.bats ├── help.bats ├── install.bats ├── run ├── setup_rbenv_and_bats ├── test_helper.bash ├── unalias.bats └── uninstall.bats /.gitignore: -------------------------------------------------------------------------------- 1 | bats/ 2 | rbenv/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | script: test/run 2 | # skips unnecessary Ruby-specific setup 3 | language: c 4 | -------------------------------------------------------------------------------- /CONTRIBUTING.markdown: -------------------------------------------------------------------------------- 1 | [Don't screw up the commit message.][commit messages] It's all I ask. 2 | 3 | [commit messages]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html 4 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) Tim Pope 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # Aliases for rbenv Ruby versions 2 | 3 | Invoke `rbenv alias ` to make a symbolic link from `` to 4 | `` in the [rbenv][] versions directory, effectively creating an 5 | alias. The cool part is that if you pass in a point release as the name, you 6 | can give `--auto` to link to the latest installed patch level. For example, 7 | `rbenv alias 1.8.7 --auto` will automatically create an alias from `1.8.7` to 8 | `1.8.7-p371` (or whatever the most recent version you have installed is). 9 | 10 | Plus, if you're using [ruby-build][], `rbenv install A.B.C-pXXX` automatically 11 | invokes `rbenv alias A.B.C --auto`, so you'll always have up to date aliases 12 | for point releases. 13 | 14 | Whether it's a good idea to use these aliases in a `.ruby-version` file, I 15 | cannot say. I created this plugin to find out. If your only concern is 16 | having to reinstall gems every time you install a new patch release, check out 17 | [rbenv-communal-gems][]. 18 | 19 | ## Installation 20 | 21 | mkdir -p "$(rbenv root)/plugins" 22 | git clone https://github.com/tpope/rbenv-aliases.git \ 23 | "$(rbenv root)/plugins/rbenv-aliases" &&\ 24 | rbenv alias --auto 25 | 26 | [rbenv]: https://github.com/sstephenson/rbenv 27 | [ruby-build]: https://github.com/sstephenson/ruby-build 28 | [rbenv-communal-gems]: https://github.com/tpope/rbenv-communal-gems 29 | -------------------------------------------------------------------------------- /bin/rbenv-alias: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Summary: Symlink a short name to an exact version 4 | # 5 | # Usage: rbenv alias [ | --auto | --remove] 6 | # rbenv alias --auto 7 | # rbenv alias [--list] 8 | # 9 | # Symlink a short name to an exact version. Passing a second argument of 10 | # --auto selects the latest patch release of the given point version. Passing 11 | # a first argument of auto does the same for all installed point releases. 12 | 13 | shopt -s nullglob 14 | 15 | cd "$RBENV_ROOT/versions" 16 | 17 | resolve_link() { 18 | $(type -p greadlink readlink | head -1) "$1" 19 | } 20 | 21 | list() { 22 | local exit=1 23 | local dir 24 | for dir in $(echo_lines *); do 25 | if [ -L "$dir" ]; then 26 | echo "$dir => $(resolve_link "$dir")" 27 | exit=0 28 | fi 29 | done 30 | return $exit 31 | } 32 | 33 | cleanup_invalid() { 34 | local version 35 | for version in ${1:-*}; do 36 | if [ -L $version -a ! -e "$(resolve_link $version)" ]; then 37 | echo "Removing invalid link from $version to $(resolve_link $version)" 38 | rm $version 39 | fi 40 | done 41 | } 42 | 43 | echo_lines() { 44 | local line 45 | for line in "$@"; do 46 | echo "$line" 47 | done 48 | } 49 | 50 | echo_lines_without_symlinks() { 51 | local file 52 | for file in "$@"; do 53 | [ ! -L "$file" ] && echo "$file" 54 | done 55 | } 56 | 57 | sort_versions() { 58 | local patch_start_point 59 | 60 | case "$1" in 61 | *.*.*) patch_start_point=$((${#1} + 3)) ;; # point_release string length + 1 (0 indexed) + 2 for `-p` separator 62 | *.*) patch_start_point=$((${#1} + 2)) ;; # point_release string length + 1 (0 indexed) + 1 for `.` separator 63 | esac 64 | 65 | sort -n -k "1.$patch_start_point" 66 | } 67 | 68 | auto_for_point() { 69 | echo_lines_without_symlinks $1* | sort_versions "$1" | tail -1 70 | } 71 | 72 | auto_symlink_point() { 73 | local auto="$(auto_for_point $1)" 74 | if [ -z "$auto" ]; then 75 | echo "Couldn't find any versions for $1" >&2 76 | else 77 | ln -nsf "$auto" "$1" 78 | echo "$1 => $auto" 79 | fi 80 | } 81 | 82 | point_releases() { 83 | echo_lines_without_symlinks *.*.* | sed -e 's/\.[^-.]*$//' -e 's/-[^.]*$//' | sort -u 84 | } 85 | 86 | abort() { 87 | { if [ "$#" -eq 0 ]; then cat - 88 | else echo "rbenv-alias: $*" 89 | fi 90 | } >&2 91 | exit 1 92 | } 93 | 94 | # Provide rbenv completions 95 | if [ --complete = "$1" ]; then 96 | shift 97 | if [ "$#" = 1 ]; then 98 | echo --auto 99 | echo --remove 100 | rbenv-versions --bare 101 | elif [ "$#" = 0 ]; then 102 | echo --auto 103 | echo --list 104 | { point_releases; rbenv-versions --bare; } | sort -u 105 | fi 106 | exit 0 107 | fi 108 | 109 | case "$#" in 110 | 111 | 2) 112 | case "$1" in --*) 113 | case "$2" in -*) abort < <(rbenv-help --usage alias) ;; esac 114 | exec rbenv-alias "$2" "$1" ;; 115 | esac 116 | if [ -e "$1" -a ! -L "$1" ]; then abort "Not clobbering $1" 117 | elif [ --remove = "$2" ]; then 118 | if [ -L "$1" ]; then rm "$1" 119 | else abort "No such alias $1" 120 | fi 121 | elif [ --auto = "$2" ]; then 122 | case "$1" in 123 | *.*) cleanup_invalid "$1" && auto_symlink_point "$1" ;; 124 | *) abort "Don't know how to automatically alias $1" ;; 125 | esac 126 | else 127 | echo "$1 => $2" 128 | ln -nsf "$2" "$1" 129 | fi 130 | ;; 131 | 132 | 1) 133 | case "$1" in 134 | --list) 135 | list 136 | ;; 137 | --auto|--all) 138 | cleanup_invalid 139 | for point in $(point_releases); do 140 | auto_symlink_point $point 141 | done 142 | ;; 143 | --help) 144 | exec rbenv-help alias 145 | ;; 146 | -*) 147 | abort < <(rbenv-help --usage alias) 148 | ;; 149 | *) 150 | if [ -L "$1" ]; then readlink "$1" 151 | elif [ -d "$1" ]; then abort "$1 is an install, not an alias" 152 | elif [ -e "$1" ]; then abort "$1 exists but is not an alias" 153 | else abort "$1 does not exist" 154 | fi 155 | esac 156 | ;; 157 | 158 | 0) list ;; 159 | 160 | *) abort < <(rbenv-help --usage alias) ;; 161 | esac 162 | -------------------------------------------------------------------------------- /bin/rbenv-unalias: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Summary: Remove one or more symlinks in the versions directory 4 | # 5 | # Usage: rbenv unalias [ ...] 6 | 7 | case "$1" in 8 | # Provide rbenv completions 9 | --complete) 10 | rbenv-alias --list | awk '{print $1}' 11 | exit 0 12 | ;; 13 | --help) 14 | exec rbenv-help unalias 15 | ;; 16 | "") 17 | rbenv-help --usage unalias >&2 18 | exit 1 19 | ;; 20 | *) 21 | for arg in "$@"; do 22 | rbenv-alias "$arg" --remove 23 | done 24 | ;; 25 | esac 26 | -------------------------------------------------------------------------------- /etc/rbenv.d/install/autoalias.bash: -------------------------------------------------------------------------------- 1 | after_install autoalias 2 | 3 | autoalias() { 4 | if [ "$STATUS" = 0 ]; then 5 | case "$VERSION_NAME" in 6 | *[0-9]-*) 7 | rbenv alias "${VERSION_NAME%-*}" --auto 2>/dev/null || true 8 | rbenv alias "${VERSION_NAME%%-*}" --auto 2>/dev/null || true 9 | ;; 10 | *.*.*) 11 | rbenv alias "${VERSION_NAME%.*}" --auto 2>/dev/null || true 12 | ;; 13 | esac 14 | fi 15 | } 16 | -------------------------------------------------------------------------------- /etc/rbenv.d/uninstall/autoalias.bash: -------------------------------------------------------------------------------- 1 | after_uninstall autoalias 2 | 3 | autoalias() { 4 | case "$VERSION_NAME" in 5 | *[0-9]-*) 6 | rbenv alias "${VERSION_NAME%-*}" --auto 2>/dev/null || true 7 | rbenv alias "${VERSION_NAME%%-*}" --auto 2>/dev/null || true 8 | ;; 9 | *.*.*) 10 | rbenv alias "${VERSION_NAME%.*}" --auto 2>/dev/null || true 11 | ;; 12 | esac 13 | } 14 | -------------------------------------------------------------------------------- /test/alias.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | @test "rbenv-alias 1.8.7 --auto" { 6 | create_versions 1.8.7-p371 1.8.7-p99 1.8.7-p100 7 | 8 | run rbenv-alias 1.8.7 --auto 9 | assert_success 10 | assert_alias_version 1.8.7 1.8.7-p371 11 | } 12 | 13 | @test "rbenv-alias jruby-1.7 --auto with jruby" { 14 | create_versions jruby-1.7.2 jruby-1.7.11 15 | 16 | run rbenv-alias jruby-1.7 --auto 17 | assert_success 18 | assert_alias_version jruby-1.7 jruby-1.7.11 19 | } 20 | 21 | @test "rbenv-alias 2.1 --auto with semver" { 22 | create_versions 2.1.5 2.1.40 23 | 24 | run rbenv-alias 2.1 --auto 25 | assert_success 26 | assert_alias_version 2.1 2.1.40 27 | } 28 | 29 | @test "rbenv-alias name 1.8.7-p100" { 30 | create_versions 1.8.7-p371 1.8.7-p99 1.8.7-p100 31 | 32 | run rbenv-alias name 1.8.7-p100 33 | assert_success 34 | assert_alias_version name 1.8.7-p100 35 | } 36 | 37 | @test "rbenv-alias --auto" { 38 | create_versions 1.8.7-p371 1.8.7-p99 1.8.7-p100 39 | create_versions 1.2.3-p99-perf 1.2.3-p234-beta 1.2.3-p1-perf 40 | create_versions jruby-1.7.2 jruby-1.7.11 41 | create_versions 2.1.5 2.1.40 42 | 43 | run rbenv-alias --auto 44 | assert_success 45 | assert_alias_version 1.8.7 1.8.7-p371 46 | assert_alias_version 1.2.3 1.2.3-p234-beta 47 | assert_alias_version jruby-1.7 jruby-1.7.11 48 | assert_alias_version 2.1 2.1.40 49 | } 50 | 51 | @test "rbenv-alias 1.8.7-p371 --auto removes dangling alias" { 52 | # alias to non-existant version 53 | create_alias 1.8.7 1.8.7-p371 54 | 55 | run rbenv-alias 1.8.7 --auto 56 | 57 | assert_success 58 | assert [ ! -L "$RBENV_ROOT/versions/1.8.7" ] 59 | } 60 | 61 | @test "rbenv-alias 1.8.7-p371 --auto redirects alias to highest remaining version" { 62 | create_versions 1.8.7-p100 63 | # alias to non-existant version 64 | create_alias 1.8.7 1.8.7-p371 65 | 66 | run rbenv-alias 1.8.7 --auto 67 | 68 | assert_success 69 | assert_alias_version 1.8.7 1.8.7-p100 70 | } 71 | -------------------------------------------------------------------------------- /test/bin/nodenv-uninstall: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Summary: Fake uninstall program 4 | # 5 | # Usage: nodenv uninstall version 6 | # 7 | 8 | set -e 9 | [ -n "$NODENV_DEBUG" ] && set -x 10 | 11 | # Define `before_uninstall` and `after_uninstall` functions that allow 12 | # plugin hooks to register a string of code for execution before or 13 | # after the installation process. 14 | declare -a after_hooks 15 | 16 | after_uninstall() { 17 | local hook="$1" 18 | after_hooks["${#after_hooks[@]}"]="$hook" 19 | } 20 | 21 | if [ -n "$UNINSTALL_HOOK" ]; then 22 | echo Sourcing "$UNINSTALL_HOOK" 23 | source "$UNINSTALL_HOOK" 24 | fi 25 | 26 | case "$1" in 27 | *[0-9].*) 28 | VERSION_NAME=${1:?} 29 | rm -rf "$NODENV_ROOT/versions/$1" 30 | echo "Uninstalled fake version $1" 31 | 32 | echo Executing after_uninstall hooks. 33 | for hook in "${after_hooks[@]}"; do eval "$hook"; done 34 | 35 | ;; 36 | *) 37 | echo 'Usage: nodenv-uninstall version' 38 | ;; 39 | esac 40 | -------------------------------------------------------------------------------- /test/bin/rbenv-install: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Summary: Fake install program 4 | # 5 | # Usage: rbenv install version 6 | # 7 | 8 | set -e 9 | [ -n "$RBENV_DEBUG" ] && set -x 10 | 11 | # Define `before_install` and `after_install` functions that allow 12 | # plugin hooks to register a string of code for execution before or 13 | # after the installation process. 14 | declare -a after_hooks 15 | 16 | after_install() { 17 | local hook="$1" 18 | after_hooks["${#after_hooks[@]}"]="$hook" 19 | } 20 | 21 | if [ -n "$INSTALL_HOOK" ] 22 | then 23 | echo Sourcing "$INSTALL_HOOK" 24 | source "$INSTALL_HOOK" 25 | fi 26 | 27 | STATUS=0 28 | 29 | case "$1" in 30 | [0-9].*) 31 | VERSION_NAME=$1 32 | d="$RBENV_ROOT/versions/$1" 33 | mkdir -p "$d" 34 | echo $1 > "$d/RELEASE.txt" 35 | echo "Installed fake version $1" 36 | echo "into $d directory" 37 | 38 | echo Executing after_install hooks. 39 | for hook in "${after_hooks[@]}"; do eval "$hook"; done 40 | 41 | ;; 42 | *) 43 | echo 'Usage: rbenv-install version' 44 | ;; 45 | esac 46 | exit $STATUS 47 | -------------------------------------------------------------------------------- /test/bin/rbenv-uninstall: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Summary: Fake uninstall program 4 | # 5 | # Usage: rbenv uninstall version 6 | # 7 | 8 | set -e 9 | [ -n "$RBENV_DEBUG" ] && set -x 10 | 11 | # Define `before_uninstall` and `after_uninstall` functions that allow 12 | # plugin hooks to register a string of code for execution before or 13 | # after the installation process. 14 | declare -a after_hooks 15 | 16 | after_uninstall() { 17 | local hook="$1" 18 | after_hooks["${#after_hooks[@]}"]="$hook" 19 | } 20 | 21 | if [ -n "$UNINSTALL_HOOK" ]; then 22 | echo Sourcing "$UNINSTALL_HOOK" 23 | source "$UNINSTALL_HOOK" 24 | fi 25 | 26 | case "$1" in 27 | [0-9].*) 28 | VERSION_NAME=${1:?} 29 | rm -rf "$RBENV_ROOT/versions/$1" 30 | echo "Uninstalled fake version $1" 31 | 32 | echo Executing after_uninstall hooks. 33 | for hook in "${after_hooks[@]}"; do eval "$hook"; done 34 | 35 | ;; 36 | *) 37 | echo 'Usage: rbenv-install version' 38 | ;; 39 | esac 40 | -------------------------------------------------------------------------------- /test/commands.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | @test "alias is listed in rbenv commands" { 6 | run rbenv-commands 7 | assert_success 8 | assert_line "alias" 9 | } 10 | 11 | @test "commands --sh should not list alias" { 12 | run rbenv-commands --sh 13 | assert_success 14 | refute_line "alias" 15 | } 16 | 17 | @test "commands --no-sh should list alias" { 18 | run rbenv-commands --no-sh 19 | assert_success 20 | assert_line "alias" 21 | } 22 | 23 | @test "unalias is listed in rbenv commands" { 24 | run rbenv-commands 25 | assert_success 26 | assert_line "unalias" 27 | } 28 | 29 | @test "commands --sh should not list unalias" { 30 | run rbenv-commands --sh 31 | assert_success 32 | refute_line "unalias" 33 | } 34 | 35 | @test "commands --no-sh should list unalias" { 36 | run rbenv-commands --no-sh 37 | assert_success 38 | assert_line "unalias" 39 | } 40 | -------------------------------------------------------------------------------- /test/help.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | @test "help for alias is available" { 6 | run rbenv-help 'alias' 7 | assert_success 8 | assert_line "Usage: rbenv alias [ | --auto | --remove]" 9 | assert_line "Symlink a short name to an exact version. Passing a second argument of" 10 | } 11 | 12 | 13 | @test "help for unalias is available" { 14 | run rbenv-help 'unalias' 15 | assert_success 16 | assert_line 'Usage: rbenv unalias [ ...]' 17 | assert_line 'Remove one or more symlinks in the versions directory' 18 | } 19 | 20 | -------------------------------------------------------------------------------- /test/install.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | @test "running rbenv-install auto installs an alias" { 6 | 7 | run rbenv-install 2.1.5 8 | assert_success 9 | assert_line 'Installed fake version 2.1.5' 10 | assert_line '2.1 => 2.1.5' 11 | assert_alias_version 2.1 2.1.5 12 | 13 | run rbenv-install 1.9.3-p123 14 | assert_success 15 | assert_line 'Installed fake version 1.9.3-p123' 16 | assert_line '1.9.3 => 1.9.3-p123' 17 | assert_alias_version 1.9.3 1.9.3-p123 18 | 19 | run rbenv-install 1.9.3-p99 20 | assert_success 21 | assert_line 'Installed fake version 1.9.3-p99' 22 | assert_alias_version 1.9.3 1.9.3-p123 23 | 24 | run rbenv-install 1.9.3-p200 25 | assert_success 26 | assert_line 'Installed fake version 1.9.3-p200' 27 | assert_line '1.9.3 => 1.9.3-p200' 28 | assert_alias_version 1.9.3 1.9.3-p200 29 | 30 | run rbenv-install 1.9.3-p456-perf 31 | assert_success 32 | assert_line 'Installed fake version 1.9.3-p456-perf' 33 | assert_line '1.9.3 => 1.9.3-p456-perf' 34 | assert_line '1.9.3-p456 => 1.9.3-p456-perf' 35 | assert_alias_version 1.9.3 1.9.3-p456-perf 36 | assert_alias_version 1.9.3-p456 1.9.3-p456-perf 37 | } 38 | -------------------------------------------------------------------------------- /test/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ ! -f test/run ]; then 4 | cd .. 5 | fi 6 | if [ ! -d bats -o ! -d rbenv ]; then 7 | test/setup_rbenv_and_bats 8 | fi 9 | 10 | exec bats/bin/bats test/$1 11 | -------------------------------------------------------------------------------- /test/setup_rbenv_and_bats: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ -d bats ]; then 4 | ( 5 | cd bats 6 | echo Updating bats: 7 | git pull 8 | ) 9 | else 10 | git clone https://github.com/sstephenson/bats.git 11 | fi 12 | 13 | if [ -d rbenv ]; then 14 | ( 15 | cd rbenv 16 | echo Updating rbenv: 17 | git pull 18 | ) 19 | else 20 | git clone https://github.com/sstephenson/rbenv.git 21 | fi 22 | 23 | -------------------------------------------------------------------------------- /test/test_helper.bash: -------------------------------------------------------------------------------- 1 | unset RBENV_VERSION 2 | unset RBENV_DIR 3 | 4 | RBENV_TEST_DIR="${BATS_TMPDIR}/rbenv" 5 | PLUGIN="${RBENV_TEST_DIR}/root/plugins/rbenv-aliases" 6 | 7 | # guard against executing this block twice due to bats internals 8 | if [ "$RBENV_ROOT" != "${RBENV_TEST_DIR}/root" ]; then 9 | export RBENV_ROOT="${RBENV_TEST_DIR}/root" 10 | export HOME="${RBENV_TEST_DIR}/home" 11 | local parent 12 | 13 | export INSTALL_HOOK="${BATS_TEST_DIRNAME}/../etc/rbenv.d/install/autoalias.bash" 14 | export UNINSTALL_HOOK="${BATS_TEST_DIRNAME}/../etc/rbenv.d/uninstall/autoalias.bash" 15 | 16 | PATH=/usr/bin:/bin:/usr/sbin:/sbin 17 | PATH="${RBENV_TEST_DIR}/bin:$PATH" 18 | PATH="${BATS_TEST_DIRNAME}/bin:$PATH" 19 | PATH="${BATS_TEST_DIRNAME}/../bin:$PATH" 20 | PATH="${BATS_TEST_DIRNAME}/../rbenv/libexec:$PATH" 21 | PATH="${BATS_TEST_DIRNAME}/../rbenv/test/libexec:$PATH" 22 | PATH="${RBENV_ROOT}/shims:$PATH" 23 | export PATH 24 | fi 25 | 26 | teardown() { 27 | rm -rf "$RBENV_TEST_DIR" 28 | } 29 | 30 | flunk() { 31 | { if [ "$#" -eq 0 ]; then cat - 32 | else echo "$@" 33 | fi 34 | } | sed "s:${RBENV_TEST_DIR}:TEST_DIR:g" >&2 35 | return 1 36 | } 37 | 38 | # Creates fake version directories 39 | create_versions() { 40 | for v in $* 41 | do 42 | #echo "Created version: $d" 43 | d="$RBENV_TEST_DIR/root/versions/$v" 44 | mkdir -p "$d/bin" 45 | echo $v > "$d/RELEASE.txt" 46 | ln -nfs /bin/echo "$d/bin/ruby" 47 | done 48 | } 49 | 50 | # Creates test aliases 51 | create_alias() { 52 | local alias="$1" 53 | local version="$2" 54 | 55 | mkdir -p "$RBENV_ROOT/versions" 56 | ln -nfs "$RBENV_ROOT/versions/$version" "$RBENV_ROOT/versions/$alias" 57 | } 58 | 59 | # assert_alias_version alias version 60 | 61 | assert_alias_version() { 62 | if [ ! -f $RBENV_ROOT/versions/$1/RELEASE.txt ] 63 | then 64 | echo "Versions:" 65 | (cd $RBENV_ROOT/versions ; ls -l) 66 | fi 67 | assert_equal "$2" "$(cat "$RBENV_ROOT/versions/$1/RELEASE.txt" 2>&1)" 68 | } 69 | 70 | assert_alias_missing() { 71 | if [ -f $RBENV_ROOT/versions/$1/RELEASE.txt ] 72 | then 73 | assert_equal "no-version" "$(cat "$RBENV_ROOT/versions/$1/RELEASE.txt" 2>&1)" 74 | fi 75 | } 76 | 77 | 78 | assert_success() { 79 | if [ "$status" -ne 0 ]; then 80 | flunk "command failed with exit status $status" 81 | elif [ "$#" -gt 0 ]; then 82 | assert_output "$1" 83 | fi 84 | } 85 | 86 | assert_failure() { 87 | if [ "$status" -eq 0 ]; then 88 | flunk "expected failed exit status" 89 | elif [ "$#" -gt 0 ]; then 90 | assert_output "$1" 91 | fi 92 | } 93 | 94 | assert_equal() { 95 | if [ "$1" != "$2" ]; then 96 | { echo "expected: $1" 97 | echo "actual: $2" 98 | } | flunk 99 | fi 100 | } 101 | 102 | assert_output() { 103 | local expected 104 | if [ $# -eq 0 ]; then expected="$(cat -)" 105 | else expected="$1" 106 | fi 107 | assert_equal "$expected" "$output" 108 | } 109 | 110 | assert_line() { 111 | if [ "$1" -ge 0 ] 2>/dev/null; then 112 | assert_equal "$2" "${lines[$1]}" 113 | else 114 | local line 115 | for line in "${lines[@]}"; do 116 | if [ "$line" = "$1" ]; then return 0; fi 117 | done 118 | flunk "expected line \`$1'" 119 | fi 120 | } 121 | 122 | assert_line_starts_with() { 123 | if [ "$1" -ge 0 ] 2>/dev/null; then 124 | assert_equal "$2" "${lines[$1]}" 125 | else 126 | local line 127 | for line in "${lines[@]}"; do 128 | if [ -n "${line#${1}}" ]; then return 0; fi 129 | done 130 | flunk "expected line \`$1'" 131 | fi 132 | } 133 | 134 | refute_line() { 135 | if [ "$1" -ge 0 ] 2>/dev/null; then 136 | local num_lines="${#lines[@]}" 137 | if [ "$1" -lt "$num_lines" ]; then 138 | flunk "output has $num_lines lines" 139 | fi 140 | else 141 | local line 142 | for line in "${lines[@]}"; do 143 | if [ "$line" = "$1" ]; then 144 | flunk "expected to not find line \`$line'" 145 | fi 146 | done 147 | fi 148 | } 149 | 150 | assert() { 151 | if ! "$@"; then 152 | flunk "failed: $@" 153 | fi 154 | } 155 | -------------------------------------------------------------------------------- /test/unalias.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | @test "running unalias removes an alias" { 6 | 7 | create_versions 1.8.7-p100 8 | run rbenv-alias 1.8.7 --auto 9 | assert_success 10 | 11 | run rbenv-unalias 1.8.7 12 | assert_success 13 | assert_alias_missing 1.8.7 14 | 15 | } 16 | -------------------------------------------------------------------------------- /test/uninstall.bats: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bats 2 | 3 | load test_helper 4 | 5 | @test "running rbenv-uninstall auto removes the alias" { 6 | create_versions 1.9.3-p123 7 | create_alias 1.9.3 1.9.3-p123 8 | 9 | run rbenv-uninstall 1.9.3-p123 10 | 11 | assert_success 12 | assert_line 'Uninstalled fake version 1.9.3-p123' 13 | assert_line_starts_with 'Removing invalid link from 1.9.3' 14 | assert [ ! -L "$RBENV_ROOT/versions/1.9.3" ] 15 | } 16 | 17 | @test "running rbenv-uninstall auto updates the alias to highest remaining version" { 18 | create_versions 1.9.3-p123 1.9.3-p456 19 | create_alias 1.9.3 1.9.3-p456 20 | 21 | run rbenv-uninstall 1.9.3-p456 22 | 23 | assert_success 24 | assert_line 'Uninstalled fake version 1.9.3-p456' 25 | assert_line_starts_with 'Removing invalid link from 1.9.3' 26 | assert_line '1.9.3 => 1.9.3-p123' 27 | assert_alias_version 1.9.3 1.9.3-p123 28 | } 29 | 30 | @test "running rbenv-uninstall auto updates the alias to highest remaining semver version" { 31 | create_versions 2.1.3 2.1.5 32 | create_alias 2.1 2.1.5 33 | 34 | run rbenv-uninstall 2.1.5 35 | 36 | assert_success 37 | assert_line 'Uninstalled fake version 2.1.5' 38 | assert_line_starts_with 'Removing invalid link from 2.1' 39 | assert_line '2.1 => 2.1.3' 40 | assert_alias_version 2.1 2.1.3 41 | } 42 | 43 | @test "running rbenv-uninstall auto updates the alias to highest remaining version, handling multi-segment patches" { 44 | create_versions 1.9.3-p123 1.9.3-p456-perf 45 | create_alias 1.9.3 1.9.3-p456-perf 46 | create_alias 1.9.3-p456 1.9.3-p456-perf 47 | 48 | run rbenv-uninstall 1.9.3-p456-perf 49 | 50 | assert_success 51 | assert_line 'Uninstalled fake version 1.9.3-p456-perf' 52 | assert_line_starts_with 'Removing invalid link from 1.9.3 ' 53 | assert_line_starts_with 'Removing invalid link from 1.9.3-p456' 54 | assert_line '1.9.3 => 1.9.3-p123' 55 | assert_alias_version 1.9.3 1.9.3-p123 56 | } 57 | --------------------------------------------------------------------------------