From ab197ef51e5d99110015907c4346fde7c5a61de4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohnic=CC=81?= Date: Mon, 8 Apr 2013 21:35:22 +0200 Subject: [PATCH] add tests for version commands --- libexec/rbenv-local | 5 +- test/local.bats | 54 ++++++++++++++++++- test/version-file-read.bats | 39 ++++++++++++++ test/version-file-write.bats | 30 +++++++++++ test/version-file.bats | 99 ++++++++++++++++++++++++++++++++++ test/version-name.bats | 65 +++++++++++++++++++++++ test/version-origin.bats | 38 +++++++++++++ test/version.bats | 38 +++++++++++++ test/versions.bats | 100 +++++++++++++++++++++++++++++++++++ 9 files changed, 464 insertions(+), 4 deletions(-) create mode 100644 test/version-file-read.bats create mode 100644 test/version-file-write.bats create mode 100644 test/version-file.bats create mode 100644 test/version-name.bats create mode 100644 test/version-origin.bats create mode 100644 test/version.bats create mode 100644 test/versions.bats 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/local.bats b/test/local.bats index 5ccdb2b5..3e93ca7f 100644 --- a/test/local.bats +++ b/test/local.bats @@ -19,6 +19,19 @@ setup() { assert_success "1.2.3" } +@test "supports legacy .rbenv-version file" { + echo "1.2.3" > .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/version-file-read.bats b/test/version-file-read.bats new file mode 100644 index 00000000..8002388d --- /dev/null +++ b/test/version-file-read.bats @@ -0,0 +1,39 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + mkdir -p "${RBENV_TEST_DIR}/myproject" + cd "${RBENV_TEST_DIR}/myproject" +} + +@test "fails without arguments" { + run rbenv-version-file-read + assert_failure "" +} + +@test "fails for invalid file" { + run rbenv-version-file-read "non-existent" + assert_failure "" +} + +@test "reads simple version file" { + cat > 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 <