Commit fa38b660 authored by Ivan Kuchin's avatar Ivan Kuchin Committed by Mislav Marohnić
Browse files

Abort with usage help on wrong number of CLI arguments

- rbenv-install accepts 0 or 1 argument
- rbenv-uninstall accepts 1 argument
- ruby-build accepts 2 arguments
parent 63f6fa2e
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -111,6 +111,8 @@ DEFINITION="${ARGUMENTS[0]}"
[ -n "$DEFINITION" ] || DEFINITION="$(rbenv-local 2>/dev/null || true)"
[ -n "$DEFINITION" ] || usage 1

# Fail if number of arguments is greater than 1
[ "${#ARGUMENTS[@]}" -le 1 ] || usage 1

# Define `before_install` and `after_install` functions that allow
# plugin hooks to register a string of code for execution before or
+9 −0
Original line number Diff line number Diff line
@@ -17,6 +17,12 @@ if [ "$1" = "--complete" ]; then
  exec rbenv versions --bare
fi

usage() {
  # We can remove the sed fallback once rbenv 0.4.0 is widely available.
  rbenv-help uninstall 2>/dev/null || sed -ne '/^#/!q;s/.//;s/.//;1,4d;p' < "$0"
  [ -z "$1" ] || exit "$1"
}

if [ -z "$RBENV_ROOT" ]; then
  RBENV_ROOT="${HOME}/.rbenv"
fi
@@ -38,6 +44,9 @@ case "$DEFINITION" in
  ;;
esac

# Fail if number of arguments is not equal 1
[ "$#" -eq 1 ] || usage 1

declare -a before_hooks after_hooks

before_uninstall() {
+3 −0
Original line number Diff line number Diff line
@@ -1046,6 +1046,9 @@ elif [ "${PREFIX_PATH#/}" = "$PREFIX_PATH" ]; then
  PREFIX_PATH="${PWD}/${PREFIX_PATH}"
fi

# Fail if number of arguments is not equal to 2
[ "${#ARGUMENTS[@]}" -eq 2 ] || usage

if [ -z "$TMPDIR" ]; then
  TMP="/tmp"
else

test/arguments.bats

0 → 100644
+53 −0
Original line number Diff line number Diff line
#!/usr/bin/env bats

load test_helper

@test "not enought arguments for ruby-build" {
  # use empty inline definition so nothing gets built anyway
  local definition="${TMP}/build-definition"
  echo '' > "$definition"

  run ruby-build "$definition"
  assert_failure
  assert_output_contains 'usage: ruby-build'
}

@test "extra arguments for ruby-build" {
  # use empty inline definition so nothing gets built anyway
  local definition="${TMP}/build-definition"
  echo '' > "$definition"

  run ruby-build "$definition" 2.1.2 "${TMP}/install"
  assert_failure
  assert_output_contains 'usage: ruby-build'
}

@test "extra arguments for rbenv-install" {
  stub ruby-build "--lib : $BATS_TEST_DIRNAME/../bin/ruby-build --lib"
  stub rbenv-hooks
  stub rbenv-rehash

  run rbenv-install 2.1.1 2.1.2
  assert_failure
  assert_output_contains 'Usage: rbenv install'

  unstub ruby-build
  unstub rbenv-hooks
  unstub rbenv-rehash
}

@test "not enough arguments rbenv-uninstall" {
  run rbenv-uninstall
  assert_failure
  assert_output_contains 'Usage: rbenv uninstall'
}

@test "extra arguments for rbenv-uninstall" {
  stub rbenv-hooks

  run rbenv-uninstall 2.1.1 2.1.2
  assert_failure
  assert_output_contains 'Usage: rbenv uninstall'

  unstub rbenv-hooks
}