Skip to content
shebang-unit 29.6 KiB
Newer Older
ant31's avatar
ant31 committed
#!/usr/bin/env bash

# Copyright (C) 2015, Arpinum
#
# shebang-unit is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# shebang-unit is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# shebang-unit. If not, see http://www.gnu.org/licenses/lgpl.html.

# shebang-unit all in one source file


configuration__load() {
  # yes/no representation used with shebang-unit parameters to activate
  # stuff like colors
  SBU_YES="yes"
  SBU_NO="no"

  # Colors for outputs
  SBU_GREEN_COLOR_CODE="\\033[1;32m"
  SBU_RED_COLOR_CODE="\\033[1;31m"
  SBU_YELLOW_COLOR_CODE="\\033[1;33m"
  SBU_DEFAULT_COLOR_CODE="\\e[0m"

  # Functions coding coventions
  SBU_GLOBAL_SETUP_FUNCTION_NAME="global_setup"
  SBU_GLOBAL_TEARDOWN_FUNCTION_NAME="global_teardown"
  SBU_SETUP_FUNCTION_NAME="setup"
  SBU_TEARDOWN_FUNCTION_NAME="teardown"
  SBU_FUNCTION_DECLARATION_REGEX="^[ ]*\(function\)\{0,1\}[ ]*\([A-Za-z0-9_-]\{1,\}\)[ ]*\(([ ]*)\)\{0,1\}[ ]*{"
  SBU_PRIVATE_FUNCTION_NAME_REGEX="^_.*"

  # Default configuration that can be modified with shebang-unit parameters
  # For more information see shebang-unit usages
  SBU_TEST_FILE_PATTERN="*_test.sh"
  SBU_TEST_FUNCTION_PATTERN="*"
  SBU_USE_COLORS="${SBU_YES}"
  SBU_RANDOM_RUN="${SBU_NO}"
  SBU_REPORTERS="simple"
  SBU_JUNIT_REPORTER_OUTPUT_FILE="./junit_report.xml"

  # Internal constants
  SBU_SUCCESS_STATUS_CODE=0
  SBU_FAILURE_STATUS_CODE=1
  SBU_VALUE_SEPARATOR=","
  SBU_TEMP_DIR="/tmp/.shebang-unit"
  SBU_LAST_ASSERTION_MSG_KEY="last_assertion_message"
  SBU_NO_RUN="${SBU_NO}"
  SBU_STANDARD_FD=42
  SBU_ERROR_FD=43
}


assertion__equal() {
  if [[ "$1" != "$2" ]]; then
    _assertion__failed "Actual: <$2>, expected: <$1>."
  fi
}

assertion__different() {
  if [[ "$1" == "$2" ]]; then
    _assertion__failed "Both values are: <$1>."
  fi
}

assertion__string_contains() {
  if ! system__string_contains "$1" "$2"; then
    _assertion__failed "String: <$1> does not contain: <$2>."
  fi
}

assertion__string_does_not_contain() {
  if system__string_contains "$1" "$2"; then
    _assertion__failed "String: <$1> contains: <$2>."
  fi
}

assertion__string_empty() {
  if [[ -n "$1" ]]; then
    _assertion__failed "String: <$1> is not empty."
  fi
}

assertion__string_not_empty() {
  if [[ -z "$1" ]]; then
    _assertion__failed "The string is empty."
  fi
}

assertion__array_contains() {
  local element=$1
  shift 1
  if ! array__contains "${element}" "$@"; then
    local array_as_string="$(system__pretty_print_array "$@")"
    _assertion__failed \
      "Array: <${array_as_string}> does not contain: <${element}>."
  fi
}

assertion__array_does_not_contain() {
  local element=$1
  shift 1
  if array__contains "${element}" "$@"; then
    local array_as_string="$(system__pretty_print_array "$@")"
    _assertion__failed \
      "Array: <${array_as_string}> contains: <${element}>."
  fi
}

assertion__status_code_is_success() {
  if (( $1 != ${SBU_SUCCESS_STATUS_CODE} )); then
    _assertion__failed \
      "Status code is failure instead of success." "$2"
  fi
}

assertion__status_code_is_failure() {
  if (( $1 == ${SBU_SUCCESS_STATUS_CODE} )); then
    _assertion__failed \
      "Status code is success instead of failure." "$2"
  fi
}

assertion__successful() {
  "$@"
  if (( $? != ${SBU_SUCCESS_STATUS_CODE} )); then
    _assertion__failed "Command is failing instead of successful."
  fi
}

assertion__failing() {
  "$@"
  if (( $? == ${SBU_SUCCESS_STATUS_CODE} )); then
    _assertion__failed "Command is successful instead of failing."
  fi
}

_assertion__failed() {
  local message_to_use="$(_assertion__get_assertion_message_to_use "$1" "$2")"
  system__print_line "Assertion failed. ${message_to_use}"
  exit ${SBU_FAILURE_STATUS_CODE}
}

_assertion__get_assertion_message_to_use() {
  local message=$1
  local custom_messsage=$2
  if [[ -n "${custom_messsage}" ]]; then
    system__print "${message} ${custom_messsage}"
  else
    system__print "${message}"
  fi
}


mock__make_function_do_nothing() {
  mock__make_function_call "$1" ":"
}

mock__make_function_prints() {
  local function=$1
  local text=$2
  eval "${function}() { printf "${text}"; }"
}

mock__make_function_call() {
  local function_to_mock=$1
  local function_to_call=$2
  shift 2
  eval "${function_to_mock}() { ${function_to_call} \"\$@\"; }"
}


runner__run_all_test_files() {
  SBU_BASE_TEST_DIRECTORY=$1
  reporter__test_files_start_running
  timer__store_current_time "global_time"
  results__test_files_start_running
  _runner__run_all_test_files_with_pattern_in_directory "$1"
  reporter__test_files_end_running "$(timer__get_time_elapsed "global_time")"
  runner__tests_are_successful
}

_runner__run_all_test_files_with_pattern_in_directory() {
  local file
  local files
  array__from_lines files <<< "$(_runner__get_test_files_in_directory "$1")"
  for file in "${files[@]}"; do
    file_runner__run_test_file "${file}"
  done
}

_runner__get_test_files_in_directory() {
Loading
Loading full blame...