1#!/usr/bin/env bats
2
3load test_helper
4
5create_version() {
6  mkdir -p "${RBENV_ROOT}/versions/$1"
7}
8
9setup() {
10  mkdir -p "$RBENV_TEST_DIR"
11  cd "$RBENV_TEST_DIR"
12}
13
14@test "no version selected" {
15  assert [ ! -d "${RBENV_ROOT}/versions" ]
16  run rbenv-version-name
17  assert_success "system"
18}
19
20@test "system version is not checked for existence" {
21  RBENV_VERSION=system run rbenv-version-name
22  assert_success "system"
23}
24
25@test "RBENV_VERSION can be overridden by hook" {
26  create_version "1.8.7"
27  create_version "1.9.3"
28  create_hook version-name test.bash <<<"RBENV_VERSION=1.9.3"
29
30  RBENV_VERSION=1.8.7 run rbenv-version-name
31  assert_success "1.9.3"
32}
33
34@test "carries original IFS within hooks" {
35  create_hook version-name hello.bash <<SH
36hellos=(\$(printf "hello\\tugly world\\nagain"))
37echo HELLO="\$(printf ":%s" "\${hellos[@]}")"
38SH
39
40  export RBENV_VERSION=system
41  IFS=$' \t\n' run rbenv-version-name env
42  assert_success
43  assert_line "HELLO=:hello:ugly:world:again"
44}
45
46@test "RBENV_VERSION has precedence over local" {
47  create_version "1.8.7"
48  create_version "1.9.3"
49
50  cat > ".ruby-version" <<<"1.8.7"
51  run rbenv-version-name
52  assert_success "1.8.7"
53
54  RBENV_VERSION=1.9.3 run rbenv-version-name
55  assert_success "1.9.3"
56}
57
58@test "local file has precedence over global" {
59  create_version "1.8.7"
60  create_version "1.9.3"
61
62  cat > "${RBENV_ROOT}/version" <<<"1.8.7"
63  run rbenv-version-name
64  assert_success "1.8.7"
65
66  cat > ".ruby-version" <<<"1.9.3"
67  run rbenv-version-name
68  assert_success "1.9.3"
69}
70
71@test "missing version" {
72  RBENV_VERSION=1.2 run rbenv-version-name
73  assert_failure "rbenv: version \`1.2' is not installed (set by RBENV_VERSION environment variable)"
74}
75
76@test "version with prefix in name" {
77  create_version "1.8.7"
78  cat > ".ruby-version" <<<"ruby-1.8.7"
79  run rbenv-version-name
80  assert_success
81  assert_output "1.8.7"
82}
83