diff --git a/libexec/rbenv-local b/libexec/rbenv-local index 34464410..9169943e 100755 --- a/libexec/rbenv-local +++ b/libexec/rbenv-local @@ -38,13 +38,14 @@ RBENV_VERSION="$1" if [ "$RBENV_VERSION" = "--unset" ]; then rm -f .ruby-version .rbenv-version elif [ -n "$RBENV_VERSION" ]; then - if [ "$(RBENV_VERSION= rbenv-version-origin)" -ef .rbenv-version ]; then + previous_file="$(RBENV_VERSION= rbenv-version-origin || true)" + rbenv-version-file-write .ruby-version "$RBENV_VERSION" + if [ "$previous_file" -ef .rbenv-version ]; then rm -f .rbenv-version { echo "rbenv: removed existing \`.rbenv-version' file and migrated" echo " local version specification to \`.ruby-version' file" } >&2 fi - rbenv-version-file-write .ruby-version "$RBENV_VERSION" else rbenv-version-file-read .ruby-version || rbenv-version-file-read .rbenv-version || diff --git a/test/--version.bats b/test/--version.bats new file mode 100644 index 00000000..e99a3a95 --- /dev/null +++ b/test/--version.bats @@ -0,0 +1,46 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + mkdir -p "$HOME" + git config --global user.name "Tester" + git config --global user.email "tester@test.local" +} + +git_commit() { + git commit --quiet --allow-empty -m "" --allow-empty-message +} + +@test "default version" { + assert [ ! -e "$RBENV_ROOT" ] + run rbenv---version + assert_success + [[ $output == "rbenv 0."* ]] +} + +@test "reads version from git repo" { + mkdir -p "$RBENV_ROOT" + cd "$RBENV_ROOT" + git init + git_commit + git tag v0.4.1 + git_commit + git_commit + + cd "$RBENV_TEST_DIR" + run rbenv---version + assert_success + [[ $output == "rbenv 0.4.1-2-g"* ]] +} + +@test "prints default version if no tags in git repo" { + mkdir -p "$RBENV_ROOT" + cd "$RBENV_ROOT" + git init + git_commit + + cd "$RBENV_TEST_DIR" + run rbenv---version + [[ $output == "rbenv 0."* ]] +} diff --git a/test/completions.bats b/test/completions.bats index 0f79654d..0feccfa9 100644 --- a/test/completions.bats +++ b/test/completions.bats @@ -38,7 +38,9 @@ else exit 1 fi" run rbenv-completions hello happy world - assert_success "\ - happy - world" + assert_success + assert_output <" { @@ -85,9 +88,10 @@ else fi SH - create_executable "rake" "\ - #!/usr/bin/env ruby - echo hello rake" + create_executable "rake" < []" + assert_line "Some useful rbenv commands are:" +} + +@test "invalid command" { + run rbenv-help hello + assert_failure "rbenv: no such command \`hello'" +} + +@test "shows help for a specific command" { + mkdir -p "${RBENV_TEST_DIR}/bin" + cat > "${RBENV_TEST_DIR}/bin/rbenv-hello" < +# Summary: Says "hello" to you, from rbenv +# This command is useful for saying hello. +echo hello +SH + + run rbenv-help hello + assert_success + assert_output < + +This command is useful for saying hello. +SH +} + +@test "replaces missing extended help with summary text" { + mkdir -p "${RBENV_TEST_DIR}/bin" + cat > "${RBENV_TEST_DIR}/bin/rbenv-hello" < +# Summary: Says "hello" to you, from rbenv +echo hello +SH + + run rbenv-help hello + assert_success + assert_output < + +Says "hello" to you, from rbenv +SH +} + +@test "extracts only usage" { + mkdir -p "${RBENV_TEST_DIR}/bin" + cat > "${RBENV_TEST_DIR}/bin/rbenv-hello" < +# Summary: Says "hello" to you, from rbenv +# This extended help won't be shown. +echo hello +SH + + run rbenv-help --usage hello + assert_success "Usage: rbenv hello " +} + +@test "multiline usage section" { + mkdir -p "${RBENV_TEST_DIR}/bin" + cat > "${RBENV_TEST_DIR}/bin/rbenv-hello" < +# rbenv hi [everybody] +# rbenv hola --translate +# Summary: Says "hello" to you, from rbenv +# Help text. +echo hello +SH + + run rbenv-help hello + assert_success + assert_output < + rbenv hi [everybody] + rbenv hola --translate + +Help text. +SH +} + +@test "multiline extended help section" { + mkdir -p "${RBENV_TEST_DIR}/bin" + cat > "${RBENV_TEST_DIR}/bin/rbenv-hello" < +# Summary: Says "hello" to you, from rbenv +# This is extended help text. +# It can contain multiple lines. +# +# And paragraphs. + +echo hello +SH + + run rbenv-help hello + assert_success + assert_output < + +This is extended help text. +It can contain multiple lines. + +And paragraphs. +SH +} diff --git a/test/hooks.bats b/test/hooks.bats index 7d3b253f..0b53d430 100644 --- a/test/hooks.bats +++ b/test/hooks.bats @@ -22,10 +22,12 @@ create_hook() { create_hook "$path2" exec "bueno.bash" RBENV_HOOK_PATH="$path1:$path2" run rbenv-hooks exec - assert_success "\ - ${RBENV_TEST_DIR}/rbenv.d/exec/ahoy.bash - ${RBENV_TEST_DIR}/rbenv.d/exec/hello.bash - ${RBENV_TEST_DIR}/etc/rbenv_hooks/exec/bueno.bash" + assert_success + assert_output < .rbenv-version + run rbenv-local + assert_success "1.2.3" +} + +@test "local .ruby-version has precedence over .rbenv-version" { + echo "1.8" > .rbenv-version + echo "2.0" > .ruby-version + run rbenv-local + assert_success "2.0" +} + @test "ignores version in parent directory" { echo "1.2.3" > .ruby-version mkdir -p "subdir" && cd "subdir" @@ -48,6 +61,43 @@ setup() { assert_success "1.0-pre" run rbenv-local 1.2.3 assert_success "" - run rbenv-local - assert_success "1.2.3" + assert [ "$(cat .ruby-version)" = "1.2.3" ] +} + +@test "renames .rbenv-version to .ruby-version" { + echo "1.8.7" > .rbenv-version + mkdir -p "${RBENV_ROOT}/versions/1.9.3" + run rbenv-local + assert_success "1.8.7" + run rbenv-local "1.9.3" + assert_success + assert_output < .rbenv-version + assert [ ! -e "${RBENV_ROOT}/versions/1.9.3" ] + run rbenv-local "1.9.3" + assert_failure "rbenv: version \`1.9.3' not installed" + assert [ ! -e .ruby-version ] + assert [ "$(cat .rbenv-version)" = "1.8.7" ] +} + +@test "unsets local version" { + touch .ruby-version + run rbenv-local --unset + assert_success "" + assert [ ! -e .rbenv-version ] +} + +@test "unsets alternate version file" { + touch .rbenv-version + run rbenv-local --unset + assert_success "" + assert [ ! -e .rbenv-version ] } diff --git a/test/rehash.bats b/test/rehash.bats index 6e9e8907..a9d23599 100755 --- a/test/rehash.bats +++ b/test/rehash.bats @@ -2,10 +2,17 @@ load test_helper +create_executable() { + local bin="${RBENV_ROOT}/versions/${1}/bin" + mkdir -p "$bin" + touch "${bin}/$2" + chmod +x "${bin}/$2" +} + @test "empty rehash" { assert [ ! -d "${RBENV_ROOT}/shims" ] run rbenv-rehash - assert_success + assert_success "" assert [ -d "${RBENV_ROOT}/shims" ] rmdir "${RBENV_ROOT}/shims" } @@ -14,14 +21,34 @@ load test_helper mkdir -p "${RBENV_ROOT}/shims" chmod -w "${RBENV_ROOT}/shims" run rbenv-rehash - assert_failure - assert_output "rbenv: cannot rehash: ${RBENV_ROOT}/shims isn't writable" + assert_failure "rbenv: cannot rehash: ${RBENV_ROOT}/shims isn't writable" } @test "rehash in progress" { mkdir -p "${RBENV_ROOT}/shims" touch "${RBENV_ROOT}/shims/.rbenv-shim" run rbenv-rehash - assert_failure - assert_output "rbenv: cannot rehash: ${RBENV_ROOT}/shims/.rbenv-shim exists" + assert_failure "rbenv: cannot rehash: ${RBENV_ROOT}/shims/.rbenv-shim exists" +} + +@test "creates shims" { + create_executable "1.8" "ruby" + create_executable "1.8" "rake" + create_executable "2.0" "ruby" + create_executable "2.0" "rspec" + + assert [ ! -e "${RBENV_ROOT}/shims/ruby" ] + assert [ ! -e "${RBENV_ROOT}/shims/rake" ] + assert [ ! -e "${RBENV_ROOT}/shims/rspec" ] + + run rbenv-rehash + assert_success "" + + run ls "${RBENV_ROOT}/shims" + assert_success + assert_output < my-version <<<"1.9.3" + run rbenv-version-file-read my-version + assert_success "1.9.3" +} + +@test "reads only the first word from file" { + cat > my-version <<<"1.9.3-p194@tag 1.8.7 hi" + run rbenv-version-file-read my-version + assert_success "1.9.3-p194@tag" +} + +@test "loads only the first line in file" { + cat > my-version < " + run rbenv-version-file-write "one" "" + assert_failure +} + +@test "setting nonexistent version fails" { + assert [ ! -e ".ruby-version" ] + run rbenv-version-file-write ".ruby-version" "1.8.7" + assert_failure "rbenv: version \`1.8.7' not installed" + assert [ ! -e ".ruby-version" ] +} + +@test "writes value to arbitrary file" { + mkdir -p "${RBENV_ROOT}/versions/1.8.7" + assert [ ! -e "my-version" ] + run rbenv-version-file-write "${PWD}/my-version" "1.8.7" + assert_success "" + assert [ "$(cat my-version)" = "1.8.7" ] +} diff --git a/test/version-file.bats b/test/version-file.bats new file mode 100644 index 00000000..ed84c484 --- /dev/null +++ b/test/version-file.bats @@ -0,0 +1,99 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + mkdir -p "$RBENV_TEST_DIR" + cd "$RBENV_TEST_DIR" +} + +create_file() { + mkdir -p "$(dirname "$1")" + touch "$1" +} + +@test "prints global file if no version files exist" { + assert [ ! -e "${RBENV_ROOT}/version" ] + assert [ ! -e ".ruby-version" ] + run rbenv-version-file + assert_success "${RBENV_ROOT}/version" +} + +@test "detects 'global' file" { + create_file "${RBENV_ROOT}/global" + run rbenv-version-file + assert_success "${RBENV_ROOT}/global" +} + +@test "detects 'default' file" { + create_file "${RBENV_ROOT}/default" + run rbenv-version-file + assert_success "${RBENV_ROOT}/default" +} + +@test "'version' has precedence over 'global' and 'default'" { + create_file "${RBENV_ROOT}/version" + create_file "${RBENV_ROOT}/global" + create_file "${RBENV_ROOT}/default" + run rbenv-version-file + assert_success "${RBENV_ROOT}/version" +} + +@test "in current directory" { + create_file ".ruby-version" + run rbenv-version-file + assert_success "${RBENV_TEST_DIR}/.ruby-version" +} + +@test "legacy file in current directory" { + create_file ".rbenv-version" + run rbenv-version-file + assert_success "${RBENV_TEST_DIR}/.rbenv-version" +} + +@test ".ruby-version has precedence over legacy file" { + create_file ".ruby-version" + create_file ".rbenv-version" + run rbenv-version-file + assert_success "${RBENV_TEST_DIR}/.ruby-version" +} + +@test "in parent directory" { + create_file ".ruby-version" + mkdir -p project + cd project + run rbenv-version-file + assert_success "${RBENV_TEST_DIR}/.ruby-version" +} + +@test "topmost file has precedence" { + create_file ".ruby-version" + create_file "project/.ruby-version" + cd project + run rbenv-version-file + assert_success "${RBENV_TEST_DIR}/project/.ruby-version" +} + +@test "legacy file has precedence if higher" { + create_file ".ruby-version" + create_file "project/.rbenv-version" + cd project + run rbenv-version-file + assert_success "${RBENV_TEST_DIR}/project/.rbenv-version" +} + +@test "RBENV_DIR has precedence over PWD" { + create_file "widget/.ruby-version" + create_file "project/.ruby-version" + cd project + RBENV_DIR="${RBENV_TEST_DIR}/widget" run rbenv-version-file + assert_success "${RBENV_TEST_DIR}/widget/.ruby-version" +} + +@test "PWD is searched if RBENV_DIR yields no results" { + mkdir -p "widget/blank" + create_file "project/.ruby-version" + cd project + RBENV_DIR="${RBENV_TEST_DIR}/widget/blank" run rbenv-version-file + assert_success "${RBENV_TEST_DIR}/project/.ruby-version" +} diff --git a/test/version-name.bats b/test/version-name.bats new file mode 100644 index 00000000..3f4d9b36 --- /dev/null +++ b/test/version-name.bats @@ -0,0 +1,65 @@ +#!/usr/bin/env bats + +load test_helper + +create_version() { + mkdir -p "${RBENV_ROOT}/versions/$1" +} + +setup() { + mkdir -p "$RBENV_TEST_DIR" + cd "$RBENV_TEST_DIR" +} + +@test "no version selected" { + assert [ ! -d "${RBENV_ROOT}/versions" ] + run rbenv-version-name + assert_success "system" +} + +@test "system version is not checked for existance" { + RBENV_VERSION=system run rbenv-version-name + assert_success "system" +} + +@test "RBENV_VERSION has precedence over local" { + create_version "1.8.7" + create_version "1.9.3" + + cat > ".ruby-version" <<<"1.8.7" + run rbenv-version-name + assert_success "1.8.7" + + RBENV_VERSION=1.9.3 run rbenv-version-name + assert_success "1.9.3" +} + +@test "local file has precedence over global" { + create_version "1.8.7" + create_version "1.9.3" + + cat > "${RBENV_ROOT}/version" <<<"1.8.7" + run rbenv-version-name + assert_success "1.8.7" + + cat > ".ruby-version" <<<"1.9.3" + run rbenv-version-name + assert_success "1.9.3" +} + +@test "missing version" { + RBENV_VERSION=1.2 run rbenv-version-name + assert_failure "rbenv: version \`1.2' is not installed" +} + +@test "version with prefix in name" { + create_version "1.8.7" + cat > ".ruby-version" <<<"ruby-1.8.7" + run rbenv-version-name + assert_success + assert_output < ".ruby-version" <<<"1.9.3" + run rbenv-version + assert_success "1.9.3 (set by ${PWD}/.ruby-version)" +} + +@test "set by global file" { + create_version "1.9.3" + cat > "${RBENV_ROOT}/version" <<<"1.9.3" + run rbenv-version + assert_success "1.9.3 (set by ${RBENV_ROOT}/version)" +} diff --git a/test/versions.bats b/test/versions.bats new file mode 100644 index 00000000..cdec7a8a --- /dev/null +++ b/test/versions.bats @@ -0,0 +1,100 @@ +#!/usr/bin/env bats + +load test_helper + +create_version() { + mkdir -p "${RBENV_ROOT}/versions/$1" +} + +@test "no versions installed" { + assert [ ! -d "${RBENV_ROOT}/versions" ] + run rbenv-versions + assert_success "* system (set by ${RBENV_ROOT}/version)" +} + +@test "bare output no versions installed" { + assert [ ! -d "${RBENV_ROOT}/versions" ] + run rbenv-versions --bare + assert_success "" +} + +@test "single version installed" { + create_version "1.9" + run rbenv-versions + assert_success + assert_output < "${RBENV_ROOT}/version" <<<"1.9.3" + run rbenv-versions + assert_success + assert_output < ".ruby-version" <<<"1.9.3" + run rbenv-versions + assert_success + assert_output <