add tests for version commands

This commit is contained in:
Mislav Marohnić 2013-04-08 21:35:22 +02:00
parent 7fc5f46bbb
commit ab197ef51e
9 changed files with 464 additions and 4 deletions

View file

@ -38,13 +38,14 @@ RBENV_VERSION="$1"
if [ "$RBENV_VERSION" = "--unset" ]; then if [ "$RBENV_VERSION" = "--unset" ]; then
rm -f .ruby-version .rbenv-version rm -f .ruby-version .rbenv-version
elif [ -n "$RBENV_VERSION" ]; then 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 rm -f .rbenv-version
{ echo "rbenv: removed existing \`.rbenv-version' file and migrated" { echo "rbenv: removed existing \`.rbenv-version' file and migrated"
echo " local version specification to \`.ruby-version' file" echo " local version specification to \`.ruby-version' file"
} >&2 } >&2
fi fi
rbenv-version-file-write .ruby-version "$RBENV_VERSION"
else else
rbenv-version-file-read .ruby-version || rbenv-version-file-read .ruby-version ||
rbenv-version-file-read .rbenv-version || rbenv-version-file-read .rbenv-version ||

View file

@ -19,6 +19,19 @@ setup() {
assert_success "1.2.3" 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" { @test "ignores version in parent directory" {
echo "1.2.3" > .ruby-version echo "1.2.3" > .ruby-version
mkdir -p "subdir" && cd "subdir" mkdir -p "subdir" && cd "subdir"
@ -48,6 +61,43 @@ setup() {
assert_success "1.0-pre" assert_success "1.0-pre"
run rbenv-local 1.2.3 run rbenv-local 1.2.3
assert_success "" assert_success ""
run rbenv-local assert [ "$(cat .ruby-version)" = "1.2.3" ]
assert_success "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 <<OUT
rbenv: removed existing \`.rbenv-version' file and migrated
local version specification to \`.ruby-version' file
OUT
assert [ ! -e .rbenv-version ]
assert [ "$(cat .ruby-version)" = "1.9.3" ]
}
@test "doesn't rename .rbenv-version if changing the version failed" {
echo "1.8.7" > .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 ]
} }

View file

@ -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 <<IN
1.8.7 one
1.9.3 two
IN
run rbenv-version-file-read my-version
assert_success "1.8.7"
}

View file

@ -0,0 +1,30 @@
#!/usr/bin/env bats
load test_helper
setup() {
mkdir -p "$RBENV_TEST_DIR"
cd "$RBENV_TEST_DIR"
}
@test "invocation without 2 arguments prints usage" {
run rbenv-version-file-write
assert_failure "Usage: rbenv version-file-write <file> <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" ]
}

99
test/version-file.bats Normal file
View file

@ -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"
}

65
test/version-name.bats Normal file
View file

@ -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 <<OUT
warning: ignoring extraneous \`ruby-' prefix in version \`ruby-1.8.7'
(set by ${PWD}/.ruby-version)
1.8.7
OUT
}

38
test/version-origin.bats Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env bats
load test_helper
setup() {
mkdir -p "$RBENV_TEST_DIR"
cd "$RBENV_TEST_DIR"
}
@test "reports global file even if it doesn't exist" {
assert [ ! -e "${RBENV_ROOT}/version" ]
run rbenv-version-origin
assert_success "${RBENV_ROOT}/version"
}
@test "detects global file" {
mkdir -p "$RBENV_ROOT"
touch "${RBENV_ROOT}/version"
run rbenv-version-origin
assert_success "${RBENV_ROOT}/version"
}
@test "detects RBENV_VERSION" {
RBENV_VERSION=1 run rbenv-version-origin
assert_success "RBENV_VERSION environment variable"
}
@test "detects local file" {
touch .ruby-version
run rbenv-version-origin
assert_success "${PWD}/.ruby-version"
}
@test "detects alternate version file" {
touch .rbenv-version
run rbenv-version-origin
assert_success "${PWD}/.rbenv-version"
}

38
test/version.bats Normal file
View file

@ -0,0 +1,38 @@
#!/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
assert_success "system (set by ${RBENV_ROOT}/version)"
}
@test "set by RBENV_VERSION" {
create_version "1.9.3"
RBENV_VERSION=1.9.3 run rbenv-version
assert_success "1.9.3 (set by RBENV_VERSION environment variable)"
}
@test "set by local file" {
create_version "1.9.3"
cat > ".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)"
}

100
test/versions.bats Normal file
View file

@ -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 <<OUT
* system (set by ${RBENV_ROOT}/version)
1.9
OUT
}
@test "single version bare" {
create_version "1.9"
run rbenv-versions --bare
assert_success "1.9"
}
@test "multiple versions" {
create_version "1.8.7"
create_version "1.9.3"
create_version "2.0.0"
run rbenv-versions
assert_success
assert_output <<OUT
* system (set by ${RBENV_ROOT}/version)
1.8.7
1.9.3
2.0.0
OUT
}
@test "indicates current version" {
create_version "1.9.3"
create_version "2.0.0"
RBENV_VERSION=1.9.3 run rbenv-versions
assert_success
assert_output <<OUT
system
* 1.9.3 (set by RBENV_VERSION environment variable)
2.0.0
OUT
}
@test "bare doesn't indicate current version" {
create_version "1.9.3"
create_version "2.0.0"
RBENV_VERSION=1.9.3 run rbenv-versions --bare
assert_success
assert_output <<OUT
1.9.3
2.0.0
OUT
}
@test "globally selected version" {
create_version "1.9.3"
create_version "2.0.0"
cat > "${RBENV_ROOT}/version" <<<"1.9.3"
run rbenv-versions
assert_success
assert_output <<OUT
system
* 1.9.3 (set by ${RBENV_ROOT}/version)
2.0.0
OUT
}
@test "per-project version" {
create_version "1.9.3"
create_version "2.0.0"
mkdir -p "$RBENV_TEST_DIR"
cd "$RBENV_TEST_DIR"
cat > ".ruby-version" <<<"1.9.3"
run rbenv-versions
assert_success
assert_output <<OUT
system
* 1.9.3 (set by ${RBENV_TEST_DIR}/.ruby-version)
2.0.0
OUT
}