From 969af1567af0617a3f08adbbe2c63e3296f59a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohnic=CC=81?= Date: Sat, 6 Apr 2013 12:30:21 +0200 Subject: [PATCH 1/4] add tests for rehash, whence, which --- test/rehash.bats | 35 ++++++++++++++++++++++++----- test/shell.bats | 7 +++--- test/whence.bats | 28 ++++++++++++++++++++++++ test/which.bats | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 9 deletions(-) create mode 100644 test/whence.bats create mode 100644 test/which.bats diff --git a/test/rehash.bats b/test/rehash.bats index 6e9e8907..1e4d6d51 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,32 @@ 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 "\ + rake + rspec + ruby" } diff --git a/test/shell.bats b/test/shell.bats index 94778985..1a5af662 100644 --- a/test/shell.bats +++ b/test/shell.bats @@ -22,14 +22,13 @@ load test_helper @test "shell change invalid version" { run rbenv-sh-shell 1.2.3 - assert_failure - assert_line "rbenv: version \`1.2.3' not installed" - assert_line "return 1" + assert_failure "\ + rbenv: version \`1.2.3' not installed + return 1" } @test "shell change version" { mkdir -p "${RBENV_ROOT}/versions/1.2.3" run rbenv-sh-shell 1.2.3 assert_success 'export RBENV_VERSION="1.2.3"' - refute_line "return 1" } diff --git a/test/whence.bats b/test/whence.bats new file mode 100644 index 00000000..583a2ca6 --- /dev/null +++ b/test/whence.bats @@ -0,0 +1,28 @@ +#!/usr/bin/env bats + +load test_helper + +create_executable() { + local bin="${RBENV_ROOT}/versions/${1}/bin" + mkdir -p "$bin" + touch "${bin}/$2" + chmod +x "${bin}/$2" +} + +@test "finds versions where present" { + create_executable "1.8" "ruby" + create_executable "1.8" "rake" + create_executable "2.0" "ruby" + create_executable "2.0" "rspec" + + run rbenv-whence ruby + assert_success "\ + 1.8 + 2.0" + + run rbenv-whence rake + assert_success "1.8" + + run rbenv-whence rspec + assert_success "2.0" +} diff --git a/test/which.bats b/test/which.bats new file mode 100644 index 00000000..530c1f4b --- /dev/null +++ b/test/which.bats @@ -0,0 +1,57 @@ +#!/usr/bin/env bats + +load test_helper + +create_executable() { + local bin + if [[ $1 == */* ]]; then bin="$1" + else bin="${RBENV_ROOT}/versions/${1}/bin" + fi + mkdir -p "$bin" + touch "${bin}/$2" + chmod +x "${bin}/$2" +} + +@test "outputs path to executable" { + create_executable "1.8" "ruby" + create_executable "2.0" "rspec" + + RBENV_VERSION=1.8 run rbenv-which ruby + assert_success "${RBENV_ROOT}/versions/1.8/bin/ruby" + + RBENV_VERSION=2.0 run rbenv-which rspec + assert_success "${RBENV_ROOT}/versions/2.0/bin/rspec" +} + +@test "searches PATH for system version" { + create_executable "${RBENV_TEST_DIR}/bin" "kill-all-humans" + create_executable "${RBENV_ROOT}/shims" "kill-all-humans" + + RBENV_VERSION=system run rbenv-which kill-all-humans + assert_success "${RBENV_TEST_DIR}/bin/kill-all-humans" +} + +@test "version not installed" { + create_executable "2.0" "rspec" + RBENV_VERSION=1.9 run rbenv-which rspec + assert_failure "rbenv: version \`1.9' is not installed" +} + +@test "no executable found" { + create_executable "1.8" "rspec" + RBENV_VERSION=1.8 run rbenv-which rake + assert_failure "rbenv: rake: command not found" +} + +@test "executable found in other versions" { + create_executable "1.8" "ruby" + create_executable "1.9" "rspec" + create_executable "2.0" "rspec" + + RBENV_VERSION=1.8 run rbenv-which rspec + assert_failure "\ + rbenv: rspec: command not found + The \`rspec' command exists in these Ruby versions: + 1.9 + 2.0" +} From 7fc5f46bbb843c6be95c3f87d19ba21fef25c5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohnic=CC=81?= Date: Sat, 6 Apr 2013 14:10:44 +0200 Subject: [PATCH 2/4] undo `assert_output_lines` in tests It was a dumb idea and it wasn't even implemented perfectly. --- test/completions.bats | 8 +++++--- test/exec.bats | 36 ++++++++++++++++++++---------------- test/hooks.bats | 18 +++++++++++------- test/rehash.bats | 10 ++++++---- test/shell.bats | 8 +++++--- test/test_helper.bash | 24 +++++++----------------- test/whence.bats | 8 +++++--- test/which.bats | 13 ++++++++----- 8 files changed, 67 insertions(+), 58 deletions(-) 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" < Date: Mon, 8 Apr 2013 21:35:22 +0200 Subject: [PATCH 3/4] 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 < Date: Mon, 8 Apr 2013 23:00:15 +0200 Subject: [PATCH 4/4] add tests for `help` and `rbenv --version` --- test/--version.bats | 46 ++++++++++++++++++ test/help.bats | 115 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 test/--version.bats create mode 100644 test/help.bats 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/help.bats b/test/help.bats new file mode 100644 index 00000000..9862d630 --- /dev/null +++ b/test/help.bats @@ -0,0 +1,115 @@ +#!/usr/bin/env bats + +load test_helper + +@test "without args shows summary of common commands" { + run rbenv-help + assert_success + assert_line "Usage: rbenv []" + 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 +}