rbenv/test/version-name.bats
Jason Karns 258e4413b1 create hook: version-name
Expose a `version-name` hook.

It is invoked *after* the traditional `RBENV_VERSION` lookup. Which means hook scripts can interrogate `$RBENV_VERSION_FILE` and/or `$RBENV_VERSION` (or use the executables).

The hooks are then run, giving plugins a chance to alter `RBENV_VERSION`. Once the hooks have run, we now have (in `$RBENV_VERSION`) the actual version we want to use (or it's empty which defaults to `system` per normal). Lastly, the same logic remains for checking if the version exists, or trimming the `ruby-` prefix.

Prime example: the ruby-bundler-ruby-version plugin can select a ruby by using the `ruby` directive from the `Gemfile` if a local `.ruby-version` doesn't exist.
2015-12-23 17:22:30 +01:00

80 lines
1.7 KiB
Bash

#!/usr/bin/env bats
load test_helper
export RBENV_HOOK_PATH="${RBENV_ROOT}/rbenv.d"
create_hook() {
mkdir -p "${RBENV_ROOT}/rbenv.d/version-name"
cat > "${RBENV_ROOT}/rbenv.d/version-name/$1" <<<"$2"
}
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 can be overridden by hook" {
create_version "1.8.7"
create_version "1.9.3"
RBENV_VERSION=1.8.7 run rbenv-version-name
assert_success "1.8.7"
create_hook test.bash "RBENV_VERSION=1.9.3"
RBENV_VERSION=1.8.7 run rbenv-version-name
assert_success "1.9.3"
}
@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 "1.8.7"
}